blob: 708951126f542d07be880453b9157d680e093672 [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"
Chad Rosier96d58e62012-10-19 20:57:14 +000014#include "llvm/MC/MCSymbol.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000015#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000016#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000017#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Chad Rosier4284e172012-10-24 22:13:37 +000021#include "llvm/ADT/APFloat.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000022#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000024#include "llvm/ADT/StringSwitch.h"
25#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000028#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000029
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000030using namespace llvm;
31
32namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000033struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000034
Devang Pateldd929fc2012-01-12 18:03:40 +000035class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000036 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &Parser;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039 MCAsmParser &getParser() const { return Parser; }
40
41 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
42
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000043 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000044 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000045 bool MatchingInlineAsm = false) {
46 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000047 return Parser.Error(L, Msg, Ranges);
48 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000049
Devang Pateld37ad242012-01-17 18:00:18 +000050 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
51 Error(Loc, Msg);
52 return 0;
53 }
54
Chris Lattner309264d2010-01-15 18:44:13 +000055 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000056 X86Operand *ParseATTOperand();
57 X86Operand *ParseIntelOperand();
Chad Rosierc0a14b82012-10-24 17:22:29 +000058 X86Operand *ParseIntelOffsetOfOperator(SMLoc StartLoc);
Chad Rosier5b0f1b32012-10-04 23:59:38 +000059 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000060 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000061 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000062
Chad Rosier22f441a2012-10-24 22:21:50 +000063 const MCExpr *ParseIntelDotOperator(const MCExpr *Disp);
64
Kevin Enderby9c656452009-09-10 20:51:44 +000065 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000066 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000067
Devang Patelb8ba13f2012-01-18 22:42:29 +000068 bool processInstruction(MCInst &Inst,
69 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
70
Chad Rosier84125ca2012-10-13 00:26:04 +000071 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000072 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000073 MCStreamer &Out, unsigned &ErrorInfo,
74 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000075
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000076 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000077 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000078 bool isSrcOp(X86Operand &Op);
79
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000080 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
81 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000082 bool isDstOp(X86Operand &Op);
83
Evan Cheng59ee62d2011-07-11 03:57:24 +000084 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000085 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000086 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000087 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000088 void SwitchMode() {
89 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
90 setAvailableFeatures(FB);
91 }
Evan Chengebdeeab2011-07-08 01:53:10 +000092
Daniel Dunbar54074b52010-07-19 05:44:09 +000093 /// @name Auto-generated Matcher Functions
94 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000095
Chris Lattner0692ee62010-09-06 19:11:01 +000096#define GET_ASSEMBLER_HEADER
97#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000098
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000099 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000100
101public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000102 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +0000103 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000104
Daniel Dunbar54074b52010-07-19 05:44:09 +0000105 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000106 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000107 }
Roman Divackybf755322011-01-27 17:14:22 +0000108 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000109
Benjamin Kramer38e59892010-07-14 22:38:02 +0000110 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000111 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000112
113 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000114
115 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000116 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000117 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000118};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000119} // end anonymous namespace
120
Sean Callanane9b466d2010-01-23 00:40:33 +0000121/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000122/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000123
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000124static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000125
126/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000127
Craig Topper76bd9382012-07-18 04:59:16 +0000128static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000129 return (( Value <= 0x000000000000007FULL)||
130 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
131 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
132}
133
134static bool isImmSExti32i8Value(uint64_t Value) {
135 return (( Value <= 0x000000000000007FULL)||
136 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
137 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
138}
139
140static bool isImmZExtu32u8Value(uint64_t Value) {
141 return (Value <= 0x00000000000000FFULL);
142}
143
144static bool isImmSExti64i8Value(uint64_t Value) {
145 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000146 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000147}
148
149static bool isImmSExti64i32Value(uint64_t Value) {
150 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000151 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000152}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000153namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000154
155/// X86Operand - Instances of this class represent a parsed X86 machine
156/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000157struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000158 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000159 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000160 Register,
161 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000162 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000163 } Kind;
164
Chris Lattner29ef9a22010-01-15 18:51:29 +0000165 SMLoc StartLoc, EndLoc;
Chad Rosier5a719fc2012-10-23 17:43:43 +0000166 SMLoc OffsetOfLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000167
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000168 union {
169 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000170 const char *Data;
171 unsigned Length;
172 } Tok;
173
174 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000175 unsigned RegNo;
176 } Reg;
177
178 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000179 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000180 } Imm;
181
182 struct {
183 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000184 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000185 unsigned BaseReg;
186 unsigned IndexReg;
187 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000188 unsigned Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000189 bool NeedSizeDir;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000190 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000191 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000192
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000193 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000194 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000195
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000196 /// getStartLoc - Get the location of the first token of this operand.
197 SMLoc getStartLoc() const { return StartLoc; }
198 /// getEndLoc - Get the location of the last token of this operand.
199 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000200 /// getLocRange - Get the range between the first and last token of this
201 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000202 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000203 /// getOffsetOfLoc - Get the location of the offset operator.
204 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000205
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000206 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000207
Daniel Dunbar20927f22009-08-07 08:26:05 +0000208 StringRef getToken() const {
209 assert(Kind == Token && "Invalid access!");
210 return StringRef(Tok.Data, Tok.Length);
211 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000212 void setTokenValue(StringRef Value) {
213 assert(Kind == Token && "Invalid access!");
214 Tok.Data = Value.data();
215 Tok.Length = Value.size();
216 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000217
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000218 unsigned getReg() const {
219 assert(Kind == Register && "Invalid access!");
220 return Reg.RegNo;
221 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000222
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000223 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000224 assert(Kind == Immediate && "Invalid access!");
225 return Imm.Val;
226 }
227
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000228 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000229 assert(Kind == Memory && "Invalid access!");
230 return Mem.Disp;
231 }
232 unsigned getMemSegReg() const {
233 assert(Kind == Memory && "Invalid access!");
234 return Mem.SegReg;
235 }
236 unsigned getMemBaseReg() const {
237 assert(Kind == Memory && "Invalid access!");
238 return Mem.BaseReg;
239 }
240 unsigned getMemIndexReg() const {
241 assert(Kind == Memory && "Invalid access!");
242 return Mem.IndexReg;
243 }
244 unsigned getMemScale() const {
245 assert(Kind == Memory && "Invalid access!");
246 return Mem.Scale;
247 }
248
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000249 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000250
251 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000252
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000253 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000254 if (!isImm())
255 return false;
256
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000257 // If this isn't a constant expr, just assume it fits and let relaxation
258 // handle it.
259 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
260 if (!CE)
261 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000262
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000263 // Otherwise, check the value is in a range that makes sense for this
264 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000265 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000266 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000267 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000268 if (!isImm())
269 return false;
270
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000271 // If this isn't a constant expr, just assume it fits and let relaxation
272 // handle it.
273 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
274 if (!CE)
275 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000276
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000277 // Otherwise, check the value is in a range that makes sense for this
278 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000279 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000280 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000281 bool isImmZExtu32u8() const {
282 if (!isImm())
283 return false;
284
285 // If this isn't a constant expr, just assume it fits and let relaxation
286 // handle it.
287 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
288 if (!CE)
289 return true;
290
291 // Otherwise, check the value is in a range that makes sense for this
292 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000293 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000294 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000295 bool isImmSExti64i8() const {
296 if (!isImm())
297 return false;
298
299 // If this isn't a constant expr, just assume it fits and let relaxation
300 // handle it.
301 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
302 if (!CE)
303 return true;
304
305 // Otherwise, check the value is in a range that makes sense for this
306 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000307 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000308 }
309 bool isImmSExti64i32() const {
310 if (!isImm())
311 return false;
312
313 // If this isn't a constant expr, just assume it fits and let relaxation
314 // handle it.
315 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
316 if (!CE)
317 return true;
318
319 // Otherwise, check the value is in a range that makes sense for this
320 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000321 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000322 }
323
Chad Rosier96d58e62012-10-19 20:57:14 +0000324 unsigned getMemSize() const {
325 assert(Kind == Memory && "Invalid access!");
326 return Mem.Size;
327 }
328
Chad Rosiera703fb92012-10-22 19:50:35 +0000329 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000330 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000331 }
332
Chad Rosier96d58e62012-10-19 20:57:14 +0000333 bool needSizeDirective() const {
334 assert(Kind == Memory && "Invalid access!");
335 return Mem.NeedSizeDir;
336 }
337
Daniel Dunbar20927f22009-08-07 08:26:05 +0000338 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000339 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000340 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000341 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000342 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000343 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000344 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000345 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000346 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000347 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000348 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000349 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000350 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000351 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000352 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000353 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000354 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000355 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000356 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000357 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000358 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000359 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000360
Craig Topper75dc33a2012-07-18 04:11:12 +0000361 bool isMemVX32() const {
362 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
363 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
364 }
365 bool isMemVY32() const {
366 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
367 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
368 }
369 bool isMemVX64() const {
370 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
371 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
372 }
373 bool isMemVY64() const {
374 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
375 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
376 }
377
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000378 bool isAbsMem() const {
379 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000380 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000381 }
382
Daniel Dunbar20927f22009-08-07 08:26:05 +0000383 bool isReg() const { return Kind == Register; }
384
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000385 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
386 // Add as immediates when possible.
387 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
388 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
389 else
390 Inst.addOperand(MCOperand::CreateExpr(Expr));
391 }
392
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000393 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000394 assert(N == 1 && "Invalid number of operands!");
395 Inst.addOperand(MCOperand::CreateReg(getReg()));
396 }
397
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000398 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000399 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000400 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000401 }
402
Chad Rosier36b8fed2012-06-27 22:34:28 +0000403 void addMem8Operands(MCInst &Inst, unsigned N) const {
404 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000405 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000406 void addMem16Operands(MCInst &Inst, unsigned N) const {
407 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000408 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000409 void addMem32Operands(MCInst &Inst, unsigned N) const {
410 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000411 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000412 void addMem64Operands(MCInst &Inst, unsigned N) const {
413 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000414 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000415 void addMem80Operands(MCInst &Inst, unsigned N) const {
416 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000417 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000418 void addMem128Operands(MCInst &Inst, unsigned N) const {
419 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000420 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000421 void addMem256Operands(MCInst &Inst, unsigned N) const {
422 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000423 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000424 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
425 addMemOperands(Inst, N);
426 }
427 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
428 addMemOperands(Inst, N);
429 }
430 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
431 addMemOperands(Inst, N);
432 }
433 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
434 addMemOperands(Inst, N);
435 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000436
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000437 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000438 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000439 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
440 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
441 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000442 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000443 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
444 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000445
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000446 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
447 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000448 // Add as immediates when possible.
449 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
450 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
451 else
452 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000453 }
454
Chris Lattnerb4307b32010-01-15 19:28:38 +0000455 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000456 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
457 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000458 Res->Tok.Data = Str.data();
459 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000460 return Res;
461 }
462
Chad Rosierc0a14b82012-10-24 17:22:29 +0000463 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
464 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000465 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000466 Res->Reg.RegNo = RegNo;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000467 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000468 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000469 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000470
Chris Lattnerb4307b32010-01-15 19:28:38 +0000471 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
472 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000473 Res->Imm.Val = Val;
474 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000475 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000476
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000477 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000478 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
479 unsigned Size = 0, bool NeedSizeDir = false){
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000480 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
481 Res->Mem.SegReg = 0;
482 Res->Mem.Disp = Disp;
483 Res->Mem.BaseReg = 0;
484 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000485 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000486 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000487 Res->Mem.NeedSizeDir = NeedSizeDir;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000488 return Res;
489 }
490
491 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000492 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
493 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000494 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000495 unsigned Size = 0, bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000496 // We should never just have a displacement, that should be parsed as an
497 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000498 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
499
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000500 // The scale should always be one of {1,2,4,8}.
501 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000502 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000503 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000504 Res->Mem.SegReg = SegReg;
505 Res->Mem.Disp = Disp;
506 Res->Mem.BaseReg = BaseReg;
507 Res->Mem.IndexReg = IndexReg;
508 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000509 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000510 Res->Mem.NeedSizeDir = NeedSizeDir;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000511 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000512 }
513};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000514
Chris Lattner37dfdec2009-07-29 06:33:53 +0000515} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000516
Devang Pateldd929fc2012-01-12 18:03:40 +0000517bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000518 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000519
520 return (Op.isMem() &&
521 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
522 isa<MCConstantExpr>(Op.Mem.Disp) &&
523 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
524 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
525}
526
Devang Pateldd929fc2012-01-12 18:03:40 +0000527bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000528 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000529
Chad Rosier36b8fed2012-06-27 22:34:28 +0000530 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000531 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000532 isa<MCConstantExpr>(Op.Mem.Disp) &&
533 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
534 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
535}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000536
Devang Pateldd929fc2012-01-12 18:03:40 +0000537bool X86AsmParser::ParseRegister(unsigned &RegNo,
538 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000539 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000540 const AsmToken &PercentTok = Parser.getTok();
541 StartLoc = PercentTok.getLoc();
542
543 // If we encounter a %, ignore it. This code handles registers with and
544 // without the prefix, unprefixed registers can occur in cfi directives.
545 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000546 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000547
Sean Callanan18b83232010-01-19 21:44:56 +0000548 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000549 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000550 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000551 return Error(StartLoc, "invalid register name",
552 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000553 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000554
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000555 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000556
Chris Lattner33d60d52010-09-22 04:11:10 +0000557 // If the match failed, try the register name as lowercase.
558 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000559 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000560
Evan Cheng5de728c2011-07-27 23:22:03 +0000561 if (!is64BitMode()) {
562 // FIXME: This should be done using Requires<In32BitMode> and
563 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
564 // checked.
565 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
566 // REX prefix.
567 if (RegNo == X86::RIZ ||
568 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
569 X86II::isX86_64NonExtLowByteReg(RegNo) ||
570 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000571 return Error(StartLoc, "register %"
572 + Tok.getString() + " is only available in 64-bit mode",
573 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000574 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000575
Chris Lattner33d60d52010-09-22 04:11:10 +0000576 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
577 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000578 RegNo = X86::ST0;
579 EndLoc = Tok.getLoc();
580 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000581
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000582 // Check to see if we have '(4)' after %st.
583 if (getLexer().isNot(AsmToken::LParen))
584 return false;
585 // Lex the paren.
586 getParser().Lex();
587
588 const AsmToken &IntTok = Parser.getTok();
589 if (IntTok.isNot(AsmToken::Integer))
590 return Error(IntTok.getLoc(), "expected stack index");
591 switch (IntTok.getIntVal()) {
592 case 0: RegNo = X86::ST0; break;
593 case 1: RegNo = X86::ST1; break;
594 case 2: RegNo = X86::ST2; break;
595 case 3: RegNo = X86::ST3; break;
596 case 4: RegNo = X86::ST4; break;
597 case 5: RegNo = X86::ST5; break;
598 case 6: RegNo = X86::ST6; break;
599 case 7: RegNo = X86::ST7; break;
600 default: return Error(IntTok.getLoc(), "invalid stack index");
601 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000602
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000603 if (getParser().Lex().isNot(AsmToken::RParen))
604 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000605
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000606 EndLoc = Tok.getLoc();
607 Parser.Lex(); // Eat ')'
608 return false;
609 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000610
Chris Lattner645b2092010-06-24 07:29:18 +0000611 // If this is "db[0-7]", match it as an alias
612 // for dr[0-7].
613 if (RegNo == 0 && Tok.getString().size() == 3 &&
614 Tok.getString().startswith("db")) {
615 switch (Tok.getString()[2]) {
616 case '0': RegNo = X86::DR0; break;
617 case '1': RegNo = X86::DR1; break;
618 case '2': RegNo = X86::DR2; break;
619 case '3': RegNo = X86::DR3; break;
620 case '4': RegNo = X86::DR4; break;
621 case '5': RegNo = X86::DR5; break;
622 case '6': RegNo = X86::DR6; break;
623 case '7': RegNo = X86::DR7; break;
624 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000625
Chris Lattner645b2092010-06-24 07:29:18 +0000626 if (RegNo != 0) {
627 EndLoc = Tok.getLoc();
628 Parser.Lex(); // Eat it.
629 return false;
630 }
631 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000632
Devang Patel1aea4302012-01-20 22:32:05 +0000633 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000634 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000635 return Error(StartLoc, "invalid register name",
636 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000637 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000638
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000639 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000640 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000641 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000642}
643
Devang Pateldd929fc2012-01-12 18:03:40 +0000644X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000645 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000646 return ParseIntelOperand();
647 return ParseATTOperand();
648}
649
Devang Pateld37ad242012-01-17 18:00:18 +0000650/// getIntelMemOperandSize - Return intel memory operand size.
651static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000652 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000653 .Cases("BYTE", "byte", 8)
654 .Cases("WORD", "word", 16)
655 .Cases("DWORD", "dword", 32)
656 .Cases("QWORD", "qword", 64)
657 .Cases("XWORD", "xword", 80)
658 .Cases("XMMWORD", "xmmword", 128)
659 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000660 .Default(0);
661 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000662}
663
Chad Rosier65c88922012-10-22 19:42:52 +0000664X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000665 unsigned Size) {
666 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Chad Rosier4284e172012-10-24 22:13:37 +0000667 const AsmToken &Tok = Parser.getTok();
668 SMLoc Start = Tok.getLoc(), End;
Devang Patel0a338862012-01-12 01:36:43 +0000669
Chad Rosier4284e172012-10-24 22:13:37 +0000670 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000671 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
672
673 // Eat '['
674 if (getLexer().isNot(AsmToken::LBrac))
675 return ErrorOperand(Start, "Expected '[' token!");
676 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000677
Devang Pateld37ad242012-01-17 18:00:18 +0000678 if (getLexer().is(AsmToken::Identifier)) {
679 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000680 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000681 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000682 if (getParser().ParseExpression(Disp, End)) return 0;
683 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000684 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000685 Parser.Lex();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000686 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000687 }
688 } else if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000689 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000690 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000691 SMLoc Loc = Tok.getLoc();
Devang Patel3e081312012-01-23 20:20:06 +0000692 if (getLexer().is(AsmToken::RBrac)) {
693 // Handle '[' number ']'
694 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000695 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
696 if (SegReg)
697 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000698 Start, End, Size);
699 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000700 } else if (getLexer().is(AsmToken::Star)) {
701 // Handle '[' Scale*IndexReg ']'
702 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000703 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000704 if (ParseRegister(IndexReg, IdxRegLoc, End))
705 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000706 Scale = Val;
707 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000708 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000709 }
710
711 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
712 bool isPlus = getLexer().is(AsmToken::Plus);
713 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000714 SMLoc PlusLoc = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000715 if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000716 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000717 Parser.Lex();
718 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000719 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000720 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000721 if (ParseRegister(IndexReg, IdxRegLoc, End))
722 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000723 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000724 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000725 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000726 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000727 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000728 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000729 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000730 // This could be an index register or a displacement expression.
Chad Rosier4284e172012-10-24 22:13:37 +0000731 End = Tok.getLoc();
Devang Patelf2d21372012-01-23 22:35:25 +0000732 if (!IndexReg)
733 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000734 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000735 }
Devang Pateld37ad242012-01-17 18:00:18 +0000736 }
737
738 if (getLexer().isNot(AsmToken::RBrac))
739 if (getParser().ParseExpression(Disp, End)) return 0;
740
Chad Rosier4284e172012-10-24 22:13:37 +0000741 End = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000742 if (getLexer().isNot(AsmToken::RBrac))
743 return ErrorOperand(End, "expected ']' token!");
744 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000745 End = Tok.getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000746
Chad Rosier22f441a2012-10-24 22:21:50 +0000747 if (Tok.getString().startswith("."))
748 Disp = ParseIntelDotOperator(Disp);
749
750 End = Tok.getLoc();
751
Devang Patelfdd3b302012-01-20 21:21:01 +0000752 // handle [-42]
753 if (!BaseReg && !IndexReg)
Chad Rosierc0a14b82012-10-24 17:22:29 +0000754 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patelfdd3b302012-01-20 21:21:01 +0000755
Devang Pateld37ad242012-01-17 18:00:18 +0000756 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000757 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000758}
759
760/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000761X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000762 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000763 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000764
765 unsigned Size = getIntelMemOperandSize(Tok.getString());
766 if (Size) {
767 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000768 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
769 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000770 Parser.Lex();
771 }
772
Chad Rosierc0a14b82012-10-24 17:22:29 +0000773 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000774 return ParseIntelBracExpression(SegReg, Size);
775
776 if (!ParseRegister(SegReg, Start, End)) {
777 // Handel SegReg : [ ... ]
778 if (getLexer().isNot(AsmToken::Colon))
779 return ErrorOperand(Start, "Expected ':' token!");
780 Parser.Lex(); // Eat :
781 if (getLexer().isNot(AsmToken::LBrac))
782 return ErrorOperand(Start, "Expected '[' token!");
783 return ParseIntelBracExpression(SegReg, Size);
784 }
Devang Pateld37ad242012-01-17 18:00:18 +0000785
786 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
787 if (getParser().ParseExpression(Disp, End)) return 0;
Chad Rosierce353b32012-10-15 17:26:38 +0000788 End = Parser.getTok().getLoc();
Chad Rosier96d58e62012-10-19 20:57:14 +0000789
790 bool NeedSizeDir = false;
791 if (!Size && isParsingInlineAsm()) {
792 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
793 const MCSymbol &Sym = SymRef->getSymbol();
794 // FIXME: The SemaLookup will fail if the name is anything other then an
795 // identifier.
796 // FIXME: Pass a valid SMLoc.
797 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Size);
798 NeedSizeDir = Size > 0;
799 }
800 }
Chad Rosier2a784132012-10-23 23:31:33 +0000801 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +0000802 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier2a784132012-10-23 23:31:33 +0000803 else
Chad Rosierd4d96ac2012-10-23 23:34:28 +0000804 // When parsing inline assembly we set the base register to a non-zero value
805 // as we don't know the actual value at this time. This is necessary to
806 // get the matching correct in some cases.
Chad Rosier2a784132012-10-23 23:31:33 +0000807 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000808 /*Scale*/1, Start, End, Size, NeedSizeDir);
809}
810
Chad Rosier22f441a2012-10-24 22:21:50 +0000811/// Parse the '.' operator.
812const MCExpr *X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp) {
813 AsmToken Tok = *&Parser.getTok();
814
815 // Drop the '.'.
816 StringRef DotDispStr = Tok.getString().drop_front(1);
817
818 Lex(); // Eat .field.
819
820 // .Imm gets lexed as a real.
821 if (Tok.is(AsmToken::Real)) {
822 APInt DotDisp;
823 DotDispStr.getAsInteger(10, DotDisp);
824 uint64_t DotDispVal = DotDisp.getZExtValue();
825
826 // Special case zero dot displacement.
827 if (!DotDispVal) return Disp;
828
829 // FIXME: Handle non-constant expressions.
830 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
831 uint64_t OrigDispVal = OrigDisp->getValue();
832 return MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
833 }
834 }
835 return Disp;
836}
837
Chad Rosierc0a14b82012-10-24 17:22:29 +0000838/// Parse the 'offset' operator. This operator is used to specify the
839/// location rather then the content of a variable.
840X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
841 SMLoc OffsetOfLoc = Start;
842 Parser.Lex(); // Eat offset.
843 Start = Parser.getTok().getLoc();
844 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
845
846 SMLoc End;
847 const MCExpr *Val;
848 if (getParser().ParseExpression(Val, End))
849 return 0;
850
851 End = Parser.getTok().getLoc();
852
853 // The offset operator will have an 'r' constraint, thus we need to create
854 // register operand to ensure proper matching. Just pick a GPR based on
855 // the size of a pointer.
856 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
857 return X86Operand::CreateReg(RegNo, Start, End, OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +0000858}
859
860X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000861 SMLoc Start = Parser.getTok().getLoc(), End;
862
Chad Rosierc0a14b82012-10-24 17:22:29 +0000863 // offset operator.
864 const AsmToken &Tok = Parser.getTok();
865 if ((Tok.getString() == "offset" || Tok.getString() == "OFFSET") &&
866 isParsingInlineAsm())
867 return ParseIntelOffsetOfOperator(Start);
868
Devang Pateld37ad242012-01-17 18:00:18 +0000869 // immediate.
870 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
871 getLexer().is(AsmToken::Minus)) {
872 const MCExpr *Val;
873 if (!getParser().ParseExpression(Val, End)) {
874 End = Parser.getTok().getLoc();
875 return X86Operand::CreateImm(Val, Start, End);
876 }
877 }
878
Devang Patel0a338862012-01-12 01:36:43 +0000879 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000880 unsigned RegNo = 0;
881 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000882 // If this is a segment register followed by a ':', then this is the start
883 // of a memory reference, otherwise this is a normal register reference.
884 if (getLexer().isNot(AsmToken::Colon))
885 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
886
887 getParser().Lex(); // Eat the colon.
888 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000889 }
890
891 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000892 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000893}
894
Devang Pateldd929fc2012-01-12 18:03:40 +0000895X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000896 switch (getLexer().getKind()) {
897 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000898 // Parse a memory operand with no segment register.
899 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000900 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000901 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000902 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000903 SMLoc Start, End;
904 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000905 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000906 Error(Start, "%eiz and %riz can only be used as index registers",
907 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000908 return 0;
909 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000910
Chris Lattnereef6d782010-04-17 18:56:34 +0000911 // If this is a segment register followed by a ':', then this is the start
912 // of a memory reference, otherwise this is a normal register reference.
913 if (getLexer().isNot(AsmToken::Colon))
914 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000915
916
Chris Lattnereef6d782010-04-17 18:56:34 +0000917 getParser().Lex(); // Eat the colon.
918 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000919 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000920 case AsmToken::Dollar: {
921 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000922 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000923 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000924 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000925 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000926 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000927 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000928 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000929 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000930}
931
Chris Lattnereef6d782010-04-17 18:56:34 +0000932/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
933/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000934X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000935
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000936 // We have to disambiguate a parenthesized expression "(4+5)" from the start
937 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000938 // only way to do this without lookahead is to eat the '(' and see what is
939 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000940 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000941 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000942 SMLoc ExprEnd;
943 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000944
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000945 // After parsing the base expression we could either have a parenthesized
946 // memory address or not. If not, return now. If so, eat the (.
947 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000948 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000949 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000950 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000951 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000952 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000953
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000954 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000955 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000956 } else {
957 // Okay, we have a '('. We don't know if this is an expression or not, but
958 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000959 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000960 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000961
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000962 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000963 // Nothing to do here, fall into the code below with the '(' part of the
964 // memory operand consumed.
965 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000966 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000967
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000968 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000969 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000970 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000971
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000972 // After parsing the base expression we could either have a parenthesized
973 // memory address or not. If not, return now. If so, eat the (.
974 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000975 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000976 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000977 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000978 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000979 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000980
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000981 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000982 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000983 }
984 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000985
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000986 // If we reached here, then we just ate the ( of the memory operand. Process
987 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000988 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000989 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000990
Chris Lattner29ef9a22010-01-15 18:51:29 +0000991 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000992 SMLoc StartLoc, EndLoc;
993 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000994 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000995 Error(StartLoc, "eiz and riz can only be used as index registers",
996 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000997 return 0;
998 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000999 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001000
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001001 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001002 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001003 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001004
1005 // Following the comma we should have either an index register, or a scale
1006 // value. We don't support the later form, but we want to parse it
1007 // correctly.
1008 //
1009 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001010 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001011 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001012 SMLoc L;
1013 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001014
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001015 if (getLexer().isNot(AsmToken::RParen)) {
1016 // Parse the scale amount:
1017 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001018 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001019 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001020 "expected comma in scale expression");
1021 return 0;
1022 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001023 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001024
1025 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001026 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001027
1028 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001029 if (getParser().ParseAbsoluteExpression(ScaleVal)){
1030 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001031 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001032 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001033
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001034 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001035 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1036 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1037 return 0;
1038 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001039 Scale = (unsigned)ScaleVal;
1040 }
1041 }
1042 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001043 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001044 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001045 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001046
1047 int64_t Value;
1048 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001049 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001050
Daniel Dunbaree910252010-08-24 19:13:38 +00001051 if (Value != 1)
1052 Warning(Loc, "scale factor without index register is ignored");
1053 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001054 }
1055 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001056
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001057 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001058 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001059 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001060 return 0;
1061 }
Sean Callanan18b83232010-01-19 21:44:56 +00001062 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001063 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001064
Kevin Enderby84faf652012-03-12 21:32:09 +00001065 // If we have both a base register and an index register make sure they are
1066 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001067 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001068 if (BaseReg != 0 && IndexReg != 0) {
1069 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001070 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1071 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001072 IndexReg != X86::RIZ) {
1073 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1074 return 0;
1075 }
1076 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001077 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1078 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001079 IndexReg != X86::EIZ){
1080 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1081 return 0;
1082 }
1083 }
1084
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001085 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1086 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001087}
1088
Devang Pateldd929fc2012-01-12 18:03:40 +00001089bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +00001090ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001091 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +00001092 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001093
Chris Lattnerd8f71792010-11-28 20:23:50 +00001094 // FIXME: Hack to recognize setneb as setne.
1095 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1096 PatchedName != "setb" && PatchedName != "setnb")
1097 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001098
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001099 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1100 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001101 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001102 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1103 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001104 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001105 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001106 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001107 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001108 .Case("eq", 0x00)
1109 .Case("lt", 0x01)
1110 .Case("le", 0x02)
1111 .Case("unord", 0x03)
1112 .Case("neq", 0x04)
1113 .Case("nlt", 0x05)
1114 .Case("nle", 0x06)
1115 .Case("ord", 0x07)
1116 /* AVX only from here */
1117 .Case("eq_uq", 0x08)
1118 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001119 .Case("ngt", 0x0A)
1120 .Case("false", 0x0B)
1121 .Case("neq_oq", 0x0C)
1122 .Case("ge", 0x0D)
1123 .Case("gt", 0x0E)
1124 .Case("true", 0x0F)
1125 .Case("eq_os", 0x10)
1126 .Case("lt_oq", 0x11)
1127 .Case("le_oq", 0x12)
1128 .Case("unord_s", 0x13)
1129 .Case("neq_us", 0x14)
1130 .Case("nlt_uq", 0x15)
1131 .Case("nle_uq", 0x16)
1132 .Case("ord_s", 0x17)
1133 .Case("eq_us", 0x18)
1134 .Case("nge_uq", 0x19)
1135 .Case("ngt_uq", 0x1A)
1136 .Case("false_os", 0x1B)
1137 .Case("neq_os", 0x1C)
1138 .Case("ge_oq", 0x1D)
1139 .Case("gt_oq", 0x1E)
1140 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001141 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001142 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001143 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1144 getParser().getContext());
1145 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001146 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001147 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001148 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001149 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001150 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001151 } else {
1152 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001153 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001154 }
1155 }
1156 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001157
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001158 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001159
Devang Patel885f65b2012-01-30 22:47:12 +00001160 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001161 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001162
Chris Lattner2544f422010-09-08 05:17:37 +00001163 // Determine whether this is an instruction prefix.
1164 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001165 Name == "lock" || Name == "rep" ||
1166 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001167 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001168 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001169
1170
Chris Lattner2544f422010-09-08 05:17:37 +00001171 // This does the actual operand parsing. Don't parse any more if we have a
1172 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1173 // just want to parse the "lock" as the first instruction and the "incl" as
1174 // the next one.
1175 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001176
1177 // Parse '*' modifier.
1178 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001179 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001180 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001181 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001182 }
1183
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001184 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001185 if (X86Operand *Op = ParseOperand())
1186 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001187 else {
1188 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001189 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001190 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001191
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001192 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001193 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001194
1195 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001196 if (X86Operand *Op = ParseOperand())
1197 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001198 else {
1199 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001200 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001201 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001202 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001203
Chris Lattnercbf8a982010-09-11 16:18:25 +00001204 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001205 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001206 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001207 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001208 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001209 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001210
Chris Lattner2544f422010-09-08 05:17:37 +00001211 if (getLexer().is(AsmToken::EndOfStatement))
1212 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001213 else if (isPrefix && getLexer().is(AsmToken::Slash))
1214 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001215
Devang Patel885f65b2012-01-30 22:47:12 +00001216 if (ExtraImmOp && isParsingIntelSyntax())
1217 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1218
Chris Lattner98c870f2010-11-06 19:25:43 +00001219 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1220 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1221 // documented form in various unofficial manuals, so a lot of code uses it.
1222 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1223 Operands.size() == 3) {
1224 X86Operand &Op = *(X86Operand*)Operands.back();
1225 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1226 isa<MCConstantExpr>(Op.Mem.Disp) &&
1227 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1228 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1229 SMLoc Loc = Op.getEndLoc();
1230 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1231 delete &Op;
1232 }
1233 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001234 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1235 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1236 Operands.size() == 3) {
1237 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1238 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1239 isa<MCConstantExpr>(Op.Mem.Disp) &&
1240 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1241 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1242 SMLoc Loc = Op.getEndLoc();
1243 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1244 delete &Op;
1245 }
1246 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001247 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1248 if (Name.startswith("ins") && Operands.size() == 3 &&
1249 (Name == "insb" || Name == "insw" || Name == "insl")) {
1250 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1251 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1252 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1253 Operands.pop_back();
1254 Operands.pop_back();
1255 delete &Op;
1256 delete &Op2;
1257 }
1258 }
1259
1260 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1261 if (Name.startswith("outs") && Operands.size() == 3 &&
1262 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1263 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1264 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1265 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1266 Operands.pop_back();
1267 Operands.pop_back();
1268 delete &Op;
1269 delete &Op2;
1270 }
1271 }
1272
1273 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1274 if (Name.startswith("movs") && Operands.size() == 3 &&
1275 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001276 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001277 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1278 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1279 if (isSrcOp(Op) && isDstOp(Op2)) {
1280 Operands.pop_back();
1281 Operands.pop_back();
1282 delete &Op;
1283 delete &Op2;
1284 }
1285 }
1286 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1287 if (Name.startswith("lods") && Operands.size() == 3 &&
1288 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001289 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001290 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1291 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1292 if (isSrcOp(*Op1) && Op2->isReg()) {
1293 const char *ins;
1294 unsigned reg = Op2->getReg();
1295 bool isLods = Name == "lods";
1296 if (reg == X86::AL && (isLods || Name == "lodsb"))
1297 ins = "lodsb";
1298 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1299 ins = "lodsw";
1300 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1301 ins = "lodsl";
1302 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1303 ins = "lodsq";
1304 else
1305 ins = NULL;
1306 if (ins != NULL) {
1307 Operands.pop_back();
1308 Operands.pop_back();
1309 delete Op1;
1310 delete Op2;
1311 if (Name != ins)
1312 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1313 }
1314 }
1315 }
1316 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1317 if (Name.startswith("stos") && Operands.size() == 3 &&
1318 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001319 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001320 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1321 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1322 if (isDstOp(*Op2) && Op1->isReg()) {
1323 const char *ins;
1324 unsigned reg = Op1->getReg();
1325 bool isStos = Name == "stos";
1326 if (reg == X86::AL && (isStos || Name == "stosb"))
1327 ins = "stosb";
1328 else if (reg == X86::AX && (isStos || Name == "stosw"))
1329 ins = "stosw";
1330 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1331 ins = "stosl";
1332 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1333 ins = "stosq";
1334 else
1335 ins = NULL;
1336 if (ins != NULL) {
1337 Operands.pop_back();
1338 Operands.pop_back();
1339 delete Op1;
1340 delete Op2;
1341 if (Name != ins)
1342 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1343 }
1344 }
1345 }
1346
Chris Lattnere9e16a32010-09-15 04:33:27 +00001347 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001348 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001349 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001350 Name.startswith("shl") || Name.startswith("sal") ||
1351 Name.startswith("rcl") || Name.startswith("rcr") ||
1352 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001353 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001354 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001355 // Intel syntax
1356 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1357 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001358 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1359 delete Operands[2];
1360 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001361 }
1362 } else {
1363 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1364 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001365 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1366 delete Operands[1];
1367 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001368 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001369 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001370 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001371
Chris Lattner15f89512011-04-09 19:41:05 +00001372 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1373 // instalias with an immediate operand yet.
1374 if (Name == "int" && Operands.size() == 2) {
1375 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1376 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1377 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1378 delete Operands[1];
1379 Operands.erase(Operands.begin() + 1);
1380 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1381 }
1382 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001383
Chris Lattner98986712010-01-14 22:21:20 +00001384 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001385}
1386
Devang Pateldd929fc2012-01-12 18:03:40 +00001387bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001388processInstruction(MCInst &Inst,
1389 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1390 switch (Inst.getOpcode()) {
1391 default: return false;
1392 case X86::AND16i16: {
1393 if (!Inst.getOperand(0).isImm() ||
1394 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1395 return false;
1396
1397 MCInst TmpInst;
1398 TmpInst.setOpcode(X86::AND16ri8);
1399 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1400 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1401 TmpInst.addOperand(Inst.getOperand(0));
1402 Inst = TmpInst;
1403 return true;
1404 }
1405 case X86::AND32i32: {
1406 if (!Inst.getOperand(0).isImm() ||
1407 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1408 return false;
1409
1410 MCInst TmpInst;
1411 TmpInst.setOpcode(X86::AND32ri8);
1412 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1413 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1414 TmpInst.addOperand(Inst.getOperand(0));
1415 Inst = TmpInst;
1416 return true;
1417 }
1418 case X86::AND64i32: {
1419 if (!Inst.getOperand(0).isImm() ||
1420 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1421 return false;
1422
1423 MCInst TmpInst;
1424 TmpInst.setOpcode(X86::AND64ri8);
1425 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1426 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1427 TmpInst.addOperand(Inst.getOperand(0));
1428 Inst = TmpInst;
1429 return true;
1430 }
Devang Patelac0f0482012-01-19 17:53:25 +00001431 case X86::XOR16i16: {
1432 if (!Inst.getOperand(0).isImm() ||
1433 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1434 return false;
1435
1436 MCInst TmpInst;
1437 TmpInst.setOpcode(X86::XOR16ri8);
1438 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1439 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1440 TmpInst.addOperand(Inst.getOperand(0));
1441 Inst = TmpInst;
1442 return true;
1443 }
1444 case X86::XOR32i32: {
1445 if (!Inst.getOperand(0).isImm() ||
1446 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1447 return false;
1448
1449 MCInst TmpInst;
1450 TmpInst.setOpcode(X86::XOR32ri8);
1451 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1452 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1453 TmpInst.addOperand(Inst.getOperand(0));
1454 Inst = TmpInst;
1455 return true;
1456 }
1457 case X86::XOR64i32: {
1458 if (!Inst.getOperand(0).isImm() ||
1459 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1460 return false;
1461
1462 MCInst TmpInst;
1463 TmpInst.setOpcode(X86::XOR64ri8);
1464 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1465 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1466 TmpInst.addOperand(Inst.getOperand(0));
1467 Inst = TmpInst;
1468 return true;
1469 }
1470 case X86::OR16i16: {
1471 if (!Inst.getOperand(0).isImm() ||
1472 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1473 return false;
1474
1475 MCInst TmpInst;
1476 TmpInst.setOpcode(X86::OR16ri8);
1477 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1478 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1479 TmpInst.addOperand(Inst.getOperand(0));
1480 Inst = TmpInst;
1481 return true;
1482 }
1483 case X86::OR32i32: {
1484 if (!Inst.getOperand(0).isImm() ||
1485 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1486 return false;
1487
1488 MCInst TmpInst;
1489 TmpInst.setOpcode(X86::OR32ri8);
1490 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1491 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1492 TmpInst.addOperand(Inst.getOperand(0));
1493 Inst = TmpInst;
1494 return true;
1495 }
1496 case X86::OR64i32: {
1497 if (!Inst.getOperand(0).isImm() ||
1498 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1499 return false;
1500
1501 MCInst TmpInst;
1502 TmpInst.setOpcode(X86::OR64ri8);
1503 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1504 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1505 TmpInst.addOperand(Inst.getOperand(0));
1506 Inst = TmpInst;
1507 return true;
1508 }
1509 case X86::CMP16i16: {
1510 if (!Inst.getOperand(0).isImm() ||
1511 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1512 return false;
1513
1514 MCInst TmpInst;
1515 TmpInst.setOpcode(X86::CMP16ri8);
1516 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1517 TmpInst.addOperand(Inst.getOperand(0));
1518 Inst = TmpInst;
1519 return true;
1520 }
1521 case X86::CMP32i32: {
1522 if (!Inst.getOperand(0).isImm() ||
1523 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1524 return false;
1525
1526 MCInst TmpInst;
1527 TmpInst.setOpcode(X86::CMP32ri8);
1528 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1529 TmpInst.addOperand(Inst.getOperand(0));
1530 Inst = TmpInst;
1531 return true;
1532 }
1533 case X86::CMP64i32: {
1534 if (!Inst.getOperand(0).isImm() ||
1535 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1536 return false;
1537
1538 MCInst TmpInst;
1539 TmpInst.setOpcode(X86::CMP64ri8);
1540 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1541 TmpInst.addOperand(Inst.getOperand(0));
1542 Inst = TmpInst;
1543 return true;
1544 }
Devang Patela951f772012-01-19 18:40:55 +00001545 case X86::ADD16i16: {
1546 if (!Inst.getOperand(0).isImm() ||
1547 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1548 return false;
1549
1550 MCInst TmpInst;
1551 TmpInst.setOpcode(X86::ADD16ri8);
1552 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1553 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1554 TmpInst.addOperand(Inst.getOperand(0));
1555 Inst = TmpInst;
1556 return true;
1557 }
1558 case X86::ADD32i32: {
1559 if (!Inst.getOperand(0).isImm() ||
1560 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1561 return false;
1562
1563 MCInst TmpInst;
1564 TmpInst.setOpcode(X86::ADD32ri8);
1565 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1566 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1567 TmpInst.addOperand(Inst.getOperand(0));
1568 Inst = TmpInst;
1569 return true;
1570 }
1571 case X86::ADD64i32: {
1572 if (!Inst.getOperand(0).isImm() ||
1573 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1574 return false;
1575
1576 MCInst TmpInst;
1577 TmpInst.setOpcode(X86::ADD64ri8);
1578 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1579 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1580 TmpInst.addOperand(Inst.getOperand(0));
1581 Inst = TmpInst;
1582 return true;
1583 }
1584 case X86::SUB16i16: {
1585 if (!Inst.getOperand(0).isImm() ||
1586 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1587 return false;
1588
1589 MCInst TmpInst;
1590 TmpInst.setOpcode(X86::SUB16ri8);
1591 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1592 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1593 TmpInst.addOperand(Inst.getOperand(0));
1594 Inst = TmpInst;
1595 return true;
1596 }
1597 case X86::SUB32i32: {
1598 if (!Inst.getOperand(0).isImm() ||
1599 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1600 return false;
1601
1602 MCInst TmpInst;
1603 TmpInst.setOpcode(X86::SUB32ri8);
1604 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1605 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1606 TmpInst.addOperand(Inst.getOperand(0));
1607 Inst = TmpInst;
1608 return true;
1609 }
1610 case X86::SUB64i32: {
1611 if (!Inst.getOperand(0).isImm() ||
1612 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1613 return false;
1614
1615 MCInst TmpInst;
1616 TmpInst.setOpcode(X86::SUB64ri8);
1617 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1618 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1619 TmpInst.addOperand(Inst.getOperand(0));
1620 Inst = TmpInst;
1621 return true;
1622 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001623 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001624}
1625
1626bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001627MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001628 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001629 MCStreamer &Out, unsigned &ErrorInfo,
1630 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001631 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001632 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1633 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001634 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001635
Chris Lattner7c51a312010-09-29 01:50:45 +00001636 // First, handle aliases that expand to multiple instructions.
1637 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001638 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001639 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001640 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001641 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001642 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001643 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001644 MCInst Inst;
1645 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001646 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001647 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001648 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001649
Chris Lattner0bb83a82010-09-30 16:39:29 +00001650 const char *Repl =
1651 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001652 .Case("finit", "fninit")
1653 .Case("fsave", "fnsave")
1654 .Case("fstcw", "fnstcw")
1655 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001656 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001657 .Case("fstsw", "fnstsw")
1658 .Case("fstsww", "fnstsw")
1659 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001660 .Default(0);
1661 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001662 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001663 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001664 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001665
Chris Lattnera008e8a2010-09-06 21:54:15 +00001666 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001667 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001668
Daniel Dunbarc918d602010-05-04 16:12:42 +00001669 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001670 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001671 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001672 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001673 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001674 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001675 // Some instructions need post-processing to, for example, tweak which
1676 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001677 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001678 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001679 while (processInstruction(Inst, Operands))
1680 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001681
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001682 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001683 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001684 Out.EmitInstruction(Inst);
1685 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001686 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001687 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001688 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001689 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001690 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001691 case Match_InvalidOperand:
1692 WasOriginallyInvalidOperand = true;
1693 break;
1694 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001695 break;
1696 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001697
Daniel Dunbarc918d602010-05-04 16:12:42 +00001698 // FIXME: Ideally, we would only attempt suffix matches for things which are
1699 // valid prefixes, and we could just infer the right unambiguous
1700 // type. However, that requires substantially more matcher support than the
1701 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001702
Daniel Dunbarc918d602010-05-04 16:12:42 +00001703 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001704 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001705 SmallString<16> Tmp;
1706 Tmp += Base;
1707 Tmp += ' ';
1708 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001709
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001710 // If this instruction starts with an 'f', then it is a floating point stack
1711 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1712 // 80-bit floating point, which use the suffixes s,l,t respectively.
1713 //
1714 // Otherwise, we assume that this may be an integer instruction, which comes
1715 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1716 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001717
Daniel Dunbarc918d602010-05-04 16:12:42 +00001718 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001719 Tmp[Base.size()] = Suffixes[0];
1720 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001721 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001722
Chad Rosier6e006d32012-10-12 22:53:36 +00001723 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1724 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001725 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001726 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1727 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001728 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001729 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1730 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001731 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001732 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1733 isParsingIntelSyntax());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001734
1735 // Restore the old token.
1736 Op->setTokenValue(Base);
1737
1738 // If exactly one matched, then we treat that as a successful match (and the
1739 // instruction will already have been filled in correctly, since the failing
1740 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001741 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001742 (Match1 == Match_Success) + (Match2 == Match_Success) +
1743 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001744 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001745 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001746 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001747 Out.EmitInstruction(Inst);
1748 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001749 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001750 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001751
Chris Lattnerec6789f2010-09-06 20:08:02 +00001752 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001753
Daniel Dunbar09062b12010-08-12 00:55:42 +00001754 // If we had multiple suffix matches, then identify this as an ambiguous
1755 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001756 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001757 char MatchChars[4];
1758 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001759 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1760 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1761 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1762 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001763
1764 SmallString<126> Msg;
1765 raw_svector_ostream OS(Msg);
1766 OS << "ambiguous instructions require an explicit suffix (could be ";
1767 for (unsigned i = 0; i != NumMatches; ++i) {
1768 if (i != 0)
1769 OS << ", ";
1770 if (i + 1 == NumMatches)
1771 OS << "or ";
1772 OS << "'" << Base << MatchChars[i] << "'";
1773 }
1774 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001775 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001776 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001777 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001778
Chris Lattnera008e8a2010-09-06 21:54:15 +00001779 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001780
Chris Lattnera008e8a2010-09-06 21:54:15 +00001781 // If all of the instructions reported an invalid mnemonic, then the original
1782 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001783 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1784 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001785 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001786 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001787 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001788 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001789 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001790 }
1791
1792 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001793 if (ErrorInfo != ~0U) {
1794 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001795 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001796 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001797
Chad Rosier84125ca2012-10-13 00:26:04 +00001798 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001799 if (Operand->getStartLoc().isValid()) {
1800 SMRange OperandRange = Operand->getLocRange();
1801 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001802 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001803 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001804 }
1805
Chad Rosierb4fdade2012-08-21 19:36:59 +00001806 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001807 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001808 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001809
Chris Lattnerec6789f2010-09-06 20:08:02 +00001810 // If one instruction matched with a missing feature, report this as a
1811 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001812 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1813 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001814 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001815 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001816 return true;
1817 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001818
Chris Lattnera008e8a2010-09-06 21:54:15 +00001819 // If one instruction matched with an invalid operand, report this as an
1820 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001821 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1822 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001823 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001824 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001825 return true;
1826 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001827
Chris Lattnerec6789f2010-09-06 20:08:02 +00001828 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001829 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001830 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001831 return true;
1832}
1833
1834
Devang Pateldd929fc2012-01-12 18:03:40 +00001835bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001836 StringRef IDVal = DirectiveID.getIdentifier();
1837 if (IDVal == ".word")
1838 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001839 else if (IDVal.startswith(".code"))
1840 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001841 else if (IDVal.startswith(".att_syntax")) {
1842 getParser().setAssemblerDialect(0);
1843 return false;
1844 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001845 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001846 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1847 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001848 // FIXME : Handle noprefix
1849 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001850 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001851 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001852 }
1853 return false;
1854 }
Chris Lattner537ca842010-10-30 17:38:55 +00001855 return true;
1856}
1857
1858/// ParseDirectiveWord
1859/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001860bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001861 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1862 for (;;) {
1863 const MCExpr *Value;
1864 if (getParser().ParseExpression(Value))
1865 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001866
Chris Lattner537ca842010-10-30 17:38:55 +00001867 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001868
Chris Lattner537ca842010-10-30 17:38:55 +00001869 if (getLexer().is(AsmToken::EndOfStatement))
1870 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001871
Chris Lattner537ca842010-10-30 17:38:55 +00001872 // FIXME: Improve diagnostic.
1873 if (getLexer().isNot(AsmToken::Comma))
1874 return Error(L, "unexpected token in directive");
1875 Parser.Lex();
1876 }
1877 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001878
Chris Lattner537ca842010-10-30 17:38:55 +00001879 Parser.Lex();
1880 return false;
1881}
1882
Evan Chengbd27f5a2011-07-27 00:38:12 +00001883/// ParseDirectiveCode
1884/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001885bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001886 if (IDVal == ".code32") {
1887 Parser.Lex();
1888 if (is64BitMode()) {
1889 SwitchMode();
1890 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1891 }
1892 } else if (IDVal == ".code64") {
1893 Parser.Lex();
1894 if (!is64BitMode()) {
1895 SwitchMode();
1896 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1897 }
1898 } else {
1899 return Error(L, "unexpected directive " + IDVal);
1900 }
Chris Lattner537ca842010-10-30 17:38:55 +00001901
Evan Chengbd27f5a2011-07-27 00:38:12 +00001902 return false;
1903}
Chris Lattner537ca842010-10-30 17:38:55 +00001904
1905
Sean Callanane88f5522010-01-23 02:43:15 +00001906extern "C" void LLVMInitializeX86AsmLexer();
1907
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001908// Force static initialization.
1909extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001910 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1911 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001912 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001913}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001914
Chris Lattner0692ee62010-09-06 19:11:01 +00001915#define GET_REGISTER_MATCHER
1916#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001917#include "X86GenAsmMatcher.inc"