blob: 6fe2e26959524a4fee9883366eb67b1bd16a8052 [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>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000043 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
Chad Rosier84125ca2012-10-13 00:26:04 +000066 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000067 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000068 MCStreamer &Out, unsigned &ErrorInfo,
69 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000070
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000071 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000072 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000073 bool isSrcOp(X86Operand &Op);
74
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000075 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
76 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000077 bool isDstOp(X86Operand &Op);
78
Evan Cheng59ee62d2011-07-11 03:57:24 +000079 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000080 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000081 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000082 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000083 void SwitchMode() {
84 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
85 setAvailableFeatures(FB);
86 }
Evan Chengebdeeab2011-07-08 01:53:10 +000087
Daniel Dunbar54074b52010-07-19 05:44:09 +000088 /// @name Auto-generated Matcher Functions
89 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000090
Chris Lattner0692ee62010-09-06 19:11:01 +000091#define GET_ASSEMBLER_HEADER
92#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000093
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000094 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000095
96public:
Devang Pateldd929fc2012-01-12 18:03:40 +000097 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +000098 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000099
Daniel Dunbar54074b52010-07-19 05:44:09 +0000100 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000101 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000102 }
Roman Divackybf755322011-01-27 17:14:22 +0000103 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000104
Benjamin Kramer38e59892010-07-14 22:38:02 +0000105 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000106 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000107
108 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000109
110 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000111 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000112 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000113};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000114} // end anonymous namespace
115
Sean Callanane9b466d2010-01-23 00:40:33 +0000116/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000117/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000118
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000119static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000120
121/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000122
Craig Topper76bd9382012-07-18 04:59:16 +0000123static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000124 return (( Value <= 0x000000000000007FULL)||
125 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
126 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
127}
128
129static bool isImmSExti32i8Value(uint64_t Value) {
130 return (( Value <= 0x000000000000007FULL)||
131 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
132 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
133}
134
135static bool isImmZExtu32u8Value(uint64_t Value) {
136 return (Value <= 0x00000000000000FFULL);
137}
138
139static bool isImmSExti64i8Value(uint64_t Value) {
140 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000141 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000142}
143
144static bool isImmSExti64i32Value(uint64_t Value) {
145 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000146 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000147}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000148namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000149
150/// X86Operand - Instances of this class represent a parsed X86 machine
151/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000152struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000153 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000154 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000155 Register,
156 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000157 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000158 } Kind;
159
Chris Lattner29ef9a22010-01-15 18:51:29 +0000160 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000161
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000162 union {
163 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000164 const char *Data;
165 unsigned Length;
166 } Tok;
167
168 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000169 unsigned RegNo;
170 } Reg;
171
172 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000173 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000174 } Imm;
175
176 struct {
177 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000178 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000179 unsigned BaseReg;
180 unsigned IndexReg;
181 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000182 unsigned Size;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000183 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000184 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000185
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000186 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000187 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000188
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000189 /// getStartLoc - Get the location of the first token of this operand.
190 SMLoc getStartLoc() const { return StartLoc; }
191 /// getEndLoc - Get the location of the last token of this operand.
192 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000193 /// getLocRange - Get the range between the first and last token of this
194 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000195 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000196
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000197 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000198
Daniel Dunbar20927f22009-08-07 08:26:05 +0000199 StringRef getToken() const {
200 assert(Kind == Token && "Invalid access!");
201 return StringRef(Tok.Data, Tok.Length);
202 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000203 void setTokenValue(StringRef Value) {
204 assert(Kind == Token && "Invalid access!");
205 Tok.Data = Value.data();
206 Tok.Length = Value.size();
207 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000208
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000209 unsigned getReg() const {
210 assert(Kind == Register && "Invalid access!");
211 return Reg.RegNo;
212 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000213
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000214 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000215 assert(Kind == Immediate && "Invalid access!");
216 return Imm.Val;
217 }
218
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000219 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000220 assert(Kind == Memory && "Invalid access!");
221 return Mem.Disp;
222 }
223 unsigned getMemSegReg() const {
224 assert(Kind == Memory && "Invalid access!");
225 return Mem.SegReg;
226 }
227 unsigned getMemBaseReg() const {
228 assert(Kind == Memory && "Invalid access!");
229 return Mem.BaseReg;
230 }
231 unsigned getMemIndexReg() const {
232 assert(Kind == Memory && "Invalid access!");
233 return Mem.IndexReg;
234 }
235 unsigned getMemScale() const {
236 assert(Kind == Memory && "Invalid access!");
237 return Mem.Scale;
238 }
239
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000240 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000241
242 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000243
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000244 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000245 if (!isImm())
246 return false;
247
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000248 // If this isn't a constant expr, just assume it fits and let relaxation
249 // handle it.
250 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
251 if (!CE)
252 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000253
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000254 // Otherwise, check the value is in a range that makes sense for this
255 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000256 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000257 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000258 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000259 if (!isImm())
260 return false;
261
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000262 // If this isn't a constant expr, just assume it fits and let relaxation
263 // handle it.
264 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
265 if (!CE)
266 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000267
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000268 // Otherwise, check the value is in a range that makes sense for this
269 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000270 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000271 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000272 bool isImmZExtu32u8() const {
273 if (!isImm())
274 return false;
275
276 // If this isn't a constant expr, just assume it fits and let relaxation
277 // handle it.
278 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
279 if (!CE)
280 return true;
281
282 // Otherwise, check the value is in a range that makes sense for this
283 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000284 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000285 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000286 bool isImmSExti64i8() const {
287 if (!isImm())
288 return false;
289
290 // If this isn't a constant expr, just assume it fits and let relaxation
291 // handle it.
292 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
293 if (!CE)
294 return true;
295
296 // Otherwise, check the value is in a range that makes sense for this
297 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000298 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000299 }
300 bool isImmSExti64i32() const {
301 if (!isImm())
302 return false;
303
304 // If this isn't a constant expr, just assume it fits and let relaxation
305 // handle it.
306 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
307 if (!CE)
308 return true;
309
310 // Otherwise, check the value is in a range that makes sense for this
311 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000312 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000313 }
314
Daniel Dunbar20927f22009-08-07 08:26:05 +0000315 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000316 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000317 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000318 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000319 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000320 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000321 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000322 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000323 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000324 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000325 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000326 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000327 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000328 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000329 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000330 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000331 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000332 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000333 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000334 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000335 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000336 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000337
Craig Topper75dc33a2012-07-18 04:11:12 +0000338 bool isMemVX32() const {
339 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
340 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
341 }
342 bool isMemVY32() const {
343 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
344 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
345 }
346 bool isMemVX64() const {
347 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
348 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
349 }
350 bool isMemVY64() const {
351 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
352 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
353 }
354
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000355 bool isAbsMem() const {
356 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000357 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000358 }
359
Daniel Dunbar20927f22009-08-07 08:26:05 +0000360 bool isReg() const { return Kind == Register; }
361
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000362 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
363 // Add as immediates when possible.
364 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
365 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
366 else
367 Inst.addOperand(MCOperand::CreateExpr(Expr));
368 }
369
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000370 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000371 assert(N == 1 && "Invalid number of operands!");
372 Inst.addOperand(MCOperand::CreateReg(getReg()));
373 }
374
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000375 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000376 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000377 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000378 }
379
Chad Rosier36b8fed2012-06-27 22:34:28 +0000380 void addMem8Operands(MCInst &Inst, unsigned N) const {
381 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000382 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000383 void addMem16Operands(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 addMem32Operands(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 addMem64Operands(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 addMem80Operands(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 addMem128Operands(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 addMem256Operands(MCInst &Inst, unsigned N) const {
399 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000400 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000401 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
402 addMemOperands(Inst, N);
403 }
404 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
405 addMemOperands(Inst, N);
406 }
407 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
408 addMemOperands(Inst, N);
409 }
410 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
411 addMemOperands(Inst, N);
412 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000413
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000414 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000415 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000416 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
417 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
418 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000419 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000420 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
421 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000422
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000423 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
424 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000425 // Add as immediates when possible.
426 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
427 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
428 else
429 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000430 }
431
Chris Lattnerb4307b32010-01-15 19:28:38 +0000432 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000433 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
434 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000435 Res->Tok.Data = Str.data();
436 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000437 return Res;
438 }
439
Chris Lattner29ef9a22010-01-15 18:51:29 +0000440 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000441 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000442 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000443 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000444 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000445
Chris Lattnerb4307b32010-01-15 19:28:38 +0000446 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
447 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000448 Res->Imm.Val = Val;
449 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000450 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000451
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000452 /// Create an absolute memory operand.
453 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000454 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000455 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
456 Res->Mem.SegReg = 0;
457 Res->Mem.Disp = Disp;
458 Res->Mem.BaseReg = 0;
459 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000460 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000461 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000462 return Res;
463 }
464
465 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000466 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
467 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000468 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
469 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000470 // We should never just have a displacement, that should be parsed as an
471 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000472 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
473
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000474 // The scale should always be one of {1,2,4,8}.
475 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000476 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000477 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000478 Res->Mem.SegReg = SegReg;
479 Res->Mem.Disp = Disp;
480 Res->Mem.BaseReg = BaseReg;
481 Res->Mem.IndexReg = IndexReg;
482 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000483 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000484 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000485 }
486};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000487
Chris Lattner37dfdec2009-07-29 06:33:53 +0000488} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000489
Devang Pateldd929fc2012-01-12 18:03:40 +0000490bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000491 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000492
493 return (Op.isMem() &&
494 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
495 isa<MCConstantExpr>(Op.Mem.Disp) &&
496 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
497 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
498}
499
Devang Pateldd929fc2012-01-12 18:03:40 +0000500bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000501 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000502
Chad Rosier36b8fed2012-06-27 22:34:28 +0000503 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000504 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000505 isa<MCConstantExpr>(Op.Mem.Disp) &&
506 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
507 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
508}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000509
Devang Pateldd929fc2012-01-12 18:03:40 +0000510bool X86AsmParser::ParseRegister(unsigned &RegNo,
511 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000512 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000513 const AsmToken &PercentTok = Parser.getTok();
514 StartLoc = PercentTok.getLoc();
515
516 // If we encounter a %, ignore it. This code handles registers with and
517 // without the prefix, unprefixed registers can occur in cfi directives.
518 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000519 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000520
Sean Callanan18b83232010-01-19 21:44:56 +0000521 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000522 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000523 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000524 return Error(StartLoc, "invalid register name",
525 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000526 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000527
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000528 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000529
Chris Lattner33d60d52010-09-22 04:11:10 +0000530 // If the match failed, try the register name as lowercase.
531 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000532 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000533
Evan Cheng5de728c2011-07-27 23:22:03 +0000534 if (!is64BitMode()) {
535 // FIXME: This should be done using Requires<In32BitMode> and
536 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
537 // checked.
538 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
539 // REX prefix.
540 if (RegNo == X86::RIZ ||
541 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
542 X86II::isX86_64NonExtLowByteReg(RegNo) ||
543 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000544 return Error(StartLoc, "register %"
545 + Tok.getString() + " is only available in 64-bit mode",
546 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000547 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000548
Chris Lattner33d60d52010-09-22 04:11:10 +0000549 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
550 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000551 RegNo = X86::ST0;
552 EndLoc = Tok.getLoc();
553 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000554
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000555 // Check to see if we have '(4)' after %st.
556 if (getLexer().isNot(AsmToken::LParen))
557 return false;
558 // Lex the paren.
559 getParser().Lex();
560
561 const AsmToken &IntTok = Parser.getTok();
562 if (IntTok.isNot(AsmToken::Integer))
563 return Error(IntTok.getLoc(), "expected stack index");
564 switch (IntTok.getIntVal()) {
565 case 0: RegNo = X86::ST0; break;
566 case 1: RegNo = X86::ST1; break;
567 case 2: RegNo = X86::ST2; break;
568 case 3: RegNo = X86::ST3; break;
569 case 4: RegNo = X86::ST4; break;
570 case 5: RegNo = X86::ST5; break;
571 case 6: RegNo = X86::ST6; break;
572 case 7: RegNo = X86::ST7; break;
573 default: return Error(IntTok.getLoc(), "invalid stack index");
574 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000575
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000576 if (getParser().Lex().isNot(AsmToken::RParen))
577 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000578
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000579 EndLoc = Tok.getLoc();
580 Parser.Lex(); // Eat ')'
581 return false;
582 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000583
Chris Lattner645b2092010-06-24 07:29:18 +0000584 // If this is "db[0-7]", match it as an alias
585 // for dr[0-7].
586 if (RegNo == 0 && Tok.getString().size() == 3 &&
587 Tok.getString().startswith("db")) {
588 switch (Tok.getString()[2]) {
589 case '0': RegNo = X86::DR0; break;
590 case '1': RegNo = X86::DR1; break;
591 case '2': RegNo = X86::DR2; break;
592 case '3': RegNo = X86::DR3; break;
593 case '4': RegNo = X86::DR4; break;
594 case '5': RegNo = X86::DR5; break;
595 case '6': RegNo = X86::DR6; break;
596 case '7': RegNo = X86::DR7; break;
597 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000598
Chris Lattner645b2092010-06-24 07:29:18 +0000599 if (RegNo != 0) {
600 EndLoc = Tok.getLoc();
601 Parser.Lex(); // Eat it.
602 return false;
603 }
604 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000605
Devang Patel1aea4302012-01-20 22:32:05 +0000606 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000607 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000608 return Error(StartLoc, "invalid register name",
609 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000610 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000611
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000612 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000613 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000614 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000615}
616
Devang Pateldd929fc2012-01-12 18:03:40 +0000617X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000618 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000619 return ParseIntelOperand();
620 return ParseATTOperand();
621}
622
Devang Pateld37ad242012-01-17 18:00:18 +0000623/// getIntelMemOperandSize - Return intel memory operand size.
624static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000625 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000626 .Cases("BYTE", "byte", 8)
627 .Cases("WORD", "word", 16)
628 .Cases("DWORD", "dword", 32)
629 .Cases("QWORD", "qword", 64)
630 .Cases("XWORD", "xword", 80)
631 .Cases("XMMWORD", "xmmword", 128)
632 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000633 .Default(0);
634 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000635}
636
Devang Patel7c64fe62012-01-23 18:31:58 +0000637X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
638 unsigned Size) {
639 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000640 SMLoc Start = Parser.getTok().getLoc(), End;
641
Devang Pateld37ad242012-01-17 18:00:18 +0000642 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
643 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
644
645 // Eat '['
646 if (getLexer().isNot(AsmToken::LBrac))
647 return ErrorOperand(Start, "Expected '[' token!");
648 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000649
Devang Pateld37ad242012-01-17 18:00:18 +0000650 if (getLexer().is(AsmToken::Identifier)) {
651 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000652 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000653 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000654 if (getParser().ParseExpression(Disp, End)) return 0;
655 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000656 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000657 Parser.Lex();
658 return X86Operand::CreateMem(Disp, Start, End, Size);
659 }
660 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000661 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000662 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000663 SMLoc Loc = Parser.getTok().getLoc();
664 if (getLexer().is(AsmToken::RBrac)) {
665 // Handle '[' number ']'
666 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000667 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
668 if (SegReg)
669 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
670 Start, End, Size);
671 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000672 } else if (getLexer().is(AsmToken::Star)) {
673 // Handle '[' Scale*IndexReg ']'
674 Parser.Lex();
675 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000676 if (ParseRegister(IndexReg, IdxRegLoc, End))
677 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000678 Scale = Val;
679 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000680 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000681 }
682
683 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
684 bool isPlus = getLexer().is(AsmToken::Plus);
685 Parser.Lex();
686 SMLoc PlusLoc = Parser.getTok().getLoc();
687 if (getLexer().is(AsmToken::Integer)) {
688 int64_t Val = Parser.getTok().getIntVal();
689 Parser.Lex();
690 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000691 Parser.Lex();
692 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000693 if (ParseRegister(IndexReg, IdxRegLoc, End))
694 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000695 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000696 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000697 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000698 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000699 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000700 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000701 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000702 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000703 End = Parser.getTok().getLoc();
704 if (!IndexReg)
705 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000706 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000707 }
Devang Pateld37ad242012-01-17 18:00:18 +0000708 }
709
710 if (getLexer().isNot(AsmToken::RBrac))
711 if (getParser().ParseExpression(Disp, End)) return 0;
712
713 End = Parser.getTok().getLoc();
714 if (getLexer().isNot(AsmToken::RBrac))
715 return ErrorOperand(End, "expected ']' token!");
716 Parser.Lex();
717 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000718
719 // handle [-42]
720 if (!BaseReg && !IndexReg)
721 return X86Operand::CreateMem(Disp, Start, End, Size);
722
Devang Pateld37ad242012-01-17 18:00:18 +0000723 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000724 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000725}
726
727/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000728X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000729 const AsmToken &Tok = Parser.getTok();
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000730 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000731
732 unsigned Size = getIntelMemOperandSize(Tok.getString());
733 if (Size) {
734 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000735 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
736 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000737 Parser.Lex();
738 }
739
740 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000741 return ParseIntelBracExpression(SegReg, Size);
742
743 if (!ParseRegister(SegReg, Start, End)) {
744 // Handel SegReg : [ ... ]
745 if (getLexer().isNot(AsmToken::Colon))
746 return ErrorOperand(Start, "Expected ':' token!");
747 Parser.Lex(); // Eat :
748 if (getLexer().isNot(AsmToken::LBrac))
749 return ErrorOperand(Start, "Expected '[' token!");
750 return ParseIntelBracExpression(SegReg, Size);
751 }
Devang Pateld37ad242012-01-17 18:00:18 +0000752
753 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
754 if (getParser().ParseExpression(Disp, End)) return 0;
755 return X86Operand::CreateMem(Disp, Start, End, Size);
756}
757
758X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000759 SMLoc Start = Parser.getTok().getLoc(), End;
760
761 // immediate.
762 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
763 getLexer().is(AsmToken::Minus)) {
764 const MCExpr *Val;
765 if (!getParser().ParseExpression(Val, End)) {
766 End = Parser.getTok().getLoc();
767 return X86Operand::CreateImm(Val, Start, End);
768 }
769 }
770
Devang Patel0a338862012-01-12 01:36:43 +0000771 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000772 unsigned RegNo = 0;
773 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000774 // If this is a segment register followed by a ':', then this is the start
775 // of a memory reference, otherwise this is a normal register reference.
776 if (getLexer().isNot(AsmToken::Colon))
777 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
778
779 getParser().Lex(); // Eat the colon.
780 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000781 }
782
783 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000784 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000785}
786
Devang Pateldd929fc2012-01-12 18:03:40 +0000787X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000788 switch (getLexer().getKind()) {
789 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000790 // Parse a memory operand with no segment register.
791 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000792 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000793 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000794 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000795 SMLoc Start, End;
796 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000797 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000798 Error(Start, "%eiz and %riz can only be used as index registers",
799 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000800 return 0;
801 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000802
Chris Lattnereef6d782010-04-17 18:56:34 +0000803 // If this is a segment register followed by a ':', then this is the start
804 // of a memory reference, otherwise this is a normal register reference.
805 if (getLexer().isNot(AsmToken::Colon))
806 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000807
808
Chris Lattnereef6d782010-04-17 18:56:34 +0000809 getParser().Lex(); // Eat the colon.
810 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000811 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000812 case AsmToken::Dollar: {
813 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000814 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000815 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000816 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000817 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000818 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000819 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000820 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000821 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000822}
823
Chris Lattnereef6d782010-04-17 18:56:34 +0000824/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
825/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000826X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000827
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000828 // We have to disambiguate a parenthesized expression "(4+5)" from the start
829 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000830 // only way to do this without lookahead is to eat the '(' and see what is
831 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000832 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000833 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000834 SMLoc ExprEnd;
835 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000836
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000837 // After parsing the base expression we could either have a parenthesized
838 // memory address or not. If not, return now. If so, eat the (.
839 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000840 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000841 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000842 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000843 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000844 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000845
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000846 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000847 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000848 } else {
849 // Okay, we have a '('. We don't know if this is an expression or not, but
850 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000851 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000852 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000853
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000854 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000855 // Nothing to do here, fall into the code below with the '(' part of the
856 // memory operand consumed.
857 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000858 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000859
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000860 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000861 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000862 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000863
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000864 // After parsing the base expression we could either have a parenthesized
865 // memory address or not. If not, return now. If so, eat the (.
866 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000867 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000868 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000869 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000870 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000871 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000872
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000873 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000874 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000875 }
876 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000877
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000878 // If we reached here, then we just ate the ( of the memory operand. Process
879 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000880 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000881 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000882
Chris Lattner29ef9a22010-01-15 18:51:29 +0000883 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000884 SMLoc StartLoc, EndLoc;
885 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000886 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000887 Error(StartLoc, "eiz and riz can only be used as index registers",
888 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000889 return 0;
890 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000891 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000892
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000893 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000894 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000895 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000896
897 // Following the comma we should have either an index register, or a scale
898 // value. We don't support the later form, but we want to parse it
899 // correctly.
900 //
901 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000902 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000903 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000904 SMLoc L;
905 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000906
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000907 if (getLexer().isNot(AsmToken::RParen)) {
908 // Parse the scale amount:
909 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000910 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000911 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000912 "expected comma in scale expression");
913 return 0;
914 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000915 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000916
917 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000918 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000919
920 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000921 if (getParser().ParseAbsoluteExpression(ScaleVal)){
922 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000923 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +0000924 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000925
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000926 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000927 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
928 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
929 return 0;
930 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000931 Scale = (unsigned)ScaleVal;
932 }
933 }
934 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000935 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000936 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000937 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000938
939 int64_t Value;
940 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000941 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000942
Daniel Dunbaree910252010-08-24 19:13:38 +0000943 if (Value != 1)
944 Warning(Loc, "scale factor without index register is ignored");
945 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000946 }
947 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000948
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000949 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000950 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000951 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000952 return 0;
953 }
Sean Callanan18b83232010-01-19 21:44:56 +0000954 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000955 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000956
Kevin Enderby84faf652012-03-12 21:32:09 +0000957 // If we have both a base register and an index register make sure they are
958 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +0000959 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +0000960 if (BaseReg != 0 && IndexReg != 0) {
961 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000962 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
963 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000964 IndexReg != X86::RIZ) {
965 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
966 return 0;
967 }
968 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000969 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
970 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000971 IndexReg != X86::EIZ){
972 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
973 return 0;
974 }
975 }
976
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000977 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
978 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000979}
980
Devang Pateldd929fc2012-01-12 18:03:40 +0000981bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000982ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000983 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000984 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000985
Chris Lattnerd8f71792010-11-28 20:23:50 +0000986 // FIXME: Hack to recognize setneb as setne.
987 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
988 PatchedName != "setb" && PatchedName != "setnb")
989 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000990
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000991 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
992 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000993 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000994 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
995 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +0000996 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000997 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000998 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000999 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001000 .Case("eq", 0x00)
1001 .Case("lt", 0x01)
1002 .Case("le", 0x02)
1003 .Case("unord", 0x03)
1004 .Case("neq", 0x04)
1005 .Case("nlt", 0x05)
1006 .Case("nle", 0x06)
1007 .Case("ord", 0x07)
1008 /* AVX only from here */
1009 .Case("eq_uq", 0x08)
1010 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001011 .Case("ngt", 0x0A)
1012 .Case("false", 0x0B)
1013 .Case("neq_oq", 0x0C)
1014 .Case("ge", 0x0D)
1015 .Case("gt", 0x0E)
1016 .Case("true", 0x0F)
1017 .Case("eq_os", 0x10)
1018 .Case("lt_oq", 0x11)
1019 .Case("le_oq", 0x12)
1020 .Case("unord_s", 0x13)
1021 .Case("neq_us", 0x14)
1022 .Case("nlt_uq", 0x15)
1023 .Case("nle_uq", 0x16)
1024 .Case("ord_s", 0x17)
1025 .Case("eq_us", 0x18)
1026 .Case("nge_uq", 0x19)
1027 .Case("ngt_uq", 0x1A)
1028 .Case("false_os", 0x1B)
1029 .Case("neq_os", 0x1C)
1030 .Case("ge_oq", 0x1D)
1031 .Case("gt_oq", 0x1E)
1032 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001033 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001034 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001035 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1036 getParser().getContext());
1037 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001038 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001039 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001040 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001041 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001042 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001043 } else {
1044 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001045 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001046 }
1047 }
1048 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001049
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001050 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001051
Devang Patel885f65b2012-01-30 22:47:12 +00001052 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001053 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001054
Chris Lattner2544f422010-09-08 05:17:37 +00001055 // Determine whether this is an instruction prefix.
1056 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001057 Name == "lock" || Name == "rep" ||
1058 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001059 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001060 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001061
1062
Chris Lattner2544f422010-09-08 05:17:37 +00001063 // This does the actual operand parsing. Don't parse any more if we have a
1064 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1065 // just want to parse the "lock" as the first instruction and the "incl" as
1066 // the next one.
1067 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001068
1069 // Parse '*' modifier.
1070 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001071 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001072 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001073 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001074 }
1075
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001076 // Read the first 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 Dunbar39e2dd72010-05-25 19:49:32 +00001083
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001084 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001085 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001086
1087 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001088 if (X86Operand *Op = ParseOperand())
1089 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001090 else {
1091 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001092 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001093 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001094 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001095
Chris Lattnercbf8a982010-09-11 16:18:25 +00001096 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001097 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001098 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001099 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001100 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001101 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001102
Chris Lattner2544f422010-09-08 05:17:37 +00001103 if (getLexer().is(AsmToken::EndOfStatement))
1104 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001105 else if (isPrefix && getLexer().is(AsmToken::Slash))
1106 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001107
Devang Patel885f65b2012-01-30 22:47:12 +00001108 if (ExtraImmOp && isParsingIntelSyntax())
1109 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1110
Chris Lattner98c870f2010-11-06 19:25:43 +00001111 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1112 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1113 // documented form in various unofficial manuals, so a lot of code uses it.
1114 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1115 Operands.size() == 3) {
1116 X86Operand &Op = *(X86Operand*)Operands.back();
1117 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1118 isa<MCConstantExpr>(Op.Mem.Disp) &&
1119 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1120 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1121 SMLoc Loc = Op.getEndLoc();
1122 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1123 delete &Op;
1124 }
1125 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001126 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1127 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1128 Operands.size() == 3) {
1129 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1130 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1131 isa<MCConstantExpr>(Op.Mem.Disp) &&
1132 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1133 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1134 SMLoc Loc = Op.getEndLoc();
1135 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1136 delete &Op;
1137 }
1138 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001139 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1140 if (Name.startswith("ins") && Operands.size() == 3 &&
1141 (Name == "insb" || Name == "insw" || Name == "insl")) {
1142 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1143 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1144 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1145 Operands.pop_back();
1146 Operands.pop_back();
1147 delete &Op;
1148 delete &Op2;
1149 }
1150 }
1151
1152 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1153 if (Name.startswith("outs") && Operands.size() == 3 &&
1154 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1155 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1156 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1157 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1158 Operands.pop_back();
1159 Operands.pop_back();
1160 delete &Op;
1161 delete &Op2;
1162 }
1163 }
1164
1165 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1166 if (Name.startswith("movs") && Operands.size() == 3 &&
1167 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001168 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001169 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1170 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1171 if (isSrcOp(Op) && isDstOp(Op2)) {
1172 Operands.pop_back();
1173 Operands.pop_back();
1174 delete &Op;
1175 delete &Op2;
1176 }
1177 }
1178 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1179 if (Name.startswith("lods") && Operands.size() == 3 &&
1180 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001181 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001182 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1183 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1184 if (isSrcOp(*Op1) && Op2->isReg()) {
1185 const char *ins;
1186 unsigned reg = Op2->getReg();
1187 bool isLods = Name == "lods";
1188 if (reg == X86::AL && (isLods || Name == "lodsb"))
1189 ins = "lodsb";
1190 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1191 ins = "lodsw";
1192 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1193 ins = "lodsl";
1194 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1195 ins = "lodsq";
1196 else
1197 ins = NULL;
1198 if (ins != NULL) {
1199 Operands.pop_back();
1200 Operands.pop_back();
1201 delete Op1;
1202 delete Op2;
1203 if (Name != ins)
1204 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1205 }
1206 }
1207 }
1208 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1209 if (Name.startswith("stos") && Operands.size() == 3 &&
1210 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001211 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001212 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1213 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1214 if (isDstOp(*Op2) && Op1->isReg()) {
1215 const char *ins;
1216 unsigned reg = Op1->getReg();
1217 bool isStos = Name == "stos";
1218 if (reg == X86::AL && (isStos || Name == "stosb"))
1219 ins = "stosb";
1220 else if (reg == X86::AX && (isStos || Name == "stosw"))
1221 ins = "stosw";
1222 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1223 ins = "stosl";
1224 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1225 ins = "stosq";
1226 else
1227 ins = NULL;
1228 if (ins != NULL) {
1229 Operands.pop_back();
1230 Operands.pop_back();
1231 delete Op1;
1232 delete Op2;
1233 if (Name != ins)
1234 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1235 }
1236 }
1237 }
1238
Chris Lattnere9e16a32010-09-15 04:33:27 +00001239 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001240 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001241 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001242 Name.startswith("shl") || Name.startswith("sal") ||
1243 Name.startswith("rcl") || Name.startswith("rcr") ||
1244 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001245 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001246 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001247 // Intel syntax
1248 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1249 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001250 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1251 delete Operands[2];
1252 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001253 }
1254 } else {
1255 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1256 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001257 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1258 delete Operands[1];
1259 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001260 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001261 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001262 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001263
Chris Lattner15f89512011-04-09 19:41:05 +00001264 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1265 // instalias with an immediate operand yet.
1266 if (Name == "int" && Operands.size() == 2) {
1267 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1268 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1269 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1270 delete Operands[1];
1271 Operands.erase(Operands.begin() + 1);
1272 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1273 }
1274 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001275
Chris Lattner98986712010-01-14 22:21:20 +00001276 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001277}
1278
Devang Pateldd929fc2012-01-12 18:03:40 +00001279bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001280processInstruction(MCInst &Inst,
1281 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1282 switch (Inst.getOpcode()) {
1283 default: return false;
1284 case X86::AND16i16: {
1285 if (!Inst.getOperand(0).isImm() ||
1286 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1287 return false;
1288
1289 MCInst TmpInst;
1290 TmpInst.setOpcode(X86::AND16ri8);
1291 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1292 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1293 TmpInst.addOperand(Inst.getOperand(0));
1294 Inst = TmpInst;
1295 return true;
1296 }
1297 case X86::AND32i32: {
1298 if (!Inst.getOperand(0).isImm() ||
1299 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1300 return false;
1301
1302 MCInst TmpInst;
1303 TmpInst.setOpcode(X86::AND32ri8);
1304 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1305 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1306 TmpInst.addOperand(Inst.getOperand(0));
1307 Inst = TmpInst;
1308 return true;
1309 }
1310 case X86::AND64i32: {
1311 if (!Inst.getOperand(0).isImm() ||
1312 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1313 return false;
1314
1315 MCInst TmpInst;
1316 TmpInst.setOpcode(X86::AND64ri8);
1317 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1318 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1319 TmpInst.addOperand(Inst.getOperand(0));
1320 Inst = TmpInst;
1321 return true;
1322 }
Devang Patelac0f0482012-01-19 17:53:25 +00001323 case X86::XOR16i16: {
1324 if (!Inst.getOperand(0).isImm() ||
1325 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1326 return false;
1327
1328 MCInst TmpInst;
1329 TmpInst.setOpcode(X86::XOR16ri8);
1330 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1331 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1332 TmpInst.addOperand(Inst.getOperand(0));
1333 Inst = TmpInst;
1334 return true;
1335 }
1336 case X86::XOR32i32: {
1337 if (!Inst.getOperand(0).isImm() ||
1338 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1339 return false;
1340
1341 MCInst TmpInst;
1342 TmpInst.setOpcode(X86::XOR32ri8);
1343 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1344 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1345 TmpInst.addOperand(Inst.getOperand(0));
1346 Inst = TmpInst;
1347 return true;
1348 }
1349 case X86::XOR64i32: {
1350 if (!Inst.getOperand(0).isImm() ||
1351 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1352 return false;
1353
1354 MCInst TmpInst;
1355 TmpInst.setOpcode(X86::XOR64ri8);
1356 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1357 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1358 TmpInst.addOperand(Inst.getOperand(0));
1359 Inst = TmpInst;
1360 return true;
1361 }
1362 case X86::OR16i16: {
1363 if (!Inst.getOperand(0).isImm() ||
1364 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1365 return false;
1366
1367 MCInst TmpInst;
1368 TmpInst.setOpcode(X86::OR16ri8);
1369 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1370 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1371 TmpInst.addOperand(Inst.getOperand(0));
1372 Inst = TmpInst;
1373 return true;
1374 }
1375 case X86::OR32i32: {
1376 if (!Inst.getOperand(0).isImm() ||
1377 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1378 return false;
1379
1380 MCInst TmpInst;
1381 TmpInst.setOpcode(X86::OR32ri8);
1382 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1383 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1384 TmpInst.addOperand(Inst.getOperand(0));
1385 Inst = TmpInst;
1386 return true;
1387 }
1388 case X86::OR64i32: {
1389 if (!Inst.getOperand(0).isImm() ||
1390 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1391 return false;
1392
1393 MCInst TmpInst;
1394 TmpInst.setOpcode(X86::OR64ri8);
1395 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1396 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1397 TmpInst.addOperand(Inst.getOperand(0));
1398 Inst = TmpInst;
1399 return true;
1400 }
1401 case X86::CMP16i16: {
1402 if (!Inst.getOperand(0).isImm() ||
1403 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1404 return false;
1405
1406 MCInst TmpInst;
1407 TmpInst.setOpcode(X86::CMP16ri8);
1408 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1409 TmpInst.addOperand(Inst.getOperand(0));
1410 Inst = TmpInst;
1411 return true;
1412 }
1413 case X86::CMP32i32: {
1414 if (!Inst.getOperand(0).isImm() ||
1415 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1416 return false;
1417
1418 MCInst TmpInst;
1419 TmpInst.setOpcode(X86::CMP32ri8);
1420 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1421 TmpInst.addOperand(Inst.getOperand(0));
1422 Inst = TmpInst;
1423 return true;
1424 }
1425 case X86::CMP64i32: {
1426 if (!Inst.getOperand(0).isImm() ||
1427 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1428 return false;
1429
1430 MCInst TmpInst;
1431 TmpInst.setOpcode(X86::CMP64ri8);
1432 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1433 TmpInst.addOperand(Inst.getOperand(0));
1434 Inst = TmpInst;
1435 return true;
1436 }
Devang Patela951f772012-01-19 18:40:55 +00001437 case X86::ADD16i16: {
1438 if (!Inst.getOperand(0).isImm() ||
1439 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1440 return false;
1441
1442 MCInst TmpInst;
1443 TmpInst.setOpcode(X86::ADD16ri8);
1444 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1445 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1446 TmpInst.addOperand(Inst.getOperand(0));
1447 Inst = TmpInst;
1448 return true;
1449 }
1450 case X86::ADD32i32: {
1451 if (!Inst.getOperand(0).isImm() ||
1452 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1453 return false;
1454
1455 MCInst TmpInst;
1456 TmpInst.setOpcode(X86::ADD32ri8);
1457 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1458 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1459 TmpInst.addOperand(Inst.getOperand(0));
1460 Inst = TmpInst;
1461 return true;
1462 }
1463 case X86::ADD64i32: {
1464 if (!Inst.getOperand(0).isImm() ||
1465 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1466 return false;
1467
1468 MCInst TmpInst;
1469 TmpInst.setOpcode(X86::ADD64ri8);
1470 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1471 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1472 TmpInst.addOperand(Inst.getOperand(0));
1473 Inst = TmpInst;
1474 return true;
1475 }
1476 case X86::SUB16i16: {
1477 if (!Inst.getOperand(0).isImm() ||
1478 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1479 return false;
1480
1481 MCInst TmpInst;
1482 TmpInst.setOpcode(X86::SUB16ri8);
1483 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1484 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1485 TmpInst.addOperand(Inst.getOperand(0));
1486 Inst = TmpInst;
1487 return true;
1488 }
1489 case X86::SUB32i32: {
1490 if (!Inst.getOperand(0).isImm() ||
1491 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1492 return false;
1493
1494 MCInst TmpInst;
1495 TmpInst.setOpcode(X86::SUB32ri8);
1496 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1497 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1498 TmpInst.addOperand(Inst.getOperand(0));
1499 Inst = TmpInst;
1500 return true;
1501 }
1502 case X86::SUB64i32: {
1503 if (!Inst.getOperand(0).isImm() ||
1504 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1505 return false;
1506
1507 MCInst TmpInst;
1508 TmpInst.setOpcode(X86::SUB64ri8);
1509 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1510 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1511 TmpInst.addOperand(Inst.getOperand(0));
1512 Inst = TmpInst;
1513 return true;
1514 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001515 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001516}
1517
1518bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001519MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001520 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001521 MCStreamer &Out, unsigned &ErrorInfo,
1522 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001523 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001524 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1525 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001526 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001527
Chris Lattner7c51a312010-09-29 01:50:45 +00001528 // First, handle aliases that expand to multiple instructions.
1529 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001530 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001531 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001532 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001533 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001534 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001535 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001536 MCInst Inst;
1537 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001538 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001539 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001540 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001541
Chris Lattner0bb83a82010-09-30 16:39:29 +00001542 const char *Repl =
1543 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001544 .Case("finit", "fninit")
1545 .Case("fsave", "fnsave")
1546 .Case("fstcw", "fnstcw")
1547 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001548 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001549 .Case("fstsw", "fnstsw")
1550 .Case("fstsww", "fnstsw")
1551 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001552 .Default(0);
1553 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001554 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001555 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001556 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001557
Chris Lattnera008e8a2010-09-06 21:54:15 +00001558 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001559 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001560
Daniel Dunbarc918d602010-05-04 16:12:42 +00001561 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001562 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001563 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001564 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001565 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001566 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001567 // Some instructions need post-processing to, for example, tweak which
1568 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001569 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001570 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001571 while (processInstruction(Inst, Operands))
1572 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001573
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001574 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001575 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001576 Out.EmitInstruction(Inst);
1577 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001578 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001579 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001580 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001581 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001582 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001583 case Match_InvalidOperand:
1584 WasOriginallyInvalidOperand = true;
1585 break;
1586 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001587 break;
1588 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001589
Daniel Dunbarc918d602010-05-04 16:12:42 +00001590 // FIXME: Ideally, we would only attempt suffix matches for things which are
1591 // valid prefixes, and we could just infer the right unambiguous
1592 // type. However, that requires substantially more matcher support than the
1593 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001594
Daniel Dunbarc918d602010-05-04 16:12:42 +00001595 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001596 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001597 SmallString<16> Tmp;
1598 Tmp += Base;
1599 Tmp += ' ';
1600 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001601
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001602 // If this instruction starts with an 'f', then it is a floating point stack
1603 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1604 // 80-bit floating point, which use the suffixes s,l,t respectively.
1605 //
1606 // Otherwise, we assume that this may be an integer instruction, which comes
1607 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1608 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001609
Daniel Dunbarc918d602010-05-04 16:12:42 +00001610 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001611 Tmp[Base.size()] = Suffixes[0];
1612 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001613 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001614
Chad Rosier6e006d32012-10-12 22:53:36 +00001615 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1616 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001617 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001618 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1619 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001620 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001621 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1622 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001623 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001624 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1625 isParsingIntelSyntax());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001626
1627 // Restore the old token.
1628 Op->setTokenValue(Base);
1629
1630 // If exactly one matched, then we treat that as a successful match (and the
1631 // instruction will already have been filled in correctly, since the failing
1632 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001633 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001634 (Match1 == Match_Success) + (Match2 == Match_Success) +
1635 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001636 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001637 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001638 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001639 Out.EmitInstruction(Inst);
1640 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001641 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001642 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001643
Chris Lattnerec6789f2010-09-06 20:08:02 +00001644 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001645
Daniel Dunbar09062b12010-08-12 00:55:42 +00001646 // If we had multiple suffix matches, then identify this as an ambiguous
1647 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001648 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001649 char MatchChars[4];
1650 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001651 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1652 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1653 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1654 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001655
1656 SmallString<126> Msg;
1657 raw_svector_ostream OS(Msg);
1658 OS << "ambiguous instructions require an explicit suffix (could be ";
1659 for (unsigned i = 0; i != NumMatches; ++i) {
1660 if (i != 0)
1661 OS << ", ";
1662 if (i + 1 == NumMatches)
1663 OS << "or ";
1664 OS << "'" << Base << MatchChars[i] << "'";
1665 }
1666 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001667 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001668 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001669 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001670
Chris Lattnera008e8a2010-09-06 21:54:15 +00001671 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001672
Chris Lattnera008e8a2010-09-06 21:54:15 +00001673 // If all of the instructions reported an invalid mnemonic, then the original
1674 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001675 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1676 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001677 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001678 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001679 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001680 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001681 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001682 }
1683
1684 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001685 if (ErrorInfo != ~0U) {
1686 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001687 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001688 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001689
Chad Rosier84125ca2012-10-13 00:26:04 +00001690 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001691 if (Operand->getStartLoc().isValid()) {
1692 SMRange OperandRange = Operand->getLocRange();
1693 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001694 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001695 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001696 }
1697
Chad Rosierb4fdade2012-08-21 19:36:59 +00001698 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001699 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001700 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001701
Chris Lattnerec6789f2010-09-06 20:08:02 +00001702 // If one instruction matched with a missing feature, report this as a
1703 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001704 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1705 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001706 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001707 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001708 return true;
1709 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001710
Chris Lattnera008e8a2010-09-06 21:54:15 +00001711 // If one instruction matched with an invalid operand, report this as an
1712 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001713 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1714 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001715 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001716 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001717 return true;
1718 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001719
Chris Lattnerec6789f2010-09-06 20:08:02 +00001720 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001721 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001722 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001723 return true;
1724}
1725
1726
Devang Pateldd929fc2012-01-12 18:03:40 +00001727bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001728 StringRef IDVal = DirectiveID.getIdentifier();
1729 if (IDVal == ".word")
1730 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001731 else if (IDVal.startswith(".code"))
1732 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001733 else if (IDVal.startswith(".att_syntax")) {
1734 getParser().setAssemblerDialect(0);
1735 return false;
1736 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001737 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001738 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1739 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001740 // FIXME : Handle noprefix
1741 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001742 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001743 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001744 }
1745 return false;
1746 }
Chris Lattner537ca842010-10-30 17:38:55 +00001747 return true;
1748}
1749
1750/// ParseDirectiveWord
1751/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001752bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001753 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1754 for (;;) {
1755 const MCExpr *Value;
1756 if (getParser().ParseExpression(Value))
1757 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001758
Chris Lattner537ca842010-10-30 17:38:55 +00001759 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001760
Chris Lattner537ca842010-10-30 17:38:55 +00001761 if (getLexer().is(AsmToken::EndOfStatement))
1762 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001763
Chris Lattner537ca842010-10-30 17:38:55 +00001764 // FIXME: Improve diagnostic.
1765 if (getLexer().isNot(AsmToken::Comma))
1766 return Error(L, "unexpected token in directive");
1767 Parser.Lex();
1768 }
1769 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001770
Chris Lattner537ca842010-10-30 17:38:55 +00001771 Parser.Lex();
1772 return false;
1773}
1774
Evan Chengbd27f5a2011-07-27 00:38:12 +00001775/// ParseDirectiveCode
1776/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001777bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001778 if (IDVal == ".code32") {
1779 Parser.Lex();
1780 if (is64BitMode()) {
1781 SwitchMode();
1782 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1783 }
1784 } else if (IDVal == ".code64") {
1785 Parser.Lex();
1786 if (!is64BitMode()) {
1787 SwitchMode();
1788 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1789 }
1790 } else {
1791 return Error(L, "unexpected directive " + IDVal);
1792 }
Chris Lattner537ca842010-10-30 17:38:55 +00001793
Evan Chengbd27f5a2011-07-27 00:38:12 +00001794 return false;
1795}
Chris Lattner537ca842010-10-30 17:38:55 +00001796
1797
Sean Callanane88f5522010-01-23 02:43:15 +00001798extern "C" void LLVMInitializeX86AsmLexer();
1799
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001800// Force static initialization.
1801extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001802 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1803 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001804 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001805}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001806
Chris Lattner0692ee62010-09-06 19:11:01 +00001807#define GET_REGISTER_MATCHER
1808#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001809#include "X86GenAsmMatcher.inc"