blob: 63cce6cbc869f532d7cb668a1a547d53502c3ba9 [file] [log] [blame]
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
11#include "llvm/MC/MCTargetAsmParser.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000012#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000013#include "llvm/MC/MCExpr.h"
Chad Rosier96d58e62012-10-19 20:57:14 +000014#include "llvm/MC/MCSymbol.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000015#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000016#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000017#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000023#include "llvm/ADT/StringSwitch.h"
24#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000025#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000027#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000028
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000029using namespace llvm;
30
31namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000032struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000033
Devang Pateldd929fc2012-01-12 18:03:40 +000034class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000035 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036 MCAsmParser &Parser;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038 MCAsmParser &getParser() const { return Parser; }
39
40 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
41
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000042 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000043 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000044 bool MatchingInlineAsm = false) {
45 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000046 return Parser.Error(L, Msg, Ranges);
47 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000048
Devang Pateld37ad242012-01-17 18:00:18 +000049 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
50 Error(Loc, Msg);
51 return 0;
52 }
53
Chris Lattner309264d2010-01-15 18:44:13 +000054 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000055 X86Operand *ParseATTOperand();
56 X86Operand *ParseIntelOperand();
Chad Rosier5b0f1b32012-10-04 23:59:38 +000057 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000058 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000059 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000060
61 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000062 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000063
Devang Patelb8ba13f2012-01-18 22:42:29 +000064 bool processInstruction(MCInst &Inst,
65 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
66
Chad Rosier84125ca2012-10-13 00:26:04 +000067 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000068 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000069 MCStreamer &Out, unsigned &ErrorInfo,
70 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000071
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000072 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000073 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000074 bool isSrcOp(X86Operand &Op);
75
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000076 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
77 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000078 bool isDstOp(X86Operand &Op);
79
Evan Cheng59ee62d2011-07-11 03:57:24 +000080 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000081 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000082 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000083 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000084 void SwitchMode() {
85 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
86 setAvailableFeatures(FB);
87 }
Evan Chengebdeeab2011-07-08 01:53:10 +000088
Daniel Dunbar54074b52010-07-19 05:44:09 +000089 /// @name Auto-generated Matcher Functions
90 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000091
Chris Lattner0692ee62010-09-06 19:11:01 +000092#define GET_ASSEMBLER_HEADER
93#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000094
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000095 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000096
97public:
Devang Pateldd929fc2012-01-12 18:03:40 +000098 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +000099 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000100
Daniel Dunbar54074b52010-07-19 05:44:09 +0000101 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000102 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000103 }
Roman Divackybf755322011-01-27 17:14:22 +0000104 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000105
Benjamin Kramer38e59892010-07-14 22:38:02 +0000106 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000107 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000108
109 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000110
111 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000112 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000113 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000114};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000115} // end anonymous namespace
116
Sean Callanane9b466d2010-01-23 00:40:33 +0000117/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000118/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000119
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000120static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000121
122/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000123
Craig Topper76bd9382012-07-18 04:59:16 +0000124static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000125 return (( Value <= 0x000000000000007FULL)||
126 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
127 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
128}
129
130static bool isImmSExti32i8Value(uint64_t Value) {
131 return (( Value <= 0x000000000000007FULL)||
132 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
133 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
134}
135
136static bool isImmZExtu32u8Value(uint64_t Value) {
137 return (Value <= 0x00000000000000FFULL);
138}
139
140static bool isImmSExti64i8Value(uint64_t Value) {
141 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000142 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000143}
144
145static bool isImmSExti64i32Value(uint64_t Value) {
146 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000147 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000148}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000149namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000150
151/// X86Operand - Instances of this class represent a parsed X86 machine
152/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000153struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000154 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000155 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000156 Register,
157 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000158 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000159 } Kind;
160
Chris Lattner29ef9a22010-01-15 18:51:29 +0000161 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000162
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000163 union {
164 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000165 const char *Data;
166 unsigned Length;
167 } Tok;
168
169 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000170 unsigned RegNo;
171 } Reg;
172
173 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000174 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000175 } Imm;
176
177 struct {
178 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000179 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000180 unsigned BaseReg;
181 unsigned IndexReg;
182 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000183 unsigned Size;
Chad Rosier65c88922012-10-22 19:42:52 +0000184 bool OffsetOf;
Chad Rosier96d58e62012-10-19 20:57:14 +0000185 bool NeedSizeDir;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000186 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000187 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000188
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000189 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000190 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000191
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000192 /// getStartLoc - Get the location of the first token of this operand.
193 SMLoc getStartLoc() const { return StartLoc; }
194 /// getEndLoc - Get the location of the last token of this operand.
195 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000196 /// getLocRange - Get the range between the first and last token of this
197 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000198 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000199
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000200 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000201
Daniel Dunbar20927f22009-08-07 08:26:05 +0000202 StringRef getToken() const {
203 assert(Kind == Token && "Invalid access!");
204 return StringRef(Tok.Data, Tok.Length);
205 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000206 void setTokenValue(StringRef Value) {
207 assert(Kind == Token && "Invalid access!");
208 Tok.Data = Value.data();
209 Tok.Length = Value.size();
210 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000211
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000212 unsigned getReg() const {
213 assert(Kind == Register && "Invalid access!");
214 return Reg.RegNo;
215 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000216
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000217 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000218 assert(Kind == Immediate && "Invalid access!");
219 return Imm.Val;
220 }
221
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000222 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000223 assert(Kind == Memory && "Invalid access!");
224 return Mem.Disp;
225 }
226 unsigned getMemSegReg() const {
227 assert(Kind == Memory && "Invalid access!");
228 return Mem.SegReg;
229 }
230 unsigned getMemBaseReg() const {
231 assert(Kind == Memory && "Invalid access!");
232 return Mem.BaseReg;
233 }
234 unsigned getMemIndexReg() const {
235 assert(Kind == Memory && "Invalid access!");
236 return Mem.IndexReg;
237 }
238 unsigned getMemScale() const {
239 assert(Kind == Memory && "Invalid access!");
240 return Mem.Scale;
241 }
242
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000243 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000244
245 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000246
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000247 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000248 if (!isImm())
249 return false;
250
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000251 // If this isn't a constant expr, just assume it fits and let relaxation
252 // handle it.
253 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
254 if (!CE)
255 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000256
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000257 // Otherwise, check the value is in a range that makes sense for this
258 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000259 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000260 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000261 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000262 if (!isImm())
263 return false;
264
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000265 // If this isn't a constant expr, just assume it fits and let relaxation
266 // handle it.
267 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
268 if (!CE)
269 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000270
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000271 // Otherwise, check the value is in a range that makes sense for this
272 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000273 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000274 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000275 bool isImmZExtu32u8() const {
276 if (!isImm())
277 return false;
278
279 // If this isn't a constant expr, just assume it fits and let relaxation
280 // handle it.
281 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
282 if (!CE)
283 return true;
284
285 // Otherwise, check the value is in a range that makes sense for this
286 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000287 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000288 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000289 bool isImmSExti64i8() const {
290 if (!isImm())
291 return false;
292
293 // If this isn't a constant expr, just assume it fits and let relaxation
294 // handle it.
295 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
296 if (!CE)
297 return true;
298
299 // Otherwise, check the value is in a range that makes sense for this
300 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000301 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000302 }
303 bool isImmSExti64i32() const {
304 if (!isImm())
305 return false;
306
307 // If this isn't a constant expr, just assume it fits and let relaxation
308 // handle it.
309 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
310 if (!CE)
311 return true;
312
313 // Otherwise, check the value is in a range that makes sense for this
314 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000315 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000316 }
317
Chad Rosier96d58e62012-10-19 20:57:14 +0000318 unsigned getMemSize() const {
319 assert(Kind == Memory && "Invalid access!");
320 return Mem.Size;
321 }
322
323 bool needSizeDirective() const {
324 assert(Kind == Memory && "Invalid access!");
325 return Mem.NeedSizeDir;
326 }
327
Daniel Dunbar20927f22009-08-07 08:26:05 +0000328 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000329 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000330 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000331 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000332 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000333 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000334 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000335 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000336 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000337 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000338 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000339 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000340 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000341 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000342 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000343 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000344 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000345 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000346 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000347 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000348 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000349 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000350
Craig Topper75dc33a2012-07-18 04:11:12 +0000351 bool isMemVX32() const {
352 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
353 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
354 }
355 bool isMemVY32() const {
356 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
357 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
358 }
359 bool isMemVX64() const {
360 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
361 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
362 }
363 bool isMemVY64() const {
364 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
365 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
366 }
367
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000368 bool isAbsMem() const {
369 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000370 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000371 }
372
Daniel Dunbar20927f22009-08-07 08:26:05 +0000373 bool isReg() const { return Kind == Register; }
374
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000375 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
376 // Add as immediates when possible.
377 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
378 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
379 else
380 Inst.addOperand(MCOperand::CreateExpr(Expr));
381 }
382
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000383 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000384 assert(N == 1 && "Invalid number of operands!");
385 Inst.addOperand(MCOperand::CreateReg(getReg()));
386 }
387
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000388 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000389 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000390 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000391 }
392
Chad Rosier36b8fed2012-06-27 22:34:28 +0000393 void addMem8Operands(MCInst &Inst, unsigned N) const {
394 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000395 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000396 void addMem16Operands(MCInst &Inst, unsigned N) const {
397 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000398 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000399 void addMem32Operands(MCInst &Inst, unsigned N) const {
400 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000401 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000402 void addMem64Operands(MCInst &Inst, unsigned N) const {
403 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000404 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000405 void addMem80Operands(MCInst &Inst, unsigned N) const {
406 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000407 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000408 void addMem128Operands(MCInst &Inst, unsigned N) const {
409 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000410 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000411 void addMem256Operands(MCInst &Inst, unsigned N) const {
412 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000413 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000414 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
415 addMemOperands(Inst, N);
416 }
417 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
418 addMemOperands(Inst, N);
419 }
420 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
421 addMemOperands(Inst, N);
422 }
423 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
424 addMemOperands(Inst, N);
425 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000426
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000427 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000428 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000429 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
430 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
431 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000432 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000433 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
434 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000435
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000436 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
437 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000438 // Add as immediates when possible.
439 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
440 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
441 else
442 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000443 }
444
Chris Lattnerb4307b32010-01-15 19:28:38 +0000445 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000446 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
447 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000448 Res->Tok.Data = Str.data();
449 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000450 return Res;
451 }
452
Chris Lattner29ef9a22010-01-15 18:51:29 +0000453 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000454 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000455 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000456 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000457 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000458
Chris Lattnerb4307b32010-01-15 19:28:38 +0000459 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
460 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000461 Res->Imm.Val = Val;
462 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000463 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000464
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000465 /// Create an absolute memory operand.
466 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Chad Rosier96d58e62012-10-19 20:57:14 +0000467 SMLoc EndLoc, unsigned Size = 0,
Chad Rosier65c88922012-10-22 19:42:52 +0000468 bool OffsetOf = false, bool NeedSizeDir = false){
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000469 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
470 Res->Mem.SegReg = 0;
471 Res->Mem.Disp = Disp;
472 Res->Mem.BaseReg = 0;
473 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000474 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000475 Res->Mem.Size = Size;
Chad Rosier65c88922012-10-22 19:42:52 +0000476 Res->Mem.OffsetOf = OffsetOf;
Chad Rosier96d58e62012-10-19 20:57:14 +0000477 Res->Mem.NeedSizeDir = NeedSizeDir;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000478 return Res;
479 }
480
481 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000482 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
483 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000484 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier65c88922012-10-22 19:42:52 +0000485 unsigned Size = 0, bool OffsetOf = false,
486 bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000487 // We should never just have a displacement, that should be parsed as an
488 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000489 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
490
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000491 // The scale should always be one of {1,2,4,8}.
492 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000493 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000494 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000495 Res->Mem.SegReg = SegReg;
496 Res->Mem.Disp = Disp;
497 Res->Mem.BaseReg = BaseReg;
498 Res->Mem.IndexReg = IndexReg;
499 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000500 Res->Mem.Size = Size;
Chad Rosier65c88922012-10-22 19:42:52 +0000501 Res->Mem.OffsetOf = OffsetOf;
Chad Rosier96d58e62012-10-19 20:57:14 +0000502 Res->Mem.NeedSizeDir = NeedSizeDir;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000503 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000504 }
505};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000506
Chris Lattner37dfdec2009-07-29 06:33:53 +0000507} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000508
Devang Pateldd929fc2012-01-12 18:03:40 +0000509bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000510 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000511
512 return (Op.isMem() &&
513 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
514 isa<MCConstantExpr>(Op.Mem.Disp) &&
515 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
516 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
517}
518
Devang Pateldd929fc2012-01-12 18:03:40 +0000519bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000520 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000521
Chad Rosier36b8fed2012-06-27 22:34:28 +0000522 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000523 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000524 isa<MCConstantExpr>(Op.Mem.Disp) &&
525 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
526 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
527}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000528
Devang Pateldd929fc2012-01-12 18:03:40 +0000529bool X86AsmParser::ParseRegister(unsigned &RegNo,
530 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000531 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000532 const AsmToken &PercentTok = Parser.getTok();
533 StartLoc = PercentTok.getLoc();
534
535 // If we encounter a %, ignore it. This code handles registers with and
536 // without the prefix, unprefixed registers can occur in cfi directives.
537 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000538 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000539
Sean Callanan18b83232010-01-19 21:44:56 +0000540 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000541 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000542 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000543 return Error(StartLoc, "invalid register name",
544 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000545 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000546
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000547 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000548
Chris Lattner33d60d52010-09-22 04:11:10 +0000549 // If the match failed, try the register name as lowercase.
550 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000551 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000552
Evan Cheng5de728c2011-07-27 23:22:03 +0000553 if (!is64BitMode()) {
554 // FIXME: This should be done using Requires<In32BitMode> and
555 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
556 // checked.
557 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
558 // REX prefix.
559 if (RegNo == X86::RIZ ||
560 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
561 X86II::isX86_64NonExtLowByteReg(RegNo) ||
562 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000563 return Error(StartLoc, "register %"
564 + Tok.getString() + " is only available in 64-bit mode",
565 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000566 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000567
Chris Lattner33d60d52010-09-22 04:11:10 +0000568 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
569 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000570 RegNo = X86::ST0;
571 EndLoc = Tok.getLoc();
572 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000573
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000574 // Check to see if we have '(4)' after %st.
575 if (getLexer().isNot(AsmToken::LParen))
576 return false;
577 // Lex the paren.
578 getParser().Lex();
579
580 const AsmToken &IntTok = Parser.getTok();
581 if (IntTok.isNot(AsmToken::Integer))
582 return Error(IntTok.getLoc(), "expected stack index");
583 switch (IntTok.getIntVal()) {
584 case 0: RegNo = X86::ST0; break;
585 case 1: RegNo = X86::ST1; break;
586 case 2: RegNo = X86::ST2; break;
587 case 3: RegNo = X86::ST3; break;
588 case 4: RegNo = X86::ST4; break;
589 case 5: RegNo = X86::ST5; break;
590 case 6: RegNo = X86::ST6; break;
591 case 7: RegNo = X86::ST7; break;
592 default: return Error(IntTok.getLoc(), "invalid stack index");
593 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000594
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000595 if (getParser().Lex().isNot(AsmToken::RParen))
596 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000597
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000598 EndLoc = Tok.getLoc();
599 Parser.Lex(); // Eat ')'
600 return false;
601 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000602
Chris Lattner645b2092010-06-24 07:29:18 +0000603 // If this is "db[0-7]", match it as an alias
604 // for dr[0-7].
605 if (RegNo == 0 && Tok.getString().size() == 3 &&
606 Tok.getString().startswith("db")) {
607 switch (Tok.getString()[2]) {
608 case '0': RegNo = X86::DR0; break;
609 case '1': RegNo = X86::DR1; break;
610 case '2': RegNo = X86::DR2; break;
611 case '3': RegNo = X86::DR3; break;
612 case '4': RegNo = X86::DR4; break;
613 case '5': RegNo = X86::DR5; break;
614 case '6': RegNo = X86::DR6; break;
615 case '7': RegNo = X86::DR7; break;
616 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000617
Chris Lattner645b2092010-06-24 07:29:18 +0000618 if (RegNo != 0) {
619 EndLoc = Tok.getLoc();
620 Parser.Lex(); // Eat it.
621 return false;
622 }
623 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000624
Devang Patel1aea4302012-01-20 22:32:05 +0000625 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000626 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000627 return Error(StartLoc, "invalid register name",
628 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000629 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000630
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000631 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000632 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000633 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000634}
635
Devang Pateldd929fc2012-01-12 18:03:40 +0000636X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000637 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000638 return ParseIntelOperand();
639 return ParseATTOperand();
640}
641
Devang Pateld37ad242012-01-17 18:00:18 +0000642/// getIntelMemOperandSize - Return intel memory operand size.
643static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000644 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000645 .Cases("BYTE", "byte", 8)
646 .Cases("WORD", "word", 16)
647 .Cases("DWORD", "dword", 32)
648 .Cases("QWORD", "qword", 64)
649 .Cases("XWORD", "xword", 80)
650 .Cases("XMMWORD", "xmmword", 128)
651 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000652 .Default(0);
653 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000654}
655
Chad Rosier65c88922012-10-22 19:42:52 +0000656X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000657 unsigned Size) {
658 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000659 SMLoc Start = Parser.getTok().getLoc(), End;
660
Devang Pateld37ad242012-01-17 18:00:18 +0000661 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
662 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
663
664 // Eat '['
665 if (getLexer().isNot(AsmToken::LBrac))
666 return ErrorOperand(Start, "Expected '[' token!");
667 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000668
Devang Pateld37ad242012-01-17 18:00:18 +0000669 if (getLexer().is(AsmToken::Identifier)) {
670 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000671 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000672 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000673 if (getParser().ParseExpression(Disp, End)) return 0;
674 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000675 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000676 Parser.Lex();
677 return X86Operand::CreateMem(Disp, Start, End, Size);
678 }
679 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000680 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000681 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000682 SMLoc Loc = Parser.getTok().getLoc();
683 if (getLexer().is(AsmToken::RBrac)) {
684 // Handle '[' number ']'
685 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000686 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
687 if (SegReg)
688 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
689 Start, End, Size);
690 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000691 } else if (getLexer().is(AsmToken::Star)) {
692 // Handle '[' Scale*IndexReg ']'
693 Parser.Lex();
694 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000695 if (ParseRegister(IndexReg, IdxRegLoc, End))
696 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000697 Scale = Val;
698 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000699 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000700 }
701
702 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
703 bool isPlus = getLexer().is(AsmToken::Plus);
704 Parser.Lex();
705 SMLoc PlusLoc = Parser.getTok().getLoc();
706 if (getLexer().is(AsmToken::Integer)) {
707 int64_t Val = Parser.getTok().getIntVal();
708 Parser.Lex();
709 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000710 Parser.Lex();
711 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000712 if (ParseRegister(IndexReg, IdxRegLoc, End))
713 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000714 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000715 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000716 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000717 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000718 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000719 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000720 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000721 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000722 End = Parser.getTok().getLoc();
723 if (!IndexReg)
724 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000725 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000726 }
Devang Pateld37ad242012-01-17 18:00:18 +0000727 }
728
729 if (getLexer().isNot(AsmToken::RBrac))
730 if (getParser().ParseExpression(Disp, End)) return 0;
731
732 End = Parser.getTok().getLoc();
733 if (getLexer().isNot(AsmToken::RBrac))
734 return ErrorOperand(End, "expected ']' token!");
735 Parser.Lex();
736 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000737
738 // handle [-42]
739 if (!BaseReg && !IndexReg)
740 return X86Operand::CreateMem(Disp, Start, End, Size);
741
Devang Pateld37ad242012-01-17 18:00:18 +0000742 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000743 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000744}
745
746/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000747X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000748 const AsmToken &Tok = Parser.getTok();
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000749 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000750
751 unsigned Size = getIntelMemOperandSize(Tok.getString());
752 if (Size) {
753 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000754 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
755 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000756 Parser.Lex();
757 }
758
Chad Rosier65c88922012-10-22 19:42:52 +0000759 // Parse the 'offset' operator. This operator is used to specify the
760 // location rather then the content of a variable.
761 bool OffsetOf = false;
762 if(isParsingInlineAsm() && (Tok.getString() == "offset" ||
763 Tok.getString() == "OFFSET")) {
764 OffsetOf = true;
765 Parser.Lex(); // Eat offset.
766 }
767
768 if (getLexer().is(AsmToken::LBrac)) {
769 assert (!OffsetOf && "Unexpected offset operator!");
Devang Patel7c64fe62012-01-23 18:31:58 +0000770 return ParseIntelBracExpression(SegReg, Size);
Chad Rosier65c88922012-10-22 19:42:52 +0000771 }
Devang Patel7c64fe62012-01-23 18:31:58 +0000772
773 if (!ParseRegister(SegReg, Start, End)) {
Chad Rosier65c88922012-10-22 19:42:52 +0000774 assert (!OffsetOf && "Unexpected offset operator!");
Devang Patel7c64fe62012-01-23 18:31:58 +0000775 // Handel SegReg : [ ... ]
776 if (getLexer().isNot(AsmToken::Colon))
777 return ErrorOperand(Start, "Expected ':' token!");
778 Parser.Lex(); // Eat :
779 if (getLexer().isNot(AsmToken::LBrac))
780 return ErrorOperand(Start, "Expected '[' token!");
781 return ParseIntelBracExpression(SegReg, Size);
782 }
Devang Pateld37ad242012-01-17 18:00:18 +0000783
784 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
785 if (getParser().ParseExpression(Disp, End)) return 0;
Chad Rosierce353b32012-10-15 17:26:38 +0000786 End = Parser.getTok().getLoc();
Chad Rosier96d58e62012-10-19 20:57:14 +0000787
788 bool NeedSizeDir = false;
789 if (!Size && isParsingInlineAsm()) {
790 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
791 const MCSymbol &Sym = SymRef->getSymbol();
792 // FIXME: The SemaLookup will fail if the name is anything other then an
793 // identifier.
794 // FIXME: Pass a valid SMLoc.
795 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Size);
796 NeedSizeDir = Size > 0;
797 }
798 }
Chad Rosier65c88922012-10-22 19:42:52 +0000799 return X86Operand::CreateMem(Disp, Start, End, Size, OffsetOf, NeedSizeDir);
Devang Pateld37ad242012-01-17 18:00:18 +0000800}
801
802X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000803 SMLoc Start = Parser.getTok().getLoc(), End;
804
805 // immediate.
806 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
807 getLexer().is(AsmToken::Minus)) {
808 const MCExpr *Val;
809 if (!getParser().ParseExpression(Val, End)) {
810 End = Parser.getTok().getLoc();
811 return X86Operand::CreateImm(Val, Start, End);
812 }
813 }
814
Devang Patel0a338862012-01-12 01:36:43 +0000815 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000816 unsigned RegNo = 0;
817 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000818 // If this is a segment register followed by a ':', then this is the start
819 // of a memory reference, otherwise this is a normal register reference.
820 if (getLexer().isNot(AsmToken::Colon))
821 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
822
823 getParser().Lex(); // Eat the colon.
824 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000825 }
826
827 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000828 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000829}
830
Devang Pateldd929fc2012-01-12 18:03:40 +0000831X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000832 switch (getLexer().getKind()) {
833 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000834 // Parse a memory operand with no segment register.
835 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000836 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000837 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000838 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000839 SMLoc Start, End;
840 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000841 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000842 Error(Start, "%eiz and %riz can only be used as index registers",
843 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000844 return 0;
845 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000846
Chris Lattnereef6d782010-04-17 18:56:34 +0000847 // If this is a segment register followed by a ':', then this is the start
848 // of a memory reference, otherwise this is a normal register reference.
849 if (getLexer().isNot(AsmToken::Colon))
850 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000851
852
Chris Lattnereef6d782010-04-17 18:56:34 +0000853 getParser().Lex(); // Eat the colon.
854 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000855 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000856 case AsmToken::Dollar: {
857 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000858 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000859 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000860 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000861 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000862 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000863 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000864 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000865 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000866}
867
Chris Lattnereef6d782010-04-17 18:56:34 +0000868/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
869/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000870X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000871
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000872 // We have to disambiguate a parenthesized expression "(4+5)" from the start
873 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000874 // only way to do this without lookahead is to eat the '(' and see what is
875 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000876 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000877 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000878 SMLoc ExprEnd;
879 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000880
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000881 // After parsing the base expression we could either have a parenthesized
882 // memory address or not. If not, return now. If so, eat the (.
883 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000884 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000885 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000886 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000887 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000888 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000889
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000890 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000891 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000892 } else {
893 // Okay, we have a '('. We don't know if this is an expression or not, but
894 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000895 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000896 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000897
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000898 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000899 // Nothing to do here, fall into the code below with the '(' part of the
900 // memory operand consumed.
901 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000902 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000903
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000904 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000905 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000906 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000907
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000908 // After parsing the base expression we could either have a parenthesized
909 // memory address or not. If not, return now. If so, eat the (.
910 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000911 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000912 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000913 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000914 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000915 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000916
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000917 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000918 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000919 }
920 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000921
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000922 // If we reached here, then we just ate the ( of the memory operand. Process
923 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000924 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000925 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000926
Chris Lattner29ef9a22010-01-15 18:51:29 +0000927 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000928 SMLoc StartLoc, EndLoc;
929 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000930 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000931 Error(StartLoc, "eiz and riz can only be used as index registers",
932 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000933 return 0;
934 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000935 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000936
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000937 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000938 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000939 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000940
941 // Following the comma we should have either an index register, or a scale
942 // value. We don't support the later form, but we want to parse it
943 // correctly.
944 //
945 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000946 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000947 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000948 SMLoc L;
949 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000950
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000951 if (getLexer().isNot(AsmToken::RParen)) {
952 // Parse the scale amount:
953 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000954 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000955 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000956 "expected comma in scale expression");
957 return 0;
958 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000959 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000960
961 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000962 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000963
964 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000965 if (getParser().ParseAbsoluteExpression(ScaleVal)){
966 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000967 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +0000968 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000969
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000970 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000971 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
972 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
973 return 0;
974 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000975 Scale = (unsigned)ScaleVal;
976 }
977 }
978 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000979 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000980 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000981 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000982
983 int64_t Value;
984 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000985 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000986
Daniel Dunbaree910252010-08-24 19:13:38 +0000987 if (Value != 1)
988 Warning(Loc, "scale factor without index register is ignored");
989 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000990 }
991 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000992
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000993 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000994 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000995 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000996 return 0;
997 }
Sean Callanan18b83232010-01-19 21:44:56 +0000998 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000999 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001000
Kevin Enderby84faf652012-03-12 21:32:09 +00001001 // If we have both a base register and an index register make sure they are
1002 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001003 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001004 if (BaseReg != 0 && IndexReg != 0) {
1005 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001006 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1007 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001008 IndexReg != X86::RIZ) {
1009 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1010 return 0;
1011 }
1012 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001013 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1014 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001015 IndexReg != X86::EIZ){
1016 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1017 return 0;
1018 }
1019 }
1020
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001021 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1022 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001023}
1024
Devang Pateldd929fc2012-01-12 18:03:40 +00001025bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +00001026ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001027 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +00001028 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001029
Chris Lattnerd8f71792010-11-28 20:23:50 +00001030 // FIXME: Hack to recognize setneb as setne.
1031 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1032 PatchedName != "setb" && PatchedName != "setnb")
1033 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001034
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001035 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1036 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001037 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001038 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1039 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001040 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001041 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001042 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001043 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001044 .Case("eq", 0x00)
1045 .Case("lt", 0x01)
1046 .Case("le", 0x02)
1047 .Case("unord", 0x03)
1048 .Case("neq", 0x04)
1049 .Case("nlt", 0x05)
1050 .Case("nle", 0x06)
1051 .Case("ord", 0x07)
1052 /* AVX only from here */
1053 .Case("eq_uq", 0x08)
1054 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001055 .Case("ngt", 0x0A)
1056 .Case("false", 0x0B)
1057 .Case("neq_oq", 0x0C)
1058 .Case("ge", 0x0D)
1059 .Case("gt", 0x0E)
1060 .Case("true", 0x0F)
1061 .Case("eq_os", 0x10)
1062 .Case("lt_oq", 0x11)
1063 .Case("le_oq", 0x12)
1064 .Case("unord_s", 0x13)
1065 .Case("neq_us", 0x14)
1066 .Case("nlt_uq", 0x15)
1067 .Case("nle_uq", 0x16)
1068 .Case("ord_s", 0x17)
1069 .Case("eq_us", 0x18)
1070 .Case("nge_uq", 0x19)
1071 .Case("ngt_uq", 0x1A)
1072 .Case("false_os", 0x1B)
1073 .Case("neq_os", 0x1C)
1074 .Case("ge_oq", 0x1D)
1075 .Case("gt_oq", 0x1E)
1076 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001077 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001078 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001079 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1080 getParser().getContext());
1081 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001082 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001083 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001084 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001085 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001086 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001087 } else {
1088 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001089 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001090 }
1091 }
1092 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001093
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001094 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001095
Devang Patel885f65b2012-01-30 22:47:12 +00001096 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001097 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001098
Chris Lattner2544f422010-09-08 05:17:37 +00001099 // Determine whether this is an instruction prefix.
1100 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001101 Name == "lock" || Name == "rep" ||
1102 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001103 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001104 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001105
1106
Chris Lattner2544f422010-09-08 05:17:37 +00001107 // This does the actual operand parsing. Don't parse any more if we have a
1108 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1109 // just want to parse the "lock" as the first instruction and the "incl" as
1110 // the next one.
1111 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001112
1113 // Parse '*' modifier.
1114 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001115 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001116 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001117 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001118 }
1119
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001120 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001121 if (X86Operand *Op = ParseOperand())
1122 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001123 else {
1124 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001125 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001126 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001127
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001128 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001129 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001130
1131 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001132 if (X86Operand *Op = ParseOperand())
1133 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001134 else {
1135 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001136 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001137 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001138 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001139
Chris Lattnercbf8a982010-09-11 16:18:25 +00001140 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001141 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001142 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001143 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001144 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001145 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001146
Chris Lattner2544f422010-09-08 05:17:37 +00001147 if (getLexer().is(AsmToken::EndOfStatement))
1148 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001149 else if (isPrefix && getLexer().is(AsmToken::Slash))
1150 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001151
Devang Patel885f65b2012-01-30 22:47:12 +00001152 if (ExtraImmOp && isParsingIntelSyntax())
1153 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1154
Chris Lattner98c870f2010-11-06 19:25:43 +00001155 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1156 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1157 // documented form in various unofficial manuals, so a lot of code uses it.
1158 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1159 Operands.size() == 3) {
1160 X86Operand &Op = *(X86Operand*)Operands.back();
1161 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1162 isa<MCConstantExpr>(Op.Mem.Disp) &&
1163 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1164 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1165 SMLoc Loc = Op.getEndLoc();
1166 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1167 delete &Op;
1168 }
1169 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001170 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1171 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1172 Operands.size() == 3) {
1173 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1174 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1175 isa<MCConstantExpr>(Op.Mem.Disp) &&
1176 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1177 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1178 SMLoc Loc = Op.getEndLoc();
1179 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1180 delete &Op;
1181 }
1182 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001183 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1184 if (Name.startswith("ins") && Operands.size() == 3 &&
1185 (Name == "insb" || Name == "insw" || Name == "insl")) {
1186 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1187 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1188 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1189 Operands.pop_back();
1190 Operands.pop_back();
1191 delete &Op;
1192 delete &Op2;
1193 }
1194 }
1195
1196 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1197 if (Name.startswith("outs") && Operands.size() == 3 &&
1198 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1199 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1200 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1201 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1202 Operands.pop_back();
1203 Operands.pop_back();
1204 delete &Op;
1205 delete &Op2;
1206 }
1207 }
1208
1209 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1210 if (Name.startswith("movs") && Operands.size() == 3 &&
1211 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001212 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001213 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1214 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1215 if (isSrcOp(Op) && isDstOp(Op2)) {
1216 Operands.pop_back();
1217 Operands.pop_back();
1218 delete &Op;
1219 delete &Op2;
1220 }
1221 }
1222 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1223 if (Name.startswith("lods") && Operands.size() == 3 &&
1224 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001225 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001226 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1227 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1228 if (isSrcOp(*Op1) && Op2->isReg()) {
1229 const char *ins;
1230 unsigned reg = Op2->getReg();
1231 bool isLods = Name == "lods";
1232 if (reg == X86::AL && (isLods || Name == "lodsb"))
1233 ins = "lodsb";
1234 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1235 ins = "lodsw";
1236 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1237 ins = "lodsl";
1238 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1239 ins = "lodsq";
1240 else
1241 ins = NULL;
1242 if (ins != NULL) {
1243 Operands.pop_back();
1244 Operands.pop_back();
1245 delete Op1;
1246 delete Op2;
1247 if (Name != ins)
1248 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1249 }
1250 }
1251 }
1252 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1253 if (Name.startswith("stos") && Operands.size() == 3 &&
1254 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001255 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
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 (isDstOp(*Op2) && Op1->isReg()) {
1259 const char *ins;
1260 unsigned reg = Op1->getReg();
1261 bool isStos = Name == "stos";
1262 if (reg == X86::AL && (isStos || Name == "stosb"))
1263 ins = "stosb";
1264 else if (reg == X86::AX && (isStos || Name == "stosw"))
1265 ins = "stosw";
1266 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1267 ins = "stosl";
1268 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1269 ins = "stosq";
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
Chris Lattnere9e16a32010-09-15 04:33:27 +00001283 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001284 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001285 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001286 Name.startswith("shl") || Name.startswith("sal") ||
1287 Name.startswith("rcl") || Name.startswith("rcr") ||
1288 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001289 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001290 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001291 // Intel syntax
1292 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1293 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001294 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1295 delete Operands[2];
1296 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001297 }
1298 } else {
1299 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1300 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001301 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1302 delete Operands[1];
1303 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001304 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001305 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001306 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001307
Chris Lattner15f89512011-04-09 19:41:05 +00001308 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1309 // instalias with an immediate operand yet.
1310 if (Name == "int" && Operands.size() == 2) {
1311 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1312 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1313 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1314 delete Operands[1];
1315 Operands.erase(Operands.begin() + 1);
1316 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1317 }
1318 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001319
Chris Lattner98986712010-01-14 22:21:20 +00001320 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001321}
1322
Devang Pateldd929fc2012-01-12 18:03:40 +00001323bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001324processInstruction(MCInst &Inst,
1325 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1326 switch (Inst.getOpcode()) {
1327 default: return false;
1328 case X86::AND16i16: {
1329 if (!Inst.getOperand(0).isImm() ||
1330 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1331 return false;
1332
1333 MCInst TmpInst;
1334 TmpInst.setOpcode(X86::AND16ri8);
1335 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1336 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1337 TmpInst.addOperand(Inst.getOperand(0));
1338 Inst = TmpInst;
1339 return true;
1340 }
1341 case X86::AND32i32: {
1342 if (!Inst.getOperand(0).isImm() ||
1343 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1344 return false;
1345
1346 MCInst TmpInst;
1347 TmpInst.setOpcode(X86::AND32ri8);
1348 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1349 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1350 TmpInst.addOperand(Inst.getOperand(0));
1351 Inst = TmpInst;
1352 return true;
1353 }
1354 case X86::AND64i32: {
1355 if (!Inst.getOperand(0).isImm() ||
1356 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1357 return false;
1358
1359 MCInst TmpInst;
1360 TmpInst.setOpcode(X86::AND64ri8);
1361 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1362 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1363 TmpInst.addOperand(Inst.getOperand(0));
1364 Inst = TmpInst;
1365 return true;
1366 }
Devang Patelac0f0482012-01-19 17:53:25 +00001367 case X86::XOR16i16: {
1368 if (!Inst.getOperand(0).isImm() ||
1369 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1370 return false;
1371
1372 MCInst TmpInst;
1373 TmpInst.setOpcode(X86::XOR16ri8);
1374 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1375 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1376 TmpInst.addOperand(Inst.getOperand(0));
1377 Inst = TmpInst;
1378 return true;
1379 }
1380 case X86::XOR32i32: {
1381 if (!Inst.getOperand(0).isImm() ||
1382 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1383 return false;
1384
1385 MCInst TmpInst;
1386 TmpInst.setOpcode(X86::XOR32ri8);
1387 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1388 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1389 TmpInst.addOperand(Inst.getOperand(0));
1390 Inst = TmpInst;
1391 return true;
1392 }
1393 case X86::XOR64i32: {
1394 if (!Inst.getOperand(0).isImm() ||
1395 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1396 return false;
1397
1398 MCInst TmpInst;
1399 TmpInst.setOpcode(X86::XOR64ri8);
1400 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1401 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1402 TmpInst.addOperand(Inst.getOperand(0));
1403 Inst = TmpInst;
1404 return true;
1405 }
1406 case X86::OR16i16: {
1407 if (!Inst.getOperand(0).isImm() ||
1408 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1409 return false;
1410
1411 MCInst TmpInst;
1412 TmpInst.setOpcode(X86::OR16ri8);
1413 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1414 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1415 TmpInst.addOperand(Inst.getOperand(0));
1416 Inst = TmpInst;
1417 return true;
1418 }
1419 case X86::OR32i32: {
1420 if (!Inst.getOperand(0).isImm() ||
1421 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1422 return false;
1423
1424 MCInst TmpInst;
1425 TmpInst.setOpcode(X86::OR32ri8);
1426 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1427 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1428 TmpInst.addOperand(Inst.getOperand(0));
1429 Inst = TmpInst;
1430 return true;
1431 }
1432 case X86::OR64i32: {
1433 if (!Inst.getOperand(0).isImm() ||
1434 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1435 return false;
1436
1437 MCInst TmpInst;
1438 TmpInst.setOpcode(X86::OR64ri8);
1439 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1440 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1441 TmpInst.addOperand(Inst.getOperand(0));
1442 Inst = TmpInst;
1443 return true;
1444 }
1445 case X86::CMP16i16: {
1446 if (!Inst.getOperand(0).isImm() ||
1447 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1448 return false;
1449
1450 MCInst TmpInst;
1451 TmpInst.setOpcode(X86::CMP16ri8);
1452 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1453 TmpInst.addOperand(Inst.getOperand(0));
1454 Inst = TmpInst;
1455 return true;
1456 }
1457 case X86::CMP32i32: {
1458 if (!Inst.getOperand(0).isImm() ||
1459 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1460 return false;
1461
1462 MCInst TmpInst;
1463 TmpInst.setOpcode(X86::CMP32ri8);
1464 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1465 TmpInst.addOperand(Inst.getOperand(0));
1466 Inst = TmpInst;
1467 return true;
1468 }
1469 case X86::CMP64i32: {
1470 if (!Inst.getOperand(0).isImm() ||
1471 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1472 return false;
1473
1474 MCInst TmpInst;
1475 TmpInst.setOpcode(X86::CMP64ri8);
1476 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1477 TmpInst.addOperand(Inst.getOperand(0));
1478 Inst = TmpInst;
1479 return true;
1480 }
Devang Patela951f772012-01-19 18:40:55 +00001481 case X86::ADD16i16: {
1482 if (!Inst.getOperand(0).isImm() ||
1483 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1484 return false;
1485
1486 MCInst TmpInst;
1487 TmpInst.setOpcode(X86::ADD16ri8);
1488 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1489 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1490 TmpInst.addOperand(Inst.getOperand(0));
1491 Inst = TmpInst;
1492 return true;
1493 }
1494 case X86::ADD32i32: {
1495 if (!Inst.getOperand(0).isImm() ||
1496 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1497 return false;
1498
1499 MCInst TmpInst;
1500 TmpInst.setOpcode(X86::ADD32ri8);
1501 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1502 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1503 TmpInst.addOperand(Inst.getOperand(0));
1504 Inst = TmpInst;
1505 return true;
1506 }
1507 case X86::ADD64i32: {
1508 if (!Inst.getOperand(0).isImm() ||
1509 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1510 return false;
1511
1512 MCInst TmpInst;
1513 TmpInst.setOpcode(X86::ADD64ri8);
1514 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1515 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1516 TmpInst.addOperand(Inst.getOperand(0));
1517 Inst = TmpInst;
1518 return true;
1519 }
1520 case X86::SUB16i16: {
1521 if (!Inst.getOperand(0).isImm() ||
1522 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1523 return false;
1524
1525 MCInst TmpInst;
1526 TmpInst.setOpcode(X86::SUB16ri8);
1527 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1528 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1529 TmpInst.addOperand(Inst.getOperand(0));
1530 Inst = TmpInst;
1531 return true;
1532 }
1533 case X86::SUB32i32: {
1534 if (!Inst.getOperand(0).isImm() ||
1535 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1536 return false;
1537
1538 MCInst TmpInst;
1539 TmpInst.setOpcode(X86::SUB32ri8);
1540 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1541 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1542 TmpInst.addOperand(Inst.getOperand(0));
1543 Inst = TmpInst;
1544 return true;
1545 }
1546 case X86::SUB64i32: {
1547 if (!Inst.getOperand(0).isImm() ||
1548 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1549 return false;
1550
1551 MCInst TmpInst;
1552 TmpInst.setOpcode(X86::SUB64ri8);
1553 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1554 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1555 TmpInst.addOperand(Inst.getOperand(0));
1556 Inst = TmpInst;
1557 return true;
1558 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001559 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001560}
1561
1562bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001563MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001564 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001565 MCStreamer &Out, unsigned &ErrorInfo,
1566 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001567 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001568 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1569 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001570 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001571
Chris Lattner7c51a312010-09-29 01:50:45 +00001572 // First, handle aliases that expand to multiple instructions.
1573 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001574 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001575 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001576 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001577 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001578 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001579 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001580 MCInst Inst;
1581 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001582 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001583 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001584 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001585
Chris Lattner0bb83a82010-09-30 16:39:29 +00001586 const char *Repl =
1587 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001588 .Case("finit", "fninit")
1589 .Case("fsave", "fnsave")
1590 .Case("fstcw", "fnstcw")
1591 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001592 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001593 .Case("fstsw", "fnstsw")
1594 .Case("fstsww", "fnstsw")
1595 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001596 .Default(0);
1597 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001598 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001599 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001600 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001601
Chris Lattnera008e8a2010-09-06 21:54:15 +00001602 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001603 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001604
Daniel Dunbarc918d602010-05-04 16:12:42 +00001605 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001606 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001607 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001608 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001609 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001610 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001611 // Some instructions need post-processing to, for example, tweak which
1612 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001613 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001614 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001615 while (processInstruction(Inst, Operands))
1616 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001617
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001618 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001619 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001620 Out.EmitInstruction(Inst);
1621 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001622 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001623 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001624 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001625 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001626 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001627 case Match_InvalidOperand:
1628 WasOriginallyInvalidOperand = true;
1629 break;
1630 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001631 break;
1632 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001633
Daniel Dunbarc918d602010-05-04 16:12:42 +00001634 // FIXME: Ideally, we would only attempt suffix matches for things which are
1635 // valid prefixes, and we could just infer the right unambiguous
1636 // type. However, that requires substantially more matcher support than the
1637 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001638
Daniel Dunbarc918d602010-05-04 16:12:42 +00001639 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001640 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001641 SmallString<16> Tmp;
1642 Tmp += Base;
1643 Tmp += ' ';
1644 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001645
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001646 // If this instruction starts with an 'f', then it is a floating point stack
1647 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1648 // 80-bit floating point, which use the suffixes s,l,t respectively.
1649 //
1650 // Otherwise, we assume that this may be an integer instruction, which comes
1651 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1652 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001653
Daniel Dunbarc918d602010-05-04 16:12:42 +00001654 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001655 Tmp[Base.size()] = Suffixes[0];
1656 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001657 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001658
Chad Rosier6e006d32012-10-12 22:53:36 +00001659 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1660 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001661 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001662 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1663 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001664 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001665 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1666 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001667 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001668 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1669 isParsingIntelSyntax());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001670
1671 // Restore the old token.
1672 Op->setTokenValue(Base);
1673
1674 // If exactly one matched, then we treat that as a successful match (and the
1675 // instruction will already have been filled in correctly, since the failing
1676 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001677 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001678 (Match1 == Match_Success) + (Match2 == Match_Success) +
1679 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001680 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001681 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001682 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001683 Out.EmitInstruction(Inst);
1684 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001685 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001686 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001687
Chris Lattnerec6789f2010-09-06 20:08:02 +00001688 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001689
Daniel Dunbar09062b12010-08-12 00:55:42 +00001690 // If we had multiple suffix matches, then identify this as an ambiguous
1691 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001692 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001693 char MatchChars[4];
1694 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001695 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1696 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1697 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1698 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001699
1700 SmallString<126> Msg;
1701 raw_svector_ostream OS(Msg);
1702 OS << "ambiguous instructions require an explicit suffix (could be ";
1703 for (unsigned i = 0; i != NumMatches; ++i) {
1704 if (i != 0)
1705 OS << ", ";
1706 if (i + 1 == NumMatches)
1707 OS << "or ";
1708 OS << "'" << Base << MatchChars[i] << "'";
1709 }
1710 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001711 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001712 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001713 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001714
Chris Lattnera008e8a2010-09-06 21:54:15 +00001715 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001716
Chris Lattnera008e8a2010-09-06 21:54:15 +00001717 // If all of the instructions reported an invalid mnemonic, then the original
1718 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001719 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1720 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001721 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001722 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001723 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001724 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001725 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001726 }
1727
1728 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001729 if (ErrorInfo != ~0U) {
1730 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001731 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001732 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001733
Chad Rosier84125ca2012-10-13 00:26:04 +00001734 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001735 if (Operand->getStartLoc().isValid()) {
1736 SMRange OperandRange = Operand->getLocRange();
1737 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001738 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001739 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001740 }
1741
Chad Rosierb4fdade2012-08-21 19:36:59 +00001742 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001743 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001744 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001745
Chris Lattnerec6789f2010-09-06 20:08:02 +00001746 // If one instruction matched with a missing feature, report this as a
1747 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001748 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1749 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001750 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001751 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001752 return true;
1753 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001754
Chris Lattnera008e8a2010-09-06 21:54:15 +00001755 // If one instruction matched with an invalid operand, report this as an
1756 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001757 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1758 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001759 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001760 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001761 return true;
1762 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001763
Chris Lattnerec6789f2010-09-06 20:08:02 +00001764 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001765 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001766 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001767 return true;
1768}
1769
1770
Devang Pateldd929fc2012-01-12 18:03:40 +00001771bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001772 StringRef IDVal = DirectiveID.getIdentifier();
1773 if (IDVal == ".word")
1774 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001775 else if (IDVal.startswith(".code"))
1776 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001777 else if (IDVal.startswith(".att_syntax")) {
1778 getParser().setAssemblerDialect(0);
1779 return false;
1780 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001781 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001782 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1783 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001784 // FIXME : Handle noprefix
1785 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001786 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001787 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001788 }
1789 return false;
1790 }
Chris Lattner537ca842010-10-30 17:38:55 +00001791 return true;
1792}
1793
1794/// ParseDirectiveWord
1795/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001796bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001797 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1798 for (;;) {
1799 const MCExpr *Value;
1800 if (getParser().ParseExpression(Value))
1801 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001802
Chris Lattner537ca842010-10-30 17:38:55 +00001803 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001804
Chris Lattner537ca842010-10-30 17:38:55 +00001805 if (getLexer().is(AsmToken::EndOfStatement))
1806 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001807
Chris Lattner537ca842010-10-30 17:38:55 +00001808 // FIXME: Improve diagnostic.
1809 if (getLexer().isNot(AsmToken::Comma))
1810 return Error(L, "unexpected token in directive");
1811 Parser.Lex();
1812 }
1813 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001814
Chris Lattner537ca842010-10-30 17:38:55 +00001815 Parser.Lex();
1816 return false;
1817}
1818
Evan Chengbd27f5a2011-07-27 00:38:12 +00001819/// ParseDirectiveCode
1820/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001821bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001822 if (IDVal == ".code32") {
1823 Parser.Lex();
1824 if (is64BitMode()) {
1825 SwitchMode();
1826 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1827 }
1828 } else if (IDVal == ".code64") {
1829 Parser.Lex();
1830 if (!is64BitMode()) {
1831 SwitchMode();
1832 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1833 }
1834 } else {
1835 return Error(L, "unexpected directive " + IDVal);
1836 }
Chris Lattner537ca842010-10-30 17:38:55 +00001837
Evan Chengbd27f5a2011-07-27 00:38:12 +00001838 return false;
1839}
Chris Lattner537ca842010-10-30 17:38:55 +00001840
1841
Sean Callanane88f5522010-01-23 02:43:15 +00001842extern "C" void LLVMInitializeX86AsmLexer();
1843
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001844// Force static initialization.
1845extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001846 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1847 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001848 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001849}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001850
Chris Lattner0692ee62010-09-06 19:11:01 +00001851#define GET_REGISTER_MATCHER
1852#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001853#include "X86GenAsmMatcher.inc"