blob: 2f78b16e4453a6ca8448a9bb6247509550407861 [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();
Devang Pateld37ad242012-01-17 18:00:18 +000056 X86Operand *ParseIntelMemOperand();
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 Rosier95707c92012-10-02 21:49:07 +0000161 Memory,
162 MSAsmWildcard
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000163 } Kind;
164
Chris Lattner29ef9a22010-01-15 18:51:29 +0000165 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000166
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000167 union {
168 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000169 const char *Data;
170 unsigned Length;
171 } Tok;
172
173 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000174 unsigned RegNo;
175 } Reg;
176
177 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000178 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000179 } Imm;
180
181 struct {
182 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000183 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000184 unsigned BaseReg;
185 unsigned IndexReg;
186 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000187 unsigned Size;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000188 } Mem;
Chad Rosier95707c92012-10-02 21:49:07 +0000189
190 struct {
191 unsigned Size;
192 } MSAsm;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000193 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000194
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000195 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000196 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000197
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000198 /// getStartLoc - Get the location of the first token of this operand.
199 SMLoc getStartLoc() const { return StartLoc; }
200 /// getEndLoc - Get the location of the last token of this operand.
201 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000202 /// getLocRange - Get the range between the first and last token of this
203 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000204 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000205
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000206 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000207
Daniel Dunbar20927f22009-08-07 08:26:05 +0000208 StringRef getToken() const {
209 assert(Kind == Token && "Invalid access!");
210 return StringRef(Tok.Data, Tok.Length);
211 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000212 void setTokenValue(StringRef Value) {
213 assert(Kind == Token && "Invalid access!");
214 Tok.Data = Value.data();
215 Tok.Length = Value.size();
216 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000217
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000218 unsigned getReg() const {
219 assert(Kind == Register && "Invalid access!");
220 return Reg.RegNo;
221 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000222
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000223 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000224 assert(Kind == Immediate && "Invalid access!");
225 return Imm.Val;
226 }
227
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000228 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000229 assert(Kind == Memory && "Invalid access!");
230 return Mem.Disp;
231 }
232 unsigned getMemSegReg() const {
233 assert(Kind == Memory && "Invalid access!");
234 return Mem.SegReg;
235 }
236 unsigned getMemBaseReg() const {
237 assert(Kind == Memory && "Invalid access!");
238 return Mem.BaseReg;
239 }
240 unsigned getMemIndexReg() const {
241 assert(Kind == Memory && "Invalid access!");
242 return Mem.IndexReg;
243 }
244 unsigned getMemScale() const {
245 assert(Kind == Memory && "Invalid access!");
246 return Mem.Scale;
247 }
248
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000249 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000250
251 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000252
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000253 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000254 if (!isImm())
255 return false;
256
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000257 // If this isn't a constant expr, just assume it fits and let relaxation
258 // handle it.
259 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
260 if (!CE)
261 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000262
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000263 // Otherwise, check the value is in a range that makes sense for this
264 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000265 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000266 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000267 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000268 if (!isImm())
269 return false;
270
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000271 // If this isn't a constant expr, just assume it fits and let relaxation
272 // handle it.
273 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
274 if (!CE)
275 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000276
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000277 // Otherwise, check the value is in a range that makes sense for this
278 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000279 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000280 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000281 bool isImmZExtu32u8() const {
282 if (!isImm())
283 return false;
284
285 // If this isn't a constant expr, just assume it fits and let relaxation
286 // handle it.
287 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
288 if (!CE)
289 return true;
290
291 // Otherwise, check the value is in a range that makes sense for this
292 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000293 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000294 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000295 bool isImmSExti64i8() const {
296 if (!isImm())
297 return false;
298
299 // If this isn't a constant expr, just assume it fits and let relaxation
300 // handle it.
301 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
302 if (!CE)
303 return true;
304
305 // Otherwise, check the value is in a range that makes sense for this
306 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000307 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000308 }
309 bool isImmSExti64i32() const {
310 if (!isImm())
311 return false;
312
313 // If this isn't a constant expr, just assume it fits and let relaxation
314 // handle it.
315 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
316 if (!CE)
317 return true;
318
319 // Otherwise, check the value is in a range that makes sense for this
320 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000321 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000322 }
323
Daniel Dunbar20927f22009-08-07 08:26:05 +0000324 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000325 bool isMem8() const {
Chad Rosier95707c92012-10-02 21:49:07 +0000326 return (Kind == Memory && (!Mem.Size || Mem.Size == 8)) ||
327 (Kind == MSAsmWildcard && MSAsm.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000328 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000329 bool isMem16() const {
Chad Rosier95707c92012-10-02 21:49:07 +0000330 return (Kind == Memory && (!Mem.Size || Mem.Size == 16)) ||
331 (Kind == MSAsmWildcard && MSAsm.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000332 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000333 bool isMem32() const {
Chad Rosier95707c92012-10-02 21:49:07 +0000334 return (Kind == Memory && (!Mem.Size || Mem.Size == 32)) ||
335 (Kind == MSAsmWildcard && MSAsm.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000336 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000337 bool isMem64() const {
Chad Rosier95707c92012-10-02 21:49:07 +0000338 return (Kind == Memory && (!Mem.Size || Mem.Size == 64)) ||
339 (Kind == MSAsmWildcard && MSAsm.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000340 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000341 bool isMem80() const {
Chad Rosier95707c92012-10-02 21:49:07 +0000342 return (Kind == Memory && (!Mem.Size || Mem.Size == 80)) ||
343 (Kind == MSAsmWildcard && MSAsm.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000344 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000345 bool isMem128() const {
Chad Rosier95707c92012-10-02 21:49:07 +0000346 return (Kind == Memory && (!Mem.Size || Mem.Size == 128)) ||
347 (Kind == MSAsmWildcard && MSAsm.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000348 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000349 bool isMem256() const {
Chad Rosier95707c92012-10-02 21:49:07 +0000350 return (Kind == Memory && (!Mem.Size || Mem.Size == 256)) ||
351 (Kind == MSAsmWildcard && MSAsm.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000352 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000353
Craig Topper75dc33a2012-07-18 04:11:12 +0000354 bool isMemVX32() const {
355 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
356 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
357 }
358 bool isMemVY32() const {
359 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
360 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
361 }
362 bool isMemVX64() const {
363 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
364 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
365 }
366 bool isMemVY64() const {
367 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
368 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
369 }
370
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000371 bool isAbsMem() const {
372 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000373 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000374 }
375
Daniel Dunbar20927f22009-08-07 08:26:05 +0000376 bool isReg() const { return Kind == Register; }
377
Chad Rosier95707c92012-10-02 21:49:07 +0000378 bool isMSAsmWildcard() const { return Kind == MSAsmWildcard; }
379 void setMSAsmWildcard(unsigned Size) {
380 Kind = MSAsmWildcard;
381 this->MSAsm.Size = Size;
382 }
383
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000384 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
385 // Add as immediates when possible.
386 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
387 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
388 else
389 Inst.addOperand(MCOperand::CreateExpr(Expr));
390 }
391
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000392 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000393 assert(N == 1 && "Invalid number of operands!");
394 Inst.addOperand(MCOperand::CreateReg(getReg()));
395 }
396
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000397 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000398 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000399 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000400 }
401
Chad Rosier36b8fed2012-06-27 22:34:28 +0000402 void addMem8Operands(MCInst &Inst, unsigned N) const {
403 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000404 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000405 void addMem16Operands(MCInst &Inst, unsigned N) const {
406 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000407 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000408 void addMem32Operands(MCInst &Inst, unsigned N) const {
409 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000410 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000411 void addMem64Operands(MCInst &Inst, unsigned N) const {
412 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000413 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000414 void addMem80Operands(MCInst &Inst, unsigned N) const {
415 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000416 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000417 void addMem128Operands(MCInst &Inst, unsigned N) const {
418 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000419 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000420 void addMem256Operands(MCInst &Inst, unsigned N) const {
421 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000422 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000423 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
424 addMemOperands(Inst, N);
425 }
426 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
427 addMemOperands(Inst, N);
428 }
429 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
430 addMemOperands(Inst, N);
431 }
432 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
433 addMemOperands(Inst, N);
434 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000435
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000436 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000437 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000438 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
439 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
440 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000441 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000442 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
443 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000444
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000445 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
446 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000447 // Add as immediates when possible.
448 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
449 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
450 else
451 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000452 }
453
Chris Lattnerb4307b32010-01-15 19:28:38 +0000454 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000455 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
456 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000457 Res->Tok.Data = Str.data();
458 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000459 return Res;
460 }
461
Chris Lattner29ef9a22010-01-15 18:51:29 +0000462 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000463 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000464 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000465 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000466 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000467
Chris Lattnerb4307b32010-01-15 19:28:38 +0000468 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
469 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000470 Res->Imm.Val = Val;
471 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000472 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000473
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000474 /// Create an absolute memory operand.
475 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000476 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000477 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
478 Res->Mem.SegReg = 0;
479 Res->Mem.Disp = Disp;
480 Res->Mem.BaseReg = 0;
481 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000482 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000483 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000484 return Res;
485 }
486
487 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000488 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
489 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000490 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
491 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000492 // We should never just have a displacement, that should be parsed as an
493 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000494 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
495
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000496 // The scale should always be one of {1,2,4,8}.
497 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000498 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000499 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000500 Res->Mem.SegReg = SegReg;
501 Res->Mem.Disp = Disp;
502 Res->Mem.BaseReg = BaseReg;
503 Res->Mem.IndexReg = IndexReg;
504 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000505 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000506 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000507 }
508};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000509
Chris Lattner37dfdec2009-07-29 06:33:53 +0000510} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000511
Devang Pateldd929fc2012-01-12 18:03:40 +0000512bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000513 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000514
515 return (Op.isMem() &&
516 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
517 isa<MCConstantExpr>(Op.Mem.Disp) &&
518 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
519 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
520}
521
Devang Pateldd929fc2012-01-12 18:03:40 +0000522bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000523 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000524
Chad Rosier36b8fed2012-06-27 22:34:28 +0000525 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000526 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000527 isa<MCConstantExpr>(Op.Mem.Disp) &&
528 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
529 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
530}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000531
Devang Pateldd929fc2012-01-12 18:03:40 +0000532bool X86AsmParser::ParseRegister(unsigned &RegNo,
533 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000534 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000535 const AsmToken &PercentTok = Parser.getTok();
536 StartLoc = PercentTok.getLoc();
537
538 // If we encounter a %, ignore it. This code handles registers with and
539 // without the prefix, unprefixed registers can occur in cfi directives.
540 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000541 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000542
Sean Callanan18b83232010-01-19 21:44:56 +0000543 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000544 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000545 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000546 return Error(StartLoc, "invalid register name",
547 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000548 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000549
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000550 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000551
Chris Lattner33d60d52010-09-22 04:11:10 +0000552 // If the match failed, try the register name as lowercase.
553 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000554 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000555
Evan Cheng5de728c2011-07-27 23:22:03 +0000556 if (!is64BitMode()) {
557 // FIXME: This should be done using Requires<In32BitMode> and
558 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
559 // checked.
560 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
561 // REX prefix.
562 if (RegNo == X86::RIZ ||
563 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
564 X86II::isX86_64NonExtLowByteReg(RegNo) ||
565 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000566 return Error(StartLoc, "register %"
567 + Tok.getString() + " is only available in 64-bit mode",
568 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000569 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000570
Chris Lattner33d60d52010-09-22 04:11:10 +0000571 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
572 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000573 RegNo = X86::ST0;
574 EndLoc = Tok.getLoc();
575 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000576
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000577 // Check to see if we have '(4)' after %st.
578 if (getLexer().isNot(AsmToken::LParen))
579 return false;
580 // Lex the paren.
581 getParser().Lex();
582
583 const AsmToken &IntTok = Parser.getTok();
584 if (IntTok.isNot(AsmToken::Integer))
585 return Error(IntTok.getLoc(), "expected stack index");
586 switch (IntTok.getIntVal()) {
587 case 0: RegNo = X86::ST0; break;
588 case 1: RegNo = X86::ST1; break;
589 case 2: RegNo = X86::ST2; break;
590 case 3: RegNo = X86::ST3; break;
591 case 4: RegNo = X86::ST4; break;
592 case 5: RegNo = X86::ST5; break;
593 case 6: RegNo = X86::ST6; break;
594 case 7: RegNo = X86::ST7; break;
595 default: return Error(IntTok.getLoc(), "invalid stack index");
596 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000597
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000598 if (getParser().Lex().isNot(AsmToken::RParen))
599 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000600
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000601 EndLoc = Tok.getLoc();
602 Parser.Lex(); // Eat ')'
603 return false;
604 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000605
Chris Lattner645b2092010-06-24 07:29:18 +0000606 // If this is "db[0-7]", match it as an alias
607 // for dr[0-7].
608 if (RegNo == 0 && Tok.getString().size() == 3 &&
609 Tok.getString().startswith("db")) {
610 switch (Tok.getString()[2]) {
611 case '0': RegNo = X86::DR0; break;
612 case '1': RegNo = X86::DR1; break;
613 case '2': RegNo = X86::DR2; break;
614 case '3': RegNo = X86::DR3; break;
615 case '4': RegNo = X86::DR4; break;
616 case '5': RegNo = X86::DR5; break;
617 case '6': RegNo = X86::DR6; break;
618 case '7': RegNo = X86::DR7; break;
619 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000620
Chris Lattner645b2092010-06-24 07:29:18 +0000621 if (RegNo != 0) {
622 EndLoc = Tok.getLoc();
623 Parser.Lex(); // Eat it.
624 return false;
625 }
626 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000627
Devang Patel1aea4302012-01-20 22:32:05 +0000628 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000629 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000630 return Error(StartLoc, "invalid register name",
631 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000632 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000633
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000634 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000635 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000636 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000637}
638
Devang Pateldd929fc2012-01-12 18:03:40 +0000639X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000640 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000641 return ParseIntelOperand();
642 return ParseATTOperand();
643}
644
Devang Pateld37ad242012-01-17 18:00:18 +0000645/// getIntelMemOperandSize - Return intel memory operand size.
646static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000647 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000648 .Cases("BYTE", "byte", 8)
649 .Cases("WORD", "word", 16)
650 .Cases("DWORD", "dword", 32)
651 .Cases("QWORD", "qword", 64)
652 .Cases("XWORD", "xword", 80)
653 .Cases("XMMWORD", "xmmword", 128)
654 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000655 .Default(0);
656 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000657}
658
Devang Patel7c64fe62012-01-23 18:31:58 +0000659X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
660 unsigned Size) {
661 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000662 SMLoc Start = Parser.getTok().getLoc(), End;
663
Devang Pateld37ad242012-01-17 18:00:18 +0000664 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
665 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
666
667 // Eat '['
668 if (getLexer().isNot(AsmToken::LBrac))
669 return ErrorOperand(Start, "Expected '[' token!");
670 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000671
Devang Pateld37ad242012-01-17 18:00:18 +0000672 if (getLexer().is(AsmToken::Identifier)) {
673 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000674 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000675 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000676 if (getParser().ParseExpression(Disp, End)) return 0;
677 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000678 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000679 Parser.Lex();
680 return X86Operand::CreateMem(Disp, Start, End, Size);
681 }
682 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000683 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000684 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000685 SMLoc Loc = Parser.getTok().getLoc();
686 if (getLexer().is(AsmToken::RBrac)) {
687 // Handle '[' number ']'
688 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000689 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
690 if (SegReg)
691 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
692 Start, End, Size);
693 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000694 } else if (getLexer().is(AsmToken::Star)) {
695 // Handle '[' Scale*IndexReg ']'
696 Parser.Lex();
697 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000698 if (ParseRegister(IndexReg, IdxRegLoc, End))
699 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000700 Scale = Val;
701 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000702 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000703 }
704
705 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
706 bool isPlus = getLexer().is(AsmToken::Plus);
707 Parser.Lex();
708 SMLoc PlusLoc = Parser.getTok().getLoc();
709 if (getLexer().is(AsmToken::Integer)) {
710 int64_t Val = Parser.getTok().getIntVal();
711 Parser.Lex();
712 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000713 Parser.Lex();
714 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000715 if (ParseRegister(IndexReg, IdxRegLoc, End))
716 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000717 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000718 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000719 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000720 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000721 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000722 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000723 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000724 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000725 End = Parser.getTok().getLoc();
726 if (!IndexReg)
727 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000728 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000729 }
Devang Pateld37ad242012-01-17 18:00:18 +0000730 }
731
732 if (getLexer().isNot(AsmToken::RBrac))
733 if (getParser().ParseExpression(Disp, End)) return 0;
734
735 End = Parser.getTok().getLoc();
736 if (getLexer().isNot(AsmToken::RBrac))
737 return ErrorOperand(End, "expected ']' token!");
738 Parser.Lex();
739 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000740
741 // handle [-42]
742 if (!BaseReg && !IndexReg)
743 return X86Operand::CreateMem(Disp, Start, End, Size);
744
Devang Pateld37ad242012-01-17 18:00:18 +0000745 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000746 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000747}
748
749/// ParseIntelMemOperand - Parse intel style memory operand.
750X86Operand *X86AsmParser::ParseIntelMemOperand() {
751 const AsmToken &Tok = Parser.getTok();
752 SMLoc Start = Parser.getTok().getLoc(), End;
Devang Patel7c64fe62012-01-23 18:31:58 +0000753 unsigned SegReg = 0;
Devang Pateld37ad242012-01-17 18:00:18 +0000754
755 unsigned Size = getIntelMemOperandSize(Tok.getString());
756 if (Size) {
757 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000758 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
759 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000760 Parser.Lex();
761 }
762
763 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000764 return ParseIntelBracExpression(SegReg, Size);
765
766 if (!ParseRegister(SegReg, Start, End)) {
767 // Handel SegReg : [ ... ]
768 if (getLexer().isNot(AsmToken::Colon))
769 return ErrorOperand(Start, "Expected ':' token!");
770 Parser.Lex(); // Eat :
771 if (getLexer().isNot(AsmToken::LBrac))
772 return ErrorOperand(Start, "Expected '[' token!");
773 return ParseIntelBracExpression(SegReg, Size);
774 }
Devang Pateld37ad242012-01-17 18:00:18 +0000775
776 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
777 if (getParser().ParseExpression(Disp, End)) return 0;
778 return X86Operand::CreateMem(Disp, Start, End, Size);
779}
780
781X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000782 SMLoc Start = Parser.getTok().getLoc(), End;
783
784 // immediate.
785 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
786 getLexer().is(AsmToken::Minus)) {
787 const MCExpr *Val;
788 if (!getParser().ParseExpression(Val, End)) {
789 End = Parser.getTok().getLoc();
790 return X86Operand::CreateImm(Val, Start, End);
791 }
792 }
793
Devang Patel0a338862012-01-12 01:36:43 +0000794 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000795 unsigned RegNo = 0;
796 if (!ParseRegister(RegNo, Start, End)) {
Devang Patel0a338862012-01-12 01:36:43 +0000797 End = Parser.getTok().getLoc();
798 return X86Operand::CreateReg(RegNo, Start, End);
799 }
800
801 // mem operand
Devang Pateld37ad242012-01-17 18:00:18 +0000802 return ParseIntelMemOperand();
Devang Patel0a338862012-01-12 01:36:43 +0000803}
804
Devang Pateldd929fc2012-01-12 18:03:40 +0000805X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000806 switch (getLexer().getKind()) {
807 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000808 // Parse a memory operand with no segment register.
809 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000810 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000811 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000812 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000813 SMLoc Start, End;
814 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000815 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000816 Error(Start, "%eiz and %riz can only be used as index registers",
817 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000818 return 0;
819 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000820
Chris Lattnereef6d782010-04-17 18:56:34 +0000821 // If this is a segment register followed by a ':', then this is the start
822 // of a memory reference, otherwise this is a normal register reference.
823 if (getLexer().isNot(AsmToken::Colon))
824 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000825
826
Chris Lattnereef6d782010-04-17 18:56:34 +0000827 getParser().Lex(); // Eat the colon.
828 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000829 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000830 case AsmToken::Dollar: {
831 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000832 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000833 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000834 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000835 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000836 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000837 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000838 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000839 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000840}
841
Chris Lattnereef6d782010-04-17 18:56:34 +0000842/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
843/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000844X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000845
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000846 // We have to disambiguate a parenthesized expression "(4+5)" from the start
847 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000848 // only way to do this without lookahead is to eat the '(' and see what is
849 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000850 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000851 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000852 SMLoc ExprEnd;
853 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000854
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000855 // After parsing the base expression we could either have a parenthesized
856 // memory address or not. If not, return now. If so, eat the (.
857 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000858 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000859 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000860 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000861 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000862 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000863
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000864 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000865 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000866 } else {
867 // Okay, we have a '('. We don't know if this is an expression or not, but
868 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000869 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000870 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000871
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000872 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000873 // Nothing to do here, fall into the code below with the '(' part of the
874 // memory operand consumed.
875 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000876 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000877
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000878 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000879 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000880 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000881
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000882 // After parsing the base expression we could either have a parenthesized
883 // memory address or not. If not, return now. If so, eat the (.
884 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000885 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000886 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000887 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000888 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000889 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000890
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000891 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000892 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000893 }
894 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000895
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000896 // If we reached here, then we just ate the ( of the memory operand. Process
897 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000898 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000899 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000900
Chris Lattner29ef9a22010-01-15 18:51:29 +0000901 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000902 SMLoc StartLoc, EndLoc;
903 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000904 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000905 Error(StartLoc, "eiz and riz can only be used as index registers",
906 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000907 return 0;
908 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000909 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000910
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000911 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000912 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000913 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000914
915 // Following the comma we should have either an index register, or a scale
916 // value. We don't support the later form, but we want to parse it
917 // correctly.
918 //
919 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000920 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000921 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000922 SMLoc L;
923 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000924
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000925 if (getLexer().isNot(AsmToken::RParen)) {
926 // Parse the scale amount:
927 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000928 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000929 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000930 "expected comma in scale expression");
931 return 0;
932 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000933 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000934
935 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000936 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000937
938 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000939 if (getParser().ParseAbsoluteExpression(ScaleVal)){
940 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000941 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +0000942 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000943
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000944 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000945 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
946 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
947 return 0;
948 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000949 Scale = (unsigned)ScaleVal;
950 }
951 }
952 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000953 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000954 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000955 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000956
957 int64_t Value;
958 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000959 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000960
Daniel Dunbaree910252010-08-24 19:13:38 +0000961 if (Value != 1)
962 Warning(Loc, "scale factor without index register is ignored");
963 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000964 }
965 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000966
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000967 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000968 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000969 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000970 return 0;
971 }
Sean Callanan18b83232010-01-19 21:44:56 +0000972 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000973 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000974
Kevin Enderby84faf652012-03-12 21:32:09 +0000975 // If we have both a base register and an index register make sure they are
976 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +0000977 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +0000978 if (BaseReg != 0 && IndexReg != 0) {
979 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000980 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
981 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000982 IndexReg != X86::RIZ) {
983 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
984 return 0;
985 }
986 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000987 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
988 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000989 IndexReg != X86::EIZ){
990 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
991 return 0;
992 }
993 }
994
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000995 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
996 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000997}
998
Devang Pateldd929fc2012-01-12 18:03:40 +0000999bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +00001000ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001001 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +00001002 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001003
Chris Lattnerd8f71792010-11-28 20:23:50 +00001004 // FIXME: Hack to recognize setneb as setne.
1005 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1006 PatchedName != "setb" && PatchedName != "setnb")
1007 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001008
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001009 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1010 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001011 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001012 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1013 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001014 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001015 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001016 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001017 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001018 .Case("eq", 0x00)
1019 .Case("lt", 0x01)
1020 .Case("le", 0x02)
1021 .Case("unord", 0x03)
1022 .Case("neq", 0x04)
1023 .Case("nlt", 0x05)
1024 .Case("nle", 0x06)
1025 .Case("ord", 0x07)
1026 /* AVX only from here */
1027 .Case("eq_uq", 0x08)
1028 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001029 .Case("ngt", 0x0A)
1030 .Case("false", 0x0B)
1031 .Case("neq_oq", 0x0C)
1032 .Case("ge", 0x0D)
1033 .Case("gt", 0x0E)
1034 .Case("true", 0x0F)
1035 .Case("eq_os", 0x10)
1036 .Case("lt_oq", 0x11)
1037 .Case("le_oq", 0x12)
1038 .Case("unord_s", 0x13)
1039 .Case("neq_us", 0x14)
1040 .Case("nlt_uq", 0x15)
1041 .Case("nle_uq", 0x16)
1042 .Case("ord_s", 0x17)
1043 .Case("eq_us", 0x18)
1044 .Case("nge_uq", 0x19)
1045 .Case("ngt_uq", 0x1A)
1046 .Case("false_os", 0x1B)
1047 .Case("neq_os", 0x1C)
1048 .Case("ge_oq", 0x1D)
1049 .Case("gt_oq", 0x1E)
1050 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001051 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001052 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001053 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1054 getParser().getContext());
1055 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001056 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001057 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001058 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001059 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001060 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001061 } else {
1062 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001063 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001064 }
1065 }
1066 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001067
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001068 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001069
Devang Patel885f65b2012-01-30 22:47:12 +00001070 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001071 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001072
Chris Lattner2544f422010-09-08 05:17:37 +00001073 // Determine whether this is an instruction prefix.
1074 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001075 Name == "lock" || Name == "rep" ||
1076 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001077 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001078 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001079
1080
Chris Lattner2544f422010-09-08 05:17:37 +00001081 // This does the actual operand parsing. Don't parse any more if we have a
1082 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1083 // just want to parse the "lock" as the first instruction and the "incl" as
1084 // the next one.
1085 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001086
1087 // Parse '*' modifier.
1088 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001089 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001090 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001091 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001092 }
1093
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001094 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001095 if (X86Operand *Op = ParseOperand())
1096 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001097 else {
1098 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001099 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001100 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001101
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001102 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001103 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001104
1105 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001106 if (X86Operand *Op = ParseOperand())
1107 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001108 else {
1109 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001110 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001111 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001112 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001113
Chris Lattnercbf8a982010-09-11 16:18:25 +00001114 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001115 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001116 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001117 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001118 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001119 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001120
Chris Lattner2544f422010-09-08 05:17:37 +00001121 if (getLexer().is(AsmToken::EndOfStatement))
1122 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001123 else if (isPrefix && getLexer().is(AsmToken::Slash))
1124 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001125
Devang Patel885f65b2012-01-30 22:47:12 +00001126 if (ExtraImmOp && isParsingIntelSyntax())
1127 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1128
Chris Lattner98c870f2010-11-06 19:25:43 +00001129 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1130 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1131 // documented form in various unofficial manuals, so a lot of code uses it.
1132 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1133 Operands.size() == 3) {
1134 X86Operand &Op = *(X86Operand*)Operands.back();
1135 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1136 isa<MCConstantExpr>(Op.Mem.Disp) &&
1137 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1138 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1139 SMLoc Loc = Op.getEndLoc();
1140 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1141 delete &Op;
1142 }
1143 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001144 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1145 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1146 Operands.size() == 3) {
1147 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1148 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1149 isa<MCConstantExpr>(Op.Mem.Disp) &&
1150 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1151 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1152 SMLoc Loc = Op.getEndLoc();
1153 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1154 delete &Op;
1155 }
1156 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001157 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1158 if (Name.startswith("ins") && Operands.size() == 3 &&
1159 (Name == "insb" || Name == "insw" || Name == "insl")) {
1160 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1161 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1162 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1163 Operands.pop_back();
1164 Operands.pop_back();
1165 delete &Op;
1166 delete &Op2;
1167 }
1168 }
1169
1170 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1171 if (Name.startswith("outs") && Operands.size() == 3 &&
1172 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1173 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1174 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1175 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1176 Operands.pop_back();
1177 Operands.pop_back();
1178 delete &Op;
1179 delete &Op2;
1180 }
1181 }
1182
1183 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1184 if (Name.startswith("movs") && Operands.size() == 3 &&
1185 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001186 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001187 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1188 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1189 if (isSrcOp(Op) && isDstOp(Op2)) {
1190 Operands.pop_back();
1191 Operands.pop_back();
1192 delete &Op;
1193 delete &Op2;
1194 }
1195 }
1196 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1197 if (Name.startswith("lods") && Operands.size() == 3 &&
1198 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001199 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001200 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1201 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1202 if (isSrcOp(*Op1) && Op2->isReg()) {
1203 const char *ins;
1204 unsigned reg = Op2->getReg();
1205 bool isLods = Name == "lods";
1206 if (reg == X86::AL && (isLods || Name == "lodsb"))
1207 ins = "lodsb";
1208 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1209 ins = "lodsw";
1210 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1211 ins = "lodsl";
1212 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1213 ins = "lodsq";
1214 else
1215 ins = NULL;
1216 if (ins != NULL) {
1217 Operands.pop_back();
1218 Operands.pop_back();
1219 delete Op1;
1220 delete Op2;
1221 if (Name != ins)
1222 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1223 }
1224 }
1225 }
1226 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1227 if (Name.startswith("stos") && Operands.size() == 3 &&
1228 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001229 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001230 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1231 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1232 if (isDstOp(*Op2) && Op1->isReg()) {
1233 const char *ins;
1234 unsigned reg = Op1->getReg();
1235 bool isStos = Name == "stos";
1236 if (reg == X86::AL && (isStos || Name == "stosb"))
1237 ins = "stosb";
1238 else if (reg == X86::AX && (isStos || Name == "stosw"))
1239 ins = "stosw";
1240 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1241 ins = "stosl";
1242 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1243 ins = "stosq";
1244 else
1245 ins = NULL;
1246 if (ins != NULL) {
1247 Operands.pop_back();
1248 Operands.pop_back();
1249 delete Op1;
1250 delete Op2;
1251 if (Name != ins)
1252 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1253 }
1254 }
1255 }
1256
Chris Lattnere9e16a32010-09-15 04:33:27 +00001257 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001258 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001259 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001260 Name.startswith("shl") || Name.startswith("sal") ||
1261 Name.startswith("rcl") || Name.startswith("rcr") ||
1262 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001263 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001264 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001265 // Intel syntax
1266 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1267 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001268 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1269 delete Operands[2];
1270 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001271 }
1272 } else {
1273 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1274 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001275 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1276 delete Operands[1];
1277 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001278 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001279 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001280 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001281
Chris Lattner15f89512011-04-09 19:41:05 +00001282 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1283 // instalias with an immediate operand yet.
1284 if (Name == "int" && Operands.size() == 2) {
1285 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1286 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1287 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1288 delete Operands[1];
1289 Operands.erase(Operands.begin() + 1);
1290 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1291 }
1292 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001293
Chris Lattner98986712010-01-14 22:21:20 +00001294 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001295}
1296
Devang Pateldd929fc2012-01-12 18:03:40 +00001297bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001298processInstruction(MCInst &Inst,
1299 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1300 switch (Inst.getOpcode()) {
1301 default: return false;
1302 case X86::AND16i16: {
1303 if (!Inst.getOperand(0).isImm() ||
1304 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1305 return false;
1306
1307 MCInst TmpInst;
1308 TmpInst.setOpcode(X86::AND16ri8);
1309 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1310 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1311 TmpInst.addOperand(Inst.getOperand(0));
1312 Inst = TmpInst;
1313 return true;
1314 }
1315 case X86::AND32i32: {
1316 if (!Inst.getOperand(0).isImm() ||
1317 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1318 return false;
1319
1320 MCInst TmpInst;
1321 TmpInst.setOpcode(X86::AND32ri8);
1322 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1323 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1324 TmpInst.addOperand(Inst.getOperand(0));
1325 Inst = TmpInst;
1326 return true;
1327 }
1328 case X86::AND64i32: {
1329 if (!Inst.getOperand(0).isImm() ||
1330 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1331 return false;
1332
1333 MCInst TmpInst;
1334 TmpInst.setOpcode(X86::AND64ri8);
1335 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1336 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1337 TmpInst.addOperand(Inst.getOperand(0));
1338 Inst = TmpInst;
1339 return true;
1340 }
Devang Patelac0f0482012-01-19 17:53:25 +00001341 case X86::XOR16i16: {
1342 if (!Inst.getOperand(0).isImm() ||
1343 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1344 return false;
1345
1346 MCInst TmpInst;
1347 TmpInst.setOpcode(X86::XOR16ri8);
1348 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1349 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1350 TmpInst.addOperand(Inst.getOperand(0));
1351 Inst = TmpInst;
1352 return true;
1353 }
1354 case X86::XOR32i32: {
1355 if (!Inst.getOperand(0).isImm() ||
1356 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1357 return false;
1358
1359 MCInst TmpInst;
1360 TmpInst.setOpcode(X86::XOR32ri8);
1361 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1362 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1363 TmpInst.addOperand(Inst.getOperand(0));
1364 Inst = TmpInst;
1365 return true;
1366 }
1367 case X86::XOR64i32: {
1368 if (!Inst.getOperand(0).isImm() ||
1369 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1370 return false;
1371
1372 MCInst TmpInst;
1373 TmpInst.setOpcode(X86::XOR64ri8);
1374 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1375 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1376 TmpInst.addOperand(Inst.getOperand(0));
1377 Inst = TmpInst;
1378 return true;
1379 }
1380 case X86::OR16i16: {
1381 if (!Inst.getOperand(0).isImm() ||
1382 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1383 return false;
1384
1385 MCInst TmpInst;
1386 TmpInst.setOpcode(X86::OR16ri8);
1387 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1388 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1389 TmpInst.addOperand(Inst.getOperand(0));
1390 Inst = TmpInst;
1391 return true;
1392 }
1393 case X86::OR32i32: {
1394 if (!Inst.getOperand(0).isImm() ||
1395 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1396 return false;
1397
1398 MCInst TmpInst;
1399 TmpInst.setOpcode(X86::OR32ri8);
1400 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1401 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1402 TmpInst.addOperand(Inst.getOperand(0));
1403 Inst = TmpInst;
1404 return true;
1405 }
1406 case X86::OR64i32: {
1407 if (!Inst.getOperand(0).isImm() ||
1408 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1409 return false;
1410
1411 MCInst TmpInst;
1412 TmpInst.setOpcode(X86::OR64ri8);
1413 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1414 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1415 TmpInst.addOperand(Inst.getOperand(0));
1416 Inst = TmpInst;
1417 return true;
1418 }
1419 case X86::CMP16i16: {
1420 if (!Inst.getOperand(0).isImm() ||
1421 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1422 return false;
1423
1424 MCInst TmpInst;
1425 TmpInst.setOpcode(X86::CMP16ri8);
1426 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1427 TmpInst.addOperand(Inst.getOperand(0));
1428 Inst = TmpInst;
1429 return true;
1430 }
1431 case X86::CMP32i32: {
1432 if (!Inst.getOperand(0).isImm() ||
1433 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1434 return false;
1435
1436 MCInst TmpInst;
1437 TmpInst.setOpcode(X86::CMP32ri8);
1438 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1439 TmpInst.addOperand(Inst.getOperand(0));
1440 Inst = TmpInst;
1441 return true;
1442 }
1443 case X86::CMP64i32: {
1444 if (!Inst.getOperand(0).isImm() ||
1445 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1446 return false;
1447
1448 MCInst TmpInst;
1449 TmpInst.setOpcode(X86::CMP64ri8);
1450 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1451 TmpInst.addOperand(Inst.getOperand(0));
1452 Inst = TmpInst;
1453 return true;
1454 }
Devang Patela951f772012-01-19 18:40:55 +00001455 case X86::ADD16i16: {
1456 if (!Inst.getOperand(0).isImm() ||
1457 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1458 return false;
1459
1460 MCInst TmpInst;
1461 TmpInst.setOpcode(X86::ADD16ri8);
1462 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1463 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1464 TmpInst.addOperand(Inst.getOperand(0));
1465 Inst = TmpInst;
1466 return true;
1467 }
1468 case X86::ADD32i32: {
1469 if (!Inst.getOperand(0).isImm() ||
1470 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1471 return false;
1472
1473 MCInst TmpInst;
1474 TmpInst.setOpcode(X86::ADD32ri8);
1475 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1476 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1477 TmpInst.addOperand(Inst.getOperand(0));
1478 Inst = TmpInst;
1479 return true;
1480 }
1481 case X86::ADD64i32: {
1482 if (!Inst.getOperand(0).isImm() ||
1483 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1484 return false;
1485
1486 MCInst TmpInst;
1487 TmpInst.setOpcode(X86::ADD64ri8);
1488 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1489 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1490 TmpInst.addOperand(Inst.getOperand(0));
1491 Inst = TmpInst;
1492 return true;
1493 }
1494 case X86::SUB16i16: {
1495 if (!Inst.getOperand(0).isImm() ||
1496 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1497 return false;
1498
1499 MCInst TmpInst;
1500 TmpInst.setOpcode(X86::SUB16ri8);
1501 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1502 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1503 TmpInst.addOperand(Inst.getOperand(0));
1504 Inst = TmpInst;
1505 return true;
1506 }
1507 case X86::SUB32i32: {
1508 if (!Inst.getOperand(0).isImm() ||
1509 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1510 return false;
1511
1512 MCInst TmpInst;
1513 TmpInst.setOpcode(X86::SUB32ri8);
1514 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1515 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1516 TmpInst.addOperand(Inst.getOperand(0));
1517 Inst = TmpInst;
1518 return true;
1519 }
1520 case X86::SUB64i32: {
1521 if (!Inst.getOperand(0).isImm() ||
1522 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1523 return false;
1524
1525 MCInst TmpInst;
1526 TmpInst.setOpcode(X86::SUB64ri8);
1527 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1528 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1529 TmpInst.addOperand(Inst.getOperand(0));
1530 Inst = TmpInst;
1531 return true;
1532 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001533 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001534}
1535
1536bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001537MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001538 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001539 MCStreamer &Out) {
Chad Rosier3a86e132012-09-03 02:06:46 +00001540 unsigned Kind;
Chad Rosier22685872012-10-01 23:45:51 +00001541 unsigned Opcode;
Chad Rosier64bfcbb2012-08-21 18:14:59 +00001542 unsigned ErrorInfo;
Chad Rosier22685872012-10-01 23:45:51 +00001543 SmallVector<std::pair< unsigned, std::string >, 4> MapAndConstraints;
1544 bool Error = MatchInstruction(IDLoc, Operands, Out, Kind, Opcode,
1545 MapAndConstraints, ErrorInfo);
Chad Rosier32461762012-08-09 22:04:55 +00001546 return Error;
1547}
1548
1549bool X86AsmParser::
Chad Rosier22685872012-10-01 23:45:51 +00001550MatchInstruction(SMLoc IDLoc,
Chad Rosier32461762012-08-09 22:04:55 +00001551 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier22685872012-10-01 23:45:51 +00001552 MCStreamer &Out, unsigned &Kind, unsigned &Opcode,
1553 SmallVectorImpl<std::pair< unsigned, std::string > > &MapAndConstraints,
1554 unsigned &OrigErrorInfo, bool matchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001555 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001556 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1557 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001558 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001559
Chris Lattner7c51a312010-09-29 01:50:45 +00001560 // First, handle aliases that expand to multiple instructions.
1561 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001562 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001563 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001564 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001565 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001566 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001567 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001568 MCInst Inst;
1569 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001570 Inst.setLoc(IDLoc);
Chad Rosier22685872012-10-01 23:45:51 +00001571 if (!matchingInlineAsm)
1572 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001573
Chris Lattner0bb83a82010-09-30 16:39:29 +00001574 const char *Repl =
1575 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001576 .Case("finit", "fninit")
1577 .Case("fsave", "fnsave")
1578 .Case("fstcw", "fnstcw")
1579 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001580 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001581 .Case("fstsw", "fnstsw")
1582 .Case("fstsww", "fnstsw")
1583 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001584 .Default(0);
1585 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001586 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001587 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001588 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001589
Chris Lattnera008e8a2010-09-06 21:54:15 +00001590 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001591 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001592
Daniel Dunbarc918d602010-05-04 16:12:42 +00001593 // First, try a direct match.
Chad Rosier22685872012-10-01 23:45:51 +00001594 switch (MatchInstructionImpl(Operands, Kind, Inst, MapAndConstraints,
1595 OrigErrorInfo, matchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001596 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001597 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001598 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001599 // Some instructions need post-processing to, for example, tweak which
1600 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001601 // individual transformations can chain off each other.
Chad Rosier22685872012-10-01 23:45:51 +00001602 if (!matchingInlineAsm)
1603 while (processInstruction(Inst, Operands))
1604 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001605
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001606 Inst.setLoc(IDLoc);
Chad Rosier22685872012-10-01 23:45:51 +00001607 if (!matchingInlineAsm)
1608 Out.EmitInstruction(Inst);
1609 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001610 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001611 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001612 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
1613 EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001614 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001615 case Match_InvalidOperand:
1616 WasOriginallyInvalidOperand = true;
1617 break;
1618 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001619 break;
1620 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001621
Daniel Dunbarc918d602010-05-04 16:12:42 +00001622 // FIXME: Ideally, we would only attempt suffix matches for things which are
1623 // valid prefixes, and we could just infer the right unambiguous
1624 // type. However, that requires substantially more matcher support than the
1625 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001626
Daniel Dunbarc918d602010-05-04 16:12:42 +00001627 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001628 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001629 SmallString<16> Tmp;
1630 Tmp += Base;
1631 Tmp += ' ';
1632 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001633
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001634 // If this instruction starts with an 'f', then it is a floating point stack
1635 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1636 // 80-bit floating point, which use the suffixes s,l,t respectively.
1637 //
1638 // Otherwise, we assume that this may be an integer instruction, which comes
1639 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1640 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001641
Daniel Dunbarc918d602010-05-04 16:12:42 +00001642 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001643 Tmp[Base.size()] = Suffixes[0];
1644 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001645 unsigned Match1, Match2, Match3, Match4;
Chad Rosierc4d25602012-09-03 03:16:09 +00001646 unsigned tKind;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001647
Chad Rosier22685872012-10-01 23:45:51 +00001648 SmallVector<std::pair< unsigned, std::string >, 4> tMapAndConstraints[4];
1649 Match1 = MatchInstructionImpl(Operands, tKind, Inst, tMapAndConstraints[0],
1650 ErrorInfoIgnore, isParsingIntelSyntax());
Chad Rosierc4d25602012-09-03 03:16:09 +00001651 if (Match1 == Match_Success) Kind = tKind;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001652 Tmp[Base.size()] = Suffixes[1];
Chad Rosier22685872012-10-01 23:45:51 +00001653 Match2 = MatchInstructionImpl(Operands, tKind, Inst, tMapAndConstraints[1],
1654 ErrorInfoIgnore, isParsingIntelSyntax());
Chad Rosierc4d25602012-09-03 03:16:09 +00001655 if (Match2 == Match_Success) Kind = tKind;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001656 Tmp[Base.size()] = Suffixes[2];
Chad Rosier22685872012-10-01 23:45:51 +00001657 Match3 = MatchInstructionImpl(Operands, tKind, Inst, tMapAndConstraints[2],
1658 ErrorInfoIgnore, isParsingIntelSyntax());
Chad Rosierc4d25602012-09-03 03:16:09 +00001659 if (Match3 == Match_Success) Kind = tKind;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001660 Tmp[Base.size()] = Suffixes[3];
Chad Rosier22685872012-10-01 23:45:51 +00001661 Match4 = MatchInstructionImpl(Operands, tKind, Inst, tMapAndConstraints[3],
1662 ErrorInfoIgnore, isParsingIntelSyntax());
Chad Rosierc4d25602012-09-03 03:16:09 +00001663 if (Match4 == Match_Success) Kind = tKind;
Daniel Dunbarc918d602010-05-04 16:12:42 +00001664
1665 // Restore the old token.
1666 Op->setTokenValue(Base);
1667
1668 // If exactly one matched, then we treat that as a successful match (and the
1669 // instruction will already have been filled in correctly, since the failing
1670 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001671 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001672 (Match1 == Match_Success) + (Match2 == Match_Success) +
1673 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001674 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001675 Inst.setLoc(IDLoc);
Chad Rosier22685872012-10-01 23:45:51 +00001676 if (!matchingInlineAsm)
1677 Out.EmitInstruction(Inst);
1678 Opcode = Inst.getOpcode();
1679 // FIXME: Handle the map and constraints.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001680 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001681 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001682
Chris Lattnerec6789f2010-09-06 20:08:02 +00001683 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001684
Daniel Dunbar09062b12010-08-12 00:55:42 +00001685 // If we had multiple suffix matches, then identify this as an ambiguous
1686 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001687 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001688 char MatchChars[4];
1689 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001690 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1691 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1692 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1693 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001694
1695 SmallString<126> Msg;
1696 raw_svector_ostream OS(Msg);
1697 OS << "ambiguous instructions require an explicit suffix (could be ";
1698 for (unsigned i = 0; i != NumMatches; ++i) {
1699 if (i != 0)
1700 OS << ", ";
1701 if (i + 1 == NumMatches)
1702 OS << "or ";
1703 OS << "'" << Base << MatchChars[i] << "'";
1704 }
1705 OS << ")";
Chad Rosierb4fdade2012-08-21 19:36:59 +00001706 Error(IDLoc, OS.str(), EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001707 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001708 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001709
Chris Lattnera008e8a2010-09-06 21:54:15 +00001710 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001711
Chris Lattnera008e8a2010-09-06 21:54:15 +00001712 // If all of the instructions reported an invalid mnemonic, then the original
1713 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001714 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1715 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001716 if (!WasOriginallyInvalidOperand) {
Chad Rosier674101e2012-08-22 19:14:29 +00001717 ArrayRef<SMRange> Ranges = matchingInlineAsm ? EmptyRanges :
1718 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001719 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier674101e2012-08-22 19:14:29 +00001720 Ranges, matchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001721 }
1722
1723 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001724 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001725 if (OrigErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001726 return Error(IDLoc, "too few operands for instruction",
1727 EmptyRanges, matchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001728
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001729 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1730 if (Operand->getStartLoc().isValid()) {
1731 SMRange OperandRange = Operand->getLocRange();
1732 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosierb4fdade2012-08-21 19:36:59 +00001733 OperandRange, matchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001734 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001735 }
1736
Chad Rosierb4fdade2012-08-21 19:36:59 +00001737 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
1738 matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001739 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001740
Chris Lattnerec6789f2010-09-06 20:08:02 +00001741 // If one instruction matched with a missing feature, report this as a
1742 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001743 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1744 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001745 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
1746 EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001747 return true;
1748 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001749
Chris Lattnera008e8a2010-09-06 21:54:15 +00001750 // If one instruction matched with an invalid operand, report this as an
1751 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001752 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1753 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001754 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
1755 matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001756 return true;
1757 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001758
Chris Lattnerec6789f2010-09-06 20:08:02 +00001759 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001760 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
1761 EmptyRanges, matchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001762 return true;
1763}
1764
1765
Devang Pateldd929fc2012-01-12 18:03:40 +00001766bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001767 StringRef IDVal = DirectiveID.getIdentifier();
1768 if (IDVal == ".word")
1769 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001770 else if (IDVal.startswith(".code"))
1771 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001772 else if (IDVal.startswith(".att_syntax")) {
1773 getParser().setAssemblerDialect(0);
1774 return false;
1775 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001776 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001777 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1778 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001779 // FIXME : Handle noprefix
1780 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001781 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001782 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001783 }
1784 return false;
1785 }
Chris Lattner537ca842010-10-30 17:38:55 +00001786 return true;
1787}
1788
1789/// ParseDirectiveWord
1790/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001791bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001792 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1793 for (;;) {
1794 const MCExpr *Value;
1795 if (getParser().ParseExpression(Value))
1796 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001797
Chris Lattner537ca842010-10-30 17:38:55 +00001798 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001799
Chris Lattner537ca842010-10-30 17:38:55 +00001800 if (getLexer().is(AsmToken::EndOfStatement))
1801 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001802
Chris Lattner537ca842010-10-30 17:38:55 +00001803 // FIXME: Improve diagnostic.
1804 if (getLexer().isNot(AsmToken::Comma))
1805 return Error(L, "unexpected token in directive");
1806 Parser.Lex();
1807 }
1808 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001809
Chris Lattner537ca842010-10-30 17:38:55 +00001810 Parser.Lex();
1811 return false;
1812}
1813
Evan Chengbd27f5a2011-07-27 00:38:12 +00001814/// ParseDirectiveCode
1815/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001816bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001817 if (IDVal == ".code32") {
1818 Parser.Lex();
1819 if (is64BitMode()) {
1820 SwitchMode();
1821 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1822 }
1823 } else if (IDVal == ".code64") {
1824 Parser.Lex();
1825 if (!is64BitMode()) {
1826 SwitchMode();
1827 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1828 }
1829 } else {
1830 return Error(L, "unexpected directive " + IDVal);
1831 }
Chris Lattner537ca842010-10-30 17:38:55 +00001832
Evan Chengbd27f5a2011-07-27 00:38:12 +00001833 return false;
1834}
Chris Lattner537ca842010-10-30 17:38:55 +00001835
1836
Sean Callanane88f5522010-01-23 02:43:15 +00001837extern "C" void LLVMInitializeX86AsmLexer();
1838
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001839// Force static initialization.
1840extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001841 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1842 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001843 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001844}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001845
Chris Lattner0692ee62010-09-06 19:11:01 +00001846#define GET_REGISTER_MATCHER
1847#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001848#include "X86GenAsmMatcher.inc"