blob: 3470443240c1038fd8ed30bf6f9b9a6063c59eaa [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"
Chris Lattner33d60d52010-09-22 04:11:10 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000023#include "llvm/ADT/StringSwitch.h"
24#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000025#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000027#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000028
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000029using namespace llvm;
30
31namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000032struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000033
Devang Pateldd929fc2012-01-12 18:03:40 +000034class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000035 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036 MCAsmParser &Parser;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038 MCAsmParser &getParser() const { return Parser; }
39
40 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
41
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000042 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000043 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000044 bool MatchingInlineAsm = false) {
45 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000046 return Parser.Error(L, Msg, Ranges);
47 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000048
Devang Pateld37ad242012-01-17 18:00:18 +000049 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
50 Error(Loc, Msg);
51 return 0;
52 }
53
Chris Lattner309264d2010-01-15 18:44:13 +000054 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000055 X86Operand *ParseATTOperand();
56 X86Operand *ParseIntelOperand();
Chad Rosierc0a14b82012-10-24 17:22:29 +000057 X86Operand *ParseIntelOffsetOfOperator(SMLoc StartLoc);
Chad Rosier5b0f1b32012-10-04 23:59:38 +000058 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000059 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000060 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000061
62 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000063 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000064
Devang Patelb8ba13f2012-01-18 22:42:29 +000065 bool processInstruction(MCInst &Inst,
66 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
67
Chad Rosier84125ca2012-10-13 00:26:04 +000068 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000069 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000070 MCStreamer &Out, unsigned &ErrorInfo,
71 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000072
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000073 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000074 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000075 bool isSrcOp(X86Operand &Op);
76
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000077 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
78 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000079 bool isDstOp(X86Operand &Op);
80
Evan Cheng59ee62d2011-07-11 03:57:24 +000081 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000082 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000083 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000084 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000085 void SwitchMode() {
86 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
87 setAvailableFeatures(FB);
88 }
Evan Chengebdeeab2011-07-08 01:53:10 +000089
Daniel Dunbar54074b52010-07-19 05:44:09 +000090 /// @name Auto-generated Matcher Functions
91 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000092
Chris Lattner0692ee62010-09-06 19:11:01 +000093#define GET_ASSEMBLER_HEADER
94#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000095
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000096 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000097
98public:
Devang Pateldd929fc2012-01-12 18:03:40 +000099 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +0000100 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000101
Daniel Dunbar54074b52010-07-19 05:44:09 +0000102 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000103 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000104 }
Roman Divackybf755322011-01-27 17:14:22 +0000105 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000106
Benjamin Kramer38e59892010-07-14 22:38:02 +0000107 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000108 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000109
110 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000111
112 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000113 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000114 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000115};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000116} // end anonymous namespace
117
Sean Callanane9b466d2010-01-23 00:40:33 +0000118/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000119/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000120
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000121static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000122
123/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000124
Craig Topper76bd9382012-07-18 04:59:16 +0000125static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000126 return (( Value <= 0x000000000000007FULL)||
127 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
128 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
129}
130
131static bool isImmSExti32i8Value(uint64_t Value) {
132 return (( Value <= 0x000000000000007FULL)||
133 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
134 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
135}
136
137static bool isImmZExtu32u8Value(uint64_t Value) {
138 return (Value <= 0x00000000000000FFULL);
139}
140
141static bool isImmSExti64i8Value(uint64_t Value) {
142 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000143 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000144}
145
146static bool isImmSExti64i32Value(uint64_t Value) {
147 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000148 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000149}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000150namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000151
152/// X86Operand - Instances of this class represent a parsed X86 machine
153/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000154struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000155 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000156 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000157 Register,
158 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000159 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000160 } Kind;
161
Chris Lattner29ef9a22010-01-15 18:51:29 +0000162 SMLoc StartLoc, EndLoc;
Chad Rosier5a719fc2012-10-23 17:43:43 +0000163 SMLoc OffsetOfLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000164
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000165 union {
166 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000167 const char *Data;
168 unsigned Length;
169 } Tok;
170
171 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000172 unsigned RegNo;
173 } Reg;
174
175 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000176 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000177 } Imm;
178
179 struct {
180 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000181 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000182 unsigned BaseReg;
183 unsigned IndexReg;
184 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000185 unsigned Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000186 bool NeedSizeDir;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000187 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000188 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000189
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000190 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000191 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000192
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000193 /// getStartLoc - Get the location of the first token of this operand.
194 SMLoc getStartLoc() const { return StartLoc; }
195 /// getEndLoc - Get the location of the last token of this operand.
196 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000197 /// getLocRange - Get the range between the first and last token of this
198 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000199 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000200 /// getOffsetOfLoc - Get the location of the offset operator.
201 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000202
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000203 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000204
Daniel Dunbar20927f22009-08-07 08:26:05 +0000205 StringRef getToken() const {
206 assert(Kind == Token && "Invalid access!");
207 return StringRef(Tok.Data, Tok.Length);
208 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000209 void setTokenValue(StringRef Value) {
210 assert(Kind == Token && "Invalid access!");
211 Tok.Data = Value.data();
212 Tok.Length = Value.size();
213 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000214
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000215 unsigned getReg() const {
216 assert(Kind == Register && "Invalid access!");
217 return Reg.RegNo;
218 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000219
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000220 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000221 assert(Kind == Immediate && "Invalid access!");
222 return Imm.Val;
223 }
224
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000225 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000226 assert(Kind == Memory && "Invalid access!");
227 return Mem.Disp;
228 }
229 unsigned getMemSegReg() const {
230 assert(Kind == Memory && "Invalid access!");
231 return Mem.SegReg;
232 }
233 unsigned getMemBaseReg() const {
234 assert(Kind == Memory && "Invalid access!");
235 return Mem.BaseReg;
236 }
237 unsigned getMemIndexReg() const {
238 assert(Kind == Memory && "Invalid access!");
239 return Mem.IndexReg;
240 }
241 unsigned getMemScale() const {
242 assert(Kind == Memory && "Invalid access!");
243 return Mem.Scale;
244 }
245
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000246 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000247
248 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000249
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000250 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000251 if (!isImm())
252 return false;
253
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000254 // If this isn't a constant expr, just assume it fits and let relaxation
255 // handle it.
256 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
257 if (!CE)
258 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000259
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000260 // Otherwise, check the value is in a range that makes sense for this
261 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000262 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000263 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000264 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000265 if (!isImm())
266 return false;
267
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000268 // If this isn't a constant expr, just assume it fits and let relaxation
269 // handle it.
270 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
271 if (!CE)
272 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000273
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000274 // Otherwise, check the value is in a range that makes sense for this
275 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000276 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000277 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000278 bool isImmZExtu32u8() const {
279 if (!isImm())
280 return false;
281
282 // If this isn't a constant expr, just assume it fits and let relaxation
283 // handle it.
284 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
285 if (!CE)
286 return true;
287
288 // Otherwise, check the value is in a range that makes sense for this
289 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000290 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000291 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000292 bool isImmSExti64i8() const {
293 if (!isImm())
294 return false;
295
296 // If this isn't a constant expr, just assume it fits and let relaxation
297 // handle it.
298 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
299 if (!CE)
300 return true;
301
302 // Otherwise, check the value is in a range that makes sense for this
303 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000304 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000305 }
306 bool isImmSExti64i32() const {
307 if (!isImm())
308 return false;
309
310 // If this isn't a constant expr, just assume it fits and let relaxation
311 // handle it.
312 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
313 if (!CE)
314 return true;
315
316 // Otherwise, check the value is in a range that makes sense for this
317 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000318 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000319 }
320
Chad Rosier96d58e62012-10-19 20:57:14 +0000321 unsigned getMemSize() const {
322 assert(Kind == Memory && "Invalid access!");
323 return Mem.Size;
324 }
325
Chad Rosiera703fb92012-10-22 19:50:35 +0000326 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000327 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000328 }
329
Chad Rosier96d58e62012-10-19 20:57:14 +0000330 bool needSizeDirective() const {
331 assert(Kind == Memory && "Invalid access!");
332 return Mem.NeedSizeDir;
333 }
334
Daniel Dunbar20927f22009-08-07 08:26:05 +0000335 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000336 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000337 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000338 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000339 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000340 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000341 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000342 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000343 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000344 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000345 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000346 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000347 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000348 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000349 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000350 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000351 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000352 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000353 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000354 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000355 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000356 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000357
Craig Topper75dc33a2012-07-18 04:11:12 +0000358 bool isMemVX32() const {
359 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
360 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
361 }
362 bool isMemVY32() const {
363 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
364 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
365 }
366 bool isMemVX64() const {
367 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
368 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
369 }
370 bool isMemVY64() const {
371 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
372 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
373 }
374
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000375 bool isAbsMem() const {
376 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000377 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000378 }
379
Daniel Dunbar20927f22009-08-07 08:26:05 +0000380 bool isReg() const { return Kind == Register; }
381
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000382 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
383 // Add as immediates when possible.
384 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
385 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
386 else
387 Inst.addOperand(MCOperand::CreateExpr(Expr));
388 }
389
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000390 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000391 assert(N == 1 && "Invalid number of operands!");
392 Inst.addOperand(MCOperand::CreateReg(getReg()));
393 }
394
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000395 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000396 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000397 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000398 }
399
Chad Rosier36b8fed2012-06-27 22:34:28 +0000400 void addMem8Operands(MCInst &Inst, unsigned N) const {
401 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000402 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000403 void addMem16Operands(MCInst &Inst, unsigned N) const {
404 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000405 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000406 void addMem32Operands(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 addMem64Operands(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 addMem80Operands(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 addMem128Operands(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 addMem256Operands(MCInst &Inst, unsigned N) const {
419 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000420 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000421 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
422 addMemOperands(Inst, N);
423 }
424 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
425 addMemOperands(Inst, N);
426 }
427 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
428 addMemOperands(Inst, N);
429 }
430 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
431 addMemOperands(Inst, N);
432 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000433
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000434 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000435 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000436 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
437 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
438 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000439 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000440 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
441 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000442
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000443 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
444 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000445 // Add as immediates when possible.
446 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
447 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
448 else
449 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000450 }
451
Chris Lattnerb4307b32010-01-15 19:28:38 +0000452 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000453 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
454 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000455 Res->Tok.Data = Str.data();
456 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000457 return Res;
458 }
459
Chad Rosierc0a14b82012-10-24 17:22:29 +0000460 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
461 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000462 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000463 Res->Reg.RegNo = RegNo;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000464 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000465 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000466 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000467
Chris Lattnerb4307b32010-01-15 19:28:38 +0000468 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
469 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000470 Res->Imm.Val = Val;
471 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000472 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000473
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000474 /// Create an absolute memory operand.
475 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000476 SMLoc EndLoc, unsigned Size = 0,
Chad Rosier5a719fc2012-10-23 17:43:43 +0000477 bool NeedSizeDir = false){
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000478 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
479 Res->Mem.SegReg = 0;
480 Res->Mem.Disp = Disp;
481 Res->Mem.BaseReg = 0;
482 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000483 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000484 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000485 Res->Mem.NeedSizeDir = NeedSizeDir;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000486 return Res;
487 }
488
489 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000490 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
491 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000492 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000493 unsigned Size = 0, bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000494 // We should never just have a displacement, that should be parsed as an
495 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000496 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
497
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000498 // The scale should always be one of {1,2,4,8}.
499 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000500 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000501 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000502 Res->Mem.SegReg = SegReg;
503 Res->Mem.Disp = Disp;
504 Res->Mem.BaseReg = BaseReg;
505 Res->Mem.IndexReg = IndexReg;
506 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000507 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000508 Res->Mem.NeedSizeDir = NeedSizeDir;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000509 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000510 }
511};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000512
Chris Lattner37dfdec2009-07-29 06:33:53 +0000513} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000514
Devang Pateldd929fc2012-01-12 18:03:40 +0000515bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000516 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000517
518 return (Op.isMem() &&
519 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
520 isa<MCConstantExpr>(Op.Mem.Disp) &&
521 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
522 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
523}
524
Devang Pateldd929fc2012-01-12 18:03:40 +0000525bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000526 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000527
Chad Rosier36b8fed2012-06-27 22:34:28 +0000528 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000529 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000530 isa<MCConstantExpr>(Op.Mem.Disp) &&
531 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
532 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
533}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000534
Devang Pateldd929fc2012-01-12 18:03:40 +0000535bool X86AsmParser::ParseRegister(unsigned &RegNo,
536 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000537 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000538 const AsmToken &PercentTok = Parser.getTok();
539 StartLoc = PercentTok.getLoc();
540
541 // If we encounter a %, ignore it. This code handles registers with and
542 // without the prefix, unprefixed registers can occur in cfi directives.
543 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000544 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000545
Sean Callanan18b83232010-01-19 21:44:56 +0000546 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000547 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000548 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000549 return Error(StartLoc, "invalid register name",
550 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000551 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000552
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000553 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000554
Chris Lattner33d60d52010-09-22 04:11:10 +0000555 // If the match failed, try the register name as lowercase.
556 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000557 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000558
Evan Cheng5de728c2011-07-27 23:22:03 +0000559 if (!is64BitMode()) {
560 // FIXME: This should be done using Requires<In32BitMode> and
561 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
562 // checked.
563 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
564 // REX prefix.
565 if (RegNo == X86::RIZ ||
566 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
567 X86II::isX86_64NonExtLowByteReg(RegNo) ||
568 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000569 return Error(StartLoc, "register %"
570 + Tok.getString() + " is only available in 64-bit mode",
571 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000572 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000573
Chris Lattner33d60d52010-09-22 04:11:10 +0000574 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
575 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000576 RegNo = X86::ST0;
577 EndLoc = Tok.getLoc();
578 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000579
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000580 // Check to see if we have '(4)' after %st.
581 if (getLexer().isNot(AsmToken::LParen))
582 return false;
583 // Lex the paren.
584 getParser().Lex();
585
586 const AsmToken &IntTok = Parser.getTok();
587 if (IntTok.isNot(AsmToken::Integer))
588 return Error(IntTok.getLoc(), "expected stack index");
589 switch (IntTok.getIntVal()) {
590 case 0: RegNo = X86::ST0; break;
591 case 1: RegNo = X86::ST1; break;
592 case 2: RegNo = X86::ST2; break;
593 case 3: RegNo = X86::ST3; break;
594 case 4: RegNo = X86::ST4; break;
595 case 5: RegNo = X86::ST5; break;
596 case 6: RegNo = X86::ST6; break;
597 case 7: RegNo = X86::ST7; break;
598 default: return Error(IntTok.getLoc(), "invalid stack index");
599 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000600
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000601 if (getParser().Lex().isNot(AsmToken::RParen))
602 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000603
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000604 EndLoc = Tok.getLoc();
605 Parser.Lex(); // Eat ')'
606 return false;
607 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000608
Chris Lattner645b2092010-06-24 07:29:18 +0000609 // If this is "db[0-7]", match it as an alias
610 // for dr[0-7].
611 if (RegNo == 0 && Tok.getString().size() == 3 &&
612 Tok.getString().startswith("db")) {
613 switch (Tok.getString()[2]) {
614 case '0': RegNo = X86::DR0; break;
615 case '1': RegNo = X86::DR1; break;
616 case '2': RegNo = X86::DR2; break;
617 case '3': RegNo = X86::DR3; break;
618 case '4': RegNo = X86::DR4; break;
619 case '5': RegNo = X86::DR5; break;
620 case '6': RegNo = X86::DR6; break;
621 case '7': RegNo = X86::DR7; break;
622 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000623
Chris Lattner645b2092010-06-24 07:29:18 +0000624 if (RegNo != 0) {
625 EndLoc = Tok.getLoc();
626 Parser.Lex(); // Eat it.
627 return false;
628 }
629 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000630
Devang Patel1aea4302012-01-20 22:32:05 +0000631 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000632 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000633 return Error(StartLoc, "invalid register name",
634 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000635 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000636
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000637 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000638 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000639 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000640}
641
Devang Pateldd929fc2012-01-12 18:03:40 +0000642X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000643 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000644 return ParseIntelOperand();
645 return ParseATTOperand();
646}
647
Devang Pateld37ad242012-01-17 18:00:18 +0000648/// getIntelMemOperandSize - Return intel memory operand size.
649static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000650 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000651 .Cases("BYTE", "byte", 8)
652 .Cases("WORD", "word", 16)
653 .Cases("DWORD", "dword", 32)
654 .Cases("QWORD", "qword", 64)
655 .Cases("XWORD", "xword", 80)
656 .Cases("XMMWORD", "xmmword", 128)
657 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000658 .Default(0);
659 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000660}
661
Chad Rosier65c88922012-10-22 19:42:52 +0000662X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000663 unsigned Size) {
664 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000665 SMLoc Start = Parser.getTok().getLoc(), End;
Devang Patel0a338862012-01-12 01:36:43 +0000666
Devang Pateld37ad242012-01-17 18:00:18 +0000667 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
668 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
669
670 // Eat '['
671 if (getLexer().isNot(AsmToken::LBrac))
672 return ErrorOperand(Start, "Expected '[' token!");
673 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000674
Devang Pateld37ad242012-01-17 18:00:18 +0000675 if (getLexer().is(AsmToken::Identifier)) {
676 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000677 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000678 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000679 if (getParser().ParseExpression(Disp, End)) return 0;
680 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000681 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000682 Parser.Lex();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000683 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000684 }
685 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000686 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000687 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000688 SMLoc Loc = Parser.getTok().getLoc();
689 if (getLexer().is(AsmToken::RBrac)) {
690 // Handle '[' number ']'
691 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000692 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
693 if (SegReg)
694 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000695 Start, End, Size);
696 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000697 } else if (getLexer().is(AsmToken::Star)) {
698 // Handle '[' Scale*IndexReg ']'
699 Parser.Lex();
700 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000701 if (ParseRegister(IndexReg, IdxRegLoc, End))
702 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000703 Scale = Val;
704 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000705 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000706 }
707
708 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
709 bool isPlus = getLexer().is(AsmToken::Plus);
710 Parser.Lex();
711 SMLoc PlusLoc = Parser.getTok().getLoc();
712 if (getLexer().is(AsmToken::Integer)) {
713 int64_t Val = Parser.getTok().getIntVal();
714 Parser.Lex();
715 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000716 Parser.Lex();
717 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000718 if (ParseRegister(IndexReg, IdxRegLoc, End))
719 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000720 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000721 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000722 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000723 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000724 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000725 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000726 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000727 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000728 End = Parser.getTok().getLoc();
729 if (!IndexReg)
730 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000731 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000732 }
Devang Pateld37ad242012-01-17 18:00:18 +0000733 }
734
735 if (getLexer().isNot(AsmToken::RBrac))
736 if (getParser().ParseExpression(Disp, End)) return 0;
737
738 End = Parser.getTok().getLoc();
739 if (getLexer().isNot(AsmToken::RBrac))
740 return ErrorOperand(End, "expected ']' token!");
741 Parser.Lex();
742 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000743
744 // handle [-42]
745 if (!BaseReg && !IndexReg)
Chad Rosierc0a14b82012-10-24 17:22:29 +0000746 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patelfdd3b302012-01-20 21:21:01 +0000747
Devang Pateld37ad242012-01-17 18:00:18 +0000748 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000749 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000750}
751
752/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000753X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000754 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000755 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000756
757 unsigned Size = getIntelMemOperandSize(Tok.getString());
758 if (Size) {
759 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000760 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
761 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000762 Parser.Lex();
763 }
764
Chad Rosierc0a14b82012-10-24 17:22:29 +0000765 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000766 return ParseIntelBracExpression(SegReg, Size);
767
768 if (!ParseRegister(SegReg, Start, End)) {
769 // Handel SegReg : [ ... ]
770 if (getLexer().isNot(AsmToken::Colon))
771 return ErrorOperand(Start, "Expected ':' token!");
772 Parser.Lex(); // Eat :
773 if (getLexer().isNot(AsmToken::LBrac))
774 return ErrorOperand(Start, "Expected '[' token!");
775 return ParseIntelBracExpression(SegReg, Size);
776 }
Devang Pateld37ad242012-01-17 18:00:18 +0000777
778 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
779 if (getParser().ParseExpression(Disp, End)) return 0;
Chad Rosierce353b32012-10-15 17:26:38 +0000780 End = Parser.getTok().getLoc();
Chad Rosier96d58e62012-10-19 20:57:14 +0000781
782 bool NeedSizeDir = false;
783 if (!Size && isParsingInlineAsm()) {
784 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
785 const MCSymbol &Sym = SymRef->getSymbol();
786 // FIXME: The SemaLookup will fail if the name is anything other then an
787 // identifier.
788 // FIXME: Pass a valid SMLoc.
789 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Size);
790 NeedSizeDir = Size > 0;
791 }
792 }
Chad Rosier2a784132012-10-23 23:31:33 +0000793 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +0000794 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier2a784132012-10-23 23:31:33 +0000795 else
Chad Rosierd4d96ac2012-10-23 23:34:28 +0000796 // When parsing inline assembly we set the base register to a non-zero value
797 // as we don't know the actual value at this time. This is necessary to
798 // get the matching correct in some cases.
Chad Rosier2a784132012-10-23 23:31:33 +0000799 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000800 /*Scale*/1, Start, End, Size, NeedSizeDir);
801}
802
803/// Parse the 'offset' operator. This operator is used to specify the
804/// location rather then the content of a variable.
805X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
806 SMLoc OffsetOfLoc = Start;
807 Parser.Lex(); // Eat offset.
808 Start = Parser.getTok().getLoc();
809 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
810
811 SMLoc End;
812 const MCExpr *Val;
813 if (getParser().ParseExpression(Val, End))
814 return 0;
815
816 End = Parser.getTok().getLoc();
817
818 // The offset operator will have an 'r' constraint, thus we need to create
819 // register operand to ensure proper matching. Just pick a GPR based on
820 // the size of a pointer.
821 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
822 return X86Operand::CreateReg(RegNo, Start, End, OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +0000823}
824
825X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000826 SMLoc Start = Parser.getTok().getLoc(), End;
827
Chad Rosierc0a14b82012-10-24 17:22:29 +0000828 // offset operator.
829 const AsmToken &Tok = Parser.getTok();
830 if ((Tok.getString() == "offset" || Tok.getString() == "OFFSET") &&
831 isParsingInlineAsm())
832 return ParseIntelOffsetOfOperator(Start);
833
Devang Pateld37ad242012-01-17 18:00:18 +0000834 // immediate.
835 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
836 getLexer().is(AsmToken::Minus)) {
837 const MCExpr *Val;
838 if (!getParser().ParseExpression(Val, End)) {
839 End = Parser.getTok().getLoc();
840 return X86Operand::CreateImm(Val, Start, End);
841 }
842 }
843
Devang Patel0a338862012-01-12 01:36:43 +0000844 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000845 unsigned RegNo = 0;
846 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000847 // If this is a segment register followed by a ':', then this is the start
848 // of a memory reference, otherwise this is a normal register reference.
849 if (getLexer().isNot(AsmToken::Colon))
850 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
851
852 getParser().Lex(); // Eat the colon.
853 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000854 }
855
856 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000857 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000858}
859
Devang Pateldd929fc2012-01-12 18:03:40 +0000860X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000861 switch (getLexer().getKind()) {
862 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000863 // Parse a memory operand with no segment register.
864 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000865 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000866 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000867 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000868 SMLoc Start, End;
869 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000870 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000871 Error(Start, "%eiz and %riz can only be used as index registers",
872 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000873 return 0;
874 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000875
Chris Lattnereef6d782010-04-17 18:56:34 +0000876 // If this is a segment register followed by a ':', then this is the start
877 // of a memory reference, otherwise this is a normal register reference.
878 if (getLexer().isNot(AsmToken::Colon))
879 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000880
881
Chris Lattnereef6d782010-04-17 18:56:34 +0000882 getParser().Lex(); // Eat the colon.
883 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000884 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000885 case AsmToken::Dollar: {
886 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000887 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000888 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000889 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000890 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000891 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000892 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000893 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000894 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000895}
896
Chris Lattnereef6d782010-04-17 18:56:34 +0000897/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
898/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000899X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000900
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000901 // We have to disambiguate a parenthesized expression "(4+5)" from the start
902 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000903 // only way to do this without lookahead is to eat the '(' and see what is
904 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000905 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000906 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000907 SMLoc ExprEnd;
908 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000909
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000910 // After parsing the base expression we could either have a parenthesized
911 // memory address or not. If not, return now. If so, eat the (.
912 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000913 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000914 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000915 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000916 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000917 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000918
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000919 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000920 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000921 } else {
922 // Okay, we have a '('. We don't know if this is an expression or not, but
923 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000924 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000925 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000926
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000927 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000928 // Nothing to do here, fall into the code below with the '(' part of the
929 // memory operand consumed.
930 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000931 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000932
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000933 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000934 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000935 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000936
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000937 // After parsing the base expression we could either have a parenthesized
938 // memory address or not. If not, return now. If so, eat the (.
939 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000940 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000941 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000942 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000943 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000944 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000945
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000946 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000947 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000948 }
949 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000950
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000951 // If we reached here, then we just ate the ( of the memory operand. Process
952 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000953 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000954 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000955
Chris Lattner29ef9a22010-01-15 18:51:29 +0000956 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000957 SMLoc StartLoc, EndLoc;
958 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000959 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000960 Error(StartLoc, "eiz and riz can only be used as index registers",
961 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000962 return 0;
963 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000964 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000965
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000966 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000967 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000968 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000969
970 // Following the comma we should have either an index register, or a scale
971 // value. We don't support the later form, but we want to parse it
972 // correctly.
973 //
974 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000975 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000976 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000977 SMLoc L;
978 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000979
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000980 if (getLexer().isNot(AsmToken::RParen)) {
981 // Parse the scale amount:
982 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000983 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000984 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000985 "expected comma in scale expression");
986 return 0;
987 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000988 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000989
990 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000991 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000992
993 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000994 if (getParser().ParseAbsoluteExpression(ScaleVal)){
995 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000996 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +0000997 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000998
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000999 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001000 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1001 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1002 return 0;
1003 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001004 Scale = (unsigned)ScaleVal;
1005 }
1006 }
1007 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001008 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001009 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001010 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001011
1012 int64_t Value;
1013 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001014 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001015
Daniel Dunbaree910252010-08-24 19:13:38 +00001016 if (Value != 1)
1017 Warning(Loc, "scale factor without index register is ignored");
1018 Scale = 1;
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 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001023 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001024 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001025 return 0;
1026 }
Sean Callanan18b83232010-01-19 21:44:56 +00001027 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001028 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001029
Kevin Enderby84faf652012-03-12 21:32:09 +00001030 // If we have both a base register and an index register make sure they are
1031 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001032 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001033 if (BaseReg != 0 && IndexReg != 0) {
1034 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001035 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1036 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001037 IndexReg != X86::RIZ) {
1038 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1039 return 0;
1040 }
1041 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001042 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1043 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001044 IndexReg != X86::EIZ){
1045 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1046 return 0;
1047 }
1048 }
1049
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001050 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1051 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001052}
1053
Devang Pateldd929fc2012-01-12 18:03:40 +00001054bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +00001055ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001056 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +00001057 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001058
Chris Lattnerd8f71792010-11-28 20:23:50 +00001059 // FIXME: Hack to recognize setneb as setne.
1060 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1061 PatchedName != "setb" && PatchedName != "setnb")
1062 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001063
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001064 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1065 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001066 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001067 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1068 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001069 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001070 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001071 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001072 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001073 .Case("eq", 0x00)
1074 .Case("lt", 0x01)
1075 .Case("le", 0x02)
1076 .Case("unord", 0x03)
1077 .Case("neq", 0x04)
1078 .Case("nlt", 0x05)
1079 .Case("nle", 0x06)
1080 .Case("ord", 0x07)
1081 /* AVX only from here */
1082 .Case("eq_uq", 0x08)
1083 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001084 .Case("ngt", 0x0A)
1085 .Case("false", 0x0B)
1086 .Case("neq_oq", 0x0C)
1087 .Case("ge", 0x0D)
1088 .Case("gt", 0x0E)
1089 .Case("true", 0x0F)
1090 .Case("eq_os", 0x10)
1091 .Case("lt_oq", 0x11)
1092 .Case("le_oq", 0x12)
1093 .Case("unord_s", 0x13)
1094 .Case("neq_us", 0x14)
1095 .Case("nlt_uq", 0x15)
1096 .Case("nle_uq", 0x16)
1097 .Case("ord_s", 0x17)
1098 .Case("eq_us", 0x18)
1099 .Case("nge_uq", 0x19)
1100 .Case("ngt_uq", 0x1A)
1101 .Case("false_os", 0x1B)
1102 .Case("neq_os", 0x1C)
1103 .Case("ge_oq", 0x1D)
1104 .Case("gt_oq", 0x1E)
1105 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001106 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001107 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001108 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1109 getParser().getContext());
1110 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001111 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001112 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001113 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001114 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001115 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001116 } else {
1117 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001118 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001119 }
1120 }
1121 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001122
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001123 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001124
Devang Patel885f65b2012-01-30 22:47:12 +00001125 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001126 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001127
Chris Lattner2544f422010-09-08 05:17:37 +00001128 // Determine whether this is an instruction prefix.
1129 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001130 Name == "lock" || Name == "rep" ||
1131 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001132 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001133 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001134
1135
Chris Lattner2544f422010-09-08 05:17:37 +00001136 // This does the actual operand parsing. Don't parse any more if we have a
1137 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1138 // just want to parse the "lock" as the first instruction and the "incl" as
1139 // the next one.
1140 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001141
1142 // Parse '*' modifier.
1143 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001144 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001145 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001146 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001147 }
1148
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001149 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001150 if (X86Operand *Op = ParseOperand())
1151 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001152 else {
1153 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001154 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001155 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001156
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001157 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001158 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001159
1160 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001161 if (X86Operand *Op = ParseOperand())
1162 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001163 else {
1164 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001165 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001166 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001167 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001168
Chris Lattnercbf8a982010-09-11 16:18:25 +00001169 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001170 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001171 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001172 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001173 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001174 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001175
Chris Lattner2544f422010-09-08 05:17:37 +00001176 if (getLexer().is(AsmToken::EndOfStatement))
1177 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001178 else if (isPrefix && getLexer().is(AsmToken::Slash))
1179 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001180
Devang Patel885f65b2012-01-30 22:47:12 +00001181 if (ExtraImmOp && isParsingIntelSyntax())
1182 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1183
Chris Lattner98c870f2010-11-06 19:25:43 +00001184 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1185 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1186 // documented form in various unofficial manuals, so a lot of code uses it.
1187 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1188 Operands.size() == 3) {
1189 X86Operand &Op = *(X86Operand*)Operands.back();
1190 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1191 isa<MCConstantExpr>(Op.Mem.Disp) &&
1192 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1193 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1194 SMLoc Loc = Op.getEndLoc();
1195 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1196 delete &Op;
1197 }
1198 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001199 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1200 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1201 Operands.size() == 3) {
1202 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1203 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1204 isa<MCConstantExpr>(Op.Mem.Disp) &&
1205 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1206 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1207 SMLoc Loc = Op.getEndLoc();
1208 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1209 delete &Op;
1210 }
1211 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001212 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1213 if (Name.startswith("ins") && Operands.size() == 3 &&
1214 (Name == "insb" || Name == "insw" || Name == "insl")) {
1215 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1216 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1217 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1218 Operands.pop_back();
1219 Operands.pop_back();
1220 delete &Op;
1221 delete &Op2;
1222 }
1223 }
1224
1225 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1226 if (Name.startswith("outs") && Operands.size() == 3 &&
1227 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1228 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1229 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1230 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1231 Operands.pop_back();
1232 Operands.pop_back();
1233 delete &Op;
1234 delete &Op2;
1235 }
1236 }
1237
1238 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1239 if (Name.startswith("movs") && Operands.size() == 3 &&
1240 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001241 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001242 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1243 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1244 if (isSrcOp(Op) && isDstOp(Op2)) {
1245 Operands.pop_back();
1246 Operands.pop_back();
1247 delete &Op;
1248 delete &Op2;
1249 }
1250 }
1251 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1252 if (Name.startswith("lods") && Operands.size() == 3 &&
1253 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001254 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001255 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1256 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1257 if (isSrcOp(*Op1) && Op2->isReg()) {
1258 const char *ins;
1259 unsigned reg = Op2->getReg();
1260 bool isLods = Name == "lods";
1261 if (reg == X86::AL && (isLods || Name == "lodsb"))
1262 ins = "lodsb";
1263 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1264 ins = "lodsw";
1265 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1266 ins = "lodsl";
1267 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1268 ins = "lodsq";
1269 else
1270 ins = NULL;
1271 if (ins != NULL) {
1272 Operands.pop_back();
1273 Operands.pop_back();
1274 delete Op1;
1275 delete Op2;
1276 if (Name != ins)
1277 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1278 }
1279 }
1280 }
1281 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1282 if (Name.startswith("stos") && Operands.size() == 3 &&
1283 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001284 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001285 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1286 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1287 if (isDstOp(*Op2) && Op1->isReg()) {
1288 const char *ins;
1289 unsigned reg = Op1->getReg();
1290 bool isStos = Name == "stos";
1291 if (reg == X86::AL && (isStos || Name == "stosb"))
1292 ins = "stosb";
1293 else if (reg == X86::AX && (isStos || Name == "stosw"))
1294 ins = "stosw";
1295 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1296 ins = "stosl";
1297 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1298 ins = "stosq";
1299 else
1300 ins = NULL;
1301 if (ins != NULL) {
1302 Operands.pop_back();
1303 Operands.pop_back();
1304 delete Op1;
1305 delete Op2;
1306 if (Name != ins)
1307 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1308 }
1309 }
1310 }
1311
Chris Lattnere9e16a32010-09-15 04:33:27 +00001312 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001313 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001314 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001315 Name.startswith("shl") || Name.startswith("sal") ||
1316 Name.startswith("rcl") || Name.startswith("rcr") ||
1317 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001318 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001319 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001320 // Intel syntax
1321 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1322 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001323 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1324 delete Operands[2];
1325 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001326 }
1327 } else {
1328 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1329 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001330 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1331 delete Operands[1];
1332 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001333 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001334 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001335 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001336
Chris Lattner15f89512011-04-09 19:41:05 +00001337 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1338 // instalias with an immediate operand yet.
1339 if (Name == "int" && Operands.size() == 2) {
1340 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1341 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1342 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1343 delete Operands[1];
1344 Operands.erase(Operands.begin() + 1);
1345 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1346 }
1347 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001348
Chris Lattner98986712010-01-14 22:21:20 +00001349 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001350}
1351
Devang Pateldd929fc2012-01-12 18:03:40 +00001352bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001353processInstruction(MCInst &Inst,
1354 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1355 switch (Inst.getOpcode()) {
1356 default: return false;
1357 case X86::AND16i16: {
1358 if (!Inst.getOperand(0).isImm() ||
1359 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1360 return false;
1361
1362 MCInst TmpInst;
1363 TmpInst.setOpcode(X86::AND16ri8);
1364 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1365 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1366 TmpInst.addOperand(Inst.getOperand(0));
1367 Inst = TmpInst;
1368 return true;
1369 }
1370 case X86::AND32i32: {
1371 if (!Inst.getOperand(0).isImm() ||
1372 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1373 return false;
1374
1375 MCInst TmpInst;
1376 TmpInst.setOpcode(X86::AND32ri8);
1377 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1378 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1379 TmpInst.addOperand(Inst.getOperand(0));
1380 Inst = TmpInst;
1381 return true;
1382 }
1383 case X86::AND64i32: {
1384 if (!Inst.getOperand(0).isImm() ||
1385 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1386 return false;
1387
1388 MCInst TmpInst;
1389 TmpInst.setOpcode(X86::AND64ri8);
1390 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1391 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1392 TmpInst.addOperand(Inst.getOperand(0));
1393 Inst = TmpInst;
1394 return true;
1395 }
Devang Patelac0f0482012-01-19 17:53:25 +00001396 case X86::XOR16i16: {
1397 if (!Inst.getOperand(0).isImm() ||
1398 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1399 return false;
1400
1401 MCInst TmpInst;
1402 TmpInst.setOpcode(X86::XOR16ri8);
1403 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1404 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1405 TmpInst.addOperand(Inst.getOperand(0));
1406 Inst = TmpInst;
1407 return true;
1408 }
1409 case X86::XOR32i32: {
1410 if (!Inst.getOperand(0).isImm() ||
1411 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1412 return false;
1413
1414 MCInst TmpInst;
1415 TmpInst.setOpcode(X86::XOR32ri8);
1416 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1417 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1418 TmpInst.addOperand(Inst.getOperand(0));
1419 Inst = TmpInst;
1420 return true;
1421 }
1422 case X86::XOR64i32: {
1423 if (!Inst.getOperand(0).isImm() ||
1424 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1425 return false;
1426
1427 MCInst TmpInst;
1428 TmpInst.setOpcode(X86::XOR64ri8);
1429 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1430 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1431 TmpInst.addOperand(Inst.getOperand(0));
1432 Inst = TmpInst;
1433 return true;
1434 }
1435 case X86::OR16i16: {
1436 if (!Inst.getOperand(0).isImm() ||
1437 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1438 return false;
1439
1440 MCInst TmpInst;
1441 TmpInst.setOpcode(X86::OR16ri8);
1442 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1443 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1444 TmpInst.addOperand(Inst.getOperand(0));
1445 Inst = TmpInst;
1446 return true;
1447 }
1448 case X86::OR32i32: {
1449 if (!Inst.getOperand(0).isImm() ||
1450 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1451 return false;
1452
1453 MCInst TmpInst;
1454 TmpInst.setOpcode(X86::OR32ri8);
1455 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1456 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1457 TmpInst.addOperand(Inst.getOperand(0));
1458 Inst = TmpInst;
1459 return true;
1460 }
1461 case X86::OR64i32: {
1462 if (!Inst.getOperand(0).isImm() ||
1463 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1464 return false;
1465
1466 MCInst TmpInst;
1467 TmpInst.setOpcode(X86::OR64ri8);
1468 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1469 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1470 TmpInst.addOperand(Inst.getOperand(0));
1471 Inst = TmpInst;
1472 return true;
1473 }
1474 case X86::CMP16i16: {
1475 if (!Inst.getOperand(0).isImm() ||
1476 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1477 return false;
1478
1479 MCInst TmpInst;
1480 TmpInst.setOpcode(X86::CMP16ri8);
1481 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1482 TmpInst.addOperand(Inst.getOperand(0));
1483 Inst = TmpInst;
1484 return true;
1485 }
1486 case X86::CMP32i32: {
1487 if (!Inst.getOperand(0).isImm() ||
1488 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1489 return false;
1490
1491 MCInst TmpInst;
1492 TmpInst.setOpcode(X86::CMP32ri8);
1493 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1494 TmpInst.addOperand(Inst.getOperand(0));
1495 Inst = TmpInst;
1496 return true;
1497 }
1498 case X86::CMP64i32: {
1499 if (!Inst.getOperand(0).isImm() ||
1500 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1501 return false;
1502
1503 MCInst TmpInst;
1504 TmpInst.setOpcode(X86::CMP64ri8);
1505 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1506 TmpInst.addOperand(Inst.getOperand(0));
1507 Inst = TmpInst;
1508 return true;
1509 }
Devang Patela951f772012-01-19 18:40:55 +00001510 case X86::ADD16i16: {
1511 if (!Inst.getOperand(0).isImm() ||
1512 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1513 return false;
1514
1515 MCInst TmpInst;
1516 TmpInst.setOpcode(X86::ADD16ri8);
1517 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1518 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1519 TmpInst.addOperand(Inst.getOperand(0));
1520 Inst = TmpInst;
1521 return true;
1522 }
1523 case X86::ADD32i32: {
1524 if (!Inst.getOperand(0).isImm() ||
1525 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1526 return false;
1527
1528 MCInst TmpInst;
1529 TmpInst.setOpcode(X86::ADD32ri8);
1530 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1531 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1532 TmpInst.addOperand(Inst.getOperand(0));
1533 Inst = TmpInst;
1534 return true;
1535 }
1536 case X86::ADD64i32: {
1537 if (!Inst.getOperand(0).isImm() ||
1538 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1539 return false;
1540
1541 MCInst TmpInst;
1542 TmpInst.setOpcode(X86::ADD64ri8);
1543 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1544 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1545 TmpInst.addOperand(Inst.getOperand(0));
1546 Inst = TmpInst;
1547 return true;
1548 }
1549 case X86::SUB16i16: {
1550 if (!Inst.getOperand(0).isImm() ||
1551 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1552 return false;
1553
1554 MCInst TmpInst;
1555 TmpInst.setOpcode(X86::SUB16ri8);
1556 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1557 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1558 TmpInst.addOperand(Inst.getOperand(0));
1559 Inst = TmpInst;
1560 return true;
1561 }
1562 case X86::SUB32i32: {
1563 if (!Inst.getOperand(0).isImm() ||
1564 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1565 return false;
1566
1567 MCInst TmpInst;
1568 TmpInst.setOpcode(X86::SUB32ri8);
1569 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1570 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1571 TmpInst.addOperand(Inst.getOperand(0));
1572 Inst = TmpInst;
1573 return true;
1574 }
1575 case X86::SUB64i32: {
1576 if (!Inst.getOperand(0).isImm() ||
1577 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1578 return false;
1579
1580 MCInst TmpInst;
1581 TmpInst.setOpcode(X86::SUB64ri8);
1582 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1583 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1584 TmpInst.addOperand(Inst.getOperand(0));
1585 Inst = TmpInst;
1586 return true;
1587 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001588 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001589}
1590
1591bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001592MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001593 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001594 MCStreamer &Out, unsigned &ErrorInfo,
1595 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001596 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001597 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1598 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001599 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001600
Chris Lattner7c51a312010-09-29 01:50:45 +00001601 // First, handle aliases that expand to multiple instructions.
1602 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001603 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001604 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001605 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001606 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001607 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001608 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001609 MCInst Inst;
1610 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001611 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001612 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001613 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001614
Chris Lattner0bb83a82010-09-30 16:39:29 +00001615 const char *Repl =
1616 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001617 .Case("finit", "fninit")
1618 .Case("fsave", "fnsave")
1619 .Case("fstcw", "fnstcw")
1620 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001621 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001622 .Case("fstsw", "fnstsw")
1623 .Case("fstsww", "fnstsw")
1624 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001625 .Default(0);
1626 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001627 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001628 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001629 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001630
Chris Lattnera008e8a2010-09-06 21:54:15 +00001631 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001632 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001633
Daniel Dunbarc918d602010-05-04 16:12:42 +00001634 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001635 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001636 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001637 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001638 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001639 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001640 // Some instructions need post-processing to, for example, tweak which
1641 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001642 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001643 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001644 while (processInstruction(Inst, Operands))
1645 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001646
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001647 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001648 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001649 Out.EmitInstruction(Inst);
1650 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001651 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001652 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001653 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001654 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001655 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001656 case Match_InvalidOperand:
1657 WasOriginallyInvalidOperand = true;
1658 break;
1659 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001660 break;
1661 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001662
Daniel Dunbarc918d602010-05-04 16:12:42 +00001663 // FIXME: Ideally, we would only attempt suffix matches for things which are
1664 // valid prefixes, and we could just infer the right unambiguous
1665 // type. However, that requires substantially more matcher support than the
1666 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001667
Daniel Dunbarc918d602010-05-04 16:12:42 +00001668 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001669 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001670 SmallString<16> Tmp;
1671 Tmp += Base;
1672 Tmp += ' ';
1673 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001674
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001675 // If this instruction starts with an 'f', then it is a floating point stack
1676 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1677 // 80-bit floating point, which use the suffixes s,l,t respectively.
1678 //
1679 // Otherwise, we assume that this may be an integer instruction, which comes
1680 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1681 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001682
Daniel Dunbarc918d602010-05-04 16:12:42 +00001683 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001684 Tmp[Base.size()] = Suffixes[0];
1685 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001686 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001687
Chad Rosier6e006d32012-10-12 22:53:36 +00001688 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1689 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001690 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001691 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1692 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001693 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001694 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1695 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001696 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001697 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1698 isParsingIntelSyntax());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001699
1700 // Restore the old token.
1701 Op->setTokenValue(Base);
1702
1703 // If exactly one matched, then we treat that as a successful match (and the
1704 // instruction will already have been filled in correctly, since the failing
1705 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001706 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001707 (Match1 == Match_Success) + (Match2 == Match_Success) +
1708 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001709 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001710 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001711 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001712 Out.EmitInstruction(Inst);
1713 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001714 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001715 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001716
Chris Lattnerec6789f2010-09-06 20:08:02 +00001717 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001718
Daniel Dunbar09062b12010-08-12 00:55:42 +00001719 // If we had multiple suffix matches, then identify this as an ambiguous
1720 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001721 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001722 char MatchChars[4];
1723 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001724 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1725 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1726 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1727 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001728
1729 SmallString<126> Msg;
1730 raw_svector_ostream OS(Msg);
1731 OS << "ambiguous instructions require an explicit suffix (could be ";
1732 for (unsigned i = 0; i != NumMatches; ++i) {
1733 if (i != 0)
1734 OS << ", ";
1735 if (i + 1 == NumMatches)
1736 OS << "or ";
1737 OS << "'" << Base << MatchChars[i] << "'";
1738 }
1739 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001740 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001741 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001742 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001743
Chris Lattnera008e8a2010-09-06 21:54:15 +00001744 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001745
Chris Lattnera008e8a2010-09-06 21:54:15 +00001746 // If all of the instructions reported an invalid mnemonic, then the original
1747 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001748 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1749 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001750 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001751 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001752 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001753 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001754 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001755 }
1756
1757 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001758 if (ErrorInfo != ~0U) {
1759 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001760 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001761 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001762
Chad Rosier84125ca2012-10-13 00:26:04 +00001763 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001764 if (Operand->getStartLoc().isValid()) {
1765 SMRange OperandRange = Operand->getLocRange();
1766 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001767 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001768 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001769 }
1770
Chad Rosierb4fdade2012-08-21 19:36:59 +00001771 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001772 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001773 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001774
Chris Lattnerec6789f2010-09-06 20:08:02 +00001775 // If one instruction matched with a missing feature, report this as a
1776 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001777 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1778 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001779 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001780 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001781 return true;
1782 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001783
Chris Lattnera008e8a2010-09-06 21:54:15 +00001784 // If one instruction matched with an invalid operand, report this as an
1785 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001786 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1787 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001788 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001789 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001790 return true;
1791 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001792
Chris Lattnerec6789f2010-09-06 20:08:02 +00001793 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001794 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001795 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001796 return true;
1797}
1798
1799
Devang Pateldd929fc2012-01-12 18:03:40 +00001800bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001801 StringRef IDVal = DirectiveID.getIdentifier();
1802 if (IDVal == ".word")
1803 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001804 else if (IDVal.startswith(".code"))
1805 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001806 else if (IDVal.startswith(".att_syntax")) {
1807 getParser().setAssemblerDialect(0);
1808 return false;
1809 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001810 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001811 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1812 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001813 // FIXME : Handle noprefix
1814 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001815 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001816 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001817 }
1818 return false;
1819 }
Chris Lattner537ca842010-10-30 17:38:55 +00001820 return true;
1821}
1822
1823/// ParseDirectiveWord
1824/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001825bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001826 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1827 for (;;) {
1828 const MCExpr *Value;
1829 if (getParser().ParseExpression(Value))
1830 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001831
Chris Lattner537ca842010-10-30 17:38:55 +00001832 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001833
Chris Lattner537ca842010-10-30 17:38:55 +00001834 if (getLexer().is(AsmToken::EndOfStatement))
1835 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001836
Chris Lattner537ca842010-10-30 17:38:55 +00001837 // FIXME: Improve diagnostic.
1838 if (getLexer().isNot(AsmToken::Comma))
1839 return Error(L, "unexpected token in directive");
1840 Parser.Lex();
1841 }
1842 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001843
Chris Lattner537ca842010-10-30 17:38:55 +00001844 Parser.Lex();
1845 return false;
1846}
1847
Evan Chengbd27f5a2011-07-27 00:38:12 +00001848/// ParseDirectiveCode
1849/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001850bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001851 if (IDVal == ".code32") {
1852 Parser.Lex();
1853 if (is64BitMode()) {
1854 SwitchMode();
1855 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1856 }
1857 } else if (IDVal == ".code64") {
1858 Parser.Lex();
1859 if (!is64BitMode()) {
1860 SwitchMode();
1861 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1862 }
1863 } else {
1864 return Error(L, "unexpected directive " + IDVal);
1865 }
Chris Lattner537ca842010-10-30 17:38:55 +00001866
Evan Chengbd27f5a2011-07-27 00:38:12 +00001867 return false;
1868}
Chris Lattner537ca842010-10-30 17:38:55 +00001869
1870
Sean Callanane88f5522010-01-23 02:43:15 +00001871extern "C" void LLVMInitializeX86AsmLexer();
1872
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001873// Force static initialization.
1874extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001875 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1876 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001877 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001878}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001879
Chris Lattner0692ee62010-09-06 19:11:01 +00001880#define GET_REGISTER_MATCHER
1881#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001882#include "X86GenAsmMatcher.inc"