blob: c752d5962eab0752536ef00c7bfb2684cbb35be0 [file] [log] [blame]
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
11#include "llvm/MC/MCTargetAsmParser.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000012#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000013#include "llvm/MC/MCExpr.h"
Chad Rosier96d58e62012-10-19 20:57:14 +000014#include "llvm/MC/MCSymbol.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000015#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000016#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000017#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Chad Rosier4284e172012-10-24 22:13:37 +000021#include "llvm/ADT/APFloat.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000022#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000024#include "llvm/ADT/StringSwitch.h"
25#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000028#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000029
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000030using namespace llvm;
31
32namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000033struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000034
Devang Pateldd929fc2012-01-12 18:03:40 +000035class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000036 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &Parser;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039 MCAsmParser &getParser() const { return Parser; }
40
41 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
42
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000043 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000044 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000045 bool MatchingInlineAsm = false) {
46 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000047 return Parser.Error(L, Msg, Ranges);
48 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000049
Devang Pateld37ad242012-01-17 18:00:18 +000050 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
51 Error(Loc, Msg);
52 return 0;
53 }
54
Chris Lattner309264d2010-01-15 18:44:13 +000055 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000056 X86Operand *ParseATTOperand();
57 X86Operand *ParseIntelOperand();
Chad Rosierc0a14b82012-10-24 17:22:29 +000058 X86Operand *ParseIntelOffsetOfOperator(SMLoc StartLoc);
Chad Rosier5b0f1b32012-10-04 23:59:38 +000059 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000060 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000061 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000062
63 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000064 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000065
Devang Patelb8ba13f2012-01-18 22:42:29 +000066 bool processInstruction(MCInst &Inst,
67 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
68
Chad Rosier84125ca2012-10-13 00:26:04 +000069 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000070 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000071 MCStreamer &Out, unsigned &ErrorInfo,
72 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000073
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000074 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000075 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000076 bool isSrcOp(X86Operand &Op);
77
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000078 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
79 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000080 bool isDstOp(X86Operand &Op);
81
Evan Cheng59ee62d2011-07-11 03:57:24 +000082 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000083 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000084 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000085 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000086 void SwitchMode() {
87 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
88 setAvailableFeatures(FB);
89 }
Evan Chengebdeeab2011-07-08 01:53:10 +000090
Daniel Dunbar54074b52010-07-19 05:44:09 +000091 /// @name Auto-generated Matcher Functions
92 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000093
Chris Lattner0692ee62010-09-06 19:11:01 +000094#define GET_ASSEMBLER_HEADER
95#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000096
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000097 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000098
99public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000100 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +0000101 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000102
Daniel Dunbar54074b52010-07-19 05:44:09 +0000103 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000104 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000105 }
Roman Divackybf755322011-01-27 17:14:22 +0000106 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000107
Benjamin Kramer38e59892010-07-14 22:38:02 +0000108 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000109 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000110
111 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000112
113 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000114 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000115 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000116};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000117} // end anonymous namespace
118
Sean Callanane9b466d2010-01-23 00:40:33 +0000119/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000120/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000121
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000122static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000123
124/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000125
Craig Topper76bd9382012-07-18 04:59:16 +0000126static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000127 return (( Value <= 0x000000000000007FULL)||
128 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
129 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
130}
131
132static bool isImmSExti32i8Value(uint64_t Value) {
133 return (( Value <= 0x000000000000007FULL)||
134 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
135 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
136}
137
138static bool isImmZExtu32u8Value(uint64_t Value) {
139 return (Value <= 0x00000000000000FFULL);
140}
141
142static bool isImmSExti64i8Value(uint64_t Value) {
143 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000144 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000145}
146
147static bool isImmSExti64i32Value(uint64_t Value) {
148 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000149 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000150}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000151namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000152
153/// X86Operand - Instances of this class represent a parsed X86 machine
154/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000155struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000156 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000157 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000158 Register,
159 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000160 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000161 } Kind;
162
Chris Lattner29ef9a22010-01-15 18:51:29 +0000163 SMLoc StartLoc, EndLoc;
Chad Rosier5a719fc2012-10-23 17:43:43 +0000164 SMLoc OffsetOfLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000165
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000166 union {
167 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000168 const char *Data;
169 unsigned Length;
170 } Tok;
171
172 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000173 unsigned RegNo;
174 } Reg;
175
176 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000177 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000178 } Imm;
179
180 struct {
181 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000182 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000183 unsigned BaseReg;
184 unsigned IndexReg;
185 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000186 unsigned Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000187 bool NeedSizeDir;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000188 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000189 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000190
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000191 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000192 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000193
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000194 /// getStartLoc - Get the location of the first token of this operand.
195 SMLoc getStartLoc() const { return StartLoc; }
196 /// getEndLoc - Get the location of the last token of this operand.
197 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000198 /// getLocRange - Get the range between the first and last token of this
199 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000200 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000201 /// getOffsetOfLoc - Get the location of the offset operator.
202 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000203
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000204 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000205
Daniel Dunbar20927f22009-08-07 08:26:05 +0000206 StringRef getToken() const {
207 assert(Kind == Token && "Invalid access!");
208 return StringRef(Tok.Data, Tok.Length);
209 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000210 void setTokenValue(StringRef Value) {
211 assert(Kind == Token && "Invalid access!");
212 Tok.Data = Value.data();
213 Tok.Length = Value.size();
214 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000215
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000216 unsigned getReg() const {
217 assert(Kind == Register && "Invalid access!");
218 return Reg.RegNo;
219 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000220
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000221 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000222 assert(Kind == Immediate && "Invalid access!");
223 return Imm.Val;
224 }
225
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000226 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000227 assert(Kind == Memory && "Invalid access!");
228 return Mem.Disp;
229 }
230 unsigned getMemSegReg() const {
231 assert(Kind == Memory && "Invalid access!");
232 return Mem.SegReg;
233 }
234 unsigned getMemBaseReg() const {
235 assert(Kind == Memory && "Invalid access!");
236 return Mem.BaseReg;
237 }
238 unsigned getMemIndexReg() const {
239 assert(Kind == Memory && "Invalid access!");
240 return Mem.IndexReg;
241 }
242 unsigned getMemScale() const {
243 assert(Kind == Memory && "Invalid access!");
244 return Mem.Scale;
245 }
246
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000247 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000248
249 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000250
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000251 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000252 if (!isImm())
253 return false;
254
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000255 // If this isn't a constant expr, just assume it fits and let relaxation
256 // handle it.
257 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
258 if (!CE)
259 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000260
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000261 // Otherwise, check the value is in a range that makes sense for this
262 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000263 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000264 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000265 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000266 if (!isImm())
267 return false;
268
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000269 // If this isn't a constant expr, just assume it fits and let relaxation
270 // handle it.
271 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
272 if (!CE)
273 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000274
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000275 // Otherwise, check the value is in a range that makes sense for this
276 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000277 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000278 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000279 bool isImmZExtu32u8() const {
280 if (!isImm())
281 return false;
282
283 // If this isn't a constant expr, just assume it fits and let relaxation
284 // handle it.
285 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
286 if (!CE)
287 return true;
288
289 // Otherwise, check the value is in a range that makes sense for this
290 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000291 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000292 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000293 bool isImmSExti64i8() const {
294 if (!isImm())
295 return false;
296
297 // If this isn't a constant expr, just assume it fits and let relaxation
298 // handle it.
299 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
300 if (!CE)
301 return true;
302
303 // Otherwise, check the value is in a range that makes sense for this
304 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000305 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000306 }
307 bool isImmSExti64i32() const {
308 if (!isImm())
309 return false;
310
311 // If this isn't a constant expr, just assume it fits and let relaxation
312 // handle it.
313 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
314 if (!CE)
315 return true;
316
317 // Otherwise, check the value is in a range that makes sense for this
318 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000319 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000320 }
321
Chad Rosier96d58e62012-10-19 20:57:14 +0000322 unsigned getMemSize() const {
323 assert(Kind == Memory && "Invalid access!");
324 return Mem.Size;
325 }
326
Chad Rosiera703fb92012-10-22 19:50:35 +0000327 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000328 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000329 }
330
Chad Rosier96d58e62012-10-19 20:57:14 +0000331 bool needSizeDirective() const {
332 assert(Kind == Memory && "Invalid access!");
333 return Mem.NeedSizeDir;
334 }
335
Daniel Dunbar20927f22009-08-07 08:26:05 +0000336 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000337 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000338 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000339 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000340 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000341 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000342 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000343 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000344 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000345 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000346 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000347 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000348 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000349 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000350 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000351 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000352 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000353 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000354 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000355 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000356 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000357 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000358
Craig Topper75dc33a2012-07-18 04:11:12 +0000359 bool isMemVX32() const {
360 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
361 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
362 }
363 bool isMemVY32() const {
364 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
365 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
366 }
367 bool isMemVX64() const {
368 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
369 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
370 }
371 bool isMemVY64() const {
372 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
373 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
374 }
375
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000376 bool isAbsMem() const {
377 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000378 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000379 }
380
Daniel Dunbar20927f22009-08-07 08:26:05 +0000381 bool isReg() const { return Kind == Register; }
382
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000383 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
384 // Add as immediates when possible.
385 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
386 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
387 else
388 Inst.addOperand(MCOperand::CreateExpr(Expr));
389 }
390
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000391 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000392 assert(N == 1 && "Invalid number of operands!");
393 Inst.addOperand(MCOperand::CreateReg(getReg()));
394 }
395
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000396 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000397 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000398 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000399 }
400
Chad Rosier36b8fed2012-06-27 22:34:28 +0000401 void addMem8Operands(MCInst &Inst, unsigned N) const {
402 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000403 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000404 void addMem16Operands(MCInst &Inst, unsigned N) const {
405 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000406 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000407 void addMem32Operands(MCInst &Inst, unsigned N) const {
408 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000409 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000410 void addMem64Operands(MCInst &Inst, unsigned N) const {
411 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000412 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000413 void addMem80Operands(MCInst &Inst, unsigned N) const {
414 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000415 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000416 void addMem128Operands(MCInst &Inst, unsigned N) const {
417 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000418 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000419 void addMem256Operands(MCInst &Inst, unsigned N) const {
420 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000421 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000422 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
423 addMemOperands(Inst, N);
424 }
425 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
426 addMemOperands(Inst, N);
427 }
428 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
429 addMemOperands(Inst, N);
430 }
431 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
432 addMemOperands(Inst, N);
433 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000434
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000435 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000436 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000437 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
438 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
439 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000440 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000441 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
442 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000443
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000444 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
445 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000446 // Add as immediates when possible.
447 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
448 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
449 else
450 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000451 }
452
Chris Lattnerb4307b32010-01-15 19:28:38 +0000453 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000454 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
455 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000456 Res->Tok.Data = Str.data();
457 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000458 return Res;
459 }
460
Chad Rosierc0a14b82012-10-24 17:22:29 +0000461 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
462 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000463 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000464 Res->Reg.RegNo = RegNo;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000465 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000466 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000467 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000468
Chris Lattnerb4307b32010-01-15 19:28:38 +0000469 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
470 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000471 Res->Imm.Val = Val;
472 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000473 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000474
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000475 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000476 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
477 unsigned Size = 0, 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 Rosier4284e172012-10-24 22:13:37 +0000665 const AsmToken &Tok = Parser.getTok();
666 SMLoc Start = Tok.getLoc(), End;
Devang Patel0a338862012-01-12 01:36:43 +0000667
Chad Rosier4284e172012-10-24 22:13:37 +0000668 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000669 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
670
671 // Eat '['
672 if (getLexer().isNot(AsmToken::LBrac))
673 return ErrorOperand(Start, "Expected '[' token!");
674 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000675
Devang Pateld37ad242012-01-17 18:00:18 +0000676 if (getLexer().is(AsmToken::Identifier)) {
677 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000678 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000679 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000680 if (getParser().ParseExpression(Disp, End)) return 0;
681 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000682 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000683 Parser.Lex();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000684 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000685 }
686 } else if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000687 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000688 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000689 SMLoc Loc = Tok.getLoc();
Devang Patel3e081312012-01-23 20:20:06 +0000690 if (getLexer().is(AsmToken::RBrac)) {
691 // Handle '[' number ']'
692 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000693 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
694 if (SegReg)
695 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000696 Start, End, Size);
697 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000698 } else if (getLexer().is(AsmToken::Star)) {
699 // Handle '[' Scale*IndexReg ']'
700 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000701 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000702 if (ParseRegister(IndexReg, IdxRegLoc, End))
703 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000704 Scale = Val;
705 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000706 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000707 }
708
709 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
710 bool isPlus = getLexer().is(AsmToken::Plus);
711 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000712 SMLoc PlusLoc = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000713 if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000714 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000715 Parser.Lex();
716 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000717 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000718 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000719 if (ParseRegister(IndexReg, IdxRegLoc, End))
720 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000721 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000722 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000723 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000724 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000725 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000726 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000727 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000728 // This could be an index register or a displacement expression.
Chad Rosier4284e172012-10-24 22:13:37 +0000729 End = Tok.getLoc();
Devang Patelf2d21372012-01-23 22:35:25 +0000730 if (!IndexReg)
731 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000732 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000733 }
Devang Pateld37ad242012-01-17 18:00:18 +0000734 }
735
736 if (getLexer().isNot(AsmToken::RBrac))
737 if (getParser().ParseExpression(Disp, End)) return 0;
738
Chad Rosier4284e172012-10-24 22:13:37 +0000739 End = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000740 if (getLexer().isNot(AsmToken::RBrac))
741 return ErrorOperand(End, "expected ']' token!");
742 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000743 End = Tok.getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000744
745 // handle [-42]
746 if (!BaseReg && !IndexReg)
Chad Rosierc0a14b82012-10-24 17:22:29 +0000747 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patelfdd3b302012-01-20 21:21:01 +0000748
Devang Pateld37ad242012-01-17 18:00:18 +0000749 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000750 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000751}
752
753/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000754X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000755 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000756 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000757
758 unsigned Size = getIntelMemOperandSize(Tok.getString());
759 if (Size) {
760 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000761 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
762 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000763 Parser.Lex();
764 }
765
Chad Rosierc0a14b82012-10-24 17:22:29 +0000766 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000767 return ParseIntelBracExpression(SegReg, Size);
768
769 if (!ParseRegister(SegReg, Start, End)) {
770 // Handel SegReg : [ ... ]
771 if (getLexer().isNot(AsmToken::Colon))
772 return ErrorOperand(Start, "Expected ':' token!");
773 Parser.Lex(); // Eat :
774 if (getLexer().isNot(AsmToken::LBrac))
775 return ErrorOperand(Start, "Expected '[' token!");
776 return ParseIntelBracExpression(SegReg, Size);
777 }
Devang Pateld37ad242012-01-17 18:00:18 +0000778
779 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
780 if (getParser().ParseExpression(Disp, End)) return 0;
Chad Rosierce353b32012-10-15 17:26:38 +0000781 End = Parser.getTok().getLoc();
Chad Rosier96d58e62012-10-19 20:57:14 +0000782
783 bool NeedSizeDir = false;
784 if (!Size && isParsingInlineAsm()) {
785 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
786 const MCSymbol &Sym = SymRef->getSymbol();
787 // FIXME: The SemaLookup will fail if the name is anything other then an
788 // identifier.
789 // FIXME: Pass a valid SMLoc.
790 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Size);
791 NeedSizeDir = Size > 0;
792 }
793 }
Chad Rosier2a784132012-10-23 23:31:33 +0000794 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +0000795 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier2a784132012-10-23 23:31:33 +0000796 else
Chad Rosierd4d96ac2012-10-23 23:34:28 +0000797 // When parsing inline assembly we set the base register to a non-zero value
798 // as we don't know the actual value at this time. This is necessary to
799 // get the matching correct in some cases.
Chad Rosier2a784132012-10-23 23:31:33 +0000800 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000801 /*Scale*/1, Start, End, Size, NeedSizeDir);
802}
803
804/// Parse the 'offset' operator. This operator is used to specify the
805/// location rather then the content of a variable.
806X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
807 SMLoc OffsetOfLoc = Start;
808 Parser.Lex(); // Eat offset.
809 Start = Parser.getTok().getLoc();
810 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
811
812 SMLoc End;
813 const MCExpr *Val;
814 if (getParser().ParseExpression(Val, End))
815 return 0;
816
817 End = Parser.getTok().getLoc();
818
819 // The offset operator will have an 'r' constraint, thus we need to create
820 // register operand to ensure proper matching. Just pick a GPR based on
821 // the size of a pointer.
822 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
823 return X86Operand::CreateReg(RegNo, Start, End, OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +0000824}
825
826X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000827 SMLoc Start = Parser.getTok().getLoc(), End;
828
Chad Rosierc0a14b82012-10-24 17:22:29 +0000829 // offset operator.
830 const AsmToken &Tok = Parser.getTok();
831 if ((Tok.getString() == "offset" || Tok.getString() == "OFFSET") &&
832 isParsingInlineAsm())
833 return ParseIntelOffsetOfOperator(Start);
834
Devang Pateld37ad242012-01-17 18:00:18 +0000835 // immediate.
836 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
837 getLexer().is(AsmToken::Minus)) {
838 const MCExpr *Val;
839 if (!getParser().ParseExpression(Val, End)) {
840 End = Parser.getTok().getLoc();
841 return X86Operand::CreateImm(Val, Start, End);
842 }
843 }
844
Devang Patel0a338862012-01-12 01:36:43 +0000845 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000846 unsigned RegNo = 0;
847 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000848 // If this is a segment register followed by a ':', then this is the start
849 // of a memory reference, otherwise this is a normal register reference.
850 if (getLexer().isNot(AsmToken::Colon))
851 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
852
853 getParser().Lex(); // Eat the colon.
854 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000855 }
856
857 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000858 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000859}
860
Devang Pateldd929fc2012-01-12 18:03:40 +0000861X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000862 switch (getLexer().getKind()) {
863 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000864 // Parse a memory operand with no segment register.
865 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000866 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000867 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000868 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000869 SMLoc Start, End;
870 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000871 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000872 Error(Start, "%eiz and %riz can only be used as index registers",
873 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000874 return 0;
875 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000876
Chris Lattnereef6d782010-04-17 18:56:34 +0000877 // If this is a segment register followed by a ':', then this is the start
878 // of a memory reference, otherwise this is a normal register reference.
879 if (getLexer().isNot(AsmToken::Colon))
880 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000881
882
Chris Lattnereef6d782010-04-17 18:56:34 +0000883 getParser().Lex(); // Eat the colon.
884 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000885 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000886 case AsmToken::Dollar: {
887 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000888 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000889 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000890 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000891 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000892 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000893 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000894 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000895 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000896}
897
Chris Lattnereef6d782010-04-17 18:56:34 +0000898/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
899/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000900X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000901
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000902 // We have to disambiguate a parenthesized expression "(4+5)" from the start
903 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000904 // only way to do this without lookahead is to eat the '(' and see what is
905 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000906 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000907 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000908 SMLoc ExprEnd;
909 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000910
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000911 // After parsing the base expression we could either have a parenthesized
912 // memory address or not. If not, return now. If so, eat the (.
913 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000914 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000915 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000916 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000917 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000918 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000919
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000920 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000921 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000922 } else {
923 // Okay, we have a '('. We don't know if this is an expression or not, but
924 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000925 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000926 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000927
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000928 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000929 // Nothing to do here, fall into the code below with the '(' part of the
930 // memory operand consumed.
931 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000932 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000933
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000934 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000935 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000936 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000937
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000938 // After parsing the base expression we could either have a parenthesized
939 // memory address or not. If not, return now. If so, eat the (.
940 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000941 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000942 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000943 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000944 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000945 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000946
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000947 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000948 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000949 }
950 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000951
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000952 // If we reached here, then we just ate the ( of the memory operand. Process
953 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000954 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000955 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000956
Chris Lattner29ef9a22010-01-15 18:51:29 +0000957 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000958 SMLoc StartLoc, EndLoc;
959 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000960 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000961 Error(StartLoc, "eiz and riz can only be used as index registers",
962 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000963 return 0;
964 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000965 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000966
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000967 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000968 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000969 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000970
971 // Following the comma we should have either an index register, or a scale
972 // value. We don't support the later form, but we want to parse it
973 // correctly.
974 //
975 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000976 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000977 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000978 SMLoc L;
979 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000980
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000981 if (getLexer().isNot(AsmToken::RParen)) {
982 // Parse the scale amount:
983 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000984 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000985 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000986 "expected comma in scale expression");
987 return 0;
988 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000989 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000990
991 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000992 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000993
994 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000995 if (getParser().ParseAbsoluteExpression(ScaleVal)){
996 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000997 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +0000998 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000999
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001000 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001001 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1002 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1003 return 0;
1004 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001005 Scale = (unsigned)ScaleVal;
1006 }
1007 }
1008 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001009 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001010 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001011 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001012
1013 int64_t Value;
1014 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001015 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001016
Daniel Dunbaree910252010-08-24 19:13:38 +00001017 if (Value != 1)
1018 Warning(Loc, "scale factor without index register is ignored");
1019 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001020 }
1021 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001022
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001023 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001024 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001025 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001026 return 0;
1027 }
Sean Callanan18b83232010-01-19 21:44:56 +00001028 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001029 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001030
Kevin Enderby84faf652012-03-12 21:32:09 +00001031 // If we have both a base register and an index register make sure they are
1032 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001033 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001034 if (BaseReg != 0 && IndexReg != 0) {
1035 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001036 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1037 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001038 IndexReg != X86::RIZ) {
1039 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1040 return 0;
1041 }
1042 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001043 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1044 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001045 IndexReg != X86::EIZ){
1046 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1047 return 0;
1048 }
1049 }
1050
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001051 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1052 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001053}
1054
Devang Pateldd929fc2012-01-12 18:03:40 +00001055bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +00001056ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001057 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +00001058 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001059
Chris Lattnerd8f71792010-11-28 20:23:50 +00001060 // FIXME: Hack to recognize setneb as setne.
1061 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1062 PatchedName != "setb" && PatchedName != "setnb")
1063 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001064
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001065 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1066 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001067 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001068 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1069 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001070 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001071 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001072 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001073 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001074 .Case("eq", 0x00)
1075 .Case("lt", 0x01)
1076 .Case("le", 0x02)
1077 .Case("unord", 0x03)
1078 .Case("neq", 0x04)
1079 .Case("nlt", 0x05)
1080 .Case("nle", 0x06)
1081 .Case("ord", 0x07)
1082 /* AVX only from here */
1083 .Case("eq_uq", 0x08)
1084 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001085 .Case("ngt", 0x0A)
1086 .Case("false", 0x0B)
1087 .Case("neq_oq", 0x0C)
1088 .Case("ge", 0x0D)
1089 .Case("gt", 0x0E)
1090 .Case("true", 0x0F)
1091 .Case("eq_os", 0x10)
1092 .Case("lt_oq", 0x11)
1093 .Case("le_oq", 0x12)
1094 .Case("unord_s", 0x13)
1095 .Case("neq_us", 0x14)
1096 .Case("nlt_uq", 0x15)
1097 .Case("nle_uq", 0x16)
1098 .Case("ord_s", 0x17)
1099 .Case("eq_us", 0x18)
1100 .Case("nge_uq", 0x19)
1101 .Case("ngt_uq", 0x1A)
1102 .Case("false_os", 0x1B)
1103 .Case("neq_os", 0x1C)
1104 .Case("ge_oq", 0x1D)
1105 .Case("gt_oq", 0x1E)
1106 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001107 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001108 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001109 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1110 getParser().getContext());
1111 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001112 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001113 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001114 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001115 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001116 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001117 } else {
1118 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001119 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001120 }
1121 }
1122 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001123
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001124 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001125
Devang Patel885f65b2012-01-30 22:47:12 +00001126 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001127 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001128
Chris Lattner2544f422010-09-08 05:17:37 +00001129 // Determine whether this is an instruction prefix.
1130 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001131 Name == "lock" || Name == "rep" ||
1132 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001133 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001134 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001135
1136
Chris Lattner2544f422010-09-08 05:17:37 +00001137 // This does the actual operand parsing. Don't parse any more if we have a
1138 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1139 // just want to parse the "lock" as the first instruction and the "incl" as
1140 // the next one.
1141 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001142
1143 // Parse '*' modifier.
1144 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001145 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001146 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001147 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001148 }
1149
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001150 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001151 if (X86Operand *Op = ParseOperand())
1152 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001153 else {
1154 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001155 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001156 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001157
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001158 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001159 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001160
1161 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001162 if (X86Operand *Op = ParseOperand())
1163 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001164 else {
1165 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001166 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001167 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001168 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001169
Chris Lattnercbf8a982010-09-11 16:18:25 +00001170 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001171 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001172 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001173 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001174 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001175 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001176
Chris Lattner2544f422010-09-08 05:17:37 +00001177 if (getLexer().is(AsmToken::EndOfStatement))
1178 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001179 else if (isPrefix && getLexer().is(AsmToken::Slash))
1180 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001181
Devang Patel885f65b2012-01-30 22:47:12 +00001182 if (ExtraImmOp && isParsingIntelSyntax())
1183 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1184
Chris Lattner98c870f2010-11-06 19:25:43 +00001185 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1186 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1187 // documented form in various unofficial manuals, so a lot of code uses it.
1188 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1189 Operands.size() == 3) {
1190 X86Operand &Op = *(X86Operand*)Operands.back();
1191 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1192 isa<MCConstantExpr>(Op.Mem.Disp) &&
1193 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1194 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1195 SMLoc Loc = Op.getEndLoc();
1196 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1197 delete &Op;
1198 }
1199 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001200 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1201 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1202 Operands.size() == 3) {
1203 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1204 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1205 isa<MCConstantExpr>(Op.Mem.Disp) &&
1206 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1207 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1208 SMLoc Loc = Op.getEndLoc();
1209 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1210 delete &Op;
1211 }
1212 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001213 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1214 if (Name.startswith("ins") && Operands.size() == 3 &&
1215 (Name == "insb" || Name == "insw" || Name == "insl")) {
1216 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1217 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1218 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1219 Operands.pop_back();
1220 Operands.pop_back();
1221 delete &Op;
1222 delete &Op2;
1223 }
1224 }
1225
1226 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1227 if (Name.startswith("outs") && Operands.size() == 3 &&
1228 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1229 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1230 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1231 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1232 Operands.pop_back();
1233 Operands.pop_back();
1234 delete &Op;
1235 delete &Op2;
1236 }
1237 }
1238
1239 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1240 if (Name.startswith("movs") && Operands.size() == 3 &&
1241 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001242 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001243 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1244 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1245 if (isSrcOp(Op) && isDstOp(Op2)) {
1246 Operands.pop_back();
1247 Operands.pop_back();
1248 delete &Op;
1249 delete &Op2;
1250 }
1251 }
1252 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1253 if (Name.startswith("lods") && Operands.size() == 3 &&
1254 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001255 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001256 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1257 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1258 if (isSrcOp(*Op1) && Op2->isReg()) {
1259 const char *ins;
1260 unsigned reg = Op2->getReg();
1261 bool isLods = Name == "lods";
1262 if (reg == X86::AL && (isLods || Name == "lodsb"))
1263 ins = "lodsb";
1264 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1265 ins = "lodsw";
1266 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1267 ins = "lodsl";
1268 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1269 ins = "lodsq";
1270 else
1271 ins = NULL;
1272 if (ins != NULL) {
1273 Operands.pop_back();
1274 Operands.pop_back();
1275 delete Op1;
1276 delete Op2;
1277 if (Name != ins)
1278 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1279 }
1280 }
1281 }
1282 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1283 if (Name.startswith("stos") && Operands.size() == 3 &&
1284 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001285 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001286 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1287 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1288 if (isDstOp(*Op2) && Op1->isReg()) {
1289 const char *ins;
1290 unsigned reg = Op1->getReg();
1291 bool isStos = Name == "stos";
1292 if (reg == X86::AL && (isStos || Name == "stosb"))
1293 ins = "stosb";
1294 else if (reg == X86::AX && (isStos || Name == "stosw"))
1295 ins = "stosw";
1296 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1297 ins = "stosl";
1298 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1299 ins = "stosq";
1300 else
1301 ins = NULL;
1302 if (ins != NULL) {
1303 Operands.pop_back();
1304 Operands.pop_back();
1305 delete Op1;
1306 delete Op2;
1307 if (Name != ins)
1308 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1309 }
1310 }
1311 }
1312
Chris Lattnere9e16a32010-09-15 04:33:27 +00001313 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001314 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001315 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001316 Name.startswith("shl") || Name.startswith("sal") ||
1317 Name.startswith("rcl") || Name.startswith("rcr") ||
1318 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001319 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001320 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001321 // Intel syntax
1322 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1323 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001324 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1325 delete Operands[2];
1326 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001327 }
1328 } else {
1329 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1330 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001331 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1332 delete Operands[1];
1333 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001334 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001335 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001336 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001337
Chris Lattner15f89512011-04-09 19:41:05 +00001338 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1339 // instalias with an immediate operand yet.
1340 if (Name == "int" && Operands.size() == 2) {
1341 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1342 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1343 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1344 delete Operands[1];
1345 Operands.erase(Operands.begin() + 1);
1346 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1347 }
1348 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001349
Chris Lattner98986712010-01-14 22:21:20 +00001350 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001351}
1352
Devang Pateldd929fc2012-01-12 18:03:40 +00001353bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001354processInstruction(MCInst &Inst,
1355 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1356 switch (Inst.getOpcode()) {
1357 default: return false;
1358 case X86::AND16i16: {
1359 if (!Inst.getOperand(0).isImm() ||
1360 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1361 return false;
1362
1363 MCInst TmpInst;
1364 TmpInst.setOpcode(X86::AND16ri8);
1365 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1366 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1367 TmpInst.addOperand(Inst.getOperand(0));
1368 Inst = TmpInst;
1369 return true;
1370 }
1371 case X86::AND32i32: {
1372 if (!Inst.getOperand(0).isImm() ||
1373 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1374 return false;
1375
1376 MCInst TmpInst;
1377 TmpInst.setOpcode(X86::AND32ri8);
1378 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1379 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1380 TmpInst.addOperand(Inst.getOperand(0));
1381 Inst = TmpInst;
1382 return true;
1383 }
1384 case X86::AND64i32: {
1385 if (!Inst.getOperand(0).isImm() ||
1386 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1387 return false;
1388
1389 MCInst TmpInst;
1390 TmpInst.setOpcode(X86::AND64ri8);
1391 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1392 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1393 TmpInst.addOperand(Inst.getOperand(0));
1394 Inst = TmpInst;
1395 return true;
1396 }
Devang Patelac0f0482012-01-19 17:53:25 +00001397 case X86::XOR16i16: {
1398 if (!Inst.getOperand(0).isImm() ||
1399 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1400 return false;
1401
1402 MCInst TmpInst;
1403 TmpInst.setOpcode(X86::XOR16ri8);
1404 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1405 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1406 TmpInst.addOperand(Inst.getOperand(0));
1407 Inst = TmpInst;
1408 return true;
1409 }
1410 case X86::XOR32i32: {
1411 if (!Inst.getOperand(0).isImm() ||
1412 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1413 return false;
1414
1415 MCInst TmpInst;
1416 TmpInst.setOpcode(X86::XOR32ri8);
1417 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1418 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1419 TmpInst.addOperand(Inst.getOperand(0));
1420 Inst = TmpInst;
1421 return true;
1422 }
1423 case X86::XOR64i32: {
1424 if (!Inst.getOperand(0).isImm() ||
1425 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1426 return false;
1427
1428 MCInst TmpInst;
1429 TmpInst.setOpcode(X86::XOR64ri8);
1430 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1431 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1432 TmpInst.addOperand(Inst.getOperand(0));
1433 Inst = TmpInst;
1434 return true;
1435 }
1436 case X86::OR16i16: {
1437 if (!Inst.getOperand(0).isImm() ||
1438 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1439 return false;
1440
1441 MCInst TmpInst;
1442 TmpInst.setOpcode(X86::OR16ri8);
1443 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1444 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1445 TmpInst.addOperand(Inst.getOperand(0));
1446 Inst = TmpInst;
1447 return true;
1448 }
1449 case X86::OR32i32: {
1450 if (!Inst.getOperand(0).isImm() ||
1451 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1452 return false;
1453
1454 MCInst TmpInst;
1455 TmpInst.setOpcode(X86::OR32ri8);
1456 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1457 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1458 TmpInst.addOperand(Inst.getOperand(0));
1459 Inst = TmpInst;
1460 return true;
1461 }
1462 case X86::OR64i32: {
1463 if (!Inst.getOperand(0).isImm() ||
1464 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1465 return false;
1466
1467 MCInst TmpInst;
1468 TmpInst.setOpcode(X86::OR64ri8);
1469 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1470 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1471 TmpInst.addOperand(Inst.getOperand(0));
1472 Inst = TmpInst;
1473 return true;
1474 }
1475 case X86::CMP16i16: {
1476 if (!Inst.getOperand(0).isImm() ||
1477 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1478 return false;
1479
1480 MCInst TmpInst;
1481 TmpInst.setOpcode(X86::CMP16ri8);
1482 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1483 TmpInst.addOperand(Inst.getOperand(0));
1484 Inst = TmpInst;
1485 return true;
1486 }
1487 case X86::CMP32i32: {
1488 if (!Inst.getOperand(0).isImm() ||
1489 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1490 return false;
1491
1492 MCInst TmpInst;
1493 TmpInst.setOpcode(X86::CMP32ri8);
1494 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1495 TmpInst.addOperand(Inst.getOperand(0));
1496 Inst = TmpInst;
1497 return true;
1498 }
1499 case X86::CMP64i32: {
1500 if (!Inst.getOperand(0).isImm() ||
1501 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1502 return false;
1503
1504 MCInst TmpInst;
1505 TmpInst.setOpcode(X86::CMP64ri8);
1506 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1507 TmpInst.addOperand(Inst.getOperand(0));
1508 Inst = TmpInst;
1509 return true;
1510 }
Devang Patela951f772012-01-19 18:40:55 +00001511 case X86::ADD16i16: {
1512 if (!Inst.getOperand(0).isImm() ||
1513 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1514 return false;
1515
1516 MCInst TmpInst;
1517 TmpInst.setOpcode(X86::ADD16ri8);
1518 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1519 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1520 TmpInst.addOperand(Inst.getOperand(0));
1521 Inst = TmpInst;
1522 return true;
1523 }
1524 case X86::ADD32i32: {
1525 if (!Inst.getOperand(0).isImm() ||
1526 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1527 return false;
1528
1529 MCInst TmpInst;
1530 TmpInst.setOpcode(X86::ADD32ri8);
1531 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1532 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1533 TmpInst.addOperand(Inst.getOperand(0));
1534 Inst = TmpInst;
1535 return true;
1536 }
1537 case X86::ADD64i32: {
1538 if (!Inst.getOperand(0).isImm() ||
1539 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1540 return false;
1541
1542 MCInst TmpInst;
1543 TmpInst.setOpcode(X86::ADD64ri8);
1544 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1545 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1546 TmpInst.addOperand(Inst.getOperand(0));
1547 Inst = TmpInst;
1548 return true;
1549 }
1550 case X86::SUB16i16: {
1551 if (!Inst.getOperand(0).isImm() ||
1552 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1553 return false;
1554
1555 MCInst TmpInst;
1556 TmpInst.setOpcode(X86::SUB16ri8);
1557 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1558 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1559 TmpInst.addOperand(Inst.getOperand(0));
1560 Inst = TmpInst;
1561 return true;
1562 }
1563 case X86::SUB32i32: {
1564 if (!Inst.getOperand(0).isImm() ||
1565 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1566 return false;
1567
1568 MCInst TmpInst;
1569 TmpInst.setOpcode(X86::SUB32ri8);
1570 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1571 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1572 TmpInst.addOperand(Inst.getOperand(0));
1573 Inst = TmpInst;
1574 return true;
1575 }
1576 case X86::SUB64i32: {
1577 if (!Inst.getOperand(0).isImm() ||
1578 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1579 return false;
1580
1581 MCInst TmpInst;
1582 TmpInst.setOpcode(X86::SUB64ri8);
1583 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1584 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1585 TmpInst.addOperand(Inst.getOperand(0));
1586 Inst = TmpInst;
1587 return true;
1588 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001589 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001590}
1591
1592bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001593MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001594 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001595 MCStreamer &Out, unsigned &ErrorInfo,
1596 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001597 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001598 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1599 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001600 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001601
Chris Lattner7c51a312010-09-29 01:50:45 +00001602 // First, handle aliases that expand to multiple instructions.
1603 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001604 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001605 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001606 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001607 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001608 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001609 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001610 MCInst Inst;
1611 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001612 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001613 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001614 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001615
Chris Lattner0bb83a82010-09-30 16:39:29 +00001616 const char *Repl =
1617 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001618 .Case("finit", "fninit")
1619 .Case("fsave", "fnsave")
1620 .Case("fstcw", "fnstcw")
1621 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001622 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001623 .Case("fstsw", "fnstsw")
1624 .Case("fstsww", "fnstsw")
1625 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001626 .Default(0);
1627 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001628 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001629 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001630 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001631
Chris Lattnera008e8a2010-09-06 21:54:15 +00001632 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001633 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001634
Daniel Dunbarc918d602010-05-04 16:12:42 +00001635 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001636 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001637 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001638 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001639 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001640 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001641 // Some instructions need post-processing to, for example, tweak which
1642 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001643 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001644 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001645 while (processInstruction(Inst, Operands))
1646 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001647
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001648 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001649 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001650 Out.EmitInstruction(Inst);
1651 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001652 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001653 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001654 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001655 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001656 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001657 case Match_InvalidOperand:
1658 WasOriginallyInvalidOperand = true;
1659 break;
1660 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001661 break;
1662 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001663
Daniel Dunbarc918d602010-05-04 16:12:42 +00001664 // FIXME: Ideally, we would only attempt suffix matches for things which are
1665 // valid prefixes, and we could just infer the right unambiguous
1666 // type. However, that requires substantially more matcher support than the
1667 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001668
Daniel Dunbarc918d602010-05-04 16:12:42 +00001669 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001670 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001671 SmallString<16> Tmp;
1672 Tmp += Base;
1673 Tmp += ' ';
1674 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001675
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001676 // If this instruction starts with an 'f', then it is a floating point stack
1677 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1678 // 80-bit floating point, which use the suffixes s,l,t respectively.
1679 //
1680 // Otherwise, we assume that this may be an integer instruction, which comes
1681 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1682 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001683
Daniel Dunbarc918d602010-05-04 16:12:42 +00001684 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001685 Tmp[Base.size()] = Suffixes[0];
1686 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001687 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001688
Chad Rosier6e006d32012-10-12 22:53:36 +00001689 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1690 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001691 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001692 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1693 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001694 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001695 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1696 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001697 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001698 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1699 isParsingIntelSyntax());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001700
1701 // Restore the old token.
1702 Op->setTokenValue(Base);
1703
1704 // If exactly one matched, then we treat that as a successful match (and the
1705 // instruction will already have been filled in correctly, since the failing
1706 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001707 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001708 (Match1 == Match_Success) + (Match2 == Match_Success) +
1709 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001710 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001711 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001712 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001713 Out.EmitInstruction(Inst);
1714 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001715 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001716 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001717
Chris Lattnerec6789f2010-09-06 20:08:02 +00001718 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001719
Daniel Dunbar09062b12010-08-12 00:55:42 +00001720 // If we had multiple suffix matches, then identify this as an ambiguous
1721 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001722 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001723 char MatchChars[4];
1724 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001725 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1726 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1727 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1728 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001729
1730 SmallString<126> Msg;
1731 raw_svector_ostream OS(Msg);
1732 OS << "ambiguous instructions require an explicit suffix (could be ";
1733 for (unsigned i = 0; i != NumMatches; ++i) {
1734 if (i != 0)
1735 OS << ", ";
1736 if (i + 1 == NumMatches)
1737 OS << "or ";
1738 OS << "'" << Base << MatchChars[i] << "'";
1739 }
1740 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001741 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001742 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001743 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001744
Chris Lattnera008e8a2010-09-06 21:54:15 +00001745 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001746
Chris Lattnera008e8a2010-09-06 21:54:15 +00001747 // If all of the instructions reported an invalid mnemonic, then the original
1748 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001749 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1750 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001751 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001752 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001753 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001754 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001755 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001756 }
1757
1758 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001759 if (ErrorInfo != ~0U) {
1760 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001761 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001762 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001763
Chad Rosier84125ca2012-10-13 00:26:04 +00001764 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001765 if (Operand->getStartLoc().isValid()) {
1766 SMRange OperandRange = Operand->getLocRange();
1767 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001768 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001769 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001770 }
1771
Chad Rosierb4fdade2012-08-21 19:36:59 +00001772 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001773 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001774 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001775
Chris Lattnerec6789f2010-09-06 20:08:02 +00001776 // If one instruction matched with a missing feature, report this as a
1777 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001778 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1779 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001780 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001781 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001782 return true;
1783 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001784
Chris Lattnera008e8a2010-09-06 21:54:15 +00001785 // If one instruction matched with an invalid operand, report this as an
1786 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001787 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1788 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001789 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001790 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001791 return true;
1792 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001793
Chris Lattnerec6789f2010-09-06 20:08:02 +00001794 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001795 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001796 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001797 return true;
1798}
1799
1800
Devang Pateldd929fc2012-01-12 18:03:40 +00001801bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001802 StringRef IDVal = DirectiveID.getIdentifier();
1803 if (IDVal == ".word")
1804 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001805 else if (IDVal.startswith(".code"))
1806 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001807 else if (IDVal.startswith(".att_syntax")) {
1808 getParser().setAssemblerDialect(0);
1809 return false;
1810 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001811 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001812 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1813 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001814 // FIXME : Handle noprefix
1815 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001816 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001817 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001818 }
1819 return false;
1820 }
Chris Lattner537ca842010-10-30 17:38:55 +00001821 return true;
1822}
1823
1824/// ParseDirectiveWord
1825/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001826bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001827 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1828 for (;;) {
1829 const MCExpr *Value;
1830 if (getParser().ParseExpression(Value))
1831 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001832
Chris Lattner537ca842010-10-30 17:38:55 +00001833 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001834
Chris Lattner537ca842010-10-30 17:38:55 +00001835 if (getLexer().is(AsmToken::EndOfStatement))
1836 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001837
Chris Lattner537ca842010-10-30 17:38:55 +00001838 // FIXME: Improve diagnostic.
1839 if (getLexer().isNot(AsmToken::Comma))
1840 return Error(L, "unexpected token in directive");
1841 Parser.Lex();
1842 }
1843 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001844
Chris Lattner537ca842010-10-30 17:38:55 +00001845 Parser.Lex();
1846 return false;
1847}
1848
Evan Chengbd27f5a2011-07-27 00:38:12 +00001849/// ParseDirectiveCode
1850/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001851bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001852 if (IDVal == ".code32") {
1853 Parser.Lex();
1854 if (is64BitMode()) {
1855 SwitchMode();
1856 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1857 }
1858 } else if (IDVal == ".code64") {
1859 Parser.Lex();
1860 if (!is64BitMode()) {
1861 SwitchMode();
1862 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1863 }
1864 } else {
1865 return Error(L, "unexpected directive " + IDVal);
1866 }
Chris Lattner537ca842010-10-30 17:38:55 +00001867
Evan Chengbd27f5a2011-07-27 00:38:12 +00001868 return false;
1869}
Chris Lattner537ca842010-10-30 17:38:55 +00001870
1871
Sean Callanane88f5522010-01-23 02:43:15 +00001872extern "C" void LLVMInitializeX86AsmLexer();
1873
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001874// Force static initialization.
1875extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001876 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1877 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001878 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001879}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001880
Chris Lattner0692ee62010-09-06 19:11:01 +00001881#define GET_REGISTER_MATCHER
1882#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001883#include "X86GenAsmMatcher.inc"