blob: 9e88472d60b64cd19aa6f493dd3dde09b7fcd014 [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"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000020#include "llvm/ADT/OwningPtr.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000023#include "llvm/ADT/StringSwitch.h"
24#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000025#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000027#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000028
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000029using namespace llvm;
30
31namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000032struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000033
Devang Pateldd929fc2012-01-12 18:03:40 +000034class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000035 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036 MCAsmParser &Parser;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038 MCAsmParser &getParser() const { return Parser; }
39
40 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
41
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000042 bool Error(SMLoc L, const Twine &Msg,
43 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) {
44 return Parser.Error(L, Msg, Ranges);
45 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000046
Devang Pateld37ad242012-01-17 18:00:18 +000047 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
48 Error(Loc, Msg);
49 return 0;
50 }
51
Chris Lattner309264d2010-01-15 18:44:13 +000052 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000053 X86Operand *ParseATTOperand();
54 X86Operand *ParseIntelOperand();
Devang Pateld37ad242012-01-17 18:00:18 +000055 X86Operand *ParseIntelMemOperand();
Devang Patel7c64fe62012-01-23 18:31:58 +000056 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000057 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000058
59 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000060 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000061
Devang Patelb8ba13f2012-01-18 22:42:29 +000062 bool processInstruction(MCInst &Inst,
63 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
64
Chris Lattner7036f8b2010-09-29 01:42:58 +000065 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000066 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000067 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000068
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000069 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000070 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000071 bool isSrcOp(X86Operand &Op);
72
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000073 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
74 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000075 bool isDstOp(X86Operand &Op);
76
Evan Cheng59ee62d2011-07-11 03:57:24 +000077 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000078 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000079 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000080 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000081 void SwitchMode() {
82 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
83 setAvailableFeatures(FB);
84 }
Evan Chengebdeeab2011-07-08 01:53:10 +000085
Daniel Dunbar54074b52010-07-19 05:44:09 +000086 /// @name Auto-generated Matcher Functions
87 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000088
Chris Lattner0692ee62010-09-06 19:11:01 +000089#define GET_ASSEMBLER_HEADER
90#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000091
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000092 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000093
94public:
Devang Pateldd929fc2012-01-12 18:03:40 +000095 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +000096 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000097
Daniel Dunbar54074b52010-07-19 05:44:09 +000098 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +000099 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000100 }
Roman Divackybf755322011-01-27 17:14:22 +0000101 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000102
Benjamin Kramer38e59892010-07-14 22:38:02 +0000103 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000104 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000105
106 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000107
108 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000109 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000110 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000111};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000112} // end anonymous namespace
113
Sean Callanane9b466d2010-01-23 00:40:33 +0000114/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000115/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000116
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000117static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000118
119/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000120
Devang Patelb8ba13f2012-01-18 22:42:29 +0000121static bool isImmSExti16i8Value(uint64_t Value) {
122 return (( Value <= 0x000000000000007FULL)||
123 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
124 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
125}
126
127static bool isImmSExti32i8Value(uint64_t Value) {
128 return (( Value <= 0x000000000000007FULL)||
129 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
130 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
131}
132
133static bool isImmZExtu32u8Value(uint64_t Value) {
134 return (Value <= 0x00000000000000FFULL);
135}
136
137static bool isImmSExti64i8Value(uint64_t Value) {
138 return (( Value <= 0x000000000000007FULL)||
139 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
140}
141
142static bool isImmSExti64i32Value(uint64_t Value) {
143 return (( Value <= 0x000000007FFFFFFFULL)||
144 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
145}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000146namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000147
148/// X86Operand - Instances of this class represent a parsed X86 machine
149/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000150struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000151 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000152 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000153 Register,
154 Immediate,
155 Memory
156 } Kind;
157
Chris Lattner29ef9a22010-01-15 18:51:29 +0000158 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000159
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000160 union {
161 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000162 const char *Data;
163 unsigned Length;
164 } Tok;
165
166 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000167 unsigned RegNo;
168 } Reg;
169
170 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000171 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000172 } Imm;
173
174 struct {
175 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000176 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000177 unsigned BaseReg;
178 unsigned IndexReg;
179 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000180 unsigned Size;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000181 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000182 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000183
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000184 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000185 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000186
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000187 /// getStartLoc - Get the location of the first token of this operand.
188 SMLoc getStartLoc() const { return StartLoc; }
189 /// getEndLoc - Get the location of the last token of this operand.
190 SMLoc getEndLoc() const { return EndLoc; }
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000191
192 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000193
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000194 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000195
Daniel Dunbar20927f22009-08-07 08:26:05 +0000196 StringRef getToken() const {
197 assert(Kind == Token && "Invalid access!");
198 return StringRef(Tok.Data, Tok.Length);
199 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000200 void setTokenValue(StringRef Value) {
201 assert(Kind == Token && "Invalid access!");
202 Tok.Data = Value.data();
203 Tok.Length = Value.size();
204 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000205
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000206 unsigned getReg() const {
207 assert(Kind == Register && "Invalid access!");
208 return Reg.RegNo;
209 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000210
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000211 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000212 assert(Kind == Immediate && "Invalid access!");
213 return Imm.Val;
214 }
215
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000216 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000217 assert(Kind == Memory && "Invalid access!");
218 return Mem.Disp;
219 }
220 unsigned getMemSegReg() const {
221 assert(Kind == Memory && "Invalid access!");
222 return Mem.SegReg;
223 }
224 unsigned getMemBaseReg() const {
225 assert(Kind == Memory && "Invalid access!");
226 return Mem.BaseReg;
227 }
228 unsigned getMemIndexReg() const {
229 assert(Kind == Memory && "Invalid access!");
230 return Mem.IndexReg;
231 }
232 unsigned getMemScale() const {
233 assert(Kind == Memory && "Invalid access!");
234 return Mem.Scale;
235 }
236
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000237 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000238
239 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000240
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000241 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000242 if (!isImm())
243 return false;
244
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000245 // If this isn't a constant expr, just assume it fits and let relaxation
246 // handle it.
247 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
248 if (!CE)
249 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000250
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000251 // Otherwise, check the value is in a range that makes sense for this
252 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000253 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000254 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000255 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000256 if (!isImm())
257 return false;
258
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000259 // If this isn't a constant expr, just assume it fits and let relaxation
260 // handle it.
261 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
262 if (!CE)
263 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000264
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000265 // Otherwise, check the value is in a range that makes sense for this
266 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000267 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000268 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000269 bool isImmZExtu32u8() const {
270 if (!isImm())
271 return false;
272
273 // If this isn't a constant expr, just assume it fits and let relaxation
274 // handle it.
275 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
276 if (!CE)
277 return true;
278
279 // Otherwise, check the value is in a range that makes sense for this
280 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000281 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000282 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000283 bool isImmSExti64i8() const {
284 if (!isImm())
285 return false;
286
287 // If this isn't a constant expr, just assume it fits and let relaxation
288 // handle it.
289 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
290 if (!CE)
291 return true;
292
293 // Otherwise, check the value is in a range that makes sense for this
294 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000295 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000296 }
297 bool isImmSExti64i32() const {
298 if (!isImm())
299 return false;
300
301 // If this isn't a constant expr, just assume it fits and let relaxation
302 // handle it.
303 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
304 if (!CE)
305 return true;
306
307 // Otherwise, check the value is in a range that makes sense for this
308 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000309 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000310 }
311
Daniel Dunbar20927f22009-08-07 08:26:05 +0000312 bool isMem() const { return Kind == Memory; }
Devang Patelc59d9df2012-01-12 01:51:42 +0000313 bool isMem8() const {
314 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
315 }
316 bool isMem16() const {
317 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
318 }
319 bool isMem32() const {
320 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
321 }
322 bool isMem64() const {
323 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
324 }
325 bool isMem80() const {
326 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
327 }
328 bool isMem128() const {
329 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
330 }
331 bool isMem256() const {
332 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
333 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000334
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000335 bool isAbsMem() const {
336 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000337 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000338 }
339
Daniel Dunbar20927f22009-08-07 08:26:05 +0000340 bool isReg() const { return Kind == Register; }
341
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000342 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
343 // Add as immediates when possible.
344 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
345 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
346 else
347 Inst.addOperand(MCOperand::CreateExpr(Expr));
348 }
349
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000350 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000351 assert(N == 1 && "Invalid number of operands!");
352 Inst.addOperand(MCOperand::CreateReg(getReg()));
353 }
354
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000355 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000356 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000357 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000358 }
359
Devang Patelc59d9df2012-01-12 01:51:42 +0000360 void addMem8Operands(MCInst &Inst, unsigned N) const {
361 addMemOperands(Inst, N);
362 }
363 void addMem16Operands(MCInst &Inst, unsigned N) const {
364 addMemOperands(Inst, N);
365 }
366 void addMem32Operands(MCInst &Inst, unsigned N) const {
367 addMemOperands(Inst, N);
368 }
369 void addMem64Operands(MCInst &Inst, unsigned N) const {
370 addMemOperands(Inst, N);
371 }
372 void addMem80Operands(MCInst &Inst, unsigned N) const {
373 addMemOperands(Inst, N);
374 }
375 void addMem128Operands(MCInst &Inst, unsigned N) const {
376 addMemOperands(Inst, N);
377 }
378 void addMem256Operands(MCInst &Inst, unsigned N) const {
379 addMemOperands(Inst, N);
380 }
381
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000382 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000383 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000384 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
385 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
386 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000387 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000388 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
389 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000390
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000391 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
392 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000393 // Add as immediates when possible.
394 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
395 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
396 else
397 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000398 }
399
Chris Lattnerb4307b32010-01-15 19:28:38 +0000400 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000401 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
402 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000403 Res->Tok.Data = Str.data();
404 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000405 return Res;
406 }
407
Chris Lattner29ef9a22010-01-15 18:51:29 +0000408 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000409 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000410 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000411 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000412 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000413
Chris Lattnerb4307b32010-01-15 19:28:38 +0000414 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
415 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000416 Res->Imm.Val = Val;
417 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000418 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000419
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000420 /// Create an absolute memory operand.
421 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000422 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000423 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
424 Res->Mem.SegReg = 0;
425 Res->Mem.Disp = Disp;
426 Res->Mem.BaseReg = 0;
427 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000428 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000429 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000430 return Res;
431 }
432
433 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000434 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
435 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000436 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
437 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000438 // We should never just have a displacement, that should be parsed as an
439 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000440 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
441
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000442 // The scale should always be one of {1,2,4,8}.
443 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000444 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000445 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000446 Res->Mem.SegReg = SegReg;
447 Res->Mem.Disp = Disp;
448 Res->Mem.BaseReg = BaseReg;
449 Res->Mem.IndexReg = IndexReg;
450 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000451 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000452 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000453 }
454};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000455
Chris Lattner37dfdec2009-07-29 06:33:53 +0000456} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000457
Devang Pateldd929fc2012-01-12 18:03:40 +0000458bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000459 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000460
461 return (Op.isMem() &&
462 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
463 isa<MCConstantExpr>(Op.Mem.Disp) &&
464 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
465 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
466}
467
Devang Pateldd929fc2012-01-12 18:03:40 +0000468bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000469 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000470
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000471 return Op.isMem() &&
472 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000473 isa<MCConstantExpr>(Op.Mem.Disp) &&
474 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
475 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
476}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000477
Devang Pateldd929fc2012-01-12 18:03:40 +0000478bool X86AsmParser::ParseRegister(unsigned &RegNo,
479 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000480 RegNo = 0;
Devang Patelbe3e3102012-01-30 20:02:42 +0000481 if (!isParsingIntelSyntax()) {
Devang Patel1aea4302012-01-20 22:32:05 +0000482 const AsmToken &TokPercent = Parser.getTok();
Devang Pateld37ad242012-01-17 18:00:18 +0000483 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
484 StartLoc = TokPercent.getLoc();
485 Parser.Lex(); // Eat percent token.
486 }
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000487
Sean Callanan18b83232010-01-19 21:44:56 +0000488 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000489 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000490 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000491 return Error(StartLoc, "invalid register name",
492 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000493 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000494
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000495 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000496
Chris Lattner33d60d52010-09-22 04:11:10 +0000497 // If the match failed, try the register name as lowercase.
498 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000499 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000500
Evan Cheng5de728c2011-07-27 23:22:03 +0000501 if (!is64BitMode()) {
502 // FIXME: This should be done using Requires<In32BitMode> and
503 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
504 // checked.
505 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
506 // REX prefix.
507 if (RegNo == X86::RIZ ||
508 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
509 X86II::isX86_64NonExtLowByteReg(RegNo) ||
510 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000511 return Error(StartLoc, "register %"
512 + Tok.getString() + " is only available in 64-bit mode",
513 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000514 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000515
Chris Lattner33d60d52010-09-22 04:11:10 +0000516 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
517 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000518 RegNo = X86::ST0;
519 EndLoc = Tok.getLoc();
520 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000521
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000522 // Check to see if we have '(4)' after %st.
523 if (getLexer().isNot(AsmToken::LParen))
524 return false;
525 // Lex the paren.
526 getParser().Lex();
527
528 const AsmToken &IntTok = Parser.getTok();
529 if (IntTok.isNot(AsmToken::Integer))
530 return Error(IntTok.getLoc(), "expected stack index");
531 switch (IntTok.getIntVal()) {
532 case 0: RegNo = X86::ST0; break;
533 case 1: RegNo = X86::ST1; break;
534 case 2: RegNo = X86::ST2; break;
535 case 3: RegNo = X86::ST3; break;
536 case 4: RegNo = X86::ST4; break;
537 case 5: RegNo = X86::ST5; break;
538 case 6: RegNo = X86::ST6; break;
539 case 7: RegNo = X86::ST7; break;
540 default: return Error(IntTok.getLoc(), "invalid stack index");
541 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000542
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000543 if (getParser().Lex().isNot(AsmToken::RParen))
544 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000545
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000546 EndLoc = Tok.getLoc();
547 Parser.Lex(); // Eat ')'
548 return false;
549 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000550
Chris Lattner645b2092010-06-24 07:29:18 +0000551 // If this is "db[0-7]", match it as an alias
552 // for dr[0-7].
553 if (RegNo == 0 && Tok.getString().size() == 3 &&
554 Tok.getString().startswith("db")) {
555 switch (Tok.getString()[2]) {
556 case '0': RegNo = X86::DR0; break;
557 case '1': RegNo = X86::DR1; break;
558 case '2': RegNo = X86::DR2; break;
559 case '3': RegNo = X86::DR3; break;
560 case '4': RegNo = X86::DR4; break;
561 case '5': RegNo = X86::DR5; break;
562 case '6': RegNo = X86::DR6; break;
563 case '7': RegNo = X86::DR7; break;
564 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000565
Chris Lattner645b2092010-06-24 07:29:18 +0000566 if (RegNo != 0) {
567 EndLoc = Tok.getLoc();
568 Parser.Lex(); // Eat it.
569 return false;
570 }
571 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000572
Devang Patel1aea4302012-01-20 22:32:05 +0000573 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000574 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000575 return Error(StartLoc, "invalid register name",
576 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000577 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000578
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000579 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000580 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000581 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000582}
583
Devang Pateldd929fc2012-01-12 18:03:40 +0000584X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000585 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000586 return ParseIntelOperand();
587 return ParseATTOperand();
588}
589
Devang Pateld37ad242012-01-17 18:00:18 +0000590/// getIntelMemOperandSize - Return intel memory operand size.
591static unsigned getIntelMemOperandSize(StringRef OpStr) {
592 unsigned Size = 0;
Devang Patel0a338862012-01-12 01:36:43 +0000593 if (OpStr == "BYTE") Size = 8;
594 if (OpStr == "WORD") Size = 16;
595 if (OpStr == "DWORD") Size = 32;
596 if (OpStr == "QWORD") Size = 64;
597 if (OpStr == "XWORD") Size = 80;
598 if (OpStr == "XMMWORD") Size = 128;
599 if (OpStr == "YMMWORD") Size = 256;
Devang Pateld37ad242012-01-17 18:00:18 +0000600 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000601}
602
Devang Patel7c64fe62012-01-23 18:31:58 +0000603X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
604 unsigned Size) {
605 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000606 SMLoc Start = Parser.getTok().getLoc(), End;
607
Devang Pateld37ad242012-01-17 18:00:18 +0000608 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
609 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
610
611 // Eat '['
612 if (getLexer().isNot(AsmToken::LBrac))
613 return ErrorOperand(Start, "Expected '[' token!");
614 Parser.Lex();
615
616 if (getLexer().is(AsmToken::Identifier)) {
617 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000618 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000619 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000620 if (getParser().ParseExpression(Disp, End)) return 0;
621 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000622 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000623 Parser.Lex();
624 return X86Operand::CreateMem(Disp, Start, End, Size);
625 }
626 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000627 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000628 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000629 SMLoc Loc = Parser.getTok().getLoc();
630 if (getLexer().is(AsmToken::RBrac)) {
631 // Handle '[' number ']'
632 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000633 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
634 if (SegReg)
635 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
636 Start, End, Size);
637 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000638 } else if (getLexer().is(AsmToken::Star)) {
639 // Handle '[' Scale*IndexReg ']'
640 Parser.Lex();
641 SMLoc IdxRegLoc = Parser.getTok().getLoc();
642 if (ParseRegister(IndexReg, IdxRegLoc, End))
643 return ErrorOperand(IdxRegLoc, "Expected register");
644 Scale = Val;
645 } else
646 return ErrorOperand(Loc, "Unepxeted token");
Devang Pateld37ad242012-01-17 18:00:18 +0000647 }
648
649 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
650 bool isPlus = getLexer().is(AsmToken::Plus);
651 Parser.Lex();
652 SMLoc PlusLoc = Parser.getTok().getLoc();
653 if (getLexer().is(AsmToken::Integer)) {
654 int64_t Val = Parser.getTok().getIntVal();
655 Parser.Lex();
656 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000657 Parser.Lex();
658 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Devang Patel1aea4302012-01-20 22:32:05 +0000659 if (ParseRegister(IndexReg, IdxRegLoc, End))
660 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000661 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000662 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000663 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000664 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000665 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000666 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000667 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000668 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000669 End = Parser.getTok().getLoc();
670 if (!IndexReg)
671 ParseRegister(IndexReg, Start, End);
672 else if (getParser().ParseExpression(Disp, End)) return 0;
673 }
Devang Pateld37ad242012-01-17 18:00:18 +0000674 }
675
676 if (getLexer().isNot(AsmToken::RBrac))
677 if (getParser().ParseExpression(Disp, End)) return 0;
678
679 End = Parser.getTok().getLoc();
680 if (getLexer().isNot(AsmToken::RBrac))
681 return ErrorOperand(End, "expected ']' token!");
682 Parser.Lex();
683 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000684
685 // handle [-42]
686 if (!BaseReg && !IndexReg)
687 return X86Operand::CreateMem(Disp, Start, End, Size);
688
Devang Pateld37ad242012-01-17 18:00:18 +0000689 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000690 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000691}
692
693/// ParseIntelMemOperand - Parse intel style memory operand.
694X86Operand *X86AsmParser::ParseIntelMemOperand() {
695 const AsmToken &Tok = Parser.getTok();
696 SMLoc Start = Parser.getTok().getLoc(), End;
Devang Patel7c64fe62012-01-23 18:31:58 +0000697 unsigned SegReg = 0;
Devang Pateld37ad242012-01-17 18:00:18 +0000698
699 unsigned Size = getIntelMemOperandSize(Tok.getString());
700 if (Size) {
701 Parser.Lex();
702 assert (Tok.getString() == "PTR" && "Unexpected token!");
703 Parser.Lex();
704 }
705
706 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000707 return ParseIntelBracExpression(SegReg, Size);
708
709 if (!ParseRegister(SegReg, Start, End)) {
710 // Handel SegReg : [ ... ]
711 if (getLexer().isNot(AsmToken::Colon))
712 return ErrorOperand(Start, "Expected ':' token!");
713 Parser.Lex(); // Eat :
714 if (getLexer().isNot(AsmToken::LBrac))
715 return ErrorOperand(Start, "Expected '[' token!");
716 return ParseIntelBracExpression(SegReg, Size);
717 }
Devang Pateld37ad242012-01-17 18:00:18 +0000718
719 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
720 if (getParser().ParseExpression(Disp, End)) return 0;
721 return X86Operand::CreateMem(Disp, Start, End, Size);
722}
723
724X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000725 SMLoc Start = Parser.getTok().getLoc(), End;
726
727 // immediate.
728 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
729 getLexer().is(AsmToken::Minus)) {
730 const MCExpr *Val;
731 if (!getParser().ParseExpression(Val, End)) {
732 End = Parser.getTok().getLoc();
733 return X86Operand::CreateImm(Val, Start, End);
734 }
735 }
736
Devang Patel0a338862012-01-12 01:36:43 +0000737 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000738 unsigned RegNo = 0;
739 if (!ParseRegister(RegNo, Start, End)) {
Devang Patel0a338862012-01-12 01:36:43 +0000740 End = Parser.getTok().getLoc();
741 return X86Operand::CreateReg(RegNo, Start, End);
742 }
743
744 // mem operand
Devang Pateld37ad242012-01-17 18:00:18 +0000745 return ParseIntelMemOperand();
Devang Patel0a338862012-01-12 01:36:43 +0000746}
747
Devang Pateldd929fc2012-01-12 18:03:40 +0000748X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000749 switch (getLexer().getKind()) {
750 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000751 // Parse a memory operand with no segment register.
752 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000753 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000754 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000755 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000756 SMLoc Start, End;
757 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000758 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000759 Error(Start, "%eiz and %riz can only be used as index registers",
760 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000761 return 0;
762 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000763
Chris Lattnereef6d782010-04-17 18:56:34 +0000764 // If this is a segment register followed by a ':', then this is the start
765 // of a memory reference, otherwise this is a normal register reference.
766 if (getLexer().isNot(AsmToken::Colon))
767 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000768
769
Chris Lattnereef6d782010-04-17 18:56:34 +0000770 getParser().Lex(); // Eat the colon.
771 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000772 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000773 case AsmToken::Dollar: {
774 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000775 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000776 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000777 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000778 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000779 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000780 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000781 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000782 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000783}
784
Chris Lattnereef6d782010-04-17 18:56:34 +0000785/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
786/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000787X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000788
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000789 // We have to disambiguate a parenthesized expression "(4+5)" from the start
790 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000791 // only way to do this without lookahead is to eat the '(' and see what is
792 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000793 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000794 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000795 SMLoc ExprEnd;
796 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000797
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000798 // After parsing the base expression we could either have a parenthesized
799 // memory address or not. If not, return now. If so, eat the (.
800 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000801 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000802 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000803 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000804 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000805 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000806
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000807 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000808 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000809 } else {
810 // Okay, we have a '('. We don't know if this is an expression or not, but
811 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000812 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000813 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000814
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000815 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000816 // Nothing to do here, fall into the code below with the '(' part of the
817 // memory operand consumed.
818 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000819 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000820
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000821 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000822 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000823 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000824
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000825 // After parsing the base expression we could either have a parenthesized
826 // memory address or not. If not, return now. If so, eat the (.
827 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000828 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000829 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000830 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000831 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000832 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000833
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000834 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000835 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000836 }
837 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000838
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000839 // If we reached here, then we just ate the ( of the memory operand. Process
840 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000841 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000842 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000843
Chris Lattner29ef9a22010-01-15 18:51:29 +0000844 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000845 SMLoc StartLoc, EndLoc;
846 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000847 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000848 Error(StartLoc, "eiz and riz can only be used as index registers",
849 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000850 return 0;
851 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000852 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000853
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000854 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000855 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000856 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000857
858 // Following the comma we should have either an index register, or a scale
859 // value. We don't support the later form, but we want to parse it
860 // correctly.
861 //
862 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000863 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000864 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000865 SMLoc L;
866 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000867
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000868 if (getLexer().isNot(AsmToken::RParen)) {
869 // Parse the scale amount:
870 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000871 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000872 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000873 "expected comma in scale expression");
874 return 0;
875 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000876 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000877
878 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000879 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000880
881 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000882 if (getParser().ParseAbsoluteExpression(ScaleVal)){
883 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000884 return 0;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000885 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000886
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000887 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000888 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
889 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
890 return 0;
891 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000892 Scale = (unsigned)ScaleVal;
893 }
894 }
895 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000896 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000897 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000898 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000899
900 int64_t Value;
901 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000902 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000903
Daniel Dunbaree910252010-08-24 19:13:38 +0000904 if (Value != 1)
905 Warning(Loc, "scale factor without index register is ignored");
906 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000907 }
908 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000909
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000910 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000911 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000912 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000913 return 0;
914 }
Sean Callanan18b83232010-01-19 21:44:56 +0000915 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000916 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000917
Kevin Enderby84faf652012-03-12 21:32:09 +0000918 // If we have both a base register and an index register make sure they are
919 // both 64-bit or 32-bit registers.
920 if (BaseReg != 0 && IndexReg != 0) {
921 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
922 !X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg) &&
923 IndexReg != X86::RIZ) {
924 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
925 return 0;
926 }
927 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
928 !X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg) &&
929 IndexReg != X86::EIZ){
930 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
931 return 0;
932 }
933 }
934
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000935 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
936 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000937}
938
Devang Pateldd929fc2012-01-12 18:03:40 +0000939bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000940ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000941 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000942 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000943
Chris Lattnerd8f71792010-11-28 20:23:50 +0000944 // FIXME: Hack to recognize setneb as setne.
945 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
946 PatchedName != "setb" && PatchedName != "setnb")
947 PatchedName = PatchedName.substr(0, Name.size()-1);
948
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000949 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
950 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000951 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000952 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
953 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000954 bool IsVCMP = PatchedName.startswith("vcmp");
955 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000956 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000957 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000958 .Case("eq", 0)
959 .Case("lt", 1)
960 .Case("le", 2)
961 .Case("unord", 3)
962 .Case("neq", 4)
963 .Case("nlt", 5)
964 .Case("nle", 6)
965 .Case("ord", 7)
966 .Case("eq_uq", 8)
967 .Case("nge", 9)
968 .Case("ngt", 0x0A)
969 .Case("false", 0x0B)
970 .Case("neq_oq", 0x0C)
971 .Case("ge", 0x0D)
972 .Case("gt", 0x0E)
973 .Case("true", 0x0F)
974 .Case("eq_os", 0x10)
975 .Case("lt_oq", 0x11)
976 .Case("le_oq", 0x12)
977 .Case("unord_s", 0x13)
978 .Case("neq_us", 0x14)
979 .Case("nlt_uq", 0x15)
980 .Case("nle_uq", 0x16)
981 .Case("ord_s", 0x17)
982 .Case("eq_us", 0x18)
983 .Case("nge_uq", 0x19)
984 .Case("ngt_uq", 0x1A)
985 .Case("false_os", 0x1B)
986 .Case("neq_os", 0x1C)
987 .Case("ge_oq", 0x1D)
988 .Case("gt_oq", 0x1E)
989 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000990 .Default(~0U);
991 if (SSEComparisonCode != ~0U) {
992 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
993 getParser().getContext());
994 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000995 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000996 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000997 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000998 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000999 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001000 } else {
1001 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001002 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001003 }
1004 }
1005 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001006
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001007 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001008
Devang Patel885f65b2012-01-30 22:47:12 +00001009 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001010 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001011
Chris Lattner2544f422010-09-08 05:17:37 +00001012 // Determine whether this is an instruction prefix.
1013 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001014 Name == "lock" || Name == "rep" ||
1015 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001016 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001017 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001018
1019
Chris Lattner2544f422010-09-08 05:17:37 +00001020 // This does the actual operand parsing. Don't parse any more if we have a
1021 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1022 // just want to parse the "lock" as the first instruction and the "incl" as
1023 // the next one.
1024 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001025
1026 // Parse '*' modifier.
1027 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001028 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001029 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001030 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001031 }
1032
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001033 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001034 if (X86Operand *Op = ParseOperand())
1035 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001036 else {
1037 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001038 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001039 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001040
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001041 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001042 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001043
1044 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001045 if (X86Operand *Op = ParseOperand())
1046 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001047 else {
1048 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001049 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001050 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001051 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001052
Chris Lattnercbf8a982010-09-11 16:18:25 +00001053 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001054 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001055 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001056 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001057 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001058 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001059
Chris Lattner2544f422010-09-08 05:17:37 +00001060 if (getLexer().is(AsmToken::EndOfStatement))
1061 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001062 else if (isPrefix && getLexer().is(AsmToken::Slash))
1063 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001064
Devang Patel885f65b2012-01-30 22:47:12 +00001065 if (ExtraImmOp && isParsingIntelSyntax())
1066 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1067
Chris Lattner98c870f2010-11-06 19:25:43 +00001068 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1069 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1070 // documented form in various unofficial manuals, so a lot of code uses it.
1071 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1072 Operands.size() == 3) {
1073 X86Operand &Op = *(X86Operand*)Operands.back();
1074 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1075 isa<MCConstantExpr>(Op.Mem.Disp) &&
1076 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1077 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1078 SMLoc Loc = Op.getEndLoc();
1079 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1080 delete &Op;
1081 }
1082 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001083 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1084 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1085 Operands.size() == 3) {
1086 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1087 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1088 isa<MCConstantExpr>(Op.Mem.Disp) &&
1089 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1090 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1091 SMLoc Loc = Op.getEndLoc();
1092 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1093 delete &Op;
1094 }
1095 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001096 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1097 if (Name.startswith("ins") && Operands.size() == 3 &&
1098 (Name == "insb" || Name == "insw" || Name == "insl")) {
1099 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1100 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1101 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1102 Operands.pop_back();
1103 Operands.pop_back();
1104 delete &Op;
1105 delete &Op2;
1106 }
1107 }
1108
1109 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1110 if (Name.startswith("outs") && Operands.size() == 3 &&
1111 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1112 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1113 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1114 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1115 Operands.pop_back();
1116 Operands.pop_back();
1117 delete &Op;
1118 delete &Op2;
1119 }
1120 }
1121
1122 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1123 if (Name.startswith("movs") && Operands.size() == 3 &&
1124 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001125 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001126 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1127 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1128 if (isSrcOp(Op) && isDstOp(Op2)) {
1129 Operands.pop_back();
1130 Operands.pop_back();
1131 delete &Op;
1132 delete &Op2;
1133 }
1134 }
1135 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1136 if (Name.startswith("lods") && Operands.size() == 3 &&
1137 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001138 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001139 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1140 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1141 if (isSrcOp(*Op1) && Op2->isReg()) {
1142 const char *ins;
1143 unsigned reg = Op2->getReg();
1144 bool isLods = Name == "lods";
1145 if (reg == X86::AL && (isLods || Name == "lodsb"))
1146 ins = "lodsb";
1147 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1148 ins = "lodsw";
1149 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1150 ins = "lodsl";
1151 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1152 ins = "lodsq";
1153 else
1154 ins = NULL;
1155 if (ins != NULL) {
1156 Operands.pop_back();
1157 Operands.pop_back();
1158 delete Op1;
1159 delete Op2;
1160 if (Name != ins)
1161 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1162 }
1163 }
1164 }
1165 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1166 if (Name.startswith("stos") && Operands.size() == 3 &&
1167 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001168 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001169 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1170 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1171 if (isDstOp(*Op2) && Op1->isReg()) {
1172 const char *ins;
1173 unsigned reg = Op1->getReg();
1174 bool isStos = Name == "stos";
1175 if (reg == X86::AL && (isStos || Name == "stosb"))
1176 ins = "stosb";
1177 else if (reg == X86::AX && (isStos || Name == "stosw"))
1178 ins = "stosw";
1179 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1180 ins = "stosl";
1181 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1182 ins = "stosq";
1183 else
1184 ins = NULL;
1185 if (ins != NULL) {
1186 Operands.pop_back();
1187 Operands.pop_back();
1188 delete Op1;
1189 delete Op2;
1190 if (Name != ins)
1191 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1192 }
1193 }
1194 }
1195
Chris Lattnere9e16a32010-09-15 04:33:27 +00001196 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001197 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001198 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001199 Name.startswith("shl") || Name.startswith("sal") ||
1200 Name.startswith("rcl") || Name.startswith("rcr") ||
1201 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001202 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001203 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001204 // Intel syntax
1205 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1206 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1207 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1208 delete Operands[2];
1209 Operands.pop_back();
1210 }
1211 } else {
1212 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1213 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1214 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1215 delete Operands[1];
1216 Operands.erase(Operands.begin() + 1);
1217 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001218 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001219 }
Chris Lattner15f89512011-04-09 19:41:05 +00001220
1221 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1222 // instalias with an immediate operand yet.
1223 if (Name == "int" && Operands.size() == 2) {
1224 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1225 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1226 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1227 delete Operands[1];
1228 Operands.erase(Operands.begin() + 1);
1229 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1230 }
1231 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001232
Chris Lattner98986712010-01-14 22:21:20 +00001233 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001234}
1235
Devang Pateldd929fc2012-01-12 18:03:40 +00001236bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001237processInstruction(MCInst &Inst,
1238 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1239 switch (Inst.getOpcode()) {
1240 default: return false;
1241 case X86::AND16i16: {
1242 if (!Inst.getOperand(0).isImm() ||
1243 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1244 return false;
1245
1246 MCInst TmpInst;
1247 TmpInst.setOpcode(X86::AND16ri8);
1248 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1249 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1250 TmpInst.addOperand(Inst.getOperand(0));
1251 Inst = TmpInst;
1252 return true;
1253 }
1254 case X86::AND32i32: {
1255 if (!Inst.getOperand(0).isImm() ||
1256 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1257 return false;
1258
1259 MCInst TmpInst;
1260 TmpInst.setOpcode(X86::AND32ri8);
1261 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1262 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1263 TmpInst.addOperand(Inst.getOperand(0));
1264 Inst = TmpInst;
1265 return true;
1266 }
1267 case X86::AND64i32: {
1268 if (!Inst.getOperand(0).isImm() ||
1269 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1270 return false;
1271
1272 MCInst TmpInst;
1273 TmpInst.setOpcode(X86::AND64ri8);
1274 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1275 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1276 TmpInst.addOperand(Inst.getOperand(0));
1277 Inst = TmpInst;
1278 return true;
1279 }
Devang Patelac0f0482012-01-19 17:53:25 +00001280 case X86::XOR16i16: {
1281 if (!Inst.getOperand(0).isImm() ||
1282 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1283 return false;
1284
1285 MCInst TmpInst;
1286 TmpInst.setOpcode(X86::XOR16ri8);
1287 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1288 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1289 TmpInst.addOperand(Inst.getOperand(0));
1290 Inst = TmpInst;
1291 return true;
1292 }
1293 case X86::XOR32i32: {
1294 if (!Inst.getOperand(0).isImm() ||
1295 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1296 return false;
1297
1298 MCInst TmpInst;
1299 TmpInst.setOpcode(X86::XOR32ri8);
1300 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1301 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1302 TmpInst.addOperand(Inst.getOperand(0));
1303 Inst = TmpInst;
1304 return true;
1305 }
1306 case X86::XOR64i32: {
1307 if (!Inst.getOperand(0).isImm() ||
1308 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1309 return false;
1310
1311 MCInst TmpInst;
1312 TmpInst.setOpcode(X86::XOR64ri8);
1313 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1314 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1315 TmpInst.addOperand(Inst.getOperand(0));
1316 Inst = TmpInst;
1317 return true;
1318 }
1319 case X86::OR16i16: {
1320 if (!Inst.getOperand(0).isImm() ||
1321 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1322 return false;
1323
1324 MCInst TmpInst;
1325 TmpInst.setOpcode(X86::OR16ri8);
1326 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1327 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1328 TmpInst.addOperand(Inst.getOperand(0));
1329 Inst = TmpInst;
1330 return true;
1331 }
1332 case X86::OR32i32: {
1333 if (!Inst.getOperand(0).isImm() ||
1334 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1335 return false;
1336
1337 MCInst TmpInst;
1338 TmpInst.setOpcode(X86::OR32ri8);
1339 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1340 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1341 TmpInst.addOperand(Inst.getOperand(0));
1342 Inst = TmpInst;
1343 return true;
1344 }
1345 case X86::OR64i32: {
1346 if (!Inst.getOperand(0).isImm() ||
1347 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1348 return false;
1349
1350 MCInst TmpInst;
1351 TmpInst.setOpcode(X86::OR64ri8);
1352 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1353 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1354 TmpInst.addOperand(Inst.getOperand(0));
1355 Inst = TmpInst;
1356 return true;
1357 }
1358 case X86::CMP16i16: {
1359 if (!Inst.getOperand(0).isImm() ||
1360 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1361 return false;
1362
1363 MCInst TmpInst;
1364 TmpInst.setOpcode(X86::CMP16ri8);
1365 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1366 TmpInst.addOperand(Inst.getOperand(0));
1367 Inst = TmpInst;
1368 return true;
1369 }
1370 case X86::CMP32i32: {
1371 if (!Inst.getOperand(0).isImm() ||
1372 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1373 return false;
1374
1375 MCInst TmpInst;
1376 TmpInst.setOpcode(X86::CMP32ri8);
1377 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1378 TmpInst.addOperand(Inst.getOperand(0));
1379 Inst = TmpInst;
1380 return true;
1381 }
1382 case X86::CMP64i32: {
1383 if (!Inst.getOperand(0).isImm() ||
1384 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1385 return false;
1386
1387 MCInst TmpInst;
1388 TmpInst.setOpcode(X86::CMP64ri8);
1389 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1390 TmpInst.addOperand(Inst.getOperand(0));
1391 Inst = TmpInst;
1392 return true;
1393 }
Devang Patela951f772012-01-19 18:40:55 +00001394 case X86::ADD16i16: {
1395 if (!Inst.getOperand(0).isImm() ||
1396 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1397 return false;
1398
1399 MCInst TmpInst;
1400 TmpInst.setOpcode(X86::ADD16ri8);
1401 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1402 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1403 TmpInst.addOperand(Inst.getOperand(0));
1404 Inst = TmpInst;
1405 return true;
1406 }
1407 case X86::ADD32i32: {
1408 if (!Inst.getOperand(0).isImm() ||
1409 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1410 return false;
1411
1412 MCInst TmpInst;
1413 TmpInst.setOpcode(X86::ADD32ri8);
1414 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1415 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1416 TmpInst.addOperand(Inst.getOperand(0));
1417 Inst = TmpInst;
1418 return true;
1419 }
1420 case X86::ADD64i32: {
1421 if (!Inst.getOperand(0).isImm() ||
1422 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1423 return false;
1424
1425 MCInst TmpInst;
1426 TmpInst.setOpcode(X86::ADD64ri8);
1427 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1428 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1429 TmpInst.addOperand(Inst.getOperand(0));
1430 Inst = TmpInst;
1431 return true;
1432 }
1433 case X86::SUB16i16: {
1434 if (!Inst.getOperand(0).isImm() ||
1435 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1436 return false;
1437
1438 MCInst TmpInst;
1439 TmpInst.setOpcode(X86::SUB16ri8);
1440 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1441 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1442 TmpInst.addOperand(Inst.getOperand(0));
1443 Inst = TmpInst;
1444 return true;
1445 }
1446 case X86::SUB32i32: {
1447 if (!Inst.getOperand(0).isImm() ||
1448 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1449 return false;
1450
1451 MCInst TmpInst;
1452 TmpInst.setOpcode(X86::SUB32ri8);
1453 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1454 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1455 TmpInst.addOperand(Inst.getOperand(0));
1456 Inst = TmpInst;
1457 return true;
1458 }
1459 case X86::SUB64i32: {
1460 if (!Inst.getOperand(0).isImm() ||
1461 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1462 return false;
1463
1464 MCInst TmpInst;
1465 TmpInst.setOpcode(X86::SUB64ri8);
1466 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1467 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1468 TmpInst.addOperand(Inst.getOperand(0));
1469 Inst = TmpInst;
1470 return true;
1471 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001472 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001473}
1474
1475bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001476MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001477 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001478 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001479 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001480 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1481 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001482
Chris Lattner7c51a312010-09-29 01:50:45 +00001483 // First, handle aliases that expand to multiple instructions.
1484 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +00001485 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
1486 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001487 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001488 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001489 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001490 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001491 MCInst Inst;
1492 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001493 Inst.setLoc(IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001494 Out.EmitInstruction(Inst);
1495
Chris Lattner0bb83a82010-09-30 16:39:29 +00001496 const char *Repl =
1497 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001498 .Case("finit", "fninit")
1499 .Case("fsave", "fnsave")
1500 .Case("fstcw", "fnstcw")
1501 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001502 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001503 .Case("fstsw", "fnstsw")
1504 .Case("fstsww", "fnstsw")
1505 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001506 .Default(0);
1507 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001508 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001509 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001510 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001511
Chris Lattnera008e8a2010-09-06 21:54:15 +00001512 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +00001513 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001514 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001515
Daniel Dunbarc918d602010-05-04 16:12:42 +00001516 // First, try a direct match.
Devang Patelbe3e3102012-01-30 20:02:42 +00001517 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo,
1518 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001519 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001520 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001521 // Some instructions need post-processing to, for example, tweak which
1522 // encoding is selected. Loop on it while changes happen so the
1523 // individual transformations can chain off each other.
1524 while (processInstruction(Inst, Operands))
1525 ;
1526
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001527 Inst.setLoc(IDLoc);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001528 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001529 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001530 case Match_MissingFeature:
1531 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1532 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +00001533 case Match_ConversionFail:
1534 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001535 case Match_InvalidOperand:
1536 WasOriginallyInvalidOperand = true;
1537 break;
1538 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001539 break;
1540 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001541
Daniel Dunbarc918d602010-05-04 16:12:42 +00001542 // FIXME: Ideally, we would only attempt suffix matches for things which are
1543 // valid prefixes, and we could just infer the right unambiguous
1544 // type. However, that requires substantially more matcher support than the
1545 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001546
Daniel Dunbarc918d602010-05-04 16:12:42 +00001547 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001548 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001549 SmallString<16> Tmp;
1550 Tmp += Base;
1551 Tmp += ' ';
1552 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001553
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001554 // If this instruction starts with an 'f', then it is a floating point stack
1555 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1556 // 80-bit floating point, which use the suffixes s,l,t respectively.
1557 //
1558 // Otherwise, we assume that this may be an integer instruction, which comes
1559 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1560 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
1561
Daniel Dunbarc918d602010-05-04 16:12:42 +00001562 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001563 Tmp[Base.size()] = Suffixes[0];
1564 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001565 unsigned Match1, Match2, Match3, Match4;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001566
1567 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1568 Tmp[Base.size()] = Suffixes[1];
1569 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1570 Tmp[Base.size()] = Suffixes[2];
1571 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1572 Tmp[Base.size()] = Suffixes[3];
1573 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001574
1575 // Restore the old token.
1576 Op->setTokenValue(Base);
1577
1578 // If exactly one matched, then we treat that as a successful match (and the
1579 // instruction will already have been filled in correctly, since the failing
1580 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001581 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001582 (Match1 == Match_Success) + (Match2 == Match_Success) +
1583 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001584 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001585 Inst.setLoc(IDLoc);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001586 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001587 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001588 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001589
Chris Lattnerec6789f2010-09-06 20:08:02 +00001590 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001591
Daniel Dunbar09062b12010-08-12 00:55:42 +00001592 // If we had multiple suffix matches, then identify this as an ambiguous
1593 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001594 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001595 char MatchChars[4];
1596 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001597 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1598 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1599 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1600 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001601
1602 SmallString<126> Msg;
1603 raw_svector_ostream OS(Msg);
1604 OS << "ambiguous instructions require an explicit suffix (could be ";
1605 for (unsigned i = 0; i != NumMatches; ++i) {
1606 if (i != 0)
1607 OS << ", ";
1608 if (i + 1 == NumMatches)
1609 OS << "or ";
1610 OS << "'" << Base << MatchChars[i] << "'";
1611 }
1612 OS << ")";
1613 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001614 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001615 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001616
Chris Lattnera008e8a2010-09-06 21:54:15 +00001617 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001618
Chris Lattnera008e8a2010-09-06 21:54:15 +00001619 // If all of the instructions reported an invalid mnemonic, then the original
1620 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001621 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1622 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001623 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001624 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
1625 Op->getLocRange());
Chris Lattnerce4a3352010-09-06 22:11:18 +00001626 }
1627
1628 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001629 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001630 if (OrigErrorInfo >= Operands.size())
1631 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001632
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001633 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1634 if (Operand->getStartLoc().isValid()) {
1635 SMRange OperandRange = Operand->getLocRange();
1636 return Error(Operand->getStartLoc(), "invalid operand for instruction",
1637 OperandRange);
1638 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001639 }
1640
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001641 return Error(IDLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001642 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001643
Chris Lattnerec6789f2010-09-06 20:08:02 +00001644 // If one instruction matched with a missing feature, report this as a
1645 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001646 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1647 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001648 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1649 return true;
1650 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001651
Chris Lattnera008e8a2010-09-06 21:54:15 +00001652 // If one instruction matched with an invalid operand, report this as an
1653 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001654 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1655 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001656 Error(IDLoc, "invalid operand for instruction");
1657 return true;
1658 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001659
Chris Lattnerec6789f2010-09-06 20:08:02 +00001660 // If all of these were an outright failure, report it in a useless way.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001661 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001662 return true;
1663}
1664
1665
Devang Pateldd929fc2012-01-12 18:03:40 +00001666bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001667 StringRef IDVal = DirectiveID.getIdentifier();
1668 if (IDVal == ".word")
1669 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001670 else if (IDVal.startswith(".code"))
1671 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Devang Patelbe3e3102012-01-30 20:02:42 +00001672 else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001673 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001674 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1675 if(Parser.getTok().getString() == "noprefix") {
1676 // FIXME : Handle noprefix
1677 Parser.Lex();
1678 } else
1679 return true;
1680 }
1681 return false;
1682 }
Chris Lattner537ca842010-10-30 17:38:55 +00001683 return true;
1684}
1685
1686/// ParseDirectiveWord
1687/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001688bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001689 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1690 for (;;) {
1691 const MCExpr *Value;
1692 if (getParser().ParseExpression(Value))
1693 return true;
1694
1695 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1696
1697 if (getLexer().is(AsmToken::EndOfStatement))
1698 break;
1699
1700 // FIXME: Improve diagnostic.
1701 if (getLexer().isNot(AsmToken::Comma))
1702 return Error(L, "unexpected token in directive");
1703 Parser.Lex();
1704 }
1705 }
1706
1707 Parser.Lex();
1708 return false;
1709}
1710
Evan Chengbd27f5a2011-07-27 00:38:12 +00001711/// ParseDirectiveCode
1712/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001713bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001714 if (IDVal == ".code32") {
1715 Parser.Lex();
1716 if (is64BitMode()) {
1717 SwitchMode();
1718 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1719 }
1720 } else if (IDVal == ".code64") {
1721 Parser.Lex();
1722 if (!is64BitMode()) {
1723 SwitchMode();
1724 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1725 }
1726 } else {
1727 return Error(L, "unexpected directive " + IDVal);
1728 }
Chris Lattner537ca842010-10-30 17:38:55 +00001729
Evan Chengbd27f5a2011-07-27 00:38:12 +00001730 return false;
1731}
Chris Lattner537ca842010-10-30 17:38:55 +00001732
1733
Sean Callanane88f5522010-01-23 02:43:15 +00001734extern "C" void LLVMInitializeX86AsmLexer();
1735
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001736// Force static initialization.
1737extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001738 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1739 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001740 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001741}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001742
Chris Lattner0692ee62010-09-06 19:11:01 +00001743#define GET_REGISTER_MATCHER
1744#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001745#include "X86GenAsmMatcher.inc"