blob: 24f57e071cdd4e42e154cefc3fd8c6cb9408ed1a [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"
Daniel Dunbara027d222009-07-31 02:32:59 +000014#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000015#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000016#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000017#include "llvm/MC/MCParser/MCAsmLexer.h"
18#include "llvm/MC/MCParser/MCAsmParser.h"
19#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000022#include "llvm/ADT/StringSwitch.h"
23#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000024#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000025#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000026#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000027
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000028using namespace llvm;
29
30namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000031struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000032
Devang Pateldd929fc2012-01-12 18:03:40 +000033class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000034 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000035 MCAsmParser &Parser;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &getParser() const { return Parser; }
38
39 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
40
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000041 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000042 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
43 bool matchingInlineAsm = false) {
44 if (matchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000045 return Parser.Error(L, Msg, Ranges);
46 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000047
Devang Pateld37ad242012-01-17 18:00:18 +000048 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
49 Error(Loc, Msg);
50 return 0;
51 }
52
Chris Lattner309264d2010-01-15 18:44:13 +000053 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000054 X86Operand *ParseATTOperand();
55 X86Operand *ParseIntelOperand();
Chad Rosier5b0f1b32012-10-04 23:59:38 +000056 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000057 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000058 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000059
60 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000061 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000062
Devang Patelb8ba13f2012-01-18 22:42:29 +000063 bool processInstruction(MCInst &Inst,
64 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
65
Chris Lattner7036f8b2010-09-29 01:42:58 +000066 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000067 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000068 MCStreamer &Out);
Chad Rosier22685872012-10-01 23:45:51 +000069 bool MatchInstruction(SMLoc IDLoc,
Chad Rosier32461762012-08-09 22:04:55 +000070 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier22685872012-10-01 23:45:51 +000071 MCStreamer &Out, unsigned &Kind, unsigned &Opcode,
72 SmallVectorImpl<std::pair< unsigned, std::string > > &MapAndConstraints,
73 unsigned &OrigErrorInfo, bool matchingInlineAsm = false);
Chad Rosier32461762012-08-09 22:04:55 +000074
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000075 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000076 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000077 bool isSrcOp(X86Operand &Op);
78
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000079 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
80 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000081 bool isDstOp(X86Operand &Op);
82
Evan Cheng59ee62d2011-07-11 03:57:24 +000083 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000084 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000085 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000086 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000087 void SwitchMode() {
88 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
89 setAvailableFeatures(FB);
90 }
Evan Chengebdeeab2011-07-08 01:53:10 +000091
Daniel Dunbar54074b52010-07-19 05:44:09 +000092 /// @name Auto-generated Matcher Functions
93 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000094
Chris Lattner0692ee62010-09-06 19:11:01 +000095#define GET_ASSEMBLER_HEADER
96#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000097
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000098 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000099
100public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000101 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +0000102 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000103
Daniel Dunbar54074b52010-07-19 05:44:09 +0000104 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000105 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000106 }
Roman Divackybf755322011-01-27 17:14:22 +0000107 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000108
Benjamin Kramer38e59892010-07-14 22:38:02 +0000109 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000110 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000111
112 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000113
114 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000115 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000116 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000117};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000118} // end anonymous namespace
119
Sean Callanane9b466d2010-01-23 00:40:33 +0000120/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000121/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000122
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000123static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000124
125/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000126
Craig Topper76bd9382012-07-18 04:59:16 +0000127static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000128 return (( Value <= 0x000000000000007FULL)||
129 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
130 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
131}
132
133static bool isImmSExti32i8Value(uint64_t Value) {
134 return (( Value <= 0x000000000000007FULL)||
135 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
136 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
137}
138
139static bool isImmZExtu32u8Value(uint64_t Value) {
140 return (Value <= 0x00000000000000FFULL);
141}
142
143static bool isImmSExti64i8Value(uint64_t Value) {
144 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000145 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000146}
147
148static bool isImmSExti64i32Value(uint64_t Value) {
149 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000150 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000151}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000152namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000153
154/// X86Operand - Instances of this class represent a parsed X86 machine
155/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000156struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000157 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000158 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000159 Register,
160 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000161 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000162 } Kind;
163
Chris Lattner29ef9a22010-01-15 18:51:29 +0000164 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000165
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000166 union {
167 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000168 const char *Data;
169 unsigned Length;
170 } Tok;
171
172 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000173 unsigned RegNo;
174 } Reg;
175
176 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000177 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000178 } Imm;
179
180 struct {
181 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000182 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000183 unsigned BaseReg;
184 unsigned IndexReg;
185 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000186 unsigned Size;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000187 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000188 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000189
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000190 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000191 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000192
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000193 /// getStartLoc - Get the location of the first token of this operand.
194 SMLoc getStartLoc() const { return StartLoc; }
195 /// getEndLoc - Get the location of the last token of this operand.
196 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000197 /// getLocRange - Get the range between the first and last token of this
198 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000199 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000200
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000201 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000202
Daniel Dunbar20927f22009-08-07 08:26:05 +0000203 StringRef getToken() const {
204 assert(Kind == Token && "Invalid access!");
205 return StringRef(Tok.Data, Tok.Length);
206 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000207 void setTokenValue(StringRef Value) {
208 assert(Kind == Token && "Invalid access!");
209 Tok.Data = Value.data();
210 Tok.Length = Value.size();
211 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000212
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000213 unsigned getReg() const {
214 assert(Kind == Register && "Invalid access!");
215 return Reg.RegNo;
216 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000217
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000218 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000219 assert(Kind == Immediate && "Invalid access!");
220 return Imm.Val;
221 }
222
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000223 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000224 assert(Kind == Memory && "Invalid access!");
225 return Mem.Disp;
226 }
227 unsigned getMemSegReg() const {
228 assert(Kind == Memory && "Invalid access!");
229 return Mem.SegReg;
230 }
231 unsigned getMemBaseReg() const {
232 assert(Kind == Memory && "Invalid access!");
233 return Mem.BaseReg;
234 }
235 unsigned getMemIndexReg() const {
236 assert(Kind == Memory && "Invalid access!");
237 return Mem.IndexReg;
238 }
239 unsigned getMemScale() const {
240 assert(Kind == Memory && "Invalid access!");
241 return Mem.Scale;
242 }
243
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000244 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000245
246 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000247
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000248 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000249 if (!isImm())
250 return false;
251
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000252 // If this isn't a constant expr, just assume it fits and let relaxation
253 // handle it.
254 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
255 if (!CE)
256 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000257
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000258 // Otherwise, check the value is in a range that makes sense for this
259 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000260 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000261 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000262 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000263 if (!isImm())
264 return false;
265
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000266 // If this isn't a constant expr, just assume it fits and let relaxation
267 // handle it.
268 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
269 if (!CE)
270 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000271
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000272 // Otherwise, check the value is in a range that makes sense for this
273 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000274 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000275 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000276 bool isImmZExtu32u8() const {
277 if (!isImm())
278 return false;
279
280 // If this isn't a constant expr, just assume it fits and let relaxation
281 // handle it.
282 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
283 if (!CE)
284 return true;
285
286 // Otherwise, check the value is in a range that makes sense for this
287 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000288 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000289 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000290 bool isImmSExti64i8() const {
291 if (!isImm())
292 return false;
293
294 // If this isn't a constant expr, just assume it fits and let relaxation
295 // handle it.
296 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
297 if (!CE)
298 return true;
299
300 // Otherwise, check the value is in a range that makes sense for this
301 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000302 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000303 }
304 bool isImmSExti64i32() const {
305 if (!isImm())
306 return false;
307
308 // If this isn't a constant expr, just assume it fits and let relaxation
309 // handle it.
310 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
311 if (!CE)
312 return true;
313
314 // Otherwise, check the value is in a range that makes sense for this
315 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000316 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000317 }
318
Daniel Dunbar20927f22009-08-07 08:26:05 +0000319 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000320 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000321 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000322 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000323 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000324 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000325 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000326 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000327 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000328 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000329 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000330 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000331 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000332 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000333 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000334 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000335 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000336 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000337 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000338 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000339 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000340 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000341
Craig Topper75dc33a2012-07-18 04:11:12 +0000342 bool isMemVX32() const {
343 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
344 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
345 }
346 bool isMemVY32() const {
347 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
348 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
349 }
350 bool isMemVX64() const {
351 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
352 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
353 }
354 bool isMemVY64() const {
355 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
356 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
357 }
358
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000359 bool isAbsMem() const {
360 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000361 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000362 }
363
Daniel Dunbar20927f22009-08-07 08:26:05 +0000364 bool isReg() const { return Kind == Register; }
365
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000366 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
367 // Add as immediates when possible.
368 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
369 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
370 else
371 Inst.addOperand(MCOperand::CreateExpr(Expr));
372 }
373
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000374 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000375 assert(N == 1 && "Invalid number of operands!");
376 Inst.addOperand(MCOperand::CreateReg(getReg()));
377 }
378
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000379 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000380 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000381 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000382 }
383
Chad Rosier36b8fed2012-06-27 22:34:28 +0000384 void addMem8Operands(MCInst &Inst, unsigned N) const {
385 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000386 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000387 void addMem16Operands(MCInst &Inst, unsigned N) const {
388 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000389 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000390 void addMem32Operands(MCInst &Inst, unsigned N) const {
391 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000392 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000393 void addMem64Operands(MCInst &Inst, unsigned N) const {
394 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000395 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000396 void addMem80Operands(MCInst &Inst, unsigned N) const {
397 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000398 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000399 void addMem128Operands(MCInst &Inst, unsigned N) const {
400 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000401 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000402 void addMem256Operands(MCInst &Inst, unsigned N) const {
403 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000404 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000405 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
406 addMemOperands(Inst, N);
407 }
408 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
409 addMemOperands(Inst, N);
410 }
411 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
412 addMemOperands(Inst, N);
413 }
414 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
415 addMemOperands(Inst, N);
416 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000417
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000418 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000419 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000420 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
421 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
422 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000423 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000424 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
425 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000426
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000427 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
428 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000429 // Add as immediates when possible.
430 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
431 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
432 else
433 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000434 }
435
Chris Lattnerb4307b32010-01-15 19:28:38 +0000436 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000437 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
438 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000439 Res->Tok.Data = Str.data();
440 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000441 return Res;
442 }
443
Chris Lattner29ef9a22010-01-15 18:51:29 +0000444 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000445 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000446 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000447 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000448 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000449
Chris Lattnerb4307b32010-01-15 19:28:38 +0000450 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
451 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000452 Res->Imm.Val = Val;
453 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000454 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000455
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000456 /// Create an absolute memory operand.
457 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000458 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000459 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
460 Res->Mem.SegReg = 0;
461 Res->Mem.Disp = Disp;
462 Res->Mem.BaseReg = 0;
463 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000464 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000465 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000466 return Res;
467 }
468
469 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000470 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
471 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000472 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
473 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000474 // We should never just have a displacement, that should be parsed as an
475 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000476 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
477
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000478 // The scale should always be one of {1,2,4,8}.
479 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000480 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000481 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000482 Res->Mem.SegReg = SegReg;
483 Res->Mem.Disp = Disp;
484 Res->Mem.BaseReg = BaseReg;
485 Res->Mem.IndexReg = IndexReg;
486 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000487 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000488 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000489 }
490};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000491
Chris Lattner37dfdec2009-07-29 06:33:53 +0000492} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000493
Devang Pateldd929fc2012-01-12 18:03:40 +0000494bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000495 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000496
497 return (Op.isMem() &&
498 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
499 isa<MCConstantExpr>(Op.Mem.Disp) &&
500 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
501 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
502}
503
Devang Pateldd929fc2012-01-12 18:03:40 +0000504bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000505 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000506
Chad Rosier36b8fed2012-06-27 22:34:28 +0000507 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000508 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000509 isa<MCConstantExpr>(Op.Mem.Disp) &&
510 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
511 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
512}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000513
Devang Pateldd929fc2012-01-12 18:03:40 +0000514bool X86AsmParser::ParseRegister(unsigned &RegNo,
515 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000516 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000517 const AsmToken &PercentTok = Parser.getTok();
518 StartLoc = PercentTok.getLoc();
519
520 // If we encounter a %, ignore it. This code handles registers with and
521 // without the prefix, unprefixed registers can occur in cfi directives.
522 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000523 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000524
Sean Callanan18b83232010-01-19 21:44:56 +0000525 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000526 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000527 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000528 return Error(StartLoc, "invalid register name",
529 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000530 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000531
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000532 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000533
Chris Lattner33d60d52010-09-22 04:11:10 +0000534 // If the match failed, try the register name as lowercase.
535 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000536 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000537
Evan Cheng5de728c2011-07-27 23:22:03 +0000538 if (!is64BitMode()) {
539 // FIXME: This should be done using Requires<In32BitMode> and
540 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
541 // checked.
542 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
543 // REX prefix.
544 if (RegNo == X86::RIZ ||
545 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
546 X86II::isX86_64NonExtLowByteReg(RegNo) ||
547 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000548 return Error(StartLoc, "register %"
549 + Tok.getString() + " is only available in 64-bit mode",
550 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000551 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000552
Chris Lattner33d60d52010-09-22 04:11:10 +0000553 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
554 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000555 RegNo = X86::ST0;
556 EndLoc = Tok.getLoc();
557 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000558
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000559 // Check to see if we have '(4)' after %st.
560 if (getLexer().isNot(AsmToken::LParen))
561 return false;
562 // Lex the paren.
563 getParser().Lex();
564
565 const AsmToken &IntTok = Parser.getTok();
566 if (IntTok.isNot(AsmToken::Integer))
567 return Error(IntTok.getLoc(), "expected stack index");
568 switch (IntTok.getIntVal()) {
569 case 0: RegNo = X86::ST0; break;
570 case 1: RegNo = X86::ST1; break;
571 case 2: RegNo = X86::ST2; break;
572 case 3: RegNo = X86::ST3; break;
573 case 4: RegNo = X86::ST4; break;
574 case 5: RegNo = X86::ST5; break;
575 case 6: RegNo = X86::ST6; break;
576 case 7: RegNo = X86::ST7; break;
577 default: return Error(IntTok.getLoc(), "invalid stack index");
578 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000579
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000580 if (getParser().Lex().isNot(AsmToken::RParen))
581 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000582
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000583 EndLoc = Tok.getLoc();
584 Parser.Lex(); // Eat ')'
585 return false;
586 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000587
Chris Lattner645b2092010-06-24 07:29:18 +0000588 // If this is "db[0-7]", match it as an alias
589 // for dr[0-7].
590 if (RegNo == 0 && Tok.getString().size() == 3 &&
591 Tok.getString().startswith("db")) {
592 switch (Tok.getString()[2]) {
593 case '0': RegNo = X86::DR0; break;
594 case '1': RegNo = X86::DR1; break;
595 case '2': RegNo = X86::DR2; break;
596 case '3': RegNo = X86::DR3; break;
597 case '4': RegNo = X86::DR4; break;
598 case '5': RegNo = X86::DR5; break;
599 case '6': RegNo = X86::DR6; break;
600 case '7': RegNo = X86::DR7; break;
601 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000602
Chris Lattner645b2092010-06-24 07:29:18 +0000603 if (RegNo != 0) {
604 EndLoc = Tok.getLoc();
605 Parser.Lex(); // Eat it.
606 return false;
607 }
608 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000609
Devang Patel1aea4302012-01-20 22:32:05 +0000610 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000611 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000612 return Error(StartLoc, "invalid register name",
613 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000614 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000615
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000616 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000617 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000618 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000619}
620
Devang Pateldd929fc2012-01-12 18:03:40 +0000621X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000622 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000623 return ParseIntelOperand();
624 return ParseATTOperand();
625}
626
Devang Pateld37ad242012-01-17 18:00:18 +0000627/// getIntelMemOperandSize - Return intel memory operand size.
628static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000629 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000630 .Cases("BYTE", "byte", 8)
631 .Cases("WORD", "word", 16)
632 .Cases("DWORD", "dword", 32)
633 .Cases("QWORD", "qword", 64)
634 .Cases("XWORD", "xword", 80)
635 .Cases("XMMWORD", "xmmword", 128)
636 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000637 .Default(0);
638 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000639}
640
Devang Patel7c64fe62012-01-23 18:31:58 +0000641X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
642 unsigned Size) {
643 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000644 SMLoc Start = Parser.getTok().getLoc(), End;
645
Devang Pateld37ad242012-01-17 18:00:18 +0000646 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
647 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
648
649 // Eat '['
650 if (getLexer().isNot(AsmToken::LBrac))
651 return ErrorOperand(Start, "Expected '[' token!");
652 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000653
Devang Pateld37ad242012-01-17 18:00:18 +0000654 if (getLexer().is(AsmToken::Identifier)) {
655 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000656 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000657 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000658 if (getParser().ParseExpression(Disp, End)) return 0;
659 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000660 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000661 Parser.Lex();
662 return X86Operand::CreateMem(Disp, Start, End, Size);
663 }
664 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000665 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000666 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000667 SMLoc Loc = Parser.getTok().getLoc();
668 if (getLexer().is(AsmToken::RBrac)) {
669 // Handle '[' number ']'
670 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000671 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
672 if (SegReg)
673 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
674 Start, End, Size);
675 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000676 } else if (getLexer().is(AsmToken::Star)) {
677 // Handle '[' Scale*IndexReg ']'
678 Parser.Lex();
679 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000680 if (ParseRegister(IndexReg, IdxRegLoc, End))
681 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000682 Scale = Val;
683 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000684 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000685 }
686
687 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
688 bool isPlus = getLexer().is(AsmToken::Plus);
689 Parser.Lex();
690 SMLoc PlusLoc = Parser.getTok().getLoc();
691 if (getLexer().is(AsmToken::Integer)) {
692 int64_t Val = Parser.getTok().getIntVal();
693 Parser.Lex();
694 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000695 Parser.Lex();
696 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000697 if (ParseRegister(IndexReg, IdxRegLoc, End))
698 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000699 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000700 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000701 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000702 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000703 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000704 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000705 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000706 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000707 End = Parser.getTok().getLoc();
708 if (!IndexReg)
709 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000710 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000711 }
Devang Pateld37ad242012-01-17 18:00:18 +0000712 }
713
714 if (getLexer().isNot(AsmToken::RBrac))
715 if (getParser().ParseExpression(Disp, End)) return 0;
716
717 End = Parser.getTok().getLoc();
718 if (getLexer().isNot(AsmToken::RBrac))
719 return ErrorOperand(End, "expected ']' token!");
720 Parser.Lex();
721 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000722
723 // handle [-42]
724 if (!BaseReg && !IndexReg)
725 return X86Operand::CreateMem(Disp, Start, End, Size);
726
Devang Pateld37ad242012-01-17 18:00:18 +0000727 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000728 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000729}
730
731/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000732X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000733 const AsmToken &Tok = Parser.getTok();
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000734 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000735
736 unsigned Size = getIntelMemOperandSize(Tok.getString());
737 if (Size) {
738 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000739 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
740 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000741 Parser.Lex();
742 }
743
744 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000745 return ParseIntelBracExpression(SegReg, Size);
746
747 if (!ParseRegister(SegReg, Start, End)) {
748 // Handel SegReg : [ ... ]
749 if (getLexer().isNot(AsmToken::Colon))
750 return ErrorOperand(Start, "Expected ':' token!");
751 Parser.Lex(); // Eat :
752 if (getLexer().isNot(AsmToken::LBrac))
753 return ErrorOperand(Start, "Expected '[' token!");
754 return ParseIntelBracExpression(SegReg, Size);
755 }
Devang Pateld37ad242012-01-17 18:00:18 +0000756
757 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
758 if (getParser().ParseExpression(Disp, End)) return 0;
759 return X86Operand::CreateMem(Disp, Start, End, Size);
760}
761
762X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000763 SMLoc Start = Parser.getTok().getLoc(), End;
764
765 // immediate.
766 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
767 getLexer().is(AsmToken::Minus)) {
768 const MCExpr *Val;
769 if (!getParser().ParseExpression(Val, End)) {
770 End = Parser.getTok().getLoc();
771 return X86Operand::CreateImm(Val, Start, End);
772 }
773 }
774
Devang Patel0a338862012-01-12 01:36:43 +0000775 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000776 unsigned RegNo = 0;
777 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000778 // If this is a segment register followed by a ':', then this is the start
779 // of a memory reference, otherwise this is a normal register reference.
780 if (getLexer().isNot(AsmToken::Colon))
781 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
782
783 getParser().Lex(); // Eat the colon.
784 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000785 }
786
787 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000788 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000789}
790
Devang Pateldd929fc2012-01-12 18:03:40 +0000791X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000792 switch (getLexer().getKind()) {
793 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000794 // Parse a memory operand with no segment register.
795 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000796 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000797 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000798 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000799 SMLoc Start, End;
800 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000801 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000802 Error(Start, "%eiz and %riz can only be used as index registers",
803 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000804 return 0;
805 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000806
Chris Lattnereef6d782010-04-17 18:56:34 +0000807 // If this is a segment register followed by a ':', then this is the start
808 // of a memory reference, otherwise this is a normal register reference.
809 if (getLexer().isNot(AsmToken::Colon))
810 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000811
812
Chris Lattnereef6d782010-04-17 18:56:34 +0000813 getParser().Lex(); // Eat the colon.
814 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000815 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000816 case AsmToken::Dollar: {
817 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000818 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000819 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000820 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000821 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000822 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000823 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000824 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000825 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000826}
827
Chris Lattnereef6d782010-04-17 18:56:34 +0000828/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
829/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000830X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000831
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000832 // We have to disambiguate a parenthesized expression "(4+5)" from the start
833 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000834 // only way to do this without lookahead is to eat the '(' and see what is
835 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000836 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000837 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000838 SMLoc ExprEnd;
839 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000840
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000841 // After parsing the base expression we could either have a parenthesized
842 // memory address or not. If not, return now. If so, eat the (.
843 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000844 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000845 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000846 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000847 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000848 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000849
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000850 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000851 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000852 } else {
853 // Okay, we have a '('. We don't know if this is an expression or not, but
854 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000855 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000856 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000857
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000858 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000859 // Nothing to do here, fall into the code below with the '(' part of the
860 // memory operand consumed.
861 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000862 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000863
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000864 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000865 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000866 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000867
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000868 // After parsing the base expression we could either have a parenthesized
869 // memory address or not. If not, return now. If so, eat the (.
870 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000871 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000872 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000873 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000874 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000875 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000876
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000877 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000878 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000879 }
880 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000881
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000882 // If we reached here, then we just ate the ( of the memory operand. Process
883 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000884 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000885 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000886
Chris Lattner29ef9a22010-01-15 18:51:29 +0000887 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000888 SMLoc StartLoc, EndLoc;
889 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000890 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000891 Error(StartLoc, "eiz and riz can only be used as index registers",
892 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000893 return 0;
894 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000895 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000896
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000897 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000898 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000899 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000900
901 // Following the comma we should have either an index register, or a scale
902 // value. We don't support the later form, but we want to parse it
903 // correctly.
904 //
905 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000906 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000907 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000908 SMLoc L;
909 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000910
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000911 if (getLexer().isNot(AsmToken::RParen)) {
912 // Parse the scale amount:
913 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000914 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000915 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000916 "expected comma in scale expression");
917 return 0;
918 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000919 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000920
921 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000922 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000923
924 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000925 if (getParser().ParseAbsoluteExpression(ScaleVal)){
926 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000927 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +0000928 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000929
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000930 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000931 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
932 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
933 return 0;
934 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000935 Scale = (unsigned)ScaleVal;
936 }
937 }
938 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000939 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000940 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000941 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000942
943 int64_t Value;
944 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000945 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000946
Daniel Dunbaree910252010-08-24 19:13:38 +0000947 if (Value != 1)
948 Warning(Loc, "scale factor without index register is ignored");
949 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000950 }
951 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000952
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000953 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000954 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000955 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000956 return 0;
957 }
Sean Callanan18b83232010-01-19 21:44:56 +0000958 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000959 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000960
Kevin Enderby84faf652012-03-12 21:32:09 +0000961 // If we have both a base register and an index register make sure they are
962 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +0000963 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +0000964 if (BaseReg != 0 && IndexReg != 0) {
965 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000966 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
967 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000968 IndexReg != X86::RIZ) {
969 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
970 return 0;
971 }
972 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000973 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
974 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000975 IndexReg != X86::EIZ){
976 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
977 return 0;
978 }
979 }
980
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000981 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
982 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000983}
984
Devang Pateldd929fc2012-01-12 18:03:40 +0000985bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000986ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000987 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000988 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000989
Chris Lattnerd8f71792010-11-28 20:23:50 +0000990 // FIXME: Hack to recognize setneb as setne.
991 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
992 PatchedName != "setb" && PatchedName != "setnb")
993 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000994
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000995 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
996 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000997 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000998 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
999 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001000 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001001 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001002 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001003 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001004 .Case("eq", 0x00)
1005 .Case("lt", 0x01)
1006 .Case("le", 0x02)
1007 .Case("unord", 0x03)
1008 .Case("neq", 0x04)
1009 .Case("nlt", 0x05)
1010 .Case("nle", 0x06)
1011 .Case("ord", 0x07)
1012 /* AVX only from here */
1013 .Case("eq_uq", 0x08)
1014 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001015 .Case("ngt", 0x0A)
1016 .Case("false", 0x0B)
1017 .Case("neq_oq", 0x0C)
1018 .Case("ge", 0x0D)
1019 .Case("gt", 0x0E)
1020 .Case("true", 0x0F)
1021 .Case("eq_os", 0x10)
1022 .Case("lt_oq", 0x11)
1023 .Case("le_oq", 0x12)
1024 .Case("unord_s", 0x13)
1025 .Case("neq_us", 0x14)
1026 .Case("nlt_uq", 0x15)
1027 .Case("nle_uq", 0x16)
1028 .Case("ord_s", 0x17)
1029 .Case("eq_us", 0x18)
1030 .Case("nge_uq", 0x19)
1031 .Case("ngt_uq", 0x1A)
1032 .Case("false_os", 0x1B)
1033 .Case("neq_os", 0x1C)
1034 .Case("ge_oq", 0x1D)
1035 .Case("gt_oq", 0x1E)
1036 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001037 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001038 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001039 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1040 getParser().getContext());
1041 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001042 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001043 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001044 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001045 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001046 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001047 } else {
1048 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001049 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001050 }
1051 }
1052 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001053
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001054 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001055
Devang Patel885f65b2012-01-30 22:47:12 +00001056 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001057 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001058
Chris Lattner2544f422010-09-08 05:17:37 +00001059 // Determine whether this is an instruction prefix.
1060 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001061 Name == "lock" || Name == "rep" ||
1062 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001063 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001064 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001065
1066
Chris Lattner2544f422010-09-08 05:17:37 +00001067 // This does the actual operand parsing. Don't parse any more if we have a
1068 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1069 // just want to parse the "lock" as the first instruction and the "incl" as
1070 // the next one.
1071 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001072
1073 // Parse '*' modifier.
1074 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001075 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001076 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001077 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001078 }
1079
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001080 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001081 if (X86Operand *Op = ParseOperand())
1082 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001083 else {
1084 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001085 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001086 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001087
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001088 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001089 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001090
1091 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001092 if (X86Operand *Op = ParseOperand())
1093 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001094 else {
1095 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001096 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001097 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001098 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001099
Chris Lattnercbf8a982010-09-11 16:18:25 +00001100 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001101 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001102 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001103 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001104 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001105 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001106
Chris Lattner2544f422010-09-08 05:17:37 +00001107 if (getLexer().is(AsmToken::EndOfStatement))
1108 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001109 else if (isPrefix && getLexer().is(AsmToken::Slash))
1110 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001111
Devang Patel885f65b2012-01-30 22:47:12 +00001112 if (ExtraImmOp && isParsingIntelSyntax())
1113 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1114
Chris Lattner98c870f2010-11-06 19:25:43 +00001115 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1116 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1117 // documented form in various unofficial manuals, so a lot of code uses it.
1118 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1119 Operands.size() == 3) {
1120 X86Operand &Op = *(X86Operand*)Operands.back();
1121 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1122 isa<MCConstantExpr>(Op.Mem.Disp) &&
1123 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1124 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1125 SMLoc Loc = Op.getEndLoc();
1126 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1127 delete &Op;
1128 }
1129 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001130 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1131 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1132 Operands.size() == 3) {
1133 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1134 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1135 isa<MCConstantExpr>(Op.Mem.Disp) &&
1136 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1137 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1138 SMLoc Loc = Op.getEndLoc();
1139 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1140 delete &Op;
1141 }
1142 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001143 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1144 if (Name.startswith("ins") && Operands.size() == 3 &&
1145 (Name == "insb" || Name == "insw" || Name == "insl")) {
1146 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1147 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1148 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1149 Operands.pop_back();
1150 Operands.pop_back();
1151 delete &Op;
1152 delete &Op2;
1153 }
1154 }
1155
1156 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1157 if (Name.startswith("outs") && Operands.size() == 3 &&
1158 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1159 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1160 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1161 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1162 Operands.pop_back();
1163 Operands.pop_back();
1164 delete &Op;
1165 delete &Op2;
1166 }
1167 }
1168
1169 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1170 if (Name.startswith("movs") && Operands.size() == 3 &&
1171 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001172 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001173 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1174 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1175 if (isSrcOp(Op) && isDstOp(Op2)) {
1176 Operands.pop_back();
1177 Operands.pop_back();
1178 delete &Op;
1179 delete &Op2;
1180 }
1181 }
1182 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1183 if (Name.startswith("lods") && Operands.size() == 3 &&
1184 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001185 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001186 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1187 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1188 if (isSrcOp(*Op1) && Op2->isReg()) {
1189 const char *ins;
1190 unsigned reg = Op2->getReg();
1191 bool isLods = Name == "lods";
1192 if (reg == X86::AL && (isLods || Name == "lodsb"))
1193 ins = "lodsb";
1194 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1195 ins = "lodsw";
1196 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1197 ins = "lodsl";
1198 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1199 ins = "lodsq";
1200 else
1201 ins = NULL;
1202 if (ins != NULL) {
1203 Operands.pop_back();
1204 Operands.pop_back();
1205 delete Op1;
1206 delete Op2;
1207 if (Name != ins)
1208 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1209 }
1210 }
1211 }
1212 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1213 if (Name.startswith("stos") && Operands.size() == 3 &&
1214 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001215 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001216 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1217 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1218 if (isDstOp(*Op2) && Op1->isReg()) {
1219 const char *ins;
1220 unsigned reg = Op1->getReg();
1221 bool isStos = Name == "stos";
1222 if (reg == X86::AL && (isStos || Name == "stosb"))
1223 ins = "stosb";
1224 else if (reg == X86::AX && (isStos || Name == "stosw"))
1225 ins = "stosw";
1226 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1227 ins = "stosl";
1228 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1229 ins = "stosq";
1230 else
1231 ins = NULL;
1232 if (ins != NULL) {
1233 Operands.pop_back();
1234 Operands.pop_back();
1235 delete Op1;
1236 delete Op2;
1237 if (Name != ins)
1238 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1239 }
1240 }
1241 }
1242
Chris Lattnere9e16a32010-09-15 04:33:27 +00001243 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001244 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001245 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001246 Name.startswith("shl") || Name.startswith("sal") ||
1247 Name.startswith("rcl") || Name.startswith("rcr") ||
1248 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001249 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001250 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001251 // Intel syntax
1252 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1253 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001254 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1255 delete Operands[2];
1256 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001257 }
1258 } else {
1259 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1260 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001261 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1262 delete Operands[1];
1263 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001264 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001265 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001266 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001267
Chris Lattner15f89512011-04-09 19:41:05 +00001268 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1269 // instalias with an immediate operand yet.
1270 if (Name == "int" && Operands.size() == 2) {
1271 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1272 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1273 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1274 delete Operands[1];
1275 Operands.erase(Operands.begin() + 1);
1276 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1277 }
1278 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001279
Chris Lattner98986712010-01-14 22:21:20 +00001280 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001281}
1282
Devang Pateldd929fc2012-01-12 18:03:40 +00001283bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001284processInstruction(MCInst &Inst,
1285 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1286 switch (Inst.getOpcode()) {
1287 default: return false;
1288 case X86::AND16i16: {
1289 if (!Inst.getOperand(0).isImm() ||
1290 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1291 return false;
1292
1293 MCInst TmpInst;
1294 TmpInst.setOpcode(X86::AND16ri8);
1295 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1296 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1297 TmpInst.addOperand(Inst.getOperand(0));
1298 Inst = TmpInst;
1299 return true;
1300 }
1301 case X86::AND32i32: {
1302 if (!Inst.getOperand(0).isImm() ||
1303 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1304 return false;
1305
1306 MCInst TmpInst;
1307 TmpInst.setOpcode(X86::AND32ri8);
1308 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1309 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1310 TmpInst.addOperand(Inst.getOperand(0));
1311 Inst = TmpInst;
1312 return true;
1313 }
1314 case X86::AND64i32: {
1315 if (!Inst.getOperand(0).isImm() ||
1316 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1317 return false;
1318
1319 MCInst TmpInst;
1320 TmpInst.setOpcode(X86::AND64ri8);
1321 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1322 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1323 TmpInst.addOperand(Inst.getOperand(0));
1324 Inst = TmpInst;
1325 return true;
1326 }
Devang Patelac0f0482012-01-19 17:53:25 +00001327 case X86::XOR16i16: {
1328 if (!Inst.getOperand(0).isImm() ||
1329 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1330 return false;
1331
1332 MCInst TmpInst;
1333 TmpInst.setOpcode(X86::XOR16ri8);
1334 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1335 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1336 TmpInst.addOperand(Inst.getOperand(0));
1337 Inst = TmpInst;
1338 return true;
1339 }
1340 case X86::XOR32i32: {
1341 if (!Inst.getOperand(0).isImm() ||
1342 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1343 return false;
1344
1345 MCInst TmpInst;
1346 TmpInst.setOpcode(X86::XOR32ri8);
1347 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1348 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1349 TmpInst.addOperand(Inst.getOperand(0));
1350 Inst = TmpInst;
1351 return true;
1352 }
1353 case X86::XOR64i32: {
1354 if (!Inst.getOperand(0).isImm() ||
1355 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1356 return false;
1357
1358 MCInst TmpInst;
1359 TmpInst.setOpcode(X86::XOR64ri8);
1360 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1361 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1362 TmpInst.addOperand(Inst.getOperand(0));
1363 Inst = TmpInst;
1364 return true;
1365 }
1366 case X86::OR16i16: {
1367 if (!Inst.getOperand(0).isImm() ||
1368 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1369 return false;
1370
1371 MCInst TmpInst;
1372 TmpInst.setOpcode(X86::OR16ri8);
1373 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1374 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1375 TmpInst.addOperand(Inst.getOperand(0));
1376 Inst = TmpInst;
1377 return true;
1378 }
1379 case X86::OR32i32: {
1380 if (!Inst.getOperand(0).isImm() ||
1381 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1382 return false;
1383
1384 MCInst TmpInst;
1385 TmpInst.setOpcode(X86::OR32ri8);
1386 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1387 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1388 TmpInst.addOperand(Inst.getOperand(0));
1389 Inst = TmpInst;
1390 return true;
1391 }
1392 case X86::OR64i32: {
1393 if (!Inst.getOperand(0).isImm() ||
1394 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1395 return false;
1396
1397 MCInst TmpInst;
1398 TmpInst.setOpcode(X86::OR64ri8);
1399 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1400 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1401 TmpInst.addOperand(Inst.getOperand(0));
1402 Inst = TmpInst;
1403 return true;
1404 }
1405 case X86::CMP16i16: {
1406 if (!Inst.getOperand(0).isImm() ||
1407 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1408 return false;
1409
1410 MCInst TmpInst;
1411 TmpInst.setOpcode(X86::CMP16ri8);
1412 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1413 TmpInst.addOperand(Inst.getOperand(0));
1414 Inst = TmpInst;
1415 return true;
1416 }
1417 case X86::CMP32i32: {
1418 if (!Inst.getOperand(0).isImm() ||
1419 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1420 return false;
1421
1422 MCInst TmpInst;
1423 TmpInst.setOpcode(X86::CMP32ri8);
1424 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1425 TmpInst.addOperand(Inst.getOperand(0));
1426 Inst = TmpInst;
1427 return true;
1428 }
1429 case X86::CMP64i32: {
1430 if (!Inst.getOperand(0).isImm() ||
1431 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1432 return false;
1433
1434 MCInst TmpInst;
1435 TmpInst.setOpcode(X86::CMP64ri8);
1436 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1437 TmpInst.addOperand(Inst.getOperand(0));
1438 Inst = TmpInst;
1439 return true;
1440 }
Devang Patela951f772012-01-19 18:40:55 +00001441 case X86::ADD16i16: {
1442 if (!Inst.getOperand(0).isImm() ||
1443 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1444 return false;
1445
1446 MCInst TmpInst;
1447 TmpInst.setOpcode(X86::ADD16ri8);
1448 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1449 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1450 TmpInst.addOperand(Inst.getOperand(0));
1451 Inst = TmpInst;
1452 return true;
1453 }
1454 case X86::ADD32i32: {
1455 if (!Inst.getOperand(0).isImm() ||
1456 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1457 return false;
1458
1459 MCInst TmpInst;
1460 TmpInst.setOpcode(X86::ADD32ri8);
1461 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1462 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1463 TmpInst.addOperand(Inst.getOperand(0));
1464 Inst = TmpInst;
1465 return true;
1466 }
1467 case X86::ADD64i32: {
1468 if (!Inst.getOperand(0).isImm() ||
1469 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1470 return false;
1471
1472 MCInst TmpInst;
1473 TmpInst.setOpcode(X86::ADD64ri8);
1474 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1475 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1476 TmpInst.addOperand(Inst.getOperand(0));
1477 Inst = TmpInst;
1478 return true;
1479 }
1480 case X86::SUB16i16: {
1481 if (!Inst.getOperand(0).isImm() ||
1482 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1483 return false;
1484
1485 MCInst TmpInst;
1486 TmpInst.setOpcode(X86::SUB16ri8);
1487 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1488 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1489 TmpInst.addOperand(Inst.getOperand(0));
1490 Inst = TmpInst;
1491 return true;
1492 }
1493 case X86::SUB32i32: {
1494 if (!Inst.getOperand(0).isImm() ||
1495 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1496 return false;
1497
1498 MCInst TmpInst;
1499 TmpInst.setOpcode(X86::SUB32ri8);
1500 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1501 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1502 TmpInst.addOperand(Inst.getOperand(0));
1503 Inst = TmpInst;
1504 return true;
1505 }
1506 case X86::SUB64i32: {
1507 if (!Inst.getOperand(0).isImm() ||
1508 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1509 return false;
1510
1511 MCInst TmpInst;
1512 TmpInst.setOpcode(X86::SUB64ri8);
1513 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1514 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1515 TmpInst.addOperand(Inst.getOperand(0));
1516 Inst = TmpInst;
1517 return true;
1518 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001519 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001520}
1521
1522bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001523MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001524 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001525 MCStreamer &Out) {
Chad Rosier3a86e132012-09-03 02:06:46 +00001526 unsigned Kind;
Chad Rosier22685872012-10-01 23:45:51 +00001527 unsigned Opcode;
Chad Rosier64bfcbb2012-08-21 18:14:59 +00001528 unsigned ErrorInfo;
Chad Rosier22685872012-10-01 23:45:51 +00001529 SmallVector<std::pair< unsigned, std::string >, 4> MapAndConstraints;
1530 bool Error = MatchInstruction(IDLoc, Operands, Out, Kind, Opcode,
1531 MapAndConstraints, ErrorInfo);
Chad Rosier32461762012-08-09 22:04:55 +00001532 return Error;
1533}
1534
1535bool X86AsmParser::
Chad Rosier22685872012-10-01 23:45:51 +00001536MatchInstruction(SMLoc IDLoc,
Chad Rosier32461762012-08-09 22:04:55 +00001537 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier22685872012-10-01 23:45:51 +00001538 MCStreamer &Out, unsigned &Kind, unsigned &Opcode,
1539 SmallVectorImpl<std::pair< unsigned, std::string > > &MapAndConstraints,
1540 unsigned &OrigErrorInfo, bool matchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001541 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001542 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1543 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001544 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001545
Chris Lattner7c51a312010-09-29 01:50:45 +00001546 // First, handle aliases that expand to multiple instructions.
1547 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001548 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001549 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001550 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001551 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001552 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001553 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001554 MCInst Inst;
1555 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001556 Inst.setLoc(IDLoc);
Chad Rosier22685872012-10-01 23:45:51 +00001557 if (!matchingInlineAsm)
1558 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001559
Chris Lattner0bb83a82010-09-30 16:39:29 +00001560 const char *Repl =
1561 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001562 .Case("finit", "fninit")
1563 .Case("fsave", "fnsave")
1564 .Case("fstcw", "fnstcw")
1565 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001566 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001567 .Case("fstsw", "fnstsw")
1568 .Case("fstsww", "fnstsw")
1569 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001570 .Default(0);
1571 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001572 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001573 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001574 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001575
Chris Lattnera008e8a2010-09-06 21:54:15 +00001576 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001577 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001578
Daniel Dunbarc918d602010-05-04 16:12:42 +00001579 // First, try a direct match.
Chad Rosier22685872012-10-01 23:45:51 +00001580 switch (MatchInstructionImpl(Operands, Kind, Inst, MapAndConstraints,
1581 OrigErrorInfo, matchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001582 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001583 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001584 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001585 // Some instructions need post-processing to, for example, tweak which
1586 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001587 // individual transformations can chain off each other.
Chad Rosier22685872012-10-01 23:45:51 +00001588 if (!matchingInlineAsm)
1589 while (processInstruction(Inst, Operands))
1590 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001591
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001592 Inst.setLoc(IDLoc);
Chad Rosier22685872012-10-01 23:45:51 +00001593 if (!matchingInlineAsm)
1594 Out.EmitInstruction(Inst);
1595 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001596 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001597 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001598 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
1599 EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001600 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001601 case Match_InvalidOperand:
1602 WasOriginallyInvalidOperand = true;
1603 break;
1604 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001605 break;
1606 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001607
Daniel Dunbarc918d602010-05-04 16:12:42 +00001608 // FIXME: Ideally, we would only attempt suffix matches for things which are
1609 // valid prefixes, and we could just infer the right unambiguous
1610 // type. However, that requires substantially more matcher support than the
1611 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001612
Daniel Dunbarc918d602010-05-04 16:12:42 +00001613 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001614 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001615 SmallString<16> Tmp;
1616 Tmp += Base;
1617 Tmp += ' ';
1618 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001619
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001620 // If this instruction starts with an 'f', then it is a floating point stack
1621 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1622 // 80-bit floating point, which use the suffixes s,l,t respectively.
1623 //
1624 // Otherwise, we assume that this may be an integer instruction, which comes
1625 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1626 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001627
Daniel Dunbarc918d602010-05-04 16:12:42 +00001628 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001629 Tmp[Base.size()] = Suffixes[0];
1630 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001631 unsigned Match1, Match2, Match3, Match4;
Chad Rosierc4d25602012-09-03 03:16:09 +00001632 unsigned tKind;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001633
Chad Rosier22685872012-10-01 23:45:51 +00001634 SmallVector<std::pair< unsigned, std::string >, 4> tMapAndConstraints[4];
1635 Match1 = MatchInstructionImpl(Operands, tKind, Inst, tMapAndConstraints[0],
1636 ErrorInfoIgnore, isParsingIntelSyntax());
Chad Rosierc4d25602012-09-03 03:16:09 +00001637 if (Match1 == Match_Success) Kind = tKind;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001638 Tmp[Base.size()] = Suffixes[1];
Chad Rosier22685872012-10-01 23:45:51 +00001639 Match2 = MatchInstructionImpl(Operands, tKind, Inst, tMapAndConstraints[1],
1640 ErrorInfoIgnore, isParsingIntelSyntax());
Chad Rosierc4d25602012-09-03 03:16:09 +00001641 if (Match2 == Match_Success) Kind = tKind;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001642 Tmp[Base.size()] = Suffixes[2];
Chad Rosier22685872012-10-01 23:45:51 +00001643 Match3 = MatchInstructionImpl(Operands, tKind, Inst, tMapAndConstraints[2],
1644 ErrorInfoIgnore, isParsingIntelSyntax());
Chad Rosierc4d25602012-09-03 03:16:09 +00001645 if (Match3 == Match_Success) Kind = tKind;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001646 Tmp[Base.size()] = Suffixes[3];
Chad Rosier22685872012-10-01 23:45:51 +00001647 Match4 = MatchInstructionImpl(Operands, tKind, Inst, tMapAndConstraints[3],
1648 ErrorInfoIgnore, isParsingIntelSyntax());
Chad Rosierc4d25602012-09-03 03:16:09 +00001649 if (Match4 == Match_Success) Kind = tKind;
Daniel Dunbarc918d602010-05-04 16:12:42 +00001650
1651 // Restore the old token.
1652 Op->setTokenValue(Base);
1653
1654 // If exactly one matched, then we treat that as a successful match (and the
1655 // instruction will already have been filled in correctly, since the failing
1656 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001657 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001658 (Match1 == Match_Success) + (Match2 == Match_Success) +
1659 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001660 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001661 Inst.setLoc(IDLoc);
Chad Rosier22685872012-10-01 23:45:51 +00001662 if (!matchingInlineAsm)
1663 Out.EmitInstruction(Inst);
1664 Opcode = Inst.getOpcode();
1665 // FIXME: Handle the map and constraints.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001666 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001667 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001668
Chris Lattnerec6789f2010-09-06 20:08:02 +00001669 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001670
Daniel Dunbar09062b12010-08-12 00:55:42 +00001671 // If we had multiple suffix matches, then identify this as an ambiguous
1672 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001673 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001674 char MatchChars[4];
1675 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001676 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1677 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1678 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1679 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001680
1681 SmallString<126> Msg;
1682 raw_svector_ostream OS(Msg);
1683 OS << "ambiguous instructions require an explicit suffix (could be ";
1684 for (unsigned i = 0; i != NumMatches; ++i) {
1685 if (i != 0)
1686 OS << ", ";
1687 if (i + 1 == NumMatches)
1688 OS << "or ";
1689 OS << "'" << Base << MatchChars[i] << "'";
1690 }
1691 OS << ")";
Chad Rosierb4fdade2012-08-21 19:36:59 +00001692 Error(IDLoc, OS.str(), EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001693 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001694 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001695
Chris Lattnera008e8a2010-09-06 21:54:15 +00001696 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001697
Chris Lattnera008e8a2010-09-06 21:54:15 +00001698 // If all of the instructions reported an invalid mnemonic, then the original
1699 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001700 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1701 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001702 if (!WasOriginallyInvalidOperand) {
Chad Rosier674101e2012-08-22 19:14:29 +00001703 ArrayRef<SMRange> Ranges = matchingInlineAsm ? EmptyRanges :
1704 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001705 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier674101e2012-08-22 19:14:29 +00001706 Ranges, matchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001707 }
1708
1709 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001710 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001711 if (OrigErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001712 return Error(IDLoc, "too few operands for instruction",
1713 EmptyRanges, matchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001714
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001715 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1716 if (Operand->getStartLoc().isValid()) {
1717 SMRange OperandRange = Operand->getLocRange();
1718 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosierb4fdade2012-08-21 19:36:59 +00001719 OperandRange, matchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001720 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001721 }
1722
Chad Rosierb4fdade2012-08-21 19:36:59 +00001723 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
1724 matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001725 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001726
Chris Lattnerec6789f2010-09-06 20:08:02 +00001727 // If one instruction matched with a missing feature, report this as a
1728 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001729 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1730 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001731 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
1732 EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001733 return true;
1734 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001735
Chris Lattnera008e8a2010-09-06 21:54:15 +00001736 // If one instruction matched with an invalid operand, report this as an
1737 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001738 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1739 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001740 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
1741 matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001742 return true;
1743 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001744
Chris Lattnerec6789f2010-09-06 20:08:02 +00001745 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001746 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
1747 EmptyRanges, matchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001748 return true;
1749}
1750
1751
Devang Pateldd929fc2012-01-12 18:03:40 +00001752bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001753 StringRef IDVal = DirectiveID.getIdentifier();
1754 if (IDVal == ".word")
1755 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001756 else if (IDVal.startswith(".code"))
1757 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001758 else if (IDVal.startswith(".att_syntax")) {
1759 getParser().setAssemblerDialect(0);
1760 return false;
1761 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001762 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001763 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1764 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001765 // FIXME : Handle noprefix
1766 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001767 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001768 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001769 }
1770 return false;
1771 }
Chris Lattner537ca842010-10-30 17:38:55 +00001772 return true;
1773}
1774
1775/// ParseDirectiveWord
1776/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001777bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001778 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1779 for (;;) {
1780 const MCExpr *Value;
1781 if (getParser().ParseExpression(Value))
1782 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001783
Chris Lattner537ca842010-10-30 17:38:55 +00001784 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001785
Chris Lattner537ca842010-10-30 17:38:55 +00001786 if (getLexer().is(AsmToken::EndOfStatement))
1787 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001788
Chris Lattner537ca842010-10-30 17:38:55 +00001789 // FIXME: Improve diagnostic.
1790 if (getLexer().isNot(AsmToken::Comma))
1791 return Error(L, "unexpected token in directive");
1792 Parser.Lex();
1793 }
1794 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001795
Chris Lattner537ca842010-10-30 17:38:55 +00001796 Parser.Lex();
1797 return false;
1798}
1799
Evan Chengbd27f5a2011-07-27 00:38:12 +00001800/// ParseDirectiveCode
1801/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001802bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001803 if (IDVal == ".code32") {
1804 Parser.Lex();
1805 if (is64BitMode()) {
1806 SwitchMode();
1807 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1808 }
1809 } else if (IDVal == ".code64") {
1810 Parser.Lex();
1811 if (!is64BitMode()) {
1812 SwitchMode();
1813 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1814 }
1815 } else {
1816 return Error(L, "unexpected directive " + IDVal);
1817 }
Chris Lattner537ca842010-10-30 17:38:55 +00001818
Evan Chengbd27f5a2011-07-27 00:38:12 +00001819 return false;
1820}
Chris Lattner537ca842010-10-30 17:38:55 +00001821
1822
Sean Callanane88f5522010-01-23 02:43:15 +00001823extern "C" void LLVMInitializeX86AsmLexer();
1824
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001825// Force static initialization.
1826extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001827 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1828 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001829 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001830}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001831
Chris Lattner0692ee62010-09-06 19:11:01 +00001832#define GET_REGISTER_MATCHER
1833#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001834#include "X86GenAsmMatcher.inc"