blob: 7f47ef4c63ec2fa6659a9b4b60d8581e5e9760d9 [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();
Chad Rosierec130222012-10-25 21:51:10 +0000844 } else if (Tok.is(AsmToken::Identifier)) {
845 // We should only see an identifier when parsing the original inline asm.
846 // The front-end should rewrite this in terms of immediates.
847 assert (isParsingInlineAsm() && "Unexpected field name!");
848
849 unsigned DotDisp;
850 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
851 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
852 DotDisp)) {
853 Err = "Unable to lookup field reference!";
854 return true;
855 }
856 DotDispVal = DotDisp;
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000857 } else {
858 Err = "Unexpected token type!";
859 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +0000860 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000861
Chad Rosierec130222012-10-25 21:51:10 +0000862 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
863 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
864 unsigned Len = DotDispStr.size();
865 unsigned Val = OrigDispVal + DotDispVal;
866 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
867 Val));
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000868 }
869
870 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
871 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +0000872}
873
Chad Rosierc0a14b82012-10-24 17:22:29 +0000874/// Parse the 'offset' operator. This operator is used to specify the
875/// location rather then the content of a variable.
876X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
877 SMLoc OffsetOfLoc = Start;
878 Parser.Lex(); // Eat offset.
879 Start = Parser.getTok().getLoc();
880 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
881
882 SMLoc End;
883 const MCExpr *Val;
884 if (getParser().ParseExpression(Val, End))
885 return 0;
886
887 End = Parser.getTok().getLoc();
888
889 // The offset operator will have an 'r' constraint, thus we need to create
890 // register operand to ensure proper matching. Just pick a GPR based on
891 // the size of a pointer.
892 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
893 return X86Operand::CreateReg(RegNo, Start, End, OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +0000894}
895
896X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000897 SMLoc Start = Parser.getTok().getLoc(), End;
898
Chad Rosierc0a14b82012-10-24 17:22:29 +0000899 // offset operator.
900 const AsmToken &Tok = Parser.getTok();
901 if ((Tok.getString() == "offset" || Tok.getString() == "OFFSET") &&
902 isParsingInlineAsm())
903 return ParseIntelOffsetOfOperator(Start);
904
Devang Pateld37ad242012-01-17 18:00:18 +0000905 // immediate.
906 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
907 getLexer().is(AsmToken::Minus)) {
908 const MCExpr *Val;
909 if (!getParser().ParseExpression(Val, End)) {
910 End = Parser.getTok().getLoc();
911 return X86Operand::CreateImm(Val, Start, End);
912 }
913 }
914
Devang Patel0a338862012-01-12 01:36:43 +0000915 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000916 unsigned RegNo = 0;
917 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000918 // If this is a segment register followed by a ':', then this is the start
919 // of a memory reference, otherwise this is a normal register reference.
920 if (getLexer().isNot(AsmToken::Colon))
921 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
922
923 getParser().Lex(); // Eat the colon.
924 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000925 }
926
927 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000928 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000929}
930
Devang Pateldd929fc2012-01-12 18:03:40 +0000931X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000932 switch (getLexer().getKind()) {
933 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000934 // Parse a memory operand with no segment register.
935 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000936 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000937 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000938 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000939 SMLoc Start, End;
940 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000941 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000942 Error(Start, "%eiz and %riz can only be used as index registers",
943 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000944 return 0;
945 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000946
Chris Lattnereef6d782010-04-17 18:56:34 +0000947 // If this is a segment register followed by a ':', then this is the start
948 // of a memory reference, otherwise this is a normal register reference.
949 if (getLexer().isNot(AsmToken::Colon))
950 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000951
952
Chris Lattnereef6d782010-04-17 18:56:34 +0000953 getParser().Lex(); // Eat the colon.
954 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000955 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000956 case AsmToken::Dollar: {
957 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000958 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000959 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000960 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000961 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000962 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000963 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000964 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000965 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000966}
967
Chris Lattnereef6d782010-04-17 18:56:34 +0000968/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
969/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000970X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000971
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000972 // We have to disambiguate a parenthesized expression "(4+5)" from the start
973 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000974 // only way to do this without lookahead is to eat the '(' and see what is
975 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000976 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000977 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000978 SMLoc ExprEnd;
979 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000980
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000981 // After parsing the base expression we could either have a parenthesized
982 // memory address or not. If not, return now. If so, eat the (.
983 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000984 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000985 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000986 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000987 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000988 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000989
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000990 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000991 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000992 } else {
993 // Okay, we have a '('. We don't know if this is an expression or not, but
994 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000995 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000996 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000997
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000998 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000999 // Nothing to do here, fall into the code below with the '(' part of the
1000 // memory operand consumed.
1001 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001002 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001003
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001004 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001005 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +00001006 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001007
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001008 // After parsing the base expression we could either have a parenthesized
1009 // memory address or not. If not, return now. If so, eat the (.
1010 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001011 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001012 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001013 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001014 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001015 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001016
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001017 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001018 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001019 }
1020 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001021
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001022 // If we reached here, then we just ate the ( of the memory operand. Process
1023 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001024 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001025 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001026
Chris Lattner29ef9a22010-01-15 18:51:29 +00001027 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001028 SMLoc StartLoc, EndLoc;
1029 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001030 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001031 Error(StartLoc, "eiz and riz can only be used as index registers",
1032 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001033 return 0;
1034 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001035 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001036
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001037 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001038 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001039 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001040
1041 // Following the comma we should have either an index register, or a scale
1042 // value. We don't support the later form, but we want to parse it
1043 // correctly.
1044 //
1045 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001046 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001047 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001048 SMLoc L;
1049 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001050
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001051 if (getLexer().isNot(AsmToken::RParen)) {
1052 // Parse the scale amount:
1053 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001054 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001055 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001056 "expected comma in scale expression");
1057 return 0;
1058 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001059 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001060
1061 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001062 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001063
1064 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001065 if (getParser().ParseAbsoluteExpression(ScaleVal)){
1066 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001067 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001068 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001069
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001070 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001071 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1072 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1073 return 0;
1074 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001075 Scale = (unsigned)ScaleVal;
1076 }
1077 }
1078 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001079 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001080 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001081 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001082
1083 int64_t Value;
1084 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001085 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001086
Daniel Dunbaree910252010-08-24 19:13:38 +00001087 if (Value != 1)
1088 Warning(Loc, "scale factor without index register is ignored");
1089 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001090 }
1091 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001092
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001093 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001094 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001095 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001096 return 0;
1097 }
Sean Callanan18b83232010-01-19 21:44:56 +00001098 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001099 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001100
Kevin Enderby84faf652012-03-12 21:32:09 +00001101 // If we have both a base register and an index register make sure they are
1102 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001103 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001104 if (BaseReg != 0 && IndexReg != 0) {
1105 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001106 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1107 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001108 IndexReg != X86::RIZ) {
1109 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1110 return 0;
1111 }
1112 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001113 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1114 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001115 IndexReg != X86::EIZ){
1116 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1117 return 0;
1118 }
1119 }
1120
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001121 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1122 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001123}
1124
Devang Pateldd929fc2012-01-12 18:03:40 +00001125bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001126ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001127 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001128 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001129 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001130
Chris Lattnerd8f71792010-11-28 20:23:50 +00001131 // FIXME: Hack to recognize setneb as setne.
1132 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1133 PatchedName != "setb" && PatchedName != "setnb")
1134 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001135
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001136 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1137 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001138 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001139 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1140 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001141 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001142 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001143 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001144 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001145 .Case("eq", 0x00)
1146 .Case("lt", 0x01)
1147 .Case("le", 0x02)
1148 .Case("unord", 0x03)
1149 .Case("neq", 0x04)
1150 .Case("nlt", 0x05)
1151 .Case("nle", 0x06)
1152 .Case("ord", 0x07)
1153 /* AVX only from here */
1154 .Case("eq_uq", 0x08)
1155 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001156 .Case("ngt", 0x0A)
1157 .Case("false", 0x0B)
1158 .Case("neq_oq", 0x0C)
1159 .Case("ge", 0x0D)
1160 .Case("gt", 0x0E)
1161 .Case("true", 0x0F)
1162 .Case("eq_os", 0x10)
1163 .Case("lt_oq", 0x11)
1164 .Case("le_oq", 0x12)
1165 .Case("unord_s", 0x13)
1166 .Case("neq_us", 0x14)
1167 .Case("nlt_uq", 0x15)
1168 .Case("nle_uq", 0x16)
1169 .Case("ord_s", 0x17)
1170 .Case("eq_us", 0x18)
1171 .Case("nge_uq", 0x19)
1172 .Case("ngt_uq", 0x1A)
1173 .Case("false_os", 0x1B)
1174 .Case("neq_os", 0x1C)
1175 .Case("ge_oq", 0x1D)
1176 .Case("gt_oq", 0x1E)
1177 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001178 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001179 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001180 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1181 getParser().getContext());
1182 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001183 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001184 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001185 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001186 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001187 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001188 } else {
1189 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001190 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001191 }
1192 }
1193 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001194
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001195 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001196
Devang Patel885f65b2012-01-30 22:47:12 +00001197 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001198 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001199
Chris Lattner2544f422010-09-08 05:17:37 +00001200 // Determine whether this is an instruction prefix.
1201 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001202 Name == "lock" || Name == "rep" ||
1203 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001204 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001205 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001206
1207
Chris Lattner2544f422010-09-08 05:17:37 +00001208 // This does the actual operand parsing. Don't parse any more if we have a
1209 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1210 // just want to parse the "lock" as the first instruction and the "incl" as
1211 // the next one.
1212 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001213
1214 // Parse '*' modifier.
1215 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001216 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001217 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001218 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001219 }
1220
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001221 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001222 if (X86Operand *Op = ParseOperand())
1223 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001224 else {
1225 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001226 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001227 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001228
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001229 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001230 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001231
1232 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001233 if (X86Operand *Op = ParseOperand())
1234 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001235 else {
1236 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001237 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001238 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001239 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001240
Chris Lattnercbf8a982010-09-11 16:18:25 +00001241 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001242 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001243 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001244 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001245 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001246 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001247
Chris Lattner2544f422010-09-08 05:17:37 +00001248 if (getLexer().is(AsmToken::EndOfStatement))
1249 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001250 else if (isPrefix && getLexer().is(AsmToken::Slash))
1251 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001252
Devang Patel885f65b2012-01-30 22:47:12 +00001253 if (ExtraImmOp && isParsingIntelSyntax())
1254 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1255
Chris Lattner98c870f2010-11-06 19:25:43 +00001256 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1257 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1258 // documented form in various unofficial manuals, so a lot of code uses it.
1259 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1260 Operands.size() == 3) {
1261 X86Operand &Op = *(X86Operand*)Operands.back();
1262 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1263 isa<MCConstantExpr>(Op.Mem.Disp) &&
1264 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1265 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1266 SMLoc Loc = Op.getEndLoc();
1267 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1268 delete &Op;
1269 }
1270 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001271 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1272 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1273 Operands.size() == 3) {
1274 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1275 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1276 isa<MCConstantExpr>(Op.Mem.Disp) &&
1277 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1278 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1279 SMLoc Loc = Op.getEndLoc();
1280 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1281 delete &Op;
1282 }
1283 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001284 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1285 if (Name.startswith("ins") && Operands.size() == 3 &&
1286 (Name == "insb" || Name == "insw" || Name == "insl")) {
1287 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1288 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1289 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1290 Operands.pop_back();
1291 Operands.pop_back();
1292 delete &Op;
1293 delete &Op2;
1294 }
1295 }
1296
1297 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1298 if (Name.startswith("outs") && Operands.size() == 3 &&
1299 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1300 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1301 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1302 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1303 Operands.pop_back();
1304 Operands.pop_back();
1305 delete &Op;
1306 delete &Op2;
1307 }
1308 }
1309
1310 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1311 if (Name.startswith("movs") && Operands.size() == 3 &&
1312 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001313 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001314 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1315 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1316 if (isSrcOp(Op) && isDstOp(Op2)) {
1317 Operands.pop_back();
1318 Operands.pop_back();
1319 delete &Op;
1320 delete &Op2;
1321 }
1322 }
1323 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1324 if (Name.startswith("lods") && Operands.size() == 3 &&
1325 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001326 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001327 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1328 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1329 if (isSrcOp(*Op1) && Op2->isReg()) {
1330 const char *ins;
1331 unsigned reg = Op2->getReg();
1332 bool isLods = Name == "lods";
1333 if (reg == X86::AL && (isLods || Name == "lodsb"))
1334 ins = "lodsb";
1335 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1336 ins = "lodsw";
1337 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1338 ins = "lodsl";
1339 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1340 ins = "lodsq";
1341 else
1342 ins = NULL;
1343 if (ins != NULL) {
1344 Operands.pop_back();
1345 Operands.pop_back();
1346 delete Op1;
1347 delete Op2;
1348 if (Name != ins)
1349 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1350 }
1351 }
1352 }
1353 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1354 if (Name.startswith("stos") && Operands.size() == 3 &&
1355 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001356 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001357 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1358 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1359 if (isDstOp(*Op2) && Op1->isReg()) {
1360 const char *ins;
1361 unsigned reg = Op1->getReg();
1362 bool isStos = Name == "stos";
1363 if (reg == X86::AL && (isStos || Name == "stosb"))
1364 ins = "stosb";
1365 else if (reg == X86::AX && (isStos || Name == "stosw"))
1366 ins = "stosw";
1367 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1368 ins = "stosl";
1369 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1370 ins = "stosq";
1371 else
1372 ins = NULL;
1373 if (ins != NULL) {
1374 Operands.pop_back();
1375 Operands.pop_back();
1376 delete Op1;
1377 delete Op2;
1378 if (Name != ins)
1379 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1380 }
1381 }
1382 }
1383
Chris Lattnere9e16a32010-09-15 04:33:27 +00001384 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001385 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001386 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001387 Name.startswith("shl") || Name.startswith("sal") ||
1388 Name.startswith("rcl") || Name.startswith("rcr") ||
1389 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001390 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001391 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001392 // Intel syntax
1393 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1394 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001395 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1396 delete Operands[2];
1397 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001398 }
1399 } else {
1400 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1401 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001402 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1403 delete Operands[1];
1404 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001405 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001406 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001407 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001408
Chris Lattner15f89512011-04-09 19:41:05 +00001409 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1410 // instalias with an immediate operand yet.
1411 if (Name == "int" && Operands.size() == 2) {
1412 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1413 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1414 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1415 delete Operands[1];
1416 Operands.erase(Operands.begin() + 1);
1417 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1418 }
1419 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001420
Chris Lattner98986712010-01-14 22:21:20 +00001421 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001422}
1423
Devang Pateldd929fc2012-01-12 18:03:40 +00001424bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001425processInstruction(MCInst &Inst,
1426 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1427 switch (Inst.getOpcode()) {
1428 default: return false;
1429 case X86::AND16i16: {
1430 if (!Inst.getOperand(0).isImm() ||
1431 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1432 return false;
1433
1434 MCInst TmpInst;
1435 TmpInst.setOpcode(X86::AND16ri8);
1436 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1437 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1438 TmpInst.addOperand(Inst.getOperand(0));
1439 Inst = TmpInst;
1440 return true;
1441 }
1442 case X86::AND32i32: {
1443 if (!Inst.getOperand(0).isImm() ||
1444 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1445 return false;
1446
1447 MCInst TmpInst;
1448 TmpInst.setOpcode(X86::AND32ri8);
1449 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1450 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1451 TmpInst.addOperand(Inst.getOperand(0));
1452 Inst = TmpInst;
1453 return true;
1454 }
1455 case X86::AND64i32: {
1456 if (!Inst.getOperand(0).isImm() ||
1457 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1458 return false;
1459
1460 MCInst TmpInst;
1461 TmpInst.setOpcode(X86::AND64ri8);
1462 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1463 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1464 TmpInst.addOperand(Inst.getOperand(0));
1465 Inst = TmpInst;
1466 return true;
1467 }
Devang Patelac0f0482012-01-19 17:53:25 +00001468 case X86::XOR16i16: {
1469 if (!Inst.getOperand(0).isImm() ||
1470 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1471 return false;
1472
1473 MCInst TmpInst;
1474 TmpInst.setOpcode(X86::XOR16ri8);
1475 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1476 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1477 TmpInst.addOperand(Inst.getOperand(0));
1478 Inst = TmpInst;
1479 return true;
1480 }
1481 case X86::XOR32i32: {
1482 if (!Inst.getOperand(0).isImm() ||
1483 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1484 return false;
1485
1486 MCInst TmpInst;
1487 TmpInst.setOpcode(X86::XOR32ri8);
1488 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1489 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1490 TmpInst.addOperand(Inst.getOperand(0));
1491 Inst = TmpInst;
1492 return true;
1493 }
1494 case X86::XOR64i32: {
1495 if (!Inst.getOperand(0).isImm() ||
1496 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1497 return false;
1498
1499 MCInst TmpInst;
1500 TmpInst.setOpcode(X86::XOR64ri8);
1501 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1502 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1503 TmpInst.addOperand(Inst.getOperand(0));
1504 Inst = TmpInst;
1505 return true;
1506 }
1507 case X86::OR16i16: {
1508 if (!Inst.getOperand(0).isImm() ||
1509 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1510 return false;
1511
1512 MCInst TmpInst;
1513 TmpInst.setOpcode(X86::OR16ri8);
1514 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1515 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1516 TmpInst.addOperand(Inst.getOperand(0));
1517 Inst = TmpInst;
1518 return true;
1519 }
1520 case X86::OR32i32: {
1521 if (!Inst.getOperand(0).isImm() ||
1522 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1523 return false;
1524
1525 MCInst TmpInst;
1526 TmpInst.setOpcode(X86::OR32ri8);
1527 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1528 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1529 TmpInst.addOperand(Inst.getOperand(0));
1530 Inst = TmpInst;
1531 return true;
1532 }
1533 case X86::OR64i32: {
1534 if (!Inst.getOperand(0).isImm() ||
1535 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1536 return false;
1537
1538 MCInst TmpInst;
1539 TmpInst.setOpcode(X86::OR64ri8);
1540 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1541 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1542 TmpInst.addOperand(Inst.getOperand(0));
1543 Inst = TmpInst;
1544 return true;
1545 }
1546 case X86::CMP16i16: {
1547 if (!Inst.getOperand(0).isImm() ||
1548 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1549 return false;
1550
1551 MCInst TmpInst;
1552 TmpInst.setOpcode(X86::CMP16ri8);
1553 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1554 TmpInst.addOperand(Inst.getOperand(0));
1555 Inst = TmpInst;
1556 return true;
1557 }
1558 case X86::CMP32i32: {
1559 if (!Inst.getOperand(0).isImm() ||
1560 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1561 return false;
1562
1563 MCInst TmpInst;
1564 TmpInst.setOpcode(X86::CMP32ri8);
1565 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1566 TmpInst.addOperand(Inst.getOperand(0));
1567 Inst = TmpInst;
1568 return true;
1569 }
1570 case X86::CMP64i32: {
1571 if (!Inst.getOperand(0).isImm() ||
1572 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1573 return false;
1574
1575 MCInst TmpInst;
1576 TmpInst.setOpcode(X86::CMP64ri8);
1577 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1578 TmpInst.addOperand(Inst.getOperand(0));
1579 Inst = TmpInst;
1580 return true;
1581 }
Devang Patela951f772012-01-19 18:40:55 +00001582 case X86::ADD16i16: {
1583 if (!Inst.getOperand(0).isImm() ||
1584 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1585 return false;
1586
1587 MCInst TmpInst;
1588 TmpInst.setOpcode(X86::ADD16ri8);
1589 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1590 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1591 TmpInst.addOperand(Inst.getOperand(0));
1592 Inst = TmpInst;
1593 return true;
1594 }
1595 case X86::ADD32i32: {
1596 if (!Inst.getOperand(0).isImm() ||
1597 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1598 return false;
1599
1600 MCInst TmpInst;
1601 TmpInst.setOpcode(X86::ADD32ri8);
1602 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1603 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1604 TmpInst.addOperand(Inst.getOperand(0));
1605 Inst = TmpInst;
1606 return true;
1607 }
1608 case X86::ADD64i32: {
1609 if (!Inst.getOperand(0).isImm() ||
1610 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1611 return false;
1612
1613 MCInst TmpInst;
1614 TmpInst.setOpcode(X86::ADD64ri8);
1615 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1616 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1617 TmpInst.addOperand(Inst.getOperand(0));
1618 Inst = TmpInst;
1619 return true;
1620 }
1621 case X86::SUB16i16: {
1622 if (!Inst.getOperand(0).isImm() ||
1623 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1624 return false;
1625
1626 MCInst TmpInst;
1627 TmpInst.setOpcode(X86::SUB16ri8);
1628 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1629 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1630 TmpInst.addOperand(Inst.getOperand(0));
1631 Inst = TmpInst;
1632 return true;
1633 }
1634 case X86::SUB32i32: {
1635 if (!Inst.getOperand(0).isImm() ||
1636 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1637 return false;
1638
1639 MCInst TmpInst;
1640 TmpInst.setOpcode(X86::SUB32ri8);
1641 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1642 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1643 TmpInst.addOperand(Inst.getOperand(0));
1644 Inst = TmpInst;
1645 return true;
1646 }
1647 case X86::SUB64i32: {
1648 if (!Inst.getOperand(0).isImm() ||
1649 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1650 return false;
1651
1652 MCInst TmpInst;
1653 TmpInst.setOpcode(X86::SUB64ri8);
1654 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1655 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1656 TmpInst.addOperand(Inst.getOperand(0));
1657 Inst = TmpInst;
1658 return true;
1659 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001660 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001661}
1662
1663bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001664MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001665 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001666 MCStreamer &Out, unsigned &ErrorInfo,
1667 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001668 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001669 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1670 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001671 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001672
Chris Lattner7c51a312010-09-29 01:50:45 +00001673 // First, handle aliases that expand to multiple instructions.
1674 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001675 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001676 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001677 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001678 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001679 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001680 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001681 MCInst Inst;
1682 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001683 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001684 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001685 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001686
Chris Lattner0bb83a82010-09-30 16:39:29 +00001687 const char *Repl =
1688 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001689 .Case("finit", "fninit")
1690 .Case("fsave", "fnsave")
1691 .Case("fstcw", "fnstcw")
1692 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001693 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001694 .Case("fstsw", "fnstsw")
1695 .Case("fstsww", "fnstsw")
1696 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001697 .Default(0);
1698 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001699 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001700 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001701 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001702
Chris Lattnera008e8a2010-09-06 21:54:15 +00001703 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001704 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001705
Daniel Dunbarc918d602010-05-04 16:12:42 +00001706 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001707 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001708 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001709 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001710 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001711 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001712 // Some instructions need post-processing to, for example, tweak which
1713 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001714 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001715 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001716 while (processInstruction(Inst, Operands))
1717 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001718
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001719 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001720 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001721 Out.EmitInstruction(Inst);
1722 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001723 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001724 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001725 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001726 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001727 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001728 case Match_InvalidOperand:
1729 WasOriginallyInvalidOperand = true;
1730 break;
1731 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001732 break;
1733 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001734
Daniel Dunbarc918d602010-05-04 16:12:42 +00001735 // FIXME: Ideally, we would only attempt suffix matches for things which are
1736 // valid prefixes, and we could just infer the right unambiguous
1737 // type. However, that requires substantially more matcher support than the
1738 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001739
Daniel Dunbarc918d602010-05-04 16:12:42 +00001740 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001741 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001742 SmallString<16> Tmp;
1743 Tmp += Base;
1744 Tmp += ' ';
1745 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001746
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001747 // If this instruction starts with an 'f', then it is a floating point stack
1748 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1749 // 80-bit floating point, which use the suffixes s,l,t respectively.
1750 //
1751 // Otherwise, we assume that this may be an integer instruction, which comes
1752 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1753 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001754
Daniel Dunbarc918d602010-05-04 16:12:42 +00001755 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001756 Tmp[Base.size()] = Suffixes[0];
1757 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001758 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001759
Chad Rosier6e006d32012-10-12 22:53:36 +00001760 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1761 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001762 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001763 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1764 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001765 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001766 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1767 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001768 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001769 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1770 isParsingIntelSyntax());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001771
1772 // Restore the old token.
1773 Op->setTokenValue(Base);
1774
1775 // If exactly one matched, then we treat that as a successful match (and the
1776 // instruction will already have been filled in correctly, since the failing
1777 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001778 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001779 (Match1 == Match_Success) + (Match2 == Match_Success) +
1780 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001781 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001782 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001783 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001784 Out.EmitInstruction(Inst);
1785 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001786 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001787 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001788
Chris Lattnerec6789f2010-09-06 20:08:02 +00001789 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001790
Daniel Dunbar09062b12010-08-12 00:55:42 +00001791 // If we had multiple suffix matches, then identify this as an ambiguous
1792 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001793 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001794 char MatchChars[4];
1795 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001796 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1797 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1798 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1799 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001800
1801 SmallString<126> Msg;
1802 raw_svector_ostream OS(Msg);
1803 OS << "ambiguous instructions require an explicit suffix (could be ";
1804 for (unsigned i = 0; i != NumMatches; ++i) {
1805 if (i != 0)
1806 OS << ", ";
1807 if (i + 1 == NumMatches)
1808 OS << "or ";
1809 OS << "'" << Base << MatchChars[i] << "'";
1810 }
1811 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001812 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001813 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001814 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001815
Chris Lattnera008e8a2010-09-06 21:54:15 +00001816 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001817
Chris Lattnera008e8a2010-09-06 21:54:15 +00001818 // If all of the instructions reported an invalid mnemonic, then the original
1819 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001820 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1821 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001822 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001823 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001824 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001825 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001826 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001827 }
1828
1829 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001830 if (ErrorInfo != ~0U) {
1831 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001832 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001833 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001834
Chad Rosier84125ca2012-10-13 00:26:04 +00001835 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001836 if (Operand->getStartLoc().isValid()) {
1837 SMRange OperandRange = Operand->getLocRange();
1838 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001839 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001840 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001841 }
1842
Chad Rosierb4fdade2012-08-21 19:36:59 +00001843 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001844 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001845 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001846
Chris Lattnerec6789f2010-09-06 20:08:02 +00001847 // If one instruction matched with a missing feature, report this as a
1848 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001849 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1850 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001851 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001852 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001853 return true;
1854 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001855
Chris Lattnera008e8a2010-09-06 21:54:15 +00001856 // If one instruction matched with an invalid operand, report this as an
1857 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001858 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1859 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001860 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001861 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001862 return true;
1863 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001864
Chris Lattnerec6789f2010-09-06 20:08:02 +00001865 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001866 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001867 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001868 return true;
1869}
1870
1871
Devang Pateldd929fc2012-01-12 18:03:40 +00001872bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001873 StringRef IDVal = DirectiveID.getIdentifier();
1874 if (IDVal == ".word")
1875 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001876 else if (IDVal.startswith(".code"))
1877 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001878 else if (IDVal.startswith(".att_syntax")) {
1879 getParser().setAssemblerDialect(0);
1880 return false;
1881 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001882 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001883 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1884 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001885 // FIXME : Handle noprefix
1886 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001887 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001888 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001889 }
1890 return false;
1891 }
Chris Lattner537ca842010-10-30 17:38:55 +00001892 return true;
1893}
1894
1895/// ParseDirectiveWord
1896/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001897bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001898 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1899 for (;;) {
1900 const MCExpr *Value;
1901 if (getParser().ParseExpression(Value))
1902 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001903
Chris Lattner537ca842010-10-30 17:38:55 +00001904 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001905
Chris Lattner537ca842010-10-30 17:38:55 +00001906 if (getLexer().is(AsmToken::EndOfStatement))
1907 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001908
Chris Lattner537ca842010-10-30 17:38:55 +00001909 // FIXME: Improve diagnostic.
1910 if (getLexer().isNot(AsmToken::Comma))
1911 return Error(L, "unexpected token in directive");
1912 Parser.Lex();
1913 }
1914 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001915
Chris Lattner537ca842010-10-30 17:38:55 +00001916 Parser.Lex();
1917 return false;
1918}
1919
Evan Chengbd27f5a2011-07-27 00:38:12 +00001920/// ParseDirectiveCode
1921/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001922bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001923 if (IDVal == ".code32") {
1924 Parser.Lex();
1925 if (is64BitMode()) {
1926 SwitchMode();
1927 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1928 }
1929 } else if (IDVal == ".code64") {
1930 Parser.Lex();
1931 if (!is64BitMode()) {
1932 SwitchMode();
1933 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1934 }
1935 } else {
1936 return Error(L, "unexpected directive " + IDVal);
1937 }
Chris Lattner537ca842010-10-30 17:38:55 +00001938
Evan Chengbd27f5a2011-07-27 00:38:12 +00001939 return false;
1940}
Chris Lattner537ca842010-10-30 17:38:55 +00001941
1942
Sean Callanane88f5522010-01-23 02:43:15 +00001943extern "C" void LLVMInitializeX86AsmLexer();
1944
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001945// Force static initialization.
1946extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001947 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1948 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001949 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001950}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001951
Chris Lattner0692ee62010-09-06 19:11:01 +00001952#define GET_REGISTER_MATCHER
1953#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001954#include "X86GenAsmMatcher.inc"