blob: 6833266cf5c76c709a89be9dcb7777c003532370 [file] [log] [blame]
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
11#include "llvm/MC/MCTargetAsmParser.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000012#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000013#include "llvm/MC/MCExpr.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000014#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000015#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000016#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000017#include "llvm/MC/MCParser/MCAsmLexer.h"
18#include "llvm/MC/MCParser/MCAsmParser.h"
19#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000022#include "llvm/ADT/StringSwitch.h"
23#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000024#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000025#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000026#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000027
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000028using namespace llvm;
29
30namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000031struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000032
Devang Pateldd929fc2012-01-12 18:03:40 +000033class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000034 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000035 MCAsmParser &Parser;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &getParser() const { return Parser; }
38
39 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
40
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000041 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000042 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
43 bool matchingInlineAsm = false) {
44 if (matchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000045 return Parser.Error(L, Msg, Ranges);
46 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000047
Devang Pateld37ad242012-01-17 18:00:18 +000048 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
49 Error(Loc, Msg);
50 return 0;
51 }
52
Chris Lattner309264d2010-01-15 18:44:13 +000053 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000054 X86Operand *ParseATTOperand();
55 X86Operand *ParseIntelOperand();
Chad Rosier5b0f1b32012-10-04 23:59:38 +000056 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000057 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000058 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000059
60 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000061 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000062
Devang Patelb8ba13f2012-01-18 22:42:29 +000063 bool processInstruction(MCInst &Inst,
64 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
65
Chris Lattner7036f8b2010-09-29 01:42:58 +000066 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000067 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000068 MCStreamer &Out);
Chad Rosier22685872012-10-01 23:45:51 +000069 bool MatchInstruction(SMLoc IDLoc,
Chad Rosier32461762012-08-09 22:04:55 +000070 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier6e006d32012-10-12 22:53:36 +000071 MCStreamer &Out, unsigned &Opcode,
72 unsigned &OrigErrorInfo, bool matchingInlineAsm = false);
Chad Rosier32461762012-08-09 22:04:55 +000073
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000074 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000075 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000076 bool isSrcOp(X86Operand &Op);
77
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000078 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
79 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000080 bool isDstOp(X86Operand &Op);
81
Evan Cheng59ee62d2011-07-11 03:57:24 +000082 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000083 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000084 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000085 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000086 void SwitchMode() {
87 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
88 setAvailableFeatures(FB);
89 }
Evan Chengebdeeab2011-07-08 01:53:10 +000090
Daniel Dunbar54074b52010-07-19 05:44:09 +000091 /// @name Auto-generated Matcher Functions
92 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000093
Chris Lattner0692ee62010-09-06 19:11:01 +000094#define GET_ASSEMBLER_HEADER
95#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000096
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000097 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000098
99public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000100 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +0000101 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000102
Daniel Dunbar54074b52010-07-19 05:44:09 +0000103 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000104 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000105 }
Roman Divackybf755322011-01-27 17:14:22 +0000106 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000107
Benjamin Kramer38e59892010-07-14 22:38:02 +0000108 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000109 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000110
111 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000112
113 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000114 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000115 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000116};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000117} // end anonymous namespace
118
Sean Callanane9b466d2010-01-23 00:40:33 +0000119/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000120/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000121
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000122static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000123
124/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000125
Craig Topper76bd9382012-07-18 04:59:16 +0000126static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000127 return (( Value <= 0x000000000000007FULL)||
128 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
129 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
130}
131
132static bool isImmSExti32i8Value(uint64_t Value) {
133 return (( Value <= 0x000000000000007FULL)||
134 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
135 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
136}
137
138static bool isImmZExtu32u8Value(uint64_t Value) {
139 return (Value <= 0x00000000000000FFULL);
140}
141
142static bool isImmSExti64i8Value(uint64_t Value) {
143 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000144 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000145}
146
147static bool isImmSExti64i32Value(uint64_t Value) {
148 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000149 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000150}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000151namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000152
153/// X86Operand - Instances of this class represent a parsed X86 machine
154/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000155struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000156 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000157 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000158 Register,
159 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000160 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000161 } Kind;
162
Chris Lattner29ef9a22010-01-15 18:51:29 +0000163 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000164
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000165 union {
166 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000167 const char *Data;
168 unsigned Length;
169 } Tok;
170
171 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000172 unsigned RegNo;
173 } Reg;
174
175 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000176 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000177 } Imm;
178
179 struct {
180 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000181 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000182 unsigned BaseReg;
183 unsigned IndexReg;
184 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000185 unsigned Size;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000186 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000187 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000188
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000189 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000190 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000191
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000192 /// getStartLoc - Get the location of the first token of this operand.
193 SMLoc getStartLoc() const { return StartLoc; }
194 /// getEndLoc - Get the location of the last token of this operand.
195 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000196 /// getLocRange - Get the range between the first and last token of this
197 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000198 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000199
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000200 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000201
Daniel Dunbar20927f22009-08-07 08:26:05 +0000202 StringRef getToken() const {
203 assert(Kind == Token && "Invalid access!");
204 return StringRef(Tok.Data, Tok.Length);
205 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000206 void setTokenValue(StringRef Value) {
207 assert(Kind == Token && "Invalid access!");
208 Tok.Data = Value.data();
209 Tok.Length = Value.size();
210 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000211
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000212 unsigned getReg() const {
213 assert(Kind == Register && "Invalid access!");
214 return Reg.RegNo;
215 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000216
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000217 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000218 assert(Kind == Immediate && "Invalid access!");
219 return Imm.Val;
220 }
221
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000222 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000223 assert(Kind == Memory && "Invalid access!");
224 return Mem.Disp;
225 }
226 unsigned getMemSegReg() const {
227 assert(Kind == Memory && "Invalid access!");
228 return Mem.SegReg;
229 }
230 unsigned getMemBaseReg() const {
231 assert(Kind == Memory && "Invalid access!");
232 return Mem.BaseReg;
233 }
234 unsigned getMemIndexReg() const {
235 assert(Kind == Memory && "Invalid access!");
236 return Mem.IndexReg;
237 }
238 unsigned getMemScale() const {
239 assert(Kind == Memory && "Invalid access!");
240 return Mem.Scale;
241 }
242
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000243 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000244
245 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000246
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000247 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000248 if (!isImm())
249 return false;
250
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000251 // If this isn't a constant expr, just assume it fits and let relaxation
252 // handle it.
253 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
254 if (!CE)
255 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000256
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000257 // Otherwise, check the value is in a range that makes sense for this
258 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000259 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000260 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000261 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000262 if (!isImm())
263 return false;
264
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000265 // If this isn't a constant expr, just assume it fits and let relaxation
266 // handle it.
267 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
268 if (!CE)
269 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000270
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000271 // Otherwise, check the value is in a range that makes sense for this
272 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000273 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000274 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000275 bool isImmZExtu32u8() const {
276 if (!isImm())
277 return false;
278
279 // If this isn't a constant expr, just assume it fits and let relaxation
280 // handle it.
281 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
282 if (!CE)
283 return true;
284
285 // Otherwise, check the value is in a range that makes sense for this
286 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000287 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000288 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000289 bool isImmSExti64i8() const {
290 if (!isImm())
291 return false;
292
293 // If this isn't a constant expr, just assume it fits and let relaxation
294 // handle it.
295 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
296 if (!CE)
297 return true;
298
299 // Otherwise, check the value is in a range that makes sense for this
300 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000301 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000302 }
303 bool isImmSExti64i32() const {
304 if (!isImm())
305 return false;
306
307 // If this isn't a constant expr, just assume it fits and let relaxation
308 // handle it.
309 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
310 if (!CE)
311 return true;
312
313 // Otherwise, check the value is in a range that makes sense for this
314 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000315 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000316 }
317
Daniel Dunbar20927f22009-08-07 08:26:05 +0000318 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000319 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000320 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000321 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000322 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000323 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000324 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000325 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000326 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000327 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000328 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000329 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000330 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000331 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000332 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000333 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000334 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000335 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000336 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000337 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000338 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000339 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000340
Craig Topper75dc33a2012-07-18 04:11:12 +0000341 bool isMemVX32() const {
342 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
343 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
344 }
345 bool isMemVY32() const {
346 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
347 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
348 }
349 bool isMemVX64() const {
350 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
351 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
352 }
353 bool isMemVY64() const {
354 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
355 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
356 }
357
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000358 bool isAbsMem() const {
359 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000360 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000361 }
362
Daniel Dunbar20927f22009-08-07 08:26:05 +0000363 bool isReg() const { return Kind == Register; }
364
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000365 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
366 // Add as immediates when possible.
367 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
368 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
369 else
370 Inst.addOperand(MCOperand::CreateExpr(Expr));
371 }
372
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000373 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000374 assert(N == 1 && "Invalid number of operands!");
375 Inst.addOperand(MCOperand::CreateReg(getReg()));
376 }
377
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000378 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000379 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000380 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000381 }
382
Chad Rosier36b8fed2012-06-27 22:34:28 +0000383 void addMem8Operands(MCInst &Inst, unsigned N) const {
384 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000385 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000386 void addMem16Operands(MCInst &Inst, unsigned N) const {
387 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000388 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000389 void addMem32Operands(MCInst &Inst, unsigned N) const {
390 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000391 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000392 void addMem64Operands(MCInst &Inst, unsigned N) const {
393 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000394 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000395 void addMem80Operands(MCInst &Inst, unsigned N) const {
396 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000397 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000398 void addMem128Operands(MCInst &Inst, unsigned N) const {
399 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000400 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000401 void addMem256Operands(MCInst &Inst, unsigned N) const {
402 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000403 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000404 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
405 addMemOperands(Inst, N);
406 }
407 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
408 addMemOperands(Inst, N);
409 }
410 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
411 addMemOperands(Inst, N);
412 }
413 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
414 addMemOperands(Inst, N);
415 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000416
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000417 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000418 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000419 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
420 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
421 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000422 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000423 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
424 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000425
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000426 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
427 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000428 // Add as immediates when possible.
429 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
430 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
431 else
432 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000433 }
434
Chris Lattnerb4307b32010-01-15 19:28:38 +0000435 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000436 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
437 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000438 Res->Tok.Data = Str.data();
439 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000440 return Res;
441 }
442
Chris Lattner29ef9a22010-01-15 18:51:29 +0000443 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000444 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000445 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000446 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000447 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000448
Chris Lattnerb4307b32010-01-15 19:28:38 +0000449 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
450 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000451 Res->Imm.Val = Val;
452 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000453 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000454
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000455 /// Create an absolute memory operand.
456 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000457 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000458 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
459 Res->Mem.SegReg = 0;
460 Res->Mem.Disp = Disp;
461 Res->Mem.BaseReg = 0;
462 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000463 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000464 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000465 return Res;
466 }
467
468 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000469 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
470 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000471 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
472 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000473 // We should never just have a displacement, that should be parsed as an
474 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000475 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
476
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000477 // The scale should always be one of {1,2,4,8}.
478 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000479 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000480 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000481 Res->Mem.SegReg = SegReg;
482 Res->Mem.Disp = Disp;
483 Res->Mem.BaseReg = BaseReg;
484 Res->Mem.IndexReg = IndexReg;
485 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000486 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000487 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000488 }
489};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000490
Chris Lattner37dfdec2009-07-29 06:33:53 +0000491} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000492
Devang Pateldd929fc2012-01-12 18:03:40 +0000493bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000494 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000495
496 return (Op.isMem() &&
497 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
498 isa<MCConstantExpr>(Op.Mem.Disp) &&
499 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
500 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
501}
502
Devang Pateldd929fc2012-01-12 18:03:40 +0000503bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000504 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000505
Chad Rosier36b8fed2012-06-27 22:34:28 +0000506 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000507 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000508 isa<MCConstantExpr>(Op.Mem.Disp) &&
509 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
510 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
511}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000512
Devang Pateldd929fc2012-01-12 18:03:40 +0000513bool X86AsmParser::ParseRegister(unsigned &RegNo,
514 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000515 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000516 const AsmToken &PercentTok = Parser.getTok();
517 StartLoc = PercentTok.getLoc();
518
519 // If we encounter a %, ignore it. This code handles registers with and
520 // without the prefix, unprefixed registers can occur in cfi directives.
521 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000522 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000523
Sean Callanan18b83232010-01-19 21:44:56 +0000524 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000525 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000526 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000527 return Error(StartLoc, "invalid register name",
528 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000529 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000530
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000531 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000532
Chris Lattner33d60d52010-09-22 04:11:10 +0000533 // If the match failed, try the register name as lowercase.
534 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000535 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000536
Evan Cheng5de728c2011-07-27 23:22:03 +0000537 if (!is64BitMode()) {
538 // FIXME: This should be done using Requires<In32BitMode> and
539 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
540 // checked.
541 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
542 // REX prefix.
543 if (RegNo == X86::RIZ ||
544 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
545 X86II::isX86_64NonExtLowByteReg(RegNo) ||
546 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000547 return Error(StartLoc, "register %"
548 + Tok.getString() + " is only available in 64-bit mode",
549 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000550 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000551
Chris Lattner33d60d52010-09-22 04:11:10 +0000552 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
553 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000554 RegNo = X86::ST0;
555 EndLoc = Tok.getLoc();
556 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000557
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000558 // Check to see if we have '(4)' after %st.
559 if (getLexer().isNot(AsmToken::LParen))
560 return false;
561 // Lex the paren.
562 getParser().Lex();
563
564 const AsmToken &IntTok = Parser.getTok();
565 if (IntTok.isNot(AsmToken::Integer))
566 return Error(IntTok.getLoc(), "expected stack index");
567 switch (IntTok.getIntVal()) {
568 case 0: RegNo = X86::ST0; break;
569 case 1: RegNo = X86::ST1; break;
570 case 2: RegNo = X86::ST2; break;
571 case 3: RegNo = X86::ST3; break;
572 case 4: RegNo = X86::ST4; break;
573 case 5: RegNo = X86::ST5; break;
574 case 6: RegNo = X86::ST6; break;
575 case 7: RegNo = X86::ST7; break;
576 default: return Error(IntTok.getLoc(), "invalid stack index");
577 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000578
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000579 if (getParser().Lex().isNot(AsmToken::RParen))
580 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000581
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000582 EndLoc = Tok.getLoc();
583 Parser.Lex(); // Eat ')'
584 return false;
585 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000586
Chris Lattner645b2092010-06-24 07:29:18 +0000587 // If this is "db[0-7]", match it as an alias
588 // for dr[0-7].
589 if (RegNo == 0 && Tok.getString().size() == 3 &&
590 Tok.getString().startswith("db")) {
591 switch (Tok.getString()[2]) {
592 case '0': RegNo = X86::DR0; break;
593 case '1': RegNo = X86::DR1; break;
594 case '2': RegNo = X86::DR2; break;
595 case '3': RegNo = X86::DR3; break;
596 case '4': RegNo = X86::DR4; break;
597 case '5': RegNo = X86::DR5; break;
598 case '6': RegNo = X86::DR6; break;
599 case '7': RegNo = X86::DR7; break;
600 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000601
Chris Lattner645b2092010-06-24 07:29:18 +0000602 if (RegNo != 0) {
603 EndLoc = Tok.getLoc();
604 Parser.Lex(); // Eat it.
605 return false;
606 }
607 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000608
Devang Patel1aea4302012-01-20 22:32:05 +0000609 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000610 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000611 return Error(StartLoc, "invalid register name",
612 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000613 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000614
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000615 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000616 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000617 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000618}
619
Devang Pateldd929fc2012-01-12 18:03:40 +0000620X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000621 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000622 return ParseIntelOperand();
623 return ParseATTOperand();
624}
625
Devang Pateld37ad242012-01-17 18:00:18 +0000626/// getIntelMemOperandSize - Return intel memory operand size.
627static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000628 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000629 .Cases("BYTE", "byte", 8)
630 .Cases("WORD", "word", 16)
631 .Cases("DWORD", "dword", 32)
632 .Cases("QWORD", "qword", 64)
633 .Cases("XWORD", "xword", 80)
634 .Cases("XMMWORD", "xmmword", 128)
635 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000636 .Default(0);
637 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000638}
639
Devang Patel7c64fe62012-01-23 18:31:58 +0000640X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
641 unsigned Size) {
642 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000643 SMLoc Start = Parser.getTok().getLoc(), End;
644
Devang Pateld37ad242012-01-17 18:00:18 +0000645 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
646 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
647
648 // Eat '['
649 if (getLexer().isNot(AsmToken::LBrac))
650 return ErrorOperand(Start, "Expected '[' token!");
651 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000652
Devang Pateld37ad242012-01-17 18:00:18 +0000653 if (getLexer().is(AsmToken::Identifier)) {
654 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000655 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000656 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000657 if (getParser().ParseExpression(Disp, End)) return 0;
658 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000659 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000660 Parser.Lex();
661 return X86Operand::CreateMem(Disp, Start, End, Size);
662 }
663 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000664 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000665 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000666 SMLoc Loc = Parser.getTok().getLoc();
667 if (getLexer().is(AsmToken::RBrac)) {
668 // Handle '[' number ']'
669 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000670 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
671 if (SegReg)
672 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
673 Start, End, Size);
674 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000675 } else if (getLexer().is(AsmToken::Star)) {
676 // Handle '[' Scale*IndexReg ']'
677 Parser.Lex();
678 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000679 if (ParseRegister(IndexReg, IdxRegLoc, End))
680 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000681 Scale = Val;
682 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000683 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000684 }
685
686 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
687 bool isPlus = getLexer().is(AsmToken::Plus);
688 Parser.Lex();
689 SMLoc PlusLoc = Parser.getTok().getLoc();
690 if (getLexer().is(AsmToken::Integer)) {
691 int64_t Val = Parser.getTok().getIntVal();
692 Parser.Lex();
693 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000694 Parser.Lex();
695 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000696 if (ParseRegister(IndexReg, IdxRegLoc, End))
697 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000698 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000699 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000700 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000701 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000702 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000703 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000704 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000705 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000706 End = Parser.getTok().getLoc();
707 if (!IndexReg)
708 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000709 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000710 }
Devang Pateld37ad242012-01-17 18:00:18 +0000711 }
712
713 if (getLexer().isNot(AsmToken::RBrac))
714 if (getParser().ParseExpression(Disp, End)) return 0;
715
716 End = Parser.getTok().getLoc();
717 if (getLexer().isNot(AsmToken::RBrac))
718 return ErrorOperand(End, "expected ']' token!");
719 Parser.Lex();
720 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000721
722 // handle [-42]
723 if (!BaseReg && !IndexReg)
724 return X86Operand::CreateMem(Disp, Start, End, Size);
725
Devang Pateld37ad242012-01-17 18:00:18 +0000726 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000727 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000728}
729
730/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000731X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000732 const AsmToken &Tok = Parser.getTok();
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000733 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000734
735 unsigned Size = getIntelMemOperandSize(Tok.getString());
736 if (Size) {
737 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000738 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
739 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000740 Parser.Lex();
741 }
742
743 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000744 return ParseIntelBracExpression(SegReg, Size);
745
746 if (!ParseRegister(SegReg, Start, End)) {
747 // Handel SegReg : [ ... ]
748 if (getLexer().isNot(AsmToken::Colon))
749 return ErrorOperand(Start, "Expected ':' token!");
750 Parser.Lex(); // Eat :
751 if (getLexer().isNot(AsmToken::LBrac))
752 return ErrorOperand(Start, "Expected '[' token!");
753 return ParseIntelBracExpression(SegReg, Size);
754 }
Devang Pateld37ad242012-01-17 18:00:18 +0000755
756 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
757 if (getParser().ParseExpression(Disp, End)) return 0;
758 return X86Operand::CreateMem(Disp, Start, End, Size);
759}
760
761X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000762 SMLoc Start = Parser.getTok().getLoc(), End;
763
764 // immediate.
765 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
766 getLexer().is(AsmToken::Minus)) {
767 const MCExpr *Val;
768 if (!getParser().ParseExpression(Val, End)) {
769 End = Parser.getTok().getLoc();
770 return X86Operand::CreateImm(Val, Start, End);
771 }
772 }
773
Devang Patel0a338862012-01-12 01:36:43 +0000774 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000775 unsigned RegNo = 0;
776 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000777 // If this is a segment register followed by a ':', then this is the start
778 // of a memory reference, otherwise this is a normal register reference.
779 if (getLexer().isNot(AsmToken::Colon))
780 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
781
782 getParser().Lex(); // Eat the colon.
783 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000784 }
785
786 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000787 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000788}
789
Devang Pateldd929fc2012-01-12 18:03:40 +0000790X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000791 switch (getLexer().getKind()) {
792 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000793 // Parse a memory operand with no segment register.
794 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000795 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000796 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000797 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000798 SMLoc Start, End;
799 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000800 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000801 Error(Start, "%eiz and %riz can only be used as index registers",
802 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000803 return 0;
804 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000805
Chris Lattnereef6d782010-04-17 18:56:34 +0000806 // If this is a segment register followed by a ':', then this is the start
807 // of a memory reference, otherwise this is a normal register reference.
808 if (getLexer().isNot(AsmToken::Colon))
809 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000810
811
Chris Lattnereef6d782010-04-17 18:56:34 +0000812 getParser().Lex(); // Eat the colon.
813 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000814 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000815 case AsmToken::Dollar: {
816 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000817 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000818 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000819 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000820 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000821 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000822 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000823 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000824 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000825}
826
Chris Lattnereef6d782010-04-17 18:56:34 +0000827/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
828/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000829X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000830
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000831 // We have to disambiguate a parenthesized expression "(4+5)" from the start
832 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000833 // only way to do this without lookahead is to eat the '(' and see what is
834 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000835 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000836 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000837 SMLoc ExprEnd;
838 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000839
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000840 // After parsing the base expression we could either have a parenthesized
841 // memory address or not. If not, return now. If so, eat the (.
842 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000843 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000844 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000845 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000846 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000847 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000848
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000849 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000850 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000851 } else {
852 // Okay, we have a '('. We don't know if this is an expression or not, but
853 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000854 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000855 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000856
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000857 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000858 // Nothing to do here, fall into the code below with the '(' part of the
859 // memory operand consumed.
860 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000861 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000862
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000863 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000864 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000865 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000866
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000867 // After parsing the base expression we could either have a parenthesized
868 // memory address or not. If not, return now. If so, eat the (.
869 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000870 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000871 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000872 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000873 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000874 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000875
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000876 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000877 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000878 }
879 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000880
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000881 // If we reached here, then we just ate the ( of the memory operand. Process
882 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000883 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000884 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000885
Chris Lattner29ef9a22010-01-15 18:51:29 +0000886 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000887 SMLoc StartLoc, EndLoc;
888 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000889 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000890 Error(StartLoc, "eiz and riz can only be used as index registers",
891 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000892 return 0;
893 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000894 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000895
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000896 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000897 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000898 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000899
900 // Following the comma we should have either an index register, or a scale
901 // value. We don't support the later form, but we want to parse it
902 // correctly.
903 //
904 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000905 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000906 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000907 SMLoc L;
908 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000909
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000910 if (getLexer().isNot(AsmToken::RParen)) {
911 // Parse the scale amount:
912 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000913 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000914 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000915 "expected comma in scale expression");
916 return 0;
917 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000918 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000919
920 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000921 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000922
923 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000924 if (getParser().ParseAbsoluteExpression(ScaleVal)){
925 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000926 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +0000927 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000928
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000929 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000930 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
931 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
932 return 0;
933 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000934 Scale = (unsigned)ScaleVal;
935 }
936 }
937 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000938 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000939 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000940 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000941
942 int64_t Value;
943 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000944 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000945
Daniel Dunbaree910252010-08-24 19:13:38 +0000946 if (Value != 1)
947 Warning(Loc, "scale factor without index register is ignored");
948 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000949 }
950 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000951
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000952 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000953 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000954 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000955 return 0;
956 }
Sean Callanan18b83232010-01-19 21:44:56 +0000957 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000958 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000959
Kevin Enderby84faf652012-03-12 21:32:09 +0000960 // If we have both a base register and an index register make sure they are
961 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +0000962 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +0000963 if (BaseReg != 0 && IndexReg != 0) {
964 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000965 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
966 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000967 IndexReg != X86::RIZ) {
968 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
969 return 0;
970 }
971 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000972 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
973 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000974 IndexReg != X86::EIZ){
975 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
976 return 0;
977 }
978 }
979
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000980 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
981 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000982}
983
Devang Pateldd929fc2012-01-12 18:03:40 +0000984bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000985ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000986 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000987 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000988
Chris Lattnerd8f71792010-11-28 20:23:50 +0000989 // FIXME: Hack to recognize setneb as setne.
990 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
991 PatchedName != "setb" && PatchedName != "setnb")
992 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000993
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000994 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
995 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000996 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000997 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
998 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +0000999 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001000 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001001 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001002 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001003 .Case("eq", 0x00)
1004 .Case("lt", 0x01)
1005 .Case("le", 0x02)
1006 .Case("unord", 0x03)
1007 .Case("neq", 0x04)
1008 .Case("nlt", 0x05)
1009 .Case("nle", 0x06)
1010 .Case("ord", 0x07)
1011 /* AVX only from here */
1012 .Case("eq_uq", 0x08)
1013 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001014 .Case("ngt", 0x0A)
1015 .Case("false", 0x0B)
1016 .Case("neq_oq", 0x0C)
1017 .Case("ge", 0x0D)
1018 .Case("gt", 0x0E)
1019 .Case("true", 0x0F)
1020 .Case("eq_os", 0x10)
1021 .Case("lt_oq", 0x11)
1022 .Case("le_oq", 0x12)
1023 .Case("unord_s", 0x13)
1024 .Case("neq_us", 0x14)
1025 .Case("nlt_uq", 0x15)
1026 .Case("nle_uq", 0x16)
1027 .Case("ord_s", 0x17)
1028 .Case("eq_us", 0x18)
1029 .Case("nge_uq", 0x19)
1030 .Case("ngt_uq", 0x1A)
1031 .Case("false_os", 0x1B)
1032 .Case("neq_os", 0x1C)
1033 .Case("ge_oq", 0x1D)
1034 .Case("gt_oq", 0x1E)
1035 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001036 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001037 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001038 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1039 getParser().getContext());
1040 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001041 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001042 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001043 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001044 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001045 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001046 } else {
1047 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001048 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001049 }
1050 }
1051 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001052
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001053 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001054
Devang Patel885f65b2012-01-30 22:47:12 +00001055 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001056 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001057
Chris Lattner2544f422010-09-08 05:17:37 +00001058 // Determine whether this is an instruction prefix.
1059 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001060 Name == "lock" || Name == "rep" ||
1061 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001062 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001063 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001064
1065
Chris Lattner2544f422010-09-08 05:17:37 +00001066 // This does the actual operand parsing. Don't parse any more if we have a
1067 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1068 // just want to parse the "lock" as the first instruction and the "incl" as
1069 // the next one.
1070 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001071
1072 // Parse '*' modifier.
1073 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001074 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001075 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001076 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001077 }
1078
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001079 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001080 if (X86Operand *Op = ParseOperand())
1081 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001082 else {
1083 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001084 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001085 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001086
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001087 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001088 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001089
1090 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001091 if (X86Operand *Op = ParseOperand())
1092 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001093 else {
1094 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001095 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001096 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001097 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001098
Chris Lattnercbf8a982010-09-11 16:18:25 +00001099 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001100 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001101 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001102 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001103 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001104 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001105
Chris Lattner2544f422010-09-08 05:17:37 +00001106 if (getLexer().is(AsmToken::EndOfStatement))
1107 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001108 else if (isPrefix && getLexer().is(AsmToken::Slash))
1109 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001110
Devang Patel885f65b2012-01-30 22:47:12 +00001111 if (ExtraImmOp && isParsingIntelSyntax())
1112 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1113
Chris Lattner98c870f2010-11-06 19:25:43 +00001114 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1115 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1116 // documented form in various unofficial manuals, so a lot of code uses it.
1117 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1118 Operands.size() == 3) {
1119 X86Operand &Op = *(X86Operand*)Operands.back();
1120 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1121 isa<MCConstantExpr>(Op.Mem.Disp) &&
1122 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1123 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1124 SMLoc Loc = Op.getEndLoc();
1125 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1126 delete &Op;
1127 }
1128 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001129 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1130 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1131 Operands.size() == 3) {
1132 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1133 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1134 isa<MCConstantExpr>(Op.Mem.Disp) &&
1135 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1136 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1137 SMLoc Loc = Op.getEndLoc();
1138 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1139 delete &Op;
1140 }
1141 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001142 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1143 if (Name.startswith("ins") && Operands.size() == 3 &&
1144 (Name == "insb" || Name == "insw" || Name == "insl")) {
1145 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1146 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1147 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1148 Operands.pop_back();
1149 Operands.pop_back();
1150 delete &Op;
1151 delete &Op2;
1152 }
1153 }
1154
1155 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1156 if (Name.startswith("outs") && Operands.size() == 3 &&
1157 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1158 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1159 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1160 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1161 Operands.pop_back();
1162 Operands.pop_back();
1163 delete &Op;
1164 delete &Op2;
1165 }
1166 }
1167
1168 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1169 if (Name.startswith("movs") && Operands.size() == 3 &&
1170 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001171 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001172 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1173 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1174 if (isSrcOp(Op) && isDstOp(Op2)) {
1175 Operands.pop_back();
1176 Operands.pop_back();
1177 delete &Op;
1178 delete &Op2;
1179 }
1180 }
1181 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1182 if (Name.startswith("lods") && Operands.size() == 3 &&
1183 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001184 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001185 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1186 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1187 if (isSrcOp(*Op1) && Op2->isReg()) {
1188 const char *ins;
1189 unsigned reg = Op2->getReg();
1190 bool isLods = Name == "lods";
1191 if (reg == X86::AL && (isLods || Name == "lodsb"))
1192 ins = "lodsb";
1193 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1194 ins = "lodsw";
1195 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1196 ins = "lodsl";
1197 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1198 ins = "lodsq";
1199 else
1200 ins = NULL;
1201 if (ins != NULL) {
1202 Operands.pop_back();
1203 Operands.pop_back();
1204 delete Op1;
1205 delete Op2;
1206 if (Name != ins)
1207 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1208 }
1209 }
1210 }
1211 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1212 if (Name.startswith("stos") && Operands.size() == 3 &&
1213 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001214 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001215 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1216 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1217 if (isDstOp(*Op2) && Op1->isReg()) {
1218 const char *ins;
1219 unsigned reg = Op1->getReg();
1220 bool isStos = Name == "stos";
1221 if (reg == X86::AL && (isStos || Name == "stosb"))
1222 ins = "stosb";
1223 else if (reg == X86::AX && (isStos || Name == "stosw"))
1224 ins = "stosw";
1225 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1226 ins = "stosl";
1227 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1228 ins = "stosq";
1229 else
1230 ins = NULL;
1231 if (ins != NULL) {
1232 Operands.pop_back();
1233 Operands.pop_back();
1234 delete Op1;
1235 delete Op2;
1236 if (Name != ins)
1237 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1238 }
1239 }
1240 }
1241
Chris Lattnere9e16a32010-09-15 04:33:27 +00001242 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001243 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001244 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001245 Name.startswith("shl") || Name.startswith("sal") ||
1246 Name.startswith("rcl") || Name.startswith("rcr") ||
1247 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001248 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001249 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001250 // Intel syntax
1251 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1252 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001253 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1254 delete Operands[2];
1255 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001256 }
1257 } else {
1258 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1259 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001260 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1261 delete Operands[1];
1262 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001263 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001264 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001265 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001266
Chris Lattner15f89512011-04-09 19:41:05 +00001267 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1268 // instalias with an immediate operand yet.
1269 if (Name == "int" && Operands.size() == 2) {
1270 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1271 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1272 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1273 delete Operands[1];
1274 Operands.erase(Operands.begin() + 1);
1275 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1276 }
1277 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001278
Chris Lattner98986712010-01-14 22:21:20 +00001279 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001280}
1281
Devang Pateldd929fc2012-01-12 18:03:40 +00001282bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001283processInstruction(MCInst &Inst,
1284 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1285 switch (Inst.getOpcode()) {
1286 default: return false;
1287 case X86::AND16i16: {
1288 if (!Inst.getOperand(0).isImm() ||
1289 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1290 return false;
1291
1292 MCInst TmpInst;
1293 TmpInst.setOpcode(X86::AND16ri8);
1294 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1295 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1296 TmpInst.addOperand(Inst.getOperand(0));
1297 Inst = TmpInst;
1298 return true;
1299 }
1300 case X86::AND32i32: {
1301 if (!Inst.getOperand(0).isImm() ||
1302 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1303 return false;
1304
1305 MCInst TmpInst;
1306 TmpInst.setOpcode(X86::AND32ri8);
1307 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1308 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1309 TmpInst.addOperand(Inst.getOperand(0));
1310 Inst = TmpInst;
1311 return true;
1312 }
1313 case X86::AND64i32: {
1314 if (!Inst.getOperand(0).isImm() ||
1315 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1316 return false;
1317
1318 MCInst TmpInst;
1319 TmpInst.setOpcode(X86::AND64ri8);
1320 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1321 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1322 TmpInst.addOperand(Inst.getOperand(0));
1323 Inst = TmpInst;
1324 return true;
1325 }
Devang Patelac0f0482012-01-19 17:53:25 +00001326 case X86::XOR16i16: {
1327 if (!Inst.getOperand(0).isImm() ||
1328 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1329 return false;
1330
1331 MCInst TmpInst;
1332 TmpInst.setOpcode(X86::XOR16ri8);
1333 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1334 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1335 TmpInst.addOperand(Inst.getOperand(0));
1336 Inst = TmpInst;
1337 return true;
1338 }
1339 case X86::XOR32i32: {
1340 if (!Inst.getOperand(0).isImm() ||
1341 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1342 return false;
1343
1344 MCInst TmpInst;
1345 TmpInst.setOpcode(X86::XOR32ri8);
1346 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1347 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1348 TmpInst.addOperand(Inst.getOperand(0));
1349 Inst = TmpInst;
1350 return true;
1351 }
1352 case X86::XOR64i32: {
1353 if (!Inst.getOperand(0).isImm() ||
1354 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1355 return false;
1356
1357 MCInst TmpInst;
1358 TmpInst.setOpcode(X86::XOR64ri8);
1359 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1360 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1361 TmpInst.addOperand(Inst.getOperand(0));
1362 Inst = TmpInst;
1363 return true;
1364 }
1365 case X86::OR16i16: {
1366 if (!Inst.getOperand(0).isImm() ||
1367 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1368 return false;
1369
1370 MCInst TmpInst;
1371 TmpInst.setOpcode(X86::OR16ri8);
1372 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1373 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1374 TmpInst.addOperand(Inst.getOperand(0));
1375 Inst = TmpInst;
1376 return true;
1377 }
1378 case X86::OR32i32: {
1379 if (!Inst.getOperand(0).isImm() ||
1380 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1381 return false;
1382
1383 MCInst TmpInst;
1384 TmpInst.setOpcode(X86::OR32ri8);
1385 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1386 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1387 TmpInst.addOperand(Inst.getOperand(0));
1388 Inst = TmpInst;
1389 return true;
1390 }
1391 case X86::OR64i32: {
1392 if (!Inst.getOperand(0).isImm() ||
1393 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1394 return false;
1395
1396 MCInst TmpInst;
1397 TmpInst.setOpcode(X86::OR64ri8);
1398 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1399 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1400 TmpInst.addOperand(Inst.getOperand(0));
1401 Inst = TmpInst;
1402 return true;
1403 }
1404 case X86::CMP16i16: {
1405 if (!Inst.getOperand(0).isImm() ||
1406 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1407 return false;
1408
1409 MCInst TmpInst;
1410 TmpInst.setOpcode(X86::CMP16ri8);
1411 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1412 TmpInst.addOperand(Inst.getOperand(0));
1413 Inst = TmpInst;
1414 return true;
1415 }
1416 case X86::CMP32i32: {
1417 if (!Inst.getOperand(0).isImm() ||
1418 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1419 return false;
1420
1421 MCInst TmpInst;
1422 TmpInst.setOpcode(X86::CMP32ri8);
1423 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1424 TmpInst.addOperand(Inst.getOperand(0));
1425 Inst = TmpInst;
1426 return true;
1427 }
1428 case X86::CMP64i32: {
1429 if (!Inst.getOperand(0).isImm() ||
1430 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1431 return false;
1432
1433 MCInst TmpInst;
1434 TmpInst.setOpcode(X86::CMP64ri8);
1435 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1436 TmpInst.addOperand(Inst.getOperand(0));
1437 Inst = TmpInst;
1438 return true;
1439 }
Devang Patela951f772012-01-19 18:40:55 +00001440 case X86::ADD16i16: {
1441 if (!Inst.getOperand(0).isImm() ||
1442 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1443 return false;
1444
1445 MCInst TmpInst;
1446 TmpInst.setOpcode(X86::ADD16ri8);
1447 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1448 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1449 TmpInst.addOperand(Inst.getOperand(0));
1450 Inst = TmpInst;
1451 return true;
1452 }
1453 case X86::ADD32i32: {
1454 if (!Inst.getOperand(0).isImm() ||
1455 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1456 return false;
1457
1458 MCInst TmpInst;
1459 TmpInst.setOpcode(X86::ADD32ri8);
1460 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1461 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1462 TmpInst.addOperand(Inst.getOperand(0));
1463 Inst = TmpInst;
1464 return true;
1465 }
1466 case X86::ADD64i32: {
1467 if (!Inst.getOperand(0).isImm() ||
1468 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1469 return false;
1470
1471 MCInst TmpInst;
1472 TmpInst.setOpcode(X86::ADD64ri8);
1473 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1474 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1475 TmpInst.addOperand(Inst.getOperand(0));
1476 Inst = TmpInst;
1477 return true;
1478 }
1479 case X86::SUB16i16: {
1480 if (!Inst.getOperand(0).isImm() ||
1481 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1482 return false;
1483
1484 MCInst TmpInst;
1485 TmpInst.setOpcode(X86::SUB16ri8);
1486 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1487 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1488 TmpInst.addOperand(Inst.getOperand(0));
1489 Inst = TmpInst;
1490 return true;
1491 }
1492 case X86::SUB32i32: {
1493 if (!Inst.getOperand(0).isImm() ||
1494 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1495 return false;
1496
1497 MCInst TmpInst;
1498 TmpInst.setOpcode(X86::SUB32ri8);
1499 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1500 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1501 TmpInst.addOperand(Inst.getOperand(0));
1502 Inst = TmpInst;
1503 return true;
1504 }
1505 case X86::SUB64i32: {
1506 if (!Inst.getOperand(0).isImm() ||
1507 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1508 return false;
1509
1510 MCInst TmpInst;
1511 TmpInst.setOpcode(X86::SUB64ri8);
1512 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1513 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1514 TmpInst.addOperand(Inst.getOperand(0));
1515 Inst = TmpInst;
1516 return true;
1517 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001518 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001519}
1520
1521bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001522MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001523 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001524 MCStreamer &Out) {
Chad Rosier22685872012-10-01 23:45:51 +00001525 unsigned Opcode;
Chad Rosier64bfcbb2012-08-21 18:14:59 +00001526 unsigned ErrorInfo;
Chad Rosier6e006d32012-10-12 22:53:36 +00001527 bool Error = MatchInstruction(IDLoc, Operands, Out, Opcode, ErrorInfo);
Chad Rosier32461762012-08-09 22:04:55 +00001528 return Error;
1529}
1530
1531bool X86AsmParser::
Chad Rosier22685872012-10-01 23:45:51 +00001532MatchInstruction(SMLoc IDLoc,
Chad Rosier32461762012-08-09 22:04:55 +00001533 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier6e006d32012-10-12 22:53:36 +00001534 MCStreamer &Out, unsigned &Opcode, unsigned &OrigErrorInfo,
1535 bool matchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001536 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001537 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1538 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001539 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001540
Chris Lattner7c51a312010-09-29 01:50:45 +00001541 // First, handle aliases that expand to multiple instructions.
1542 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001543 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001544 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001545 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001546 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001547 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001548 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001549 MCInst Inst;
1550 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001551 Inst.setLoc(IDLoc);
Chad Rosier22685872012-10-01 23:45:51 +00001552 if (!matchingInlineAsm)
1553 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001554
Chris Lattner0bb83a82010-09-30 16:39:29 +00001555 const char *Repl =
1556 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001557 .Case("finit", "fninit")
1558 .Case("fsave", "fnsave")
1559 .Case("fstcw", "fnstcw")
1560 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001561 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001562 .Case("fstsw", "fnstsw")
1563 .Case("fstsww", "fnstsw")
1564 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001565 .Default(0);
1566 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001567 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001568 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001569 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001570
Chris Lattnera008e8a2010-09-06 21:54:15 +00001571 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001572 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001573
Daniel Dunbarc918d602010-05-04 16:12:42 +00001574 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001575 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier22685872012-10-01 23:45:51 +00001576 OrigErrorInfo, matchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001577 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001578 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001579 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001580 // Some instructions need post-processing to, for example, tweak which
1581 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001582 // individual transformations can chain off each other.
Chad Rosier22685872012-10-01 23:45:51 +00001583 if (!matchingInlineAsm)
1584 while (processInstruction(Inst, Operands))
1585 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001586
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001587 Inst.setLoc(IDLoc);
Chad Rosier22685872012-10-01 23:45:51 +00001588 if (!matchingInlineAsm)
1589 Out.EmitInstruction(Inst);
1590 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001591 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001592 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001593 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
1594 EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001595 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001596 case Match_InvalidOperand:
1597 WasOriginallyInvalidOperand = true;
1598 break;
1599 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001600 break;
1601 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001602
Daniel Dunbarc918d602010-05-04 16:12:42 +00001603 // FIXME: Ideally, we would only attempt suffix matches for things which are
1604 // valid prefixes, and we could just infer the right unambiguous
1605 // type. However, that requires substantially more matcher support than the
1606 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001607
Daniel Dunbarc918d602010-05-04 16:12:42 +00001608 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001609 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001610 SmallString<16> Tmp;
1611 Tmp += Base;
1612 Tmp += ' ';
1613 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001614
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001615 // If this instruction starts with an 'f', then it is a floating point stack
1616 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1617 // 80-bit floating point, which use the suffixes s,l,t respectively.
1618 //
1619 // Otherwise, we assume that this may be an integer instruction, which comes
1620 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1621 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001622
Daniel Dunbarc918d602010-05-04 16:12:42 +00001623 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001624 Tmp[Base.size()] = Suffixes[0];
1625 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001626 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001627
Chad Rosier6e006d32012-10-12 22:53:36 +00001628 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1629 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001630 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001631 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1632 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001633 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001634 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1635 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001636 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001637 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1638 isParsingIntelSyntax());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001639
1640 // Restore the old token.
1641 Op->setTokenValue(Base);
1642
1643 // If exactly one matched, then we treat that as a successful match (and the
1644 // instruction will already have been filled in correctly, since the failing
1645 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001646 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001647 (Match1 == Match_Success) + (Match2 == Match_Success) +
1648 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001649 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001650 Inst.setLoc(IDLoc);
Chad Rosier22685872012-10-01 23:45:51 +00001651 if (!matchingInlineAsm)
1652 Out.EmitInstruction(Inst);
1653 Opcode = Inst.getOpcode();
1654 // FIXME: Handle the map and constraints.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001655 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001656 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001657
Chris Lattnerec6789f2010-09-06 20:08:02 +00001658 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001659
Daniel Dunbar09062b12010-08-12 00:55:42 +00001660 // If we had multiple suffix matches, then identify this as an ambiguous
1661 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001662 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001663 char MatchChars[4];
1664 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001665 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1666 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1667 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1668 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001669
1670 SmallString<126> Msg;
1671 raw_svector_ostream OS(Msg);
1672 OS << "ambiguous instructions require an explicit suffix (could be ";
1673 for (unsigned i = 0; i != NumMatches; ++i) {
1674 if (i != 0)
1675 OS << ", ";
1676 if (i + 1 == NumMatches)
1677 OS << "or ";
1678 OS << "'" << Base << MatchChars[i] << "'";
1679 }
1680 OS << ")";
Chad Rosierb4fdade2012-08-21 19:36:59 +00001681 Error(IDLoc, OS.str(), EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001682 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001683 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001684
Chris Lattnera008e8a2010-09-06 21:54:15 +00001685 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001686
Chris Lattnera008e8a2010-09-06 21:54:15 +00001687 // If all of the instructions reported an invalid mnemonic, then the original
1688 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001689 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1690 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001691 if (!WasOriginallyInvalidOperand) {
Chad Rosier674101e2012-08-22 19:14:29 +00001692 ArrayRef<SMRange> Ranges = matchingInlineAsm ? EmptyRanges :
1693 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001694 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier674101e2012-08-22 19:14:29 +00001695 Ranges, matchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001696 }
1697
1698 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001699 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001700 if (OrigErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001701 return Error(IDLoc, "too few operands for instruction",
1702 EmptyRanges, matchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001703
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001704 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1705 if (Operand->getStartLoc().isValid()) {
1706 SMRange OperandRange = Operand->getLocRange();
1707 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosierb4fdade2012-08-21 19:36:59 +00001708 OperandRange, matchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001709 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001710 }
1711
Chad Rosierb4fdade2012-08-21 19:36:59 +00001712 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
1713 matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001714 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001715
Chris Lattnerec6789f2010-09-06 20:08:02 +00001716 // If one instruction matched with a missing feature, report this as a
1717 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001718 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1719 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001720 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
1721 EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001722 return true;
1723 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001724
Chris Lattnera008e8a2010-09-06 21:54:15 +00001725 // If one instruction matched with an invalid operand, report this as an
1726 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001727 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1728 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001729 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
1730 matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001731 return true;
1732 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001733
Chris Lattnerec6789f2010-09-06 20:08:02 +00001734 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001735 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
1736 EmptyRanges, matchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001737 return true;
1738}
1739
1740
Devang Pateldd929fc2012-01-12 18:03:40 +00001741bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001742 StringRef IDVal = DirectiveID.getIdentifier();
1743 if (IDVal == ".word")
1744 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001745 else if (IDVal.startswith(".code"))
1746 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001747 else if (IDVal.startswith(".att_syntax")) {
1748 getParser().setAssemblerDialect(0);
1749 return false;
1750 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001751 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001752 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1753 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001754 // FIXME : Handle noprefix
1755 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001756 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001757 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001758 }
1759 return false;
1760 }
Chris Lattner537ca842010-10-30 17:38:55 +00001761 return true;
1762}
1763
1764/// ParseDirectiveWord
1765/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001766bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001767 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1768 for (;;) {
1769 const MCExpr *Value;
1770 if (getParser().ParseExpression(Value))
1771 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001772
Chris Lattner537ca842010-10-30 17:38:55 +00001773 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001774
Chris Lattner537ca842010-10-30 17:38:55 +00001775 if (getLexer().is(AsmToken::EndOfStatement))
1776 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001777
Chris Lattner537ca842010-10-30 17:38:55 +00001778 // FIXME: Improve diagnostic.
1779 if (getLexer().isNot(AsmToken::Comma))
1780 return Error(L, "unexpected token in directive");
1781 Parser.Lex();
1782 }
1783 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001784
Chris Lattner537ca842010-10-30 17:38:55 +00001785 Parser.Lex();
1786 return false;
1787}
1788
Evan Chengbd27f5a2011-07-27 00:38:12 +00001789/// ParseDirectiveCode
1790/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001791bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001792 if (IDVal == ".code32") {
1793 Parser.Lex();
1794 if (is64BitMode()) {
1795 SwitchMode();
1796 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1797 }
1798 } else if (IDVal == ".code64") {
1799 Parser.Lex();
1800 if (!is64BitMode()) {
1801 SwitchMode();
1802 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1803 }
1804 } else {
1805 return Error(L, "unexpected directive " + IDVal);
1806 }
Chris Lattner537ca842010-10-30 17:38:55 +00001807
Evan Chengbd27f5a2011-07-27 00:38:12 +00001808 return false;
1809}
Chris Lattner537ca842010-10-30 17:38:55 +00001810
1811
Sean Callanane88f5522010-01-23 02:43:15 +00001812extern "C" void LLVMInitializeX86AsmLexer();
1813
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001814// Force static initialization.
1815extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001816 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1817 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001818 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001819}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001820
Chris Lattner0692ee62010-09-06 19:11:01 +00001821#define GET_REGISTER_MATCHER
1822#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001823#include "X86GenAsmMatcher.inc"