blob: 95e83ecab4370219b628b4b698c52ada0727af4e [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,
42 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) {
43 return Parser.Error(L, Msg, Ranges);
44 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000045
Devang Pateld37ad242012-01-17 18:00:18 +000046 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
47 Error(Loc, Msg);
48 return 0;
49 }
50
Chris Lattner309264d2010-01-15 18:44:13 +000051 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000052 X86Operand *ParseATTOperand();
53 X86Operand *ParseIntelOperand();
Devang Pateld37ad242012-01-17 18:00:18 +000054 X86Operand *ParseIntelMemOperand();
Devang Patel7c64fe62012-01-23 18:31:58 +000055 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000056 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000057
58 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000059 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000060
Devang Patelb8ba13f2012-01-18 22:42:29 +000061 bool processInstruction(MCInst &Inst,
62 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
63
Chris Lattner7036f8b2010-09-29 01:42:58 +000064 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000065 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000066 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000067
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000068 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000069 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000070 bool isSrcOp(X86Operand &Op);
71
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000072 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
73 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000074 bool isDstOp(X86Operand &Op);
75
Evan Cheng59ee62d2011-07-11 03:57:24 +000076 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000077 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000078 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000079 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000080 void SwitchMode() {
81 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
82 setAvailableFeatures(FB);
83 }
Evan Chengebdeeab2011-07-08 01:53:10 +000084
Daniel Dunbar54074b52010-07-19 05:44:09 +000085 /// @name Auto-generated Matcher Functions
86 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000087
Chris Lattner0692ee62010-09-06 19:11:01 +000088#define GET_ASSEMBLER_HEADER
89#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000090
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000091 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000092
93public:
Devang Pateldd929fc2012-01-12 18:03:40 +000094 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +000095 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000096
Daniel Dunbar54074b52010-07-19 05:44:09 +000097 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +000098 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +000099 }
Roman Divackybf755322011-01-27 17:14:22 +0000100 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000101
Benjamin Kramer38e59892010-07-14 22:38:02 +0000102 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000103 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000104
105 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000106
107 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000108 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000109 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000110};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000111} // end anonymous namespace
112
Sean Callanane9b466d2010-01-23 00:40:33 +0000113/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000114/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000115
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000116static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000117
118/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000119
Craig Topper76bd9382012-07-18 04:59:16 +0000120static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000121 return (( Value <= 0x000000000000007FULL)||
122 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
123 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
124}
125
126static bool isImmSExti32i8Value(uint64_t Value) {
127 return (( Value <= 0x000000000000007FULL)||
128 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
129 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
130}
131
132static bool isImmZExtu32u8Value(uint64_t Value) {
133 return (Value <= 0x00000000000000FFULL);
134}
135
136static bool isImmSExti64i8Value(uint64_t Value) {
137 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000138 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000139}
140
141static bool isImmSExti64i32Value(uint64_t Value) {
142 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000143 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000144}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000145namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000146
147/// X86Operand - Instances of this class represent a parsed X86 machine
148/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000149struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000150 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000151 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000152 Register,
153 Immediate,
154 Memory
155 } Kind;
156
Chris Lattner29ef9a22010-01-15 18:51:29 +0000157 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000158
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000159 union {
160 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000161 const char *Data;
162 unsigned Length;
163 } Tok;
164
165 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000166 unsigned RegNo;
167 } Reg;
168
169 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000170 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000171 } Imm;
172
173 struct {
174 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000175 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000176 unsigned BaseReg;
177 unsigned IndexReg;
178 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000179 unsigned Size;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000180 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000181 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000182
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000183 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000184 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000185
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000186 /// getStartLoc - Get the location of the first token of this operand.
187 SMLoc getStartLoc() const { return StartLoc; }
188 /// getEndLoc - Get the location of the last token of this operand.
189 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000190
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000191 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000192
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000193 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000194
Daniel Dunbar20927f22009-08-07 08:26:05 +0000195 StringRef getToken() const {
196 assert(Kind == Token && "Invalid access!");
197 return StringRef(Tok.Data, Tok.Length);
198 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000199 void setTokenValue(StringRef Value) {
200 assert(Kind == Token && "Invalid access!");
201 Tok.Data = Value.data();
202 Tok.Length = Value.size();
203 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000204
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000205 unsigned getReg() const {
206 assert(Kind == Register && "Invalid access!");
207 return Reg.RegNo;
208 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000209
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000210 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000211 assert(Kind == Immediate && "Invalid access!");
212 return Imm.Val;
213 }
214
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000215 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000216 assert(Kind == Memory && "Invalid access!");
217 return Mem.Disp;
218 }
219 unsigned getMemSegReg() const {
220 assert(Kind == Memory && "Invalid access!");
221 return Mem.SegReg;
222 }
223 unsigned getMemBaseReg() const {
224 assert(Kind == Memory && "Invalid access!");
225 return Mem.BaseReg;
226 }
227 unsigned getMemIndexReg() const {
228 assert(Kind == Memory && "Invalid access!");
229 return Mem.IndexReg;
230 }
231 unsigned getMemScale() const {
232 assert(Kind == Memory && "Invalid access!");
233 return Mem.Scale;
234 }
235
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000236 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000237
238 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000239
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000240 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000241 if (!isImm())
242 return false;
243
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000244 // If this isn't a constant expr, just assume it fits and let relaxation
245 // handle it.
246 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
247 if (!CE)
248 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000249
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000250 // Otherwise, check the value is in a range that makes sense for this
251 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000252 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000253 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000254 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000255 if (!isImm())
256 return false;
257
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000258 // If this isn't a constant expr, just assume it fits and let relaxation
259 // handle it.
260 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
261 if (!CE)
262 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000263
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000264 // Otherwise, check the value is in a range that makes sense for this
265 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000266 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000267 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000268 bool isImmZExtu32u8() const {
269 if (!isImm())
270 return false;
271
272 // If this isn't a constant expr, just assume it fits and let relaxation
273 // handle it.
274 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
275 if (!CE)
276 return true;
277
278 // Otherwise, check the value is in a range that makes sense for this
279 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000280 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000281 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000282 bool isImmSExti64i8() const {
283 if (!isImm())
284 return false;
285
286 // If this isn't a constant expr, just assume it fits and let relaxation
287 // handle it.
288 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
289 if (!CE)
290 return true;
291
292 // Otherwise, check the value is in a range that makes sense for this
293 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000294 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000295 }
296 bool isImmSExti64i32() const {
297 if (!isImm())
298 return false;
299
300 // If this isn't a constant expr, just assume it fits and let relaxation
301 // handle it.
302 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
303 if (!CE)
304 return true;
305
306 // Otherwise, check the value is in a range that makes sense for this
307 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000308 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000309 }
310
Daniel Dunbar20927f22009-08-07 08:26:05 +0000311 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000312 bool isMem8() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000313 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
314 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000315 bool isMem16() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000316 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
317 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000318 bool isMem32() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000319 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
320 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000321 bool isMem64() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000322 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
323 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000324 bool isMem80() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000325 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
326 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000327 bool isMem128() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000328 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
329 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000330 bool isMem256() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000331 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
332 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000333
Craig Topper75dc33a2012-07-18 04:11:12 +0000334 bool isMemVX32() const {
335 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
336 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
337 }
338 bool isMemVY32() const {
339 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
340 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
341 }
342 bool isMemVX64() const {
343 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
344 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
345 }
346 bool isMemVY64() const {
347 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
348 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
349 }
350
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000351 bool isAbsMem() const {
352 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000353 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000354 }
355
Daniel Dunbar20927f22009-08-07 08:26:05 +0000356 bool isReg() const { return Kind == Register; }
357
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000358 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
359 // Add as immediates when possible.
360 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
361 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
362 else
363 Inst.addOperand(MCOperand::CreateExpr(Expr));
364 }
365
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000366 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000367 assert(N == 1 && "Invalid number of operands!");
368 Inst.addOperand(MCOperand::CreateReg(getReg()));
369 }
370
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000371 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000372 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000373 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000374 }
375
Chad Rosier36b8fed2012-06-27 22:34:28 +0000376 void addMem8Operands(MCInst &Inst, unsigned N) const {
377 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000378 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000379 void addMem16Operands(MCInst &Inst, unsigned N) const {
380 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000381 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000382 void addMem32Operands(MCInst &Inst, unsigned N) const {
383 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000384 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000385 void addMem64Operands(MCInst &Inst, unsigned N) const {
386 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000387 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000388 void addMem80Operands(MCInst &Inst, unsigned N) const {
389 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000390 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000391 void addMem128Operands(MCInst &Inst, unsigned N) const {
392 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000393 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000394 void addMem256Operands(MCInst &Inst, unsigned N) const {
395 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000396 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000397 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
398 addMemOperands(Inst, N);
399 }
400 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
401 addMemOperands(Inst, N);
402 }
403 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
404 addMemOperands(Inst, N);
405 }
406 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
407 addMemOperands(Inst, N);
408 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000409
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000410 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000411 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000412 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
413 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
414 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000415 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000416 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
417 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000418
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000419 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
420 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000421 // Add as immediates when possible.
422 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
423 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
424 else
425 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000426 }
427
Chris Lattnerb4307b32010-01-15 19:28:38 +0000428 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000429 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
430 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000431 Res->Tok.Data = Str.data();
432 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000433 return Res;
434 }
435
Chris Lattner29ef9a22010-01-15 18:51:29 +0000436 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000437 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000438 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000439 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000440 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000441
Chris Lattnerb4307b32010-01-15 19:28:38 +0000442 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
443 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000444 Res->Imm.Val = Val;
445 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000446 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000447
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000448 /// Create an absolute memory operand.
449 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000450 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000451 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
452 Res->Mem.SegReg = 0;
453 Res->Mem.Disp = Disp;
454 Res->Mem.BaseReg = 0;
455 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000456 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000457 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000458 return Res;
459 }
460
461 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000462 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
463 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000464 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
465 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000466 // We should never just have a displacement, that should be parsed as an
467 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000468 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
469
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000470 // The scale should always be one of {1,2,4,8}.
471 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000472 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000473 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000474 Res->Mem.SegReg = SegReg;
475 Res->Mem.Disp = Disp;
476 Res->Mem.BaseReg = BaseReg;
477 Res->Mem.IndexReg = IndexReg;
478 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000479 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000480 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000481 }
482};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000483
Chris Lattner37dfdec2009-07-29 06:33:53 +0000484} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000485
Devang Pateldd929fc2012-01-12 18:03:40 +0000486bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000487 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000488
489 return (Op.isMem() &&
490 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
491 isa<MCConstantExpr>(Op.Mem.Disp) &&
492 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
493 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
494}
495
Devang Pateldd929fc2012-01-12 18:03:40 +0000496bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000497 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000498
Chad Rosier36b8fed2012-06-27 22:34:28 +0000499 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000500 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000501 isa<MCConstantExpr>(Op.Mem.Disp) &&
502 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
503 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
504}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000505
Devang Pateldd929fc2012-01-12 18:03:40 +0000506bool X86AsmParser::ParseRegister(unsigned &RegNo,
507 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000508 RegNo = 0;
Devang Patelbe3e3102012-01-30 20:02:42 +0000509 if (!isParsingIntelSyntax()) {
Devang Patel1aea4302012-01-20 22:32:05 +0000510 const AsmToken &TokPercent = Parser.getTok();
Devang Pateld37ad242012-01-17 18:00:18 +0000511 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
512 StartLoc = TokPercent.getLoc();
513 Parser.Lex(); // Eat percent token.
514 }
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000515
Sean Callanan18b83232010-01-19 21:44:56 +0000516 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000517 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000518 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000519 return Error(StartLoc, "invalid register name",
520 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000521 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000522
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000523 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000524
Chris Lattner33d60d52010-09-22 04:11:10 +0000525 // If the match failed, try the register name as lowercase.
526 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000527 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000528
Evan Cheng5de728c2011-07-27 23:22:03 +0000529 if (!is64BitMode()) {
530 // FIXME: This should be done using Requires<In32BitMode> and
531 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
532 // checked.
533 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
534 // REX prefix.
535 if (RegNo == X86::RIZ ||
536 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
537 X86II::isX86_64NonExtLowByteReg(RegNo) ||
538 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000539 return Error(StartLoc, "register %"
540 + Tok.getString() + " is only available in 64-bit mode",
541 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000542 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000543
Chris Lattner33d60d52010-09-22 04:11:10 +0000544 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
545 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000546 RegNo = X86::ST0;
547 EndLoc = Tok.getLoc();
548 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000549
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000550 // Check to see if we have '(4)' after %st.
551 if (getLexer().isNot(AsmToken::LParen))
552 return false;
553 // Lex the paren.
554 getParser().Lex();
555
556 const AsmToken &IntTok = Parser.getTok();
557 if (IntTok.isNot(AsmToken::Integer))
558 return Error(IntTok.getLoc(), "expected stack index");
559 switch (IntTok.getIntVal()) {
560 case 0: RegNo = X86::ST0; break;
561 case 1: RegNo = X86::ST1; break;
562 case 2: RegNo = X86::ST2; break;
563 case 3: RegNo = X86::ST3; break;
564 case 4: RegNo = X86::ST4; break;
565 case 5: RegNo = X86::ST5; break;
566 case 6: RegNo = X86::ST6; break;
567 case 7: RegNo = X86::ST7; break;
568 default: return Error(IntTok.getLoc(), "invalid stack index");
569 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000570
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000571 if (getParser().Lex().isNot(AsmToken::RParen))
572 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000573
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000574 EndLoc = Tok.getLoc();
575 Parser.Lex(); // Eat ')'
576 return false;
577 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000578
Chris Lattner645b2092010-06-24 07:29:18 +0000579 // If this is "db[0-7]", match it as an alias
580 // for dr[0-7].
581 if (RegNo == 0 && Tok.getString().size() == 3 &&
582 Tok.getString().startswith("db")) {
583 switch (Tok.getString()[2]) {
584 case '0': RegNo = X86::DR0; break;
585 case '1': RegNo = X86::DR1; break;
586 case '2': RegNo = X86::DR2; break;
587 case '3': RegNo = X86::DR3; break;
588 case '4': RegNo = X86::DR4; break;
589 case '5': RegNo = X86::DR5; break;
590 case '6': RegNo = X86::DR6; break;
591 case '7': RegNo = X86::DR7; break;
592 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000593
Chris Lattner645b2092010-06-24 07:29:18 +0000594 if (RegNo != 0) {
595 EndLoc = Tok.getLoc();
596 Parser.Lex(); // Eat it.
597 return false;
598 }
599 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000600
Devang Patel1aea4302012-01-20 22:32:05 +0000601 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000602 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000603 return Error(StartLoc, "invalid register name",
604 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000605 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000606
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000607 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000608 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000609 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000610}
611
Devang Pateldd929fc2012-01-12 18:03:40 +0000612X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000613 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000614 return ParseIntelOperand();
615 return ParseATTOperand();
616}
617
Devang Pateld37ad242012-01-17 18:00:18 +0000618/// getIntelMemOperandSize - Return intel memory operand size.
619static unsigned getIntelMemOperandSize(StringRef OpStr) {
620 unsigned Size = 0;
Devang Patel0a338862012-01-12 01:36:43 +0000621 if (OpStr == "BYTE") Size = 8;
622 if (OpStr == "WORD") Size = 16;
623 if (OpStr == "DWORD") Size = 32;
624 if (OpStr == "QWORD") Size = 64;
625 if (OpStr == "XWORD") Size = 80;
626 if (OpStr == "XMMWORD") Size = 128;
627 if (OpStr == "YMMWORD") Size = 256;
Devang Pateld37ad242012-01-17 18:00:18 +0000628 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000629}
630
Devang Patel7c64fe62012-01-23 18:31:58 +0000631X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
632 unsigned Size) {
633 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000634 SMLoc Start = Parser.getTok().getLoc(), End;
635
Devang Pateld37ad242012-01-17 18:00:18 +0000636 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
637 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
638
639 // Eat '['
640 if (getLexer().isNot(AsmToken::LBrac))
641 return ErrorOperand(Start, "Expected '[' token!");
642 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000643
Devang Pateld37ad242012-01-17 18:00:18 +0000644 if (getLexer().is(AsmToken::Identifier)) {
645 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000646 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000647 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000648 if (getParser().ParseExpression(Disp, End)) return 0;
649 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000650 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000651 Parser.Lex();
652 return X86Operand::CreateMem(Disp, Start, End, Size);
653 }
654 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000655 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000656 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000657 SMLoc Loc = Parser.getTok().getLoc();
658 if (getLexer().is(AsmToken::RBrac)) {
659 // Handle '[' number ']'
660 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000661 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
662 if (SegReg)
663 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
664 Start, End, Size);
665 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000666 } else if (getLexer().is(AsmToken::Star)) {
667 // Handle '[' Scale*IndexReg ']'
668 Parser.Lex();
669 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000670 if (ParseRegister(IndexReg, IdxRegLoc, End))
671 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000672 Scale = Val;
673 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000674 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000675 }
676
677 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
678 bool isPlus = getLexer().is(AsmToken::Plus);
679 Parser.Lex();
680 SMLoc PlusLoc = Parser.getTok().getLoc();
681 if (getLexer().is(AsmToken::Integer)) {
682 int64_t Val = Parser.getTok().getIntVal();
683 Parser.Lex();
684 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000685 Parser.Lex();
686 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000687 if (ParseRegister(IndexReg, IdxRegLoc, End))
688 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000689 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000690 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000691 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000692 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000693 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000694 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000695 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000696 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000697 End = Parser.getTok().getLoc();
698 if (!IndexReg)
699 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000700 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000701 }
Devang Pateld37ad242012-01-17 18:00:18 +0000702 }
703
704 if (getLexer().isNot(AsmToken::RBrac))
705 if (getParser().ParseExpression(Disp, End)) return 0;
706
707 End = Parser.getTok().getLoc();
708 if (getLexer().isNot(AsmToken::RBrac))
709 return ErrorOperand(End, "expected ']' token!");
710 Parser.Lex();
711 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000712
713 // handle [-42]
714 if (!BaseReg && !IndexReg)
715 return X86Operand::CreateMem(Disp, Start, End, Size);
716
Devang Pateld37ad242012-01-17 18:00:18 +0000717 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000718 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000719}
720
721/// ParseIntelMemOperand - Parse intel style memory operand.
722X86Operand *X86AsmParser::ParseIntelMemOperand() {
723 const AsmToken &Tok = Parser.getTok();
724 SMLoc Start = Parser.getTok().getLoc(), End;
Devang Patel7c64fe62012-01-23 18:31:58 +0000725 unsigned SegReg = 0;
Devang Pateld37ad242012-01-17 18:00:18 +0000726
727 unsigned Size = getIntelMemOperandSize(Tok.getString());
728 if (Size) {
729 Parser.Lex();
730 assert (Tok.getString() == "PTR" && "Unexpected token!");
731 Parser.Lex();
732 }
733
734 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000735 return ParseIntelBracExpression(SegReg, Size);
736
737 if (!ParseRegister(SegReg, Start, End)) {
738 // Handel SegReg : [ ... ]
739 if (getLexer().isNot(AsmToken::Colon))
740 return ErrorOperand(Start, "Expected ':' token!");
741 Parser.Lex(); // Eat :
742 if (getLexer().isNot(AsmToken::LBrac))
743 return ErrorOperand(Start, "Expected '[' token!");
744 return ParseIntelBracExpression(SegReg, Size);
745 }
Devang Pateld37ad242012-01-17 18:00:18 +0000746
747 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
748 if (getParser().ParseExpression(Disp, End)) return 0;
749 return X86Operand::CreateMem(Disp, Start, End, Size);
750}
751
752X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000753 SMLoc Start = Parser.getTok().getLoc(), End;
754
755 // immediate.
756 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
757 getLexer().is(AsmToken::Minus)) {
758 const MCExpr *Val;
759 if (!getParser().ParseExpression(Val, End)) {
760 End = Parser.getTok().getLoc();
761 return X86Operand::CreateImm(Val, Start, End);
762 }
763 }
764
Devang Patel0a338862012-01-12 01:36:43 +0000765 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000766 unsigned RegNo = 0;
767 if (!ParseRegister(RegNo, Start, End)) {
Devang Patel0a338862012-01-12 01:36:43 +0000768 End = Parser.getTok().getLoc();
769 return X86Operand::CreateReg(RegNo, Start, End);
770 }
771
772 // mem operand
Devang Pateld37ad242012-01-17 18:00:18 +0000773 return ParseIntelMemOperand();
Devang Patel0a338862012-01-12 01:36:43 +0000774}
775
Devang Pateldd929fc2012-01-12 18:03:40 +0000776X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000777 switch (getLexer().getKind()) {
778 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000779 // Parse a memory operand with no segment register.
780 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000781 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000782 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000783 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000784 SMLoc Start, End;
785 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000786 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000787 Error(Start, "%eiz and %riz can only be used as index registers",
788 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000789 return 0;
790 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000791
Chris Lattnereef6d782010-04-17 18:56:34 +0000792 // If this is a segment register followed by a ':', then this is the start
793 // of a memory reference, otherwise this is a normal register reference.
794 if (getLexer().isNot(AsmToken::Colon))
795 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000796
797
Chris Lattnereef6d782010-04-17 18:56:34 +0000798 getParser().Lex(); // Eat the colon.
799 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000800 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000801 case AsmToken::Dollar: {
802 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000803 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000804 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000805 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000806 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000807 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000808 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000809 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000810 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000811}
812
Chris Lattnereef6d782010-04-17 18:56:34 +0000813/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
814/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000815X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000816
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000817 // We have to disambiguate a parenthesized expression "(4+5)" from the start
818 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000819 // only way to do this without lookahead is to eat the '(' and see what is
820 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000821 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000822 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000823 SMLoc ExprEnd;
824 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000825
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000826 // After parsing the base expression we could either have a parenthesized
827 // memory address or not. If not, return now. If so, eat the (.
828 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000829 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000830 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000831 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000832 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000833 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000834
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000835 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000836 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000837 } else {
838 // Okay, we have a '('. We don't know if this is an expression or not, but
839 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000840 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000841 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000842
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000843 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000844 // Nothing to do here, fall into the code below with the '(' part of the
845 // memory operand consumed.
846 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000847 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000848
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000849 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000850 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000851 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000852
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000853 // After parsing the base expression we could either have a parenthesized
854 // memory address or not. If not, return now. If so, eat the (.
855 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000856 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000857 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000858 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000859 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000860 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000861
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000862 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000863 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000864 }
865 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000866
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000867 // If we reached here, then we just ate the ( of the memory operand. Process
868 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000869 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000870 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000871
Chris Lattner29ef9a22010-01-15 18:51:29 +0000872 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000873 SMLoc StartLoc, EndLoc;
874 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000875 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000876 Error(StartLoc, "eiz and riz can only be used as index registers",
877 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000878 return 0;
879 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000880 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000881
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000882 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000883 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000884 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000885
886 // Following the comma we should have either an index register, or a scale
887 // value. We don't support the later form, but we want to parse it
888 // correctly.
889 //
890 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000891 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000892 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000893 SMLoc L;
894 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000895
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000896 if (getLexer().isNot(AsmToken::RParen)) {
897 // Parse the scale amount:
898 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000899 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000900 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000901 "expected comma in scale expression");
902 return 0;
903 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000904 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000905
906 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000907 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000908
909 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000910 if (getParser().ParseAbsoluteExpression(ScaleVal)){
911 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000912 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +0000913 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000914
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000915 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000916 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
917 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
918 return 0;
919 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000920 Scale = (unsigned)ScaleVal;
921 }
922 }
923 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000924 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000925 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000926 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000927
928 int64_t Value;
929 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000930 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000931
Daniel Dunbaree910252010-08-24 19:13:38 +0000932 if (Value != 1)
933 Warning(Loc, "scale factor without index register is ignored");
934 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000935 }
936 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000937
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000938 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000939 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000940 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000941 return 0;
942 }
Sean Callanan18b83232010-01-19 21:44:56 +0000943 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000944 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000945
Kevin Enderby84faf652012-03-12 21:32:09 +0000946 // If we have both a base register and an index register make sure they are
947 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +0000948 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +0000949 if (BaseReg != 0 && IndexReg != 0) {
950 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000951 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
952 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000953 IndexReg != X86::RIZ) {
954 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
955 return 0;
956 }
957 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000958 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
959 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000960 IndexReg != X86::EIZ){
961 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
962 return 0;
963 }
964 }
965
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000966 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
967 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000968}
969
Devang Pateldd929fc2012-01-12 18:03:40 +0000970bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000971ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000972 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000973 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000974
Chris Lattnerd8f71792010-11-28 20:23:50 +0000975 // FIXME: Hack to recognize setneb as setne.
976 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
977 PatchedName != "setb" && PatchedName != "setnb")
978 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000979
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000980 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
981 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000982 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000983 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
984 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +0000985 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000986 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000987 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000988 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +0000989 .Case("eq", 0x00)
990 .Case("lt", 0x01)
991 .Case("le", 0x02)
992 .Case("unord", 0x03)
993 .Case("neq", 0x04)
994 .Case("nlt", 0x05)
995 .Case("nle", 0x06)
996 .Case("ord", 0x07)
997 /* AVX only from here */
998 .Case("eq_uq", 0x08)
999 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001000 .Case("ngt", 0x0A)
1001 .Case("false", 0x0B)
1002 .Case("neq_oq", 0x0C)
1003 .Case("ge", 0x0D)
1004 .Case("gt", 0x0E)
1005 .Case("true", 0x0F)
1006 .Case("eq_os", 0x10)
1007 .Case("lt_oq", 0x11)
1008 .Case("le_oq", 0x12)
1009 .Case("unord_s", 0x13)
1010 .Case("neq_us", 0x14)
1011 .Case("nlt_uq", 0x15)
1012 .Case("nle_uq", 0x16)
1013 .Case("ord_s", 0x17)
1014 .Case("eq_us", 0x18)
1015 .Case("nge_uq", 0x19)
1016 .Case("ngt_uq", 0x1A)
1017 .Case("false_os", 0x1B)
1018 .Case("neq_os", 0x1C)
1019 .Case("ge_oq", 0x1D)
1020 .Case("gt_oq", 0x1E)
1021 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001022 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001023 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001024 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1025 getParser().getContext());
1026 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001027 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001028 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001029 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001030 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001031 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001032 } else {
1033 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001034 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001035 }
1036 }
1037 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001038
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001039 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001040
Devang Patel885f65b2012-01-30 22:47:12 +00001041 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001042 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001043
Chris Lattner2544f422010-09-08 05:17:37 +00001044 // Determine whether this is an instruction prefix.
1045 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001046 Name == "lock" || Name == "rep" ||
1047 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001048 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001049 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001050
1051
Chris Lattner2544f422010-09-08 05:17:37 +00001052 // This does the actual operand parsing. Don't parse any more if we have a
1053 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1054 // just want to parse the "lock" as the first instruction and the "incl" as
1055 // the next one.
1056 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001057
1058 // Parse '*' modifier.
1059 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001060 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001061 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001062 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001063 }
1064
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001065 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001066 if (X86Operand *Op = ParseOperand())
1067 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001068 else {
1069 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001070 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001071 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001072
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001073 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001074 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001075
1076 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001077 if (X86Operand *Op = ParseOperand())
1078 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001079 else {
1080 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001081 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001082 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001083 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001084
Chris Lattnercbf8a982010-09-11 16:18:25 +00001085 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001086 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001087 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001088 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001089 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001090 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001091
Chris Lattner2544f422010-09-08 05:17:37 +00001092 if (getLexer().is(AsmToken::EndOfStatement))
1093 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001094 else if (isPrefix && getLexer().is(AsmToken::Slash))
1095 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001096
Devang Patel885f65b2012-01-30 22:47:12 +00001097 if (ExtraImmOp && isParsingIntelSyntax())
1098 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1099
Chris Lattner98c870f2010-11-06 19:25:43 +00001100 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1101 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1102 // documented form in various unofficial manuals, so a lot of code uses it.
1103 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1104 Operands.size() == 3) {
1105 X86Operand &Op = *(X86Operand*)Operands.back();
1106 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1107 isa<MCConstantExpr>(Op.Mem.Disp) &&
1108 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1109 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1110 SMLoc Loc = Op.getEndLoc();
1111 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1112 delete &Op;
1113 }
1114 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001115 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1116 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1117 Operands.size() == 3) {
1118 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1119 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1120 isa<MCConstantExpr>(Op.Mem.Disp) &&
1121 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1122 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1123 SMLoc Loc = Op.getEndLoc();
1124 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1125 delete &Op;
1126 }
1127 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001128 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1129 if (Name.startswith("ins") && Operands.size() == 3 &&
1130 (Name == "insb" || Name == "insw" || Name == "insl")) {
1131 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1132 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1133 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1134 Operands.pop_back();
1135 Operands.pop_back();
1136 delete &Op;
1137 delete &Op2;
1138 }
1139 }
1140
1141 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1142 if (Name.startswith("outs") && Operands.size() == 3 &&
1143 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1144 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1145 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1146 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1147 Operands.pop_back();
1148 Operands.pop_back();
1149 delete &Op;
1150 delete &Op2;
1151 }
1152 }
1153
1154 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1155 if (Name.startswith("movs") && Operands.size() == 3 &&
1156 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001157 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001158 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1159 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1160 if (isSrcOp(Op) && isDstOp(Op2)) {
1161 Operands.pop_back();
1162 Operands.pop_back();
1163 delete &Op;
1164 delete &Op2;
1165 }
1166 }
1167 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1168 if (Name.startswith("lods") && Operands.size() == 3 &&
1169 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001170 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001171 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1172 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1173 if (isSrcOp(*Op1) && Op2->isReg()) {
1174 const char *ins;
1175 unsigned reg = Op2->getReg();
1176 bool isLods = Name == "lods";
1177 if (reg == X86::AL && (isLods || Name == "lodsb"))
1178 ins = "lodsb";
1179 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1180 ins = "lodsw";
1181 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1182 ins = "lodsl";
1183 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1184 ins = "lodsq";
1185 else
1186 ins = NULL;
1187 if (ins != NULL) {
1188 Operands.pop_back();
1189 Operands.pop_back();
1190 delete Op1;
1191 delete Op2;
1192 if (Name != ins)
1193 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1194 }
1195 }
1196 }
1197 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1198 if (Name.startswith("stos") && Operands.size() == 3 &&
1199 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001200 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001201 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1202 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1203 if (isDstOp(*Op2) && Op1->isReg()) {
1204 const char *ins;
1205 unsigned reg = Op1->getReg();
1206 bool isStos = Name == "stos";
1207 if (reg == X86::AL && (isStos || Name == "stosb"))
1208 ins = "stosb";
1209 else if (reg == X86::AX && (isStos || Name == "stosw"))
1210 ins = "stosw";
1211 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1212 ins = "stosl";
1213 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1214 ins = "stosq";
1215 else
1216 ins = NULL;
1217 if (ins != NULL) {
1218 Operands.pop_back();
1219 Operands.pop_back();
1220 delete Op1;
1221 delete Op2;
1222 if (Name != ins)
1223 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1224 }
1225 }
1226 }
1227
Chris Lattnere9e16a32010-09-15 04:33:27 +00001228 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001229 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001230 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001231 Name.startswith("shl") || Name.startswith("sal") ||
1232 Name.startswith("rcl") || Name.startswith("rcr") ||
1233 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001234 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001235 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001236 // Intel syntax
1237 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1238 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001239 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1240 delete Operands[2];
1241 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001242 }
1243 } else {
1244 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1245 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001246 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1247 delete Operands[1];
1248 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001249 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001250 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001251 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001252
Chris Lattner15f89512011-04-09 19:41:05 +00001253 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1254 // instalias with an immediate operand yet.
1255 if (Name == "int" && Operands.size() == 2) {
1256 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1257 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1258 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1259 delete Operands[1];
1260 Operands.erase(Operands.begin() + 1);
1261 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1262 }
1263 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001264
Chris Lattner98986712010-01-14 22:21:20 +00001265 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001266}
1267
Devang Pateldd929fc2012-01-12 18:03:40 +00001268bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001269processInstruction(MCInst &Inst,
1270 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1271 switch (Inst.getOpcode()) {
1272 default: return false;
1273 case X86::AND16i16: {
1274 if (!Inst.getOperand(0).isImm() ||
1275 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1276 return false;
1277
1278 MCInst TmpInst;
1279 TmpInst.setOpcode(X86::AND16ri8);
1280 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1281 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1282 TmpInst.addOperand(Inst.getOperand(0));
1283 Inst = TmpInst;
1284 return true;
1285 }
1286 case X86::AND32i32: {
1287 if (!Inst.getOperand(0).isImm() ||
1288 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1289 return false;
1290
1291 MCInst TmpInst;
1292 TmpInst.setOpcode(X86::AND32ri8);
1293 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1294 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1295 TmpInst.addOperand(Inst.getOperand(0));
1296 Inst = TmpInst;
1297 return true;
1298 }
1299 case X86::AND64i32: {
1300 if (!Inst.getOperand(0).isImm() ||
1301 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1302 return false;
1303
1304 MCInst TmpInst;
1305 TmpInst.setOpcode(X86::AND64ri8);
1306 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1307 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1308 TmpInst.addOperand(Inst.getOperand(0));
1309 Inst = TmpInst;
1310 return true;
1311 }
Devang Patelac0f0482012-01-19 17:53:25 +00001312 case X86::XOR16i16: {
1313 if (!Inst.getOperand(0).isImm() ||
1314 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1315 return false;
1316
1317 MCInst TmpInst;
1318 TmpInst.setOpcode(X86::XOR16ri8);
1319 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1320 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1321 TmpInst.addOperand(Inst.getOperand(0));
1322 Inst = TmpInst;
1323 return true;
1324 }
1325 case X86::XOR32i32: {
1326 if (!Inst.getOperand(0).isImm() ||
1327 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1328 return false;
1329
1330 MCInst TmpInst;
1331 TmpInst.setOpcode(X86::XOR32ri8);
1332 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1333 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1334 TmpInst.addOperand(Inst.getOperand(0));
1335 Inst = TmpInst;
1336 return true;
1337 }
1338 case X86::XOR64i32: {
1339 if (!Inst.getOperand(0).isImm() ||
1340 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1341 return false;
1342
1343 MCInst TmpInst;
1344 TmpInst.setOpcode(X86::XOR64ri8);
1345 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1346 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1347 TmpInst.addOperand(Inst.getOperand(0));
1348 Inst = TmpInst;
1349 return true;
1350 }
1351 case X86::OR16i16: {
1352 if (!Inst.getOperand(0).isImm() ||
1353 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1354 return false;
1355
1356 MCInst TmpInst;
1357 TmpInst.setOpcode(X86::OR16ri8);
1358 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1359 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1360 TmpInst.addOperand(Inst.getOperand(0));
1361 Inst = TmpInst;
1362 return true;
1363 }
1364 case X86::OR32i32: {
1365 if (!Inst.getOperand(0).isImm() ||
1366 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1367 return false;
1368
1369 MCInst TmpInst;
1370 TmpInst.setOpcode(X86::OR32ri8);
1371 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1372 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1373 TmpInst.addOperand(Inst.getOperand(0));
1374 Inst = TmpInst;
1375 return true;
1376 }
1377 case X86::OR64i32: {
1378 if (!Inst.getOperand(0).isImm() ||
1379 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1380 return false;
1381
1382 MCInst TmpInst;
1383 TmpInst.setOpcode(X86::OR64ri8);
1384 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1385 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1386 TmpInst.addOperand(Inst.getOperand(0));
1387 Inst = TmpInst;
1388 return true;
1389 }
1390 case X86::CMP16i16: {
1391 if (!Inst.getOperand(0).isImm() ||
1392 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1393 return false;
1394
1395 MCInst TmpInst;
1396 TmpInst.setOpcode(X86::CMP16ri8);
1397 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1398 TmpInst.addOperand(Inst.getOperand(0));
1399 Inst = TmpInst;
1400 return true;
1401 }
1402 case X86::CMP32i32: {
1403 if (!Inst.getOperand(0).isImm() ||
1404 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1405 return false;
1406
1407 MCInst TmpInst;
1408 TmpInst.setOpcode(X86::CMP32ri8);
1409 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1410 TmpInst.addOperand(Inst.getOperand(0));
1411 Inst = TmpInst;
1412 return true;
1413 }
1414 case X86::CMP64i32: {
1415 if (!Inst.getOperand(0).isImm() ||
1416 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1417 return false;
1418
1419 MCInst TmpInst;
1420 TmpInst.setOpcode(X86::CMP64ri8);
1421 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1422 TmpInst.addOperand(Inst.getOperand(0));
1423 Inst = TmpInst;
1424 return true;
1425 }
Devang Patela951f772012-01-19 18:40:55 +00001426 case X86::ADD16i16: {
1427 if (!Inst.getOperand(0).isImm() ||
1428 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1429 return false;
1430
1431 MCInst TmpInst;
1432 TmpInst.setOpcode(X86::ADD16ri8);
1433 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1434 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1435 TmpInst.addOperand(Inst.getOperand(0));
1436 Inst = TmpInst;
1437 return true;
1438 }
1439 case X86::ADD32i32: {
1440 if (!Inst.getOperand(0).isImm() ||
1441 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1442 return false;
1443
1444 MCInst TmpInst;
1445 TmpInst.setOpcode(X86::ADD32ri8);
1446 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1447 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1448 TmpInst.addOperand(Inst.getOperand(0));
1449 Inst = TmpInst;
1450 return true;
1451 }
1452 case X86::ADD64i32: {
1453 if (!Inst.getOperand(0).isImm() ||
1454 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1455 return false;
1456
1457 MCInst TmpInst;
1458 TmpInst.setOpcode(X86::ADD64ri8);
1459 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1460 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1461 TmpInst.addOperand(Inst.getOperand(0));
1462 Inst = TmpInst;
1463 return true;
1464 }
1465 case X86::SUB16i16: {
1466 if (!Inst.getOperand(0).isImm() ||
1467 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1468 return false;
1469
1470 MCInst TmpInst;
1471 TmpInst.setOpcode(X86::SUB16ri8);
1472 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1473 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1474 TmpInst.addOperand(Inst.getOperand(0));
1475 Inst = TmpInst;
1476 return true;
1477 }
1478 case X86::SUB32i32: {
1479 if (!Inst.getOperand(0).isImm() ||
1480 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1481 return false;
1482
1483 MCInst TmpInst;
1484 TmpInst.setOpcode(X86::SUB32ri8);
1485 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1486 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1487 TmpInst.addOperand(Inst.getOperand(0));
1488 Inst = TmpInst;
1489 return true;
1490 }
1491 case X86::SUB64i32: {
1492 if (!Inst.getOperand(0).isImm() ||
1493 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1494 return false;
1495
1496 MCInst TmpInst;
1497 TmpInst.setOpcode(X86::SUB64ri8);
1498 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1499 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1500 TmpInst.addOperand(Inst.getOperand(0));
1501 Inst = TmpInst;
1502 return true;
1503 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001504 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001505}
1506
1507bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001508MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001509 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001510 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001511 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001512 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1513 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001514
Chris Lattner7c51a312010-09-29 01:50:45 +00001515 // First, handle aliases that expand to multiple instructions.
1516 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +00001517 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
1518 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001519 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001520 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001521 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001522 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001523 MCInst Inst;
1524 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001525 Inst.setLoc(IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001526 Out.EmitInstruction(Inst);
1527
Chris Lattner0bb83a82010-09-30 16:39:29 +00001528 const char *Repl =
1529 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001530 .Case("finit", "fninit")
1531 .Case("fsave", "fnsave")
1532 .Case("fstcw", "fnstcw")
1533 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001534 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001535 .Case("fstsw", "fnstsw")
1536 .Case("fstsww", "fnstsw")
1537 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001538 .Default(0);
1539 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001540 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001541 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001542 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001543
Chris Lattnera008e8a2010-09-06 21:54:15 +00001544 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +00001545 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001546 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001547
Daniel Dunbarc918d602010-05-04 16:12:42 +00001548 // First, try a direct match.
Devang Patelbe3e3102012-01-30 20:02:42 +00001549 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo,
1550 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001551 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001552 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001553 // Some instructions need post-processing to, for example, tweak which
1554 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001555 // individual transformations can chain off each other.
Devang Patelb8ba13f2012-01-18 22:42:29 +00001556 while (processInstruction(Inst, Operands))
1557 ;
1558
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001559 Inst.setLoc(IDLoc);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001560 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001561 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001562 case Match_MissingFeature:
1563 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1564 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +00001565 case Match_ConversionFail:
1566 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001567 case Match_InvalidOperand:
1568 WasOriginallyInvalidOperand = true;
1569 break;
1570 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001571 break;
1572 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001573
Daniel Dunbarc918d602010-05-04 16:12:42 +00001574 // FIXME: Ideally, we would only attempt suffix matches for things which are
1575 // valid prefixes, and we could just infer the right unambiguous
1576 // type. However, that requires substantially more matcher support than the
1577 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001578
Daniel Dunbarc918d602010-05-04 16:12:42 +00001579 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001580 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001581 SmallString<16> Tmp;
1582 Tmp += Base;
1583 Tmp += ' ';
1584 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001585
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001586 // If this instruction starts with an 'f', then it is a floating point stack
1587 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1588 // 80-bit floating point, which use the suffixes s,l,t respectively.
1589 //
1590 // Otherwise, we assume that this may be an integer instruction, which comes
1591 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1592 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001593
Daniel Dunbarc918d602010-05-04 16:12:42 +00001594 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001595 Tmp[Base.size()] = Suffixes[0];
1596 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001597 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001598
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001599 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1600 Tmp[Base.size()] = Suffixes[1];
1601 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1602 Tmp[Base.size()] = Suffixes[2];
1603 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1604 Tmp[Base.size()] = Suffixes[3];
1605 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001606
1607 // Restore the old token.
1608 Op->setTokenValue(Base);
1609
1610 // If exactly one matched, then we treat that as a successful match (and the
1611 // instruction will already have been filled in correctly, since the failing
1612 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001613 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001614 (Match1 == Match_Success) + (Match2 == Match_Success) +
1615 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001616 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001617 Inst.setLoc(IDLoc);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001618 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001619 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001620 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001621
Chris Lattnerec6789f2010-09-06 20:08:02 +00001622 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001623
Daniel Dunbar09062b12010-08-12 00:55:42 +00001624 // If we had multiple suffix matches, then identify this as an ambiguous
1625 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001626 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001627 char MatchChars[4];
1628 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001629 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1630 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1631 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1632 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001633
1634 SmallString<126> Msg;
1635 raw_svector_ostream OS(Msg);
1636 OS << "ambiguous instructions require an explicit suffix (could be ";
1637 for (unsigned i = 0; i != NumMatches; ++i) {
1638 if (i != 0)
1639 OS << ", ";
1640 if (i + 1 == NumMatches)
1641 OS << "or ";
1642 OS << "'" << Base << MatchChars[i] << "'";
1643 }
1644 OS << ")";
1645 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001646 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001647 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001648
Chris Lattnera008e8a2010-09-06 21:54:15 +00001649 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001650
Chris Lattnera008e8a2010-09-06 21:54:15 +00001651 // If all of the instructions reported an invalid mnemonic, then the original
1652 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001653 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1654 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001655 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001656 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
1657 Op->getLocRange());
Chris Lattnerce4a3352010-09-06 22:11:18 +00001658 }
1659
1660 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001661 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001662 if (OrigErrorInfo >= Operands.size())
1663 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001664
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001665 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1666 if (Operand->getStartLoc().isValid()) {
1667 SMRange OperandRange = Operand->getLocRange();
1668 return Error(Operand->getStartLoc(), "invalid operand for instruction",
1669 OperandRange);
1670 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001671 }
1672
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001673 return Error(IDLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001674 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001675
Chris Lattnerec6789f2010-09-06 20:08:02 +00001676 // If one instruction matched with a missing feature, report this as a
1677 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001678 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1679 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001680 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1681 return true;
1682 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001683
Chris Lattnera008e8a2010-09-06 21:54:15 +00001684 // If one instruction matched with an invalid operand, report this as an
1685 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001686 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1687 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001688 Error(IDLoc, "invalid operand for instruction");
1689 return true;
1690 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001691
Chris Lattnerec6789f2010-09-06 20:08:02 +00001692 // If all of these were an outright failure, report it in a useless way.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001693 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001694 return true;
1695}
1696
1697
Devang Pateldd929fc2012-01-12 18:03:40 +00001698bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001699 StringRef IDVal = DirectiveID.getIdentifier();
1700 if (IDVal == ".word")
1701 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001702 else if (IDVal.startswith(".code"))
1703 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Devang Patelbe3e3102012-01-30 20:02:42 +00001704 else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001705 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001706 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1707 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001708 // FIXME : Handle noprefix
1709 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001710 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001711 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001712 }
1713 return false;
1714 }
Chris Lattner537ca842010-10-30 17:38:55 +00001715 return true;
1716}
1717
1718/// ParseDirectiveWord
1719/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001720bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001721 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1722 for (;;) {
1723 const MCExpr *Value;
1724 if (getParser().ParseExpression(Value))
1725 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001726
Chris Lattner537ca842010-10-30 17:38:55 +00001727 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001728
Chris Lattner537ca842010-10-30 17:38:55 +00001729 if (getLexer().is(AsmToken::EndOfStatement))
1730 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001731
Chris Lattner537ca842010-10-30 17:38:55 +00001732 // FIXME: Improve diagnostic.
1733 if (getLexer().isNot(AsmToken::Comma))
1734 return Error(L, "unexpected token in directive");
1735 Parser.Lex();
1736 }
1737 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001738
Chris Lattner537ca842010-10-30 17:38:55 +00001739 Parser.Lex();
1740 return false;
1741}
1742
Evan Chengbd27f5a2011-07-27 00:38:12 +00001743/// ParseDirectiveCode
1744/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001745bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001746 if (IDVal == ".code32") {
1747 Parser.Lex();
1748 if (is64BitMode()) {
1749 SwitchMode();
1750 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1751 }
1752 } else if (IDVal == ".code64") {
1753 Parser.Lex();
1754 if (!is64BitMode()) {
1755 SwitchMode();
1756 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1757 }
1758 } else {
1759 return Error(L, "unexpected directive " + IDVal);
1760 }
Chris Lattner537ca842010-10-30 17:38:55 +00001761
Evan Chengbd27f5a2011-07-27 00:38:12 +00001762 return false;
1763}
Chris Lattner537ca842010-10-30 17:38:55 +00001764
1765
Sean Callanane88f5522010-01-23 02:43:15 +00001766extern "C" void LLVMInitializeX86AsmLexer();
1767
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001768// Force static initialization.
1769extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001770 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1771 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001772 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001773}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001774
Chris Lattner0692ee62010-09-06 19:11:01 +00001775#define GET_REGISTER_MATCHER
1776#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001777#include "X86GenAsmMatcher.inc"