blob: 00e95596a0a83675d4cd742e2276fa0a12feba67 [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;
Chad Rosier6a020a72012-10-25 20:41:34 +000038 ParseInstructionInfo *InstInfo;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000040 MCAsmParser &getParser() const { return Parser; }
41
42 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000044 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000045 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000046 bool MatchingInlineAsm = false) {
47 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000048 return Parser.Error(L, Msg, Ranges);
49 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000050
Devang Pateld37ad242012-01-17 18:00:18 +000051 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
52 Error(Loc, Msg);
53 return 0;
54 }
55
Chris Lattner309264d2010-01-15 18:44:13 +000056 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000057 X86Operand *ParseATTOperand();
58 X86Operand *ParseIntelOperand();
Chad Rosierc0a14b82012-10-24 17:22:29 +000059 X86Operand *ParseIntelOffsetOfOperator(SMLoc StartLoc);
Chad Rosier5b0f1b32012-10-04 23:59:38 +000060 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000061 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000062 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000063
Chad Rosier5e6b37f2012-10-25 17:37:43 +000064 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr **NewDisp,
65 SmallString<64> &Err);
Chad Rosier22f441a2012-10-24 22:21:50 +000066
Kevin Enderby9c656452009-09-10 20:51:44 +000067 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000068 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000069
Devang Patelb8ba13f2012-01-18 22:42:29 +000070 bool processInstruction(MCInst &Inst,
71 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
72
Chad Rosier84125ca2012-10-13 00:26:04 +000073 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000074 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000075 MCStreamer &Out, unsigned &ErrorInfo,
76 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000077
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000078 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000079 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000080 bool isSrcOp(X86Operand &Op);
81
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000082 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
83 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000084 bool isDstOp(X86Operand &Op);
85
Evan Cheng59ee62d2011-07-11 03:57:24 +000086 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000087 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000088 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000089 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000090 void SwitchMode() {
91 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
92 setAvailableFeatures(FB);
93 }
Evan Chengebdeeab2011-07-08 01:53:10 +000094
Daniel Dunbar54074b52010-07-19 05:44:09 +000095 /// @name Auto-generated Matcher Functions
96 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000097
Chris Lattner0692ee62010-09-06 19:11:01 +000098#define GET_ASSEMBLER_HEADER
99#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000100
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000101 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000102
103public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000104 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosier6a020a72012-10-25 20:41:34 +0000105 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000106
Daniel Dunbar54074b52010-07-19 05:44:09 +0000107 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000108 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000109 }
Roman Divackybf755322011-01-27 17:14:22 +0000110 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000111
Chad Rosier6a020a72012-10-25 20:41:34 +0000112 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
113 SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000114 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000115
116 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000117
118 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000119 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000120 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000121};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000122} // end anonymous namespace
123
Sean Callanane9b466d2010-01-23 00:40:33 +0000124/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000125/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000126
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000127static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000128
129/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000130
Craig Topper76bd9382012-07-18 04:59:16 +0000131static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000132 return (( Value <= 0x000000000000007FULL)||
133 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
134 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
135}
136
137static bool isImmSExti32i8Value(uint64_t Value) {
138 return (( Value <= 0x000000000000007FULL)||
139 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
140 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
141}
142
143static bool isImmZExtu32u8Value(uint64_t Value) {
144 return (Value <= 0x00000000000000FFULL);
145}
146
147static bool isImmSExti64i8Value(uint64_t Value) {
148 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000149 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000150}
151
152static bool isImmSExti64i32Value(uint64_t Value) {
153 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000154 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000155}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000156namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000157
158/// X86Operand - Instances of this class represent a parsed X86 machine
159/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000160struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000161 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000162 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000163 Register,
164 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000165 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000166 } Kind;
167
Chris Lattner29ef9a22010-01-15 18:51:29 +0000168 SMLoc StartLoc, EndLoc;
Chad Rosier5a719fc2012-10-23 17:43:43 +0000169 SMLoc OffsetOfLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000170
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000171 union {
172 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000173 const char *Data;
174 unsigned Length;
175 } Tok;
176
177 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000178 unsigned RegNo;
179 } Reg;
180
181 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000182 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000183 } Imm;
184
185 struct {
186 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000187 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000188 unsigned BaseReg;
189 unsigned IndexReg;
190 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000191 unsigned Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000192 bool NeedSizeDir;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000193 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000194 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000195
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000196 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000197 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000198
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000199 /// getStartLoc - Get the location of the first token of this operand.
200 SMLoc getStartLoc() const { return StartLoc; }
201 /// getEndLoc - Get the location of the last token of this operand.
202 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000203 /// getLocRange - Get the range between the first and last token of this
204 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000205 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000206 /// getOffsetOfLoc - Get the location of the offset operator.
207 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000208
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000209 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000210
Daniel Dunbar20927f22009-08-07 08:26:05 +0000211 StringRef getToken() const {
212 assert(Kind == Token && "Invalid access!");
213 return StringRef(Tok.Data, Tok.Length);
214 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000215 void setTokenValue(StringRef Value) {
216 assert(Kind == Token && "Invalid access!");
217 Tok.Data = Value.data();
218 Tok.Length = Value.size();
219 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000220
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000221 unsigned getReg() const {
222 assert(Kind == Register && "Invalid access!");
223 return Reg.RegNo;
224 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000225
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000226 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000227 assert(Kind == Immediate && "Invalid access!");
228 return Imm.Val;
229 }
230
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000231 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000232 assert(Kind == Memory && "Invalid access!");
233 return Mem.Disp;
234 }
235 unsigned getMemSegReg() const {
236 assert(Kind == Memory && "Invalid access!");
237 return Mem.SegReg;
238 }
239 unsigned getMemBaseReg() const {
240 assert(Kind == Memory && "Invalid access!");
241 return Mem.BaseReg;
242 }
243 unsigned getMemIndexReg() const {
244 assert(Kind == Memory && "Invalid access!");
245 return Mem.IndexReg;
246 }
247 unsigned getMemScale() const {
248 assert(Kind == Memory && "Invalid access!");
249 return Mem.Scale;
250 }
251
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000252 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000253
254 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000255
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000256 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000257 if (!isImm())
258 return false;
259
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000260 // If this isn't a constant expr, just assume it fits and let relaxation
261 // handle it.
262 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
263 if (!CE)
264 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000265
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000266 // Otherwise, check the value is in a range that makes sense for this
267 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000268 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000269 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000270 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000271 if (!isImm())
272 return false;
273
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000274 // If this isn't a constant expr, just assume it fits and let relaxation
275 // handle it.
276 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
277 if (!CE)
278 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000279
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000280 // Otherwise, check the value is in a range that makes sense for this
281 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000282 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000283 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000284 bool isImmZExtu32u8() const {
285 if (!isImm())
286 return false;
287
288 // If this isn't a constant expr, just assume it fits and let relaxation
289 // handle it.
290 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
291 if (!CE)
292 return true;
293
294 // Otherwise, check the value is in a range that makes sense for this
295 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000296 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000297 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000298 bool isImmSExti64i8() const {
299 if (!isImm())
300 return false;
301
302 // If this isn't a constant expr, just assume it fits and let relaxation
303 // handle it.
304 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
305 if (!CE)
306 return true;
307
308 // Otherwise, check the value is in a range that makes sense for this
309 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000310 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000311 }
312 bool isImmSExti64i32() const {
313 if (!isImm())
314 return false;
315
316 // If this isn't a constant expr, just assume it fits and let relaxation
317 // handle it.
318 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
319 if (!CE)
320 return true;
321
322 // Otherwise, check the value is in a range that makes sense for this
323 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000324 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000325 }
326
Chad Rosier96d58e62012-10-19 20:57:14 +0000327 unsigned getMemSize() const {
328 assert(Kind == Memory && "Invalid access!");
329 return Mem.Size;
330 }
331
Chad Rosiera703fb92012-10-22 19:50:35 +0000332 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000333 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000334 }
335
Chad Rosier96d58e62012-10-19 20:57:14 +0000336 bool needSizeDirective() const {
337 assert(Kind == Memory && "Invalid access!");
338 return Mem.NeedSizeDir;
339 }
340
Daniel Dunbar20927f22009-08-07 08:26:05 +0000341 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000342 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000343 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000344 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000345 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000346 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000347 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000348 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000349 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000350 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000351 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000352 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000353 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000354 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000355 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000356 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000357 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000358 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000359 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000360 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000361 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000362 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000363
Craig Topper75dc33a2012-07-18 04:11:12 +0000364 bool isMemVX32() const {
365 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
366 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
367 }
368 bool isMemVY32() const {
369 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
370 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
371 }
372 bool isMemVX64() const {
373 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
374 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
375 }
376 bool isMemVY64() const {
377 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
378 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
379 }
380
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000381 bool isAbsMem() const {
382 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000383 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000384 }
385
Daniel Dunbar20927f22009-08-07 08:26:05 +0000386 bool isReg() const { return Kind == Register; }
387
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000388 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
389 // Add as immediates when possible.
390 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
391 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
392 else
393 Inst.addOperand(MCOperand::CreateExpr(Expr));
394 }
395
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000396 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000397 assert(N == 1 && "Invalid number of operands!");
398 Inst.addOperand(MCOperand::CreateReg(getReg()));
399 }
400
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000401 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000402 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000403 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000404 }
405
Chad Rosier36b8fed2012-06-27 22:34:28 +0000406 void addMem8Operands(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 addMem16Operands(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 addMem32Operands(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 addMem64Operands(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 addMem80Operands(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 addMem128Operands(MCInst &Inst, unsigned N) const {
422 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000423 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000424 void addMem256Operands(MCInst &Inst, unsigned N) const {
425 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000426 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000427 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
428 addMemOperands(Inst, N);
429 }
430 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
431 addMemOperands(Inst, N);
432 }
433 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
434 addMemOperands(Inst, N);
435 }
436 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
437 addMemOperands(Inst, N);
438 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000439
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000440 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000441 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000442 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
443 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
444 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000445 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000446 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
447 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000448
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000449 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
450 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000451 // Add as immediates when possible.
452 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
453 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
454 else
455 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000456 }
457
Chris Lattnerb4307b32010-01-15 19:28:38 +0000458 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000459 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
460 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000461 Res->Tok.Data = Str.data();
462 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000463 return Res;
464 }
465
Chad Rosierc0a14b82012-10-24 17:22:29 +0000466 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
467 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000468 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000469 Res->Reg.RegNo = RegNo;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000470 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000471 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000472 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000473
Chris Lattnerb4307b32010-01-15 19:28:38 +0000474 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
475 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000476 Res->Imm.Val = Val;
477 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000478 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000479
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000480 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000481 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
482 unsigned Size = 0, bool NeedSizeDir = false){
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000483 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
484 Res->Mem.SegReg = 0;
485 Res->Mem.Disp = Disp;
486 Res->Mem.BaseReg = 0;
487 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000488 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000489 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000490 Res->Mem.NeedSizeDir = NeedSizeDir;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000491 return Res;
492 }
493
494 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000495 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
496 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000497 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000498 unsigned Size = 0, bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000499 // We should never just have a displacement, that should be parsed as an
500 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000501 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
502
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000503 // The scale should always be one of {1,2,4,8}.
504 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000505 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000506 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000507 Res->Mem.SegReg = SegReg;
508 Res->Mem.Disp = Disp;
509 Res->Mem.BaseReg = BaseReg;
510 Res->Mem.IndexReg = IndexReg;
511 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000512 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000513 Res->Mem.NeedSizeDir = NeedSizeDir;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000514 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000515 }
516};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000517
Chris Lattner37dfdec2009-07-29 06:33:53 +0000518} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000519
Devang Pateldd929fc2012-01-12 18:03:40 +0000520bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000521 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000522
523 return (Op.isMem() &&
524 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
525 isa<MCConstantExpr>(Op.Mem.Disp) &&
526 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
527 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
528}
529
Devang Pateldd929fc2012-01-12 18:03:40 +0000530bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000531 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000532
Chad Rosier36b8fed2012-06-27 22:34:28 +0000533 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000534 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000535 isa<MCConstantExpr>(Op.Mem.Disp) &&
536 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
537 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
538}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000539
Devang Pateldd929fc2012-01-12 18:03:40 +0000540bool X86AsmParser::ParseRegister(unsigned &RegNo,
541 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000542 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000543 const AsmToken &PercentTok = Parser.getTok();
544 StartLoc = PercentTok.getLoc();
545
546 // If we encounter a %, ignore it. This code handles registers with and
547 // without the prefix, unprefixed registers can occur in cfi directives.
548 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000549 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000550
Sean Callanan18b83232010-01-19 21:44:56 +0000551 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000552 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000553 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000554 return Error(StartLoc, "invalid register name",
555 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000556 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000557
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000558 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000559
Chris Lattner33d60d52010-09-22 04:11:10 +0000560 // If the match failed, try the register name as lowercase.
561 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000562 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000563
Evan Cheng5de728c2011-07-27 23:22:03 +0000564 if (!is64BitMode()) {
565 // FIXME: This should be done using Requires<In32BitMode> and
566 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
567 // checked.
568 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
569 // REX prefix.
570 if (RegNo == X86::RIZ ||
571 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
572 X86II::isX86_64NonExtLowByteReg(RegNo) ||
573 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000574 return Error(StartLoc, "register %"
575 + Tok.getString() + " is only available in 64-bit mode",
576 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000577 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000578
Chris Lattner33d60d52010-09-22 04:11:10 +0000579 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
580 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000581 RegNo = X86::ST0;
582 EndLoc = Tok.getLoc();
583 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000584
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000585 // Check to see if we have '(4)' after %st.
586 if (getLexer().isNot(AsmToken::LParen))
587 return false;
588 // Lex the paren.
589 getParser().Lex();
590
591 const AsmToken &IntTok = Parser.getTok();
592 if (IntTok.isNot(AsmToken::Integer))
593 return Error(IntTok.getLoc(), "expected stack index");
594 switch (IntTok.getIntVal()) {
595 case 0: RegNo = X86::ST0; break;
596 case 1: RegNo = X86::ST1; break;
597 case 2: RegNo = X86::ST2; break;
598 case 3: RegNo = X86::ST3; break;
599 case 4: RegNo = X86::ST4; break;
600 case 5: RegNo = X86::ST5; break;
601 case 6: RegNo = X86::ST6; break;
602 case 7: RegNo = X86::ST7; break;
603 default: return Error(IntTok.getLoc(), "invalid stack index");
604 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000605
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000606 if (getParser().Lex().isNot(AsmToken::RParen))
607 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000608
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000609 EndLoc = Tok.getLoc();
610 Parser.Lex(); // Eat ')'
611 return false;
612 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000613
Chris Lattner645b2092010-06-24 07:29:18 +0000614 // If this is "db[0-7]", match it as an alias
615 // for dr[0-7].
616 if (RegNo == 0 && Tok.getString().size() == 3 &&
617 Tok.getString().startswith("db")) {
618 switch (Tok.getString()[2]) {
619 case '0': RegNo = X86::DR0; break;
620 case '1': RegNo = X86::DR1; break;
621 case '2': RegNo = X86::DR2; break;
622 case '3': RegNo = X86::DR3; break;
623 case '4': RegNo = X86::DR4; break;
624 case '5': RegNo = X86::DR5; break;
625 case '6': RegNo = X86::DR6; break;
626 case '7': RegNo = X86::DR7; break;
627 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000628
Chris Lattner645b2092010-06-24 07:29:18 +0000629 if (RegNo != 0) {
630 EndLoc = Tok.getLoc();
631 Parser.Lex(); // Eat it.
632 return false;
633 }
634 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000635
Devang Patel1aea4302012-01-20 22:32:05 +0000636 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000637 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000638 return Error(StartLoc, "invalid register name",
639 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000640 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000641
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000642 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000643 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000644 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000645}
646
Devang Pateldd929fc2012-01-12 18:03:40 +0000647X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000648 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000649 return ParseIntelOperand();
650 return ParseATTOperand();
651}
652
Devang Pateld37ad242012-01-17 18:00:18 +0000653/// getIntelMemOperandSize - Return intel memory operand size.
654static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000655 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000656 .Cases("BYTE", "byte", 8)
657 .Cases("WORD", "word", 16)
658 .Cases("DWORD", "dword", 32)
659 .Cases("QWORD", "qword", 64)
660 .Cases("XWORD", "xword", 80)
661 .Cases("XMMWORD", "xmmword", 128)
662 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000663 .Default(0);
664 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000665}
666
Chad Rosier65c88922012-10-22 19:42:52 +0000667X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000668 unsigned Size) {
669 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Chad Rosier4284e172012-10-24 22:13:37 +0000670 const AsmToken &Tok = Parser.getTok();
671 SMLoc Start = Tok.getLoc(), End;
Devang Patel0a338862012-01-12 01:36:43 +0000672
Chad Rosier4284e172012-10-24 22:13:37 +0000673 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000674 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
675
676 // Eat '['
677 if (getLexer().isNot(AsmToken::LBrac))
678 return ErrorOperand(Start, "Expected '[' token!");
679 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000680
Devang Pateld37ad242012-01-17 18:00:18 +0000681 if (getLexer().is(AsmToken::Identifier)) {
682 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000683 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000684 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000685 if (getParser().ParseExpression(Disp, End)) return 0;
686 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000687 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000688 Parser.Lex();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000689 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000690 }
691 } else if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000692 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000693 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000694 SMLoc Loc = Tok.getLoc();
Devang Patel3e081312012-01-23 20:20:06 +0000695 if (getLexer().is(AsmToken::RBrac)) {
696 // Handle '[' number ']'
697 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000698 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
699 if (SegReg)
700 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000701 Start, End, Size);
702 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000703 } else if (getLexer().is(AsmToken::Star)) {
704 // Handle '[' Scale*IndexReg ']'
705 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000706 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000707 if (ParseRegister(IndexReg, IdxRegLoc, End))
708 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000709 Scale = Val;
710 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000711 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000712 }
713
714 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
715 bool isPlus = getLexer().is(AsmToken::Plus);
716 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000717 SMLoc PlusLoc = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000718 if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000719 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000720 Parser.Lex();
721 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000722 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000723 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000724 if (ParseRegister(IndexReg, IdxRegLoc, End))
725 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000726 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000727 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000728 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000729 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000730 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000731 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000732 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000733 // This could be an index register or a displacement expression.
Chad Rosier4284e172012-10-24 22:13:37 +0000734 End = Tok.getLoc();
Devang Patelf2d21372012-01-23 22:35:25 +0000735 if (!IndexReg)
736 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000737 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000738 }
Devang Pateld37ad242012-01-17 18:00:18 +0000739 }
740
741 if (getLexer().isNot(AsmToken::RBrac))
742 if (getParser().ParseExpression(Disp, End)) return 0;
743
Chad Rosier4284e172012-10-24 22:13:37 +0000744 End = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000745 if (getLexer().isNot(AsmToken::RBrac))
746 return ErrorOperand(End, "expected ']' token!");
747 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000748 End = Tok.getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000749
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000750 if (Tok.getString().startswith(".")) {
751 SmallString<64> Err;
752 const MCExpr *NewDisp;
753 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
754 return ErrorOperand(Tok.getLoc(), Err);
755
756 Parser.Lex(); // Eat the field.
757 Disp = NewDisp;
758 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000759
760 End = Tok.getLoc();
761
Devang Patelfdd3b302012-01-20 21:21:01 +0000762 // handle [-42]
763 if (!BaseReg && !IndexReg)
Chad Rosierc0a14b82012-10-24 17:22:29 +0000764 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patelfdd3b302012-01-20 21:21:01 +0000765
Devang Pateld37ad242012-01-17 18:00:18 +0000766 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000767 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000768}
769
770/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000771X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000772 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000773 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000774
775 unsigned Size = getIntelMemOperandSize(Tok.getString());
776 if (Size) {
777 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000778 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
779 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000780 Parser.Lex();
781 }
782
Chad Rosierc0a14b82012-10-24 17:22:29 +0000783 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000784 return ParseIntelBracExpression(SegReg, Size);
785
786 if (!ParseRegister(SegReg, Start, End)) {
787 // Handel SegReg : [ ... ]
788 if (getLexer().isNot(AsmToken::Colon))
789 return ErrorOperand(Start, "Expected ':' token!");
790 Parser.Lex(); // Eat :
791 if (getLexer().isNot(AsmToken::LBrac))
792 return ErrorOperand(Start, "Expected '[' token!");
793 return ParseIntelBracExpression(SegReg, Size);
794 }
Devang Pateld37ad242012-01-17 18:00:18 +0000795
796 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
797 if (getParser().ParseExpression(Disp, End)) return 0;
Chad Rosierce353b32012-10-15 17:26:38 +0000798 End = Parser.getTok().getLoc();
Chad Rosier96d58e62012-10-19 20:57:14 +0000799
800 bool NeedSizeDir = false;
801 if (!Size && isParsingInlineAsm()) {
802 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
803 const MCSymbol &Sym = SymRef->getSymbol();
804 // FIXME: The SemaLookup will fail if the name is anything other then an
805 // identifier.
806 // FIXME: Pass a valid SMLoc.
807 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Size);
808 NeedSizeDir = Size > 0;
809 }
810 }
Chad Rosier2a784132012-10-23 23:31:33 +0000811 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +0000812 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier2a784132012-10-23 23:31:33 +0000813 else
Chad Rosierd4d96ac2012-10-23 23:34:28 +0000814 // When parsing inline assembly we set the base register to a non-zero value
815 // as we don't know the actual value at this time. This is necessary to
816 // get the matching correct in some cases.
Chad Rosier2a784132012-10-23 23:31:33 +0000817 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000818 /*Scale*/1, Start, End, Size, NeedSizeDir);
819}
820
Chad Rosier22f441a2012-10-24 22:21:50 +0000821/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000822bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
823 const MCExpr **NewDisp,
824 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +0000825 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000826 uint64_t OrigDispVal, DotDispVal;
827
828 // FIXME: Handle non-constant expressions.
829 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
830 OrigDispVal = OrigDisp->getValue();
831 } else {
832 Err = "Non-constant offsets are not supported!";
833 return true;
834 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000835
836 // Drop the '.'.
837 StringRef DotDispStr = Tok.getString().drop_front(1);
838
Chad Rosier22f441a2012-10-24 22:21:50 +0000839 // .Imm gets lexed as a real.
840 if (Tok.is(AsmToken::Real)) {
841 APInt DotDisp;
842 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000843 DotDispVal = DotDisp.getZExtValue();
844 } else {
845 Err = "Unexpected token type!";
846 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +0000847 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000848
849 // Special case zero dot displacement.
850 if (!DotDispVal) {
851 *NewDisp = Disp;
852 return false;
853 }
854
855 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
856 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +0000857}
858
Chad Rosierc0a14b82012-10-24 17:22:29 +0000859/// Parse the 'offset' operator. This operator is used to specify the
860/// location rather then the content of a variable.
861X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
862 SMLoc OffsetOfLoc = Start;
863 Parser.Lex(); // Eat offset.
864 Start = Parser.getTok().getLoc();
865 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
866
867 SMLoc End;
868 const MCExpr *Val;
869 if (getParser().ParseExpression(Val, End))
870 return 0;
871
872 End = Parser.getTok().getLoc();
873
874 // The offset operator will have an 'r' constraint, thus we need to create
875 // register operand to ensure proper matching. Just pick a GPR based on
876 // the size of a pointer.
877 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
878 return X86Operand::CreateReg(RegNo, Start, End, OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +0000879}
880
881X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000882 SMLoc Start = Parser.getTok().getLoc(), End;
883
Chad Rosierc0a14b82012-10-24 17:22:29 +0000884 // offset operator.
885 const AsmToken &Tok = Parser.getTok();
886 if ((Tok.getString() == "offset" || Tok.getString() == "OFFSET") &&
887 isParsingInlineAsm())
888 return ParseIntelOffsetOfOperator(Start);
889
Devang Pateld37ad242012-01-17 18:00:18 +0000890 // immediate.
891 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
892 getLexer().is(AsmToken::Minus)) {
893 const MCExpr *Val;
894 if (!getParser().ParseExpression(Val, End)) {
895 End = Parser.getTok().getLoc();
896 return X86Operand::CreateImm(Val, Start, End);
897 }
898 }
899
Devang Patel0a338862012-01-12 01:36:43 +0000900 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000901 unsigned RegNo = 0;
902 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000903 // If this is a segment register followed by a ':', then this is the start
904 // of a memory reference, otherwise this is a normal register reference.
905 if (getLexer().isNot(AsmToken::Colon))
906 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
907
908 getParser().Lex(); // Eat the colon.
909 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000910 }
911
912 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000913 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000914}
915
Devang Pateldd929fc2012-01-12 18:03:40 +0000916X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000917 switch (getLexer().getKind()) {
918 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000919 // Parse a memory operand with no segment register.
920 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000921 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000922 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000923 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000924 SMLoc Start, End;
925 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000926 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000927 Error(Start, "%eiz and %riz can only be used as index registers",
928 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000929 return 0;
930 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000931
Chris Lattnereef6d782010-04-17 18:56:34 +0000932 // If this is a segment register followed by a ':', then this is the start
933 // of a memory reference, otherwise this is a normal register reference.
934 if (getLexer().isNot(AsmToken::Colon))
935 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000936
937
Chris Lattnereef6d782010-04-17 18:56:34 +0000938 getParser().Lex(); // Eat the colon.
939 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000940 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000941 case AsmToken::Dollar: {
942 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000943 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000944 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000945 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000946 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000947 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000948 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000949 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000950 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000951}
952
Chris Lattnereef6d782010-04-17 18:56:34 +0000953/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
954/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000955X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000956
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000957 // We have to disambiguate a parenthesized expression "(4+5)" from the start
958 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000959 // only way to do this without lookahead is to eat the '(' and see what is
960 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000961 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000962 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000963 SMLoc ExprEnd;
964 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000965
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000966 // After parsing the base expression we could either have a parenthesized
967 // memory address or not. If not, return now. If so, eat the (.
968 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000969 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000970 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000971 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000972 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000973 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000974
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000975 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000976 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000977 } else {
978 // Okay, we have a '('. We don't know if this is an expression or not, but
979 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000980 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000981 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000982
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000983 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000984 // Nothing to do here, fall into the code below with the '(' part of the
985 // memory operand consumed.
986 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000987 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000988
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000989 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000990 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000991 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000992
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000993 // After parsing the base expression we could either have a parenthesized
994 // memory address or not. If not, return now. If so, eat the (.
995 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000996 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000997 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000998 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000999 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001000 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001001
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001002 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001003 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001004 }
1005 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001006
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001007 // If we reached here, then we just ate the ( of the memory operand. Process
1008 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001009 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001010 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001011
Chris Lattner29ef9a22010-01-15 18:51:29 +00001012 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001013 SMLoc StartLoc, EndLoc;
1014 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001015 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001016 Error(StartLoc, "eiz and riz can only be used as index registers",
1017 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001018 return 0;
1019 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001020 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001021
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001022 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001023 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001024 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001025
1026 // Following the comma we should have either an index register, or a scale
1027 // value. We don't support the later form, but we want to parse it
1028 // correctly.
1029 //
1030 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001031 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001032 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001033 SMLoc L;
1034 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001035
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001036 if (getLexer().isNot(AsmToken::RParen)) {
1037 // Parse the scale amount:
1038 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001039 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001040 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001041 "expected comma in scale expression");
1042 return 0;
1043 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001044 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001045
1046 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001047 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001048
1049 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001050 if (getParser().ParseAbsoluteExpression(ScaleVal)){
1051 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001052 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001053 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001054
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001055 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001056 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1057 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1058 return 0;
1059 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001060 Scale = (unsigned)ScaleVal;
1061 }
1062 }
1063 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001064 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001065 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001066 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001067
1068 int64_t Value;
1069 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001070 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001071
Daniel Dunbaree910252010-08-24 19:13:38 +00001072 if (Value != 1)
1073 Warning(Loc, "scale factor without index register is ignored");
1074 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001075 }
1076 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001077
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001078 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001079 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001080 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001081 return 0;
1082 }
Sean Callanan18b83232010-01-19 21:44:56 +00001083 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001084 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001085
Kevin Enderby84faf652012-03-12 21:32:09 +00001086 // If we have both a base register and an index register make sure they are
1087 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001088 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001089 if (BaseReg != 0 && IndexReg != 0) {
1090 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001091 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1092 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001093 IndexReg != X86::RIZ) {
1094 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1095 return 0;
1096 }
1097 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001098 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1099 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001100 IndexReg != X86::EIZ){
1101 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1102 return 0;
1103 }
1104 }
1105
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001106 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1107 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001108}
1109
Devang Pateldd929fc2012-01-12 18:03:40 +00001110bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001111ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001112 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001113 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001114 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001115
Chris Lattnerd8f71792010-11-28 20:23:50 +00001116 // FIXME: Hack to recognize setneb as setne.
1117 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1118 PatchedName != "setb" && PatchedName != "setnb")
1119 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001120
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001121 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1122 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001123 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001124 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1125 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001126 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001127 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001128 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001129 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001130 .Case("eq", 0x00)
1131 .Case("lt", 0x01)
1132 .Case("le", 0x02)
1133 .Case("unord", 0x03)
1134 .Case("neq", 0x04)
1135 .Case("nlt", 0x05)
1136 .Case("nle", 0x06)
1137 .Case("ord", 0x07)
1138 /* AVX only from here */
1139 .Case("eq_uq", 0x08)
1140 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001141 .Case("ngt", 0x0A)
1142 .Case("false", 0x0B)
1143 .Case("neq_oq", 0x0C)
1144 .Case("ge", 0x0D)
1145 .Case("gt", 0x0E)
1146 .Case("true", 0x0F)
1147 .Case("eq_os", 0x10)
1148 .Case("lt_oq", 0x11)
1149 .Case("le_oq", 0x12)
1150 .Case("unord_s", 0x13)
1151 .Case("neq_us", 0x14)
1152 .Case("nlt_uq", 0x15)
1153 .Case("nle_uq", 0x16)
1154 .Case("ord_s", 0x17)
1155 .Case("eq_us", 0x18)
1156 .Case("nge_uq", 0x19)
1157 .Case("ngt_uq", 0x1A)
1158 .Case("false_os", 0x1B)
1159 .Case("neq_os", 0x1C)
1160 .Case("ge_oq", 0x1D)
1161 .Case("gt_oq", 0x1E)
1162 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001163 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001164 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001165 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1166 getParser().getContext());
1167 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001168 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001169 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001170 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001171 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001172 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001173 } else {
1174 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001175 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001176 }
1177 }
1178 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001179
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001180 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001181
Devang Patel885f65b2012-01-30 22:47:12 +00001182 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001183 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001184
Chris Lattner2544f422010-09-08 05:17:37 +00001185 // Determine whether this is an instruction prefix.
1186 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001187 Name == "lock" || Name == "rep" ||
1188 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001189 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001190 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001191
1192
Chris Lattner2544f422010-09-08 05:17:37 +00001193 // This does the actual operand parsing. Don't parse any more if we have a
1194 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1195 // just want to parse the "lock" as the first instruction and the "incl" as
1196 // the next one.
1197 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001198
1199 // Parse '*' modifier.
1200 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001201 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001202 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001203 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001204 }
1205
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001206 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001207 if (X86Operand *Op = ParseOperand())
1208 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001209 else {
1210 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001211 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001212 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001213
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001214 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001215 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001216
1217 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001218 if (X86Operand *Op = ParseOperand())
1219 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001220 else {
1221 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001222 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001223 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001224 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001225
Chris Lattnercbf8a982010-09-11 16:18:25 +00001226 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001227 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001228 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001229 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001230 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001231 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001232
Chris Lattner2544f422010-09-08 05:17:37 +00001233 if (getLexer().is(AsmToken::EndOfStatement))
1234 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001235 else if (isPrefix && getLexer().is(AsmToken::Slash))
1236 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001237
Devang Patel885f65b2012-01-30 22:47:12 +00001238 if (ExtraImmOp && isParsingIntelSyntax())
1239 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1240
Chris Lattner98c870f2010-11-06 19:25:43 +00001241 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1242 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1243 // documented form in various unofficial manuals, so a lot of code uses it.
1244 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1245 Operands.size() == 3) {
1246 X86Operand &Op = *(X86Operand*)Operands.back();
1247 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1248 isa<MCConstantExpr>(Op.Mem.Disp) &&
1249 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1250 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1251 SMLoc Loc = Op.getEndLoc();
1252 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1253 delete &Op;
1254 }
1255 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001256 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1257 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1258 Operands.size() == 3) {
1259 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1260 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1261 isa<MCConstantExpr>(Op.Mem.Disp) &&
1262 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1263 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1264 SMLoc Loc = Op.getEndLoc();
1265 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1266 delete &Op;
1267 }
1268 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001269 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1270 if (Name.startswith("ins") && Operands.size() == 3 &&
1271 (Name == "insb" || Name == "insw" || Name == "insl")) {
1272 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1273 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1274 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1275 Operands.pop_back();
1276 Operands.pop_back();
1277 delete &Op;
1278 delete &Op2;
1279 }
1280 }
1281
1282 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1283 if (Name.startswith("outs") && Operands.size() == 3 &&
1284 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1285 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1286 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1287 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1288 Operands.pop_back();
1289 Operands.pop_back();
1290 delete &Op;
1291 delete &Op2;
1292 }
1293 }
1294
1295 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1296 if (Name.startswith("movs") && Operands.size() == 3 &&
1297 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001298 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001299 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1300 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1301 if (isSrcOp(Op) && isDstOp(Op2)) {
1302 Operands.pop_back();
1303 Operands.pop_back();
1304 delete &Op;
1305 delete &Op2;
1306 }
1307 }
1308 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1309 if (Name.startswith("lods") && Operands.size() == 3 &&
1310 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001311 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001312 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1313 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1314 if (isSrcOp(*Op1) && Op2->isReg()) {
1315 const char *ins;
1316 unsigned reg = Op2->getReg();
1317 bool isLods = Name == "lods";
1318 if (reg == X86::AL && (isLods || Name == "lodsb"))
1319 ins = "lodsb";
1320 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1321 ins = "lodsw";
1322 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1323 ins = "lodsl";
1324 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1325 ins = "lodsq";
1326 else
1327 ins = NULL;
1328 if (ins != NULL) {
1329 Operands.pop_back();
1330 Operands.pop_back();
1331 delete Op1;
1332 delete Op2;
1333 if (Name != ins)
1334 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1335 }
1336 }
1337 }
1338 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1339 if (Name.startswith("stos") && Operands.size() == 3 &&
1340 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001341 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001342 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1343 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1344 if (isDstOp(*Op2) && Op1->isReg()) {
1345 const char *ins;
1346 unsigned reg = Op1->getReg();
1347 bool isStos = Name == "stos";
1348 if (reg == X86::AL && (isStos || Name == "stosb"))
1349 ins = "stosb";
1350 else if (reg == X86::AX && (isStos || Name == "stosw"))
1351 ins = "stosw";
1352 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1353 ins = "stosl";
1354 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1355 ins = "stosq";
1356 else
1357 ins = NULL;
1358 if (ins != NULL) {
1359 Operands.pop_back();
1360 Operands.pop_back();
1361 delete Op1;
1362 delete Op2;
1363 if (Name != ins)
1364 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1365 }
1366 }
1367 }
1368
Chris Lattnere9e16a32010-09-15 04:33:27 +00001369 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001370 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001371 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001372 Name.startswith("shl") || Name.startswith("sal") ||
1373 Name.startswith("rcl") || Name.startswith("rcr") ||
1374 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001375 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001376 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001377 // Intel syntax
1378 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1379 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001380 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1381 delete Operands[2];
1382 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001383 }
1384 } else {
1385 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1386 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001387 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1388 delete Operands[1];
1389 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001390 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001391 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001392 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001393
Chris Lattner15f89512011-04-09 19:41:05 +00001394 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1395 // instalias with an immediate operand yet.
1396 if (Name == "int" && Operands.size() == 2) {
1397 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1398 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1399 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1400 delete Operands[1];
1401 Operands.erase(Operands.begin() + 1);
1402 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1403 }
1404 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001405
Chris Lattner98986712010-01-14 22:21:20 +00001406 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001407}
1408
Devang Pateldd929fc2012-01-12 18:03:40 +00001409bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001410processInstruction(MCInst &Inst,
1411 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1412 switch (Inst.getOpcode()) {
1413 default: return false;
1414 case X86::AND16i16: {
1415 if (!Inst.getOperand(0).isImm() ||
1416 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1417 return false;
1418
1419 MCInst TmpInst;
1420 TmpInst.setOpcode(X86::AND16ri8);
1421 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1422 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1423 TmpInst.addOperand(Inst.getOperand(0));
1424 Inst = TmpInst;
1425 return true;
1426 }
1427 case X86::AND32i32: {
1428 if (!Inst.getOperand(0).isImm() ||
1429 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1430 return false;
1431
1432 MCInst TmpInst;
1433 TmpInst.setOpcode(X86::AND32ri8);
1434 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1435 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1436 TmpInst.addOperand(Inst.getOperand(0));
1437 Inst = TmpInst;
1438 return true;
1439 }
1440 case X86::AND64i32: {
1441 if (!Inst.getOperand(0).isImm() ||
1442 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1443 return false;
1444
1445 MCInst TmpInst;
1446 TmpInst.setOpcode(X86::AND64ri8);
1447 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1448 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1449 TmpInst.addOperand(Inst.getOperand(0));
1450 Inst = TmpInst;
1451 return true;
1452 }
Devang Patelac0f0482012-01-19 17:53:25 +00001453 case X86::XOR16i16: {
1454 if (!Inst.getOperand(0).isImm() ||
1455 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1456 return false;
1457
1458 MCInst TmpInst;
1459 TmpInst.setOpcode(X86::XOR16ri8);
1460 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1461 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1462 TmpInst.addOperand(Inst.getOperand(0));
1463 Inst = TmpInst;
1464 return true;
1465 }
1466 case X86::XOR32i32: {
1467 if (!Inst.getOperand(0).isImm() ||
1468 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1469 return false;
1470
1471 MCInst TmpInst;
1472 TmpInst.setOpcode(X86::XOR32ri8);
1473 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1474 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1475 TmpInst.addOperand(Inst.getOperand(0));
1476 Inst = TmpInst;
1477 return true;
1478 }
1479 case X86::XOR64i32: {
1480 if (!Inst.getOperand(0).isImm() ||
1481 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1482 return false;
1483
1484 MCInst TmpInst;
1485 TmpInst.setOpcode(X86::XOR64ri8);
1486 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1487 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1488 TmpInst.addOperand(Inst.getOperand(0));
1489 Inst = TmpInst;
1490 return true;
1491 }
1492 case X86::OR16i16: {
1493 if (!Inst.getOperand(0).isImm() ||
1494 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1495 return false;
1496
1497 MCInst TmpInst;
1498 TmpInst.setOpcode(X86::OR16ri8);
1499 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1500 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1501 TmpInst.addOperand(Inst.getOperand(0));
1502 Inst = TmpInst;
1503 return true;
1504 }
1505 case X86::OR32i32: {
1506 if (!Inst.getOperand(0).isImm() ||
1507 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1508 return false;
1509
1510 MCInst TmpInst;
1511 TmpInst.setOpcode(X86::OR32ri8);
1512 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1513 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1514 TmpInst.addOperand(Inst.getOperand(0));
1515 Inst = TmpInst;
1516 return true;
1517 }
1518 case X86::OR64i32: {
1519 if (!Inst.getOperand(0).isImm() ||
1520 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1521 return false;
1522
1523 MCInst TmpInst;
1524 TmpInst.setOpcode(X86::OR64ri8);
1525 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1526 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1527 TmpInst.addOperand(Inst.getOperand(0));
1528 Inst = TmpInst;
1529 return true;
1530 }
1531 case X86::CMP16i16: {
1532 if (!Inst.getOperand(0).isImm() ||
1533 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1534 return false;
1535
1536 MCInst TmpInst;
1537 TmpInst.setOpcode(X86::CMP16ri8);
1538 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1539 TmpInst.addOperand(Inst.getOperand(0));
1540 Inst = TmpInst;
1541 return true;
1542 }
1543 case X86::CMP32i32: {
1544 if (!Inst.getOperand(0).isImm() ||
1545 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1546 return false;
1547
1548 MCInst TmpInst;
1549 TmpInst.setOpcode(X86::CMP32ri8);
1550 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1551 TmpInst.addOperand(Inst.getOperand(0));
1552 Inst = TmpInst;
1553 return true;
1554 }
1555 case X86::CMP64i32: {
1556 if (!Inst.getOperand(0).isImm() ||
1557 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1558 return false;
1559
1560 MCInst TmpInst;
1561 TmpInst.setOpcode(X86::CMP64ri8);
1562 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1563 TmpInst.addOperand(Inst.getOperand(0));
1564 Inst = TmpInst;
1565 return true;
1566 }
Devang Patela951f772012-01-19 18:40:55 +00001567 case X86::ADD16i16: {
1568 if (!Inst.getOperand(0).isImm() ||
1569 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1570 return false;
1571
1572 MCInst TmpInst;
1573 TmpInst.setOpcode(X86::ADD16ri8);
1574 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1575 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1576 TmpInst.addOperand(Inst.getOperand(0));
1577 Inst = TmpInst;
1578 return true;
1579 }
1580 case X86::ADD32i32: {
1581 if (!Inst.getOperand(0).isImm() ||
1582 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1583 return false;
1584
1585 MCInst TmpInst;
1586 TmpInst.setOpcode(X86::ADD32ri8);
1587 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1588 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1589 TmpInst.addOperand(Inst.getOperand(0));
1590 Inst = TmpInst;
1591 return true;
1592 }
1593 case X86::ADD64i32: {
1594 if (!Inst.getOperand(0).isImm() ||
1595 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1596 return false;
1597
1598 MCInst TmpInst;
1599 TmpInst.setOpcode(X86::ADD64ri8);
1600 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1601 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1602 TmpInst.addOperand(Inst.getOperand(0));
1603 Inst = TmpInst;
1604 return true;
1605 }
1606 case X86::SUB16i16: {
1607 if (!Inst.getOperand(0).isImm() ||
1608 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1609 return false;
1610
1611 MCInst TmpInst;
1612 TmpInst.setOpcode(X86::SUB16ri8);
1613 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1614 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1615 TmpInst.addOperand(Inst.getOperand(0));
1616 Inst = TmpInst;
1617 return true;
1618 }
1619 case X86::SUB32i32: {
1620 if (!Inst.getOperand(0).isImm() ||
1621 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1622 return false;
1623
1624 MCInst TmpInst;
1625 TmpInst.setOpcode(X86::SUB32ri8);
1626 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1627 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1628 TmpInst.addOperand(Inst.getOperand(0));
1629 Inst = TmpInst;
1630 return true;
1631 }
1632 case X86::SUB64i32: {
1633 if (!Inst.getOperand(0).isImm() ||
1634 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1635 return false;
1636
1637 MCInst TmpInst;
1638 TmpInst.setOpcode(X86::SUB64ri8);
1639 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1640 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1641 TmpInst.addOperand(Inst.getOperand(0));
1642 Inst = TmpInst;
1643 return true;
1644 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001645 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001646}
1647
1648bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001649MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001650 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001651 MCStreamer &Out, unsigned &ErrorInfo,
1652 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001653 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001654 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1655 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001656 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001657
Chris Lattner7c51a312010-09-29 01:50:45 +00001658 // First, handle aliases that expand to multiple instructions.
1659 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001660 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001661 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001662 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001663 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001664 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001665 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001666 MCInst Inst;
1667 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001668 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001669 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001670 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001671
Chris Lattner0bb83a82010-09-30 16:39:29 +00001672 const char *Repl =
1673 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001674 .Case("finit", "fninit")
1675 .Case("fsave", "fnsave")
1676 .Case("fstcw", "fnstcw")
1677 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001678 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001679 .Case("fstsw", "fnstsw")
1680 .Case("fstsww", "fnstsw")
1681 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001682 .Default(0);
1683 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001684 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001685 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001686 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001687
Chris Lattnera008e8a2010-09-06 21:54:15 +00001688 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001689 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001690
Daniel Dunbarc918d602010-05-04 16:12:42 +00001691 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001692 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001693 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001694 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001695 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001696 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001697 // Some instructions need post-processing to, for example, tweak which
1698 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001699 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001700 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001701 while (processInstruction(Inst, Operands))
1702 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001703
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001704 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001705 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001706 Out.EmitInstruction(Inst);
1707 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001708 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001709 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001710 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001711 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001712 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001713 case Match_InvalidOperand:
1714 WasOriginallyInvalidOperand = true;
1715 break;
1716 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001717 break;
1718 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001719
Daniel Dunbarc918d602010-05-04 16:12:42 +00001720 // FIXME: Ideally, we would only attempt suffix matches for things which are
1721 // valid prefixes, and we could just infer the right unambiguous
1722 // type. However, that requires substantially more matcher support than the
1723 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001724
Daniel Dunbarc918d602010-05-04 16:12:42 +00001725 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001726 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001727 SmallString<16> Tmp;
1728 Tmp += Base;
1729 Tmp += ' ';
1730 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001731
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001732 // If this instruction starts with an 'f', then it is a floating point stack
1733 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1734 // 80-bit floating point, which use the suffixes s,l,t respectively.
1735 //
1736 // Otherwise, we assume that this may be an integer instruction, which comes
1737 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1738 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001739
Daniel Dunbarc918d602010-05-04 16:12:42 +00001740 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001741 Tmp[Base.size()] = Suffixes[0];
1742 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001743 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001744
Chad Rosier6e006d32012-10-12 22:53:36 +00001745 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1746 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001747 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001748 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1749 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001750 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001751 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1752 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001753 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001754 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1755 isParsingIntelSyntax());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001756
1757 // Restore the old token.
1758 Op->setTokenValue(Base);
1759
1760 // If exactly one matched, then we treat that as a successful match (and the
1761 // instruction will already have been filled in correctly, since the failing
1762 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001763 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001764 (Match1 == Match_Success) + (Match2 == Match_Success) +
1765 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001766 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001767 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001768 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001769 Out.EmitInstruction(Inst);
1770 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001771 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001772 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001773
Chris Lattnerec6789f2010-09-06 20:08:02 +00001774 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001775
Daniel Dunbar09062b12010-08-12 00:55:42 +00001776 // If we had multiple suffix matches, then identify this as an ambiguous
1777 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001778 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001779 char MatchChars[4];
1780 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001781 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1782 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1783 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1784 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001785
1786 SmallString<126> Msg;
1787 raw_svector_ostream OS(Msg);
1788 OS << "ambiguous instructions require an explicit suffix (could be ";
1789 for (unsigned i = 0; i != NumMatches; ++i) {
1790 if (i != 0)
1791 OS << ", ";
1792 if (i + 1 == NumMatches)
1793 OS << "or ";
1794 OS << "'" << Base << MatchChars[i] << "'";
1795 }
1796 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001797 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001798 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001799 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001800
Chris Lattnera008e8a2010-09-06 21:54:15 +00001801 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001802
Chris Lattnera008e8a2010-09-06 21:54:15 +00001803 // If all of the instructions reported an invalid mnemonic, then the original
1804 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001805 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1806 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001807 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001808 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001809 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001810 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001811 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001812 }
1813
1814 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001815 if (ErrorInfo != ~0U) {
1816 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001817 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001818 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001819
Chad Rosier84125ca2012-10-13 00:26:04 +00001820 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001821 if (Operand->getStartLoc().isValid()) {
1822 SMRange OperandRange = Operand->getLocRange();
1823 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001824 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001825 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001826 }
1827
Chad Rosierb4fdade2012-08-21 19:36:59 +00001828 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001829 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001830 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001831
Chris Lattnerec6789f2010-09-06 20:08:02 +00001832 // If one instruction matched with a missing feature, report this as a
1833 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001834 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1835 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001836 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001837 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001838 return true;
1839 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001840
Chris Lattnera008e8a2010-09-06 21:54:15 +00001841 // If one instruction matched with an invalid operand, report this as an
1842 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001843 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1844 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001845 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001846 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001847 return true;
1848 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001849
Chris Lattnerec6789f2010-09-06 20:08:02 +00001850 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001851 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001852 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001853 return true;
1854}
1855
1856
Devang Pateldd929fc2012-01-12 18:03:40 +00001857bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001858 StringRef IDVal = DirectiveID.getIdentifier();
1859 if (IDVal == ".word")
1860 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001861 else if (IDVal.startswith(".code"))
1862 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001863 else if (IDVal.startswith(".att_syntax")) {
1864 getParser().setAssemblerDialect(0);
1865 return false;
1866 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001867 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001868 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1869 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001870 // FIXME : Handle noprefix
1871 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001872 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001873 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001874 }
1875 return false;
1876 }
Chris Lattner537ca842010-10-30 17:38:55 +00001877 return true;
1878}
1879
1880/// ParseDirectiveWord
1881/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001882bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001883 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1884 for (;;) {
1885 const MCExpr *Value;
1886 if (getParser().ParseExpression(Value))
1887 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001888
Chris Lattner537ca842010-10-30 17:38:55 +00001889 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001890
Chris Lattner537ca842010-10-30 17:38:55 +00001891 if (getLexer().is(AsmToken::EndOfStatement))
1892 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001893
Chris Lattner537ca842010-10-30 17:38:55 +00001894 // FIXME: Improve diagnostic.
1895 if (getLexer().isNot(AsmToken::Comma))
1896 return Error(L, "unexpected token in directive");
1897 Parser.Lex();
1898 }
1899 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001900
Chris Lattner537ca842010-10-30 17:38:55 +00001901 Parser.Lex();
1902 return false;
1903}
1904
Evan Chengbd27f5a2011-07-27 00:38:12 +00001905/// ParseDirectiveCode
1906/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001907bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001908 if (IDVal == ".code32") {
1909 Parser.Lex();
1910 if (is64BitMode()) {
1911 SwitchMode();
1912 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1913 }
1914 } else if (IDVal == ".code64") {
1915 Parser.Lex();
1916 if (!is64BitMode()) {
1917 SwitchMode();
1918 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1919 }
1920 } else {
1921 return Error(L, "unexpected directive " + IDVal);
1922 }
Chris Lattner537ca842010-10-30 17:38:55 +00001923
Evan Chengbd27f5a2011-07-27 00:38:12 +00001924 return false;
1925}
Chris Lattner537ca842010-10-30 17:38:55 +00001926
1927
Sean Callanane88f5522010-01-23 02:43:15 +00001928extern "C" void LLVMInitializeX86AsmLexer();
1929
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001930// Force static initialization.
1931extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001932 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1933 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001934 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001935}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001936
Chris Lattner0692ee62010-09-06 19:11:01 +00001937#define GET_REGISTER_MATCHER
1938#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001939#include "X86GenAsmMatcher.inc"