blob: 8c4c447df90a3abde3db518b29a2bd0bd1092e3e [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"
Chad Rosier4284e172012-10-24 22:13:37 +000011#include "llvm/ADT/APFloat.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000012#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000014#include "llvm/ADT/StringSwitch.h"
15#include "llvm/ADT/Twine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
18#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
21#include "llvm/MC/MCRegisterInfo.h"
22#include "llvm/MC/MCStreamer.h"
23#include "llvm/MC/MCSubtargetInfo.h"
24#include "llvm/MC/MCSymbol.h"
25#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000028#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000029
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000030using namespace llvm;
31
32namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000033struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000034
Devang Pateldd929fc2012-01-12 18:03:40 +000035class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000036 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &Parser;
Chad Rosier6a020a72012-10-25 20:41:34 +000038 ParseInstructionInfo *InstInfo;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000040 MCAsmParser &getParser() const { return Parser; }
41
42 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000044 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000045 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000046 bool MatchingInlineAsm = false) {
47 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000048 return Parser.Error(L, Msg, Ranges);
49 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000050
Devang Pateld37ad242012-01-17 18:00:18 +000051 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
52 Error(Loc, Msg);
53 return 0;
54 }
55
Chris Lattner309264d2010-01-15 18:44:13 +000056 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000057 X86Operand *ParseATTOperand();
58 X86Operand *ParseIntelOperand();
Chad Rosierc0a14b82012-10-24 17:22:29 +000059 X86Operand *ParseIntelOffsetOfOperator(SMLoc StartLoc);
Chad Rosier505bca32013-01-17 19:21:48 +000060 X86Operand *ParseIntelOperator(SMLoc StartLoc, unsigned OpKind);
Chad Rosier5b0f1b32012-10-04 23:59:38 +000061 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000062 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000063 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000064
Chad Rosier5e6b37f2012-10-25 17:37:43 +000065 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr **NewDisp,
66 SmallString<64> &Err);
Chad Rosier22f441a2012-10-24 22:21:50 +000067
Kevin Enderby9c656452009-09-10 20:51:44 +000068 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000069 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000070
Devang Patelb8ba13f2012-01-18 22:42:29 +000071 bool processInstruction(MCInst &Inst,
72 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
73
Chad Rosier84125ca2012-10-13 00:26:04 +000074 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000075 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000076 MCStreamer &Out, unsigned &ErrorInfo,
77 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000078
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000079 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000080 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000081 bool isSrcOp(X86Operand &Op);
82
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000083 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
84 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000085 bool isDstOp(X86Operand &Op);
86
Evan Cheng59ee62d2011-07-11 03:57:24 +000087 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000088 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000089 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000090 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000091 void SwitchMode() {
92 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
93 setAvailableFeatures(FB);
94 }
Evan Chengebdeeab2011-07-08 01:53:10 +000095
Daniel Dunbar54074b52010-07-19 05:44:09 +000096 /// @name Auto-generated Matcher Functions
97 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000098
Chris Lattner0692ee62010-09-06 19:11:01 +000099#define GET_ASSEMBLER_HEADER
100#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000101
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000102 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000103
104public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000105 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosier6a020a72012-10-25 20:41:34 +0000106 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000107
Daniel Dunbar54074b52010-07-19 05:44:09 +0000108 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000109 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000110 }
Roman Divackybf755322011-01-27 17:14:22 +0000111 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000112
Chad Rosier6a020a72012-10-25 20:41:34 +0000113 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
114 SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000115 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000116
117 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000118
119 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000120 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000121 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000122};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000123} // end anonymous namespace
124
Sean Callanane9b466d2010-01-23 00:40:33 +0000125/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000126/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000127
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000128static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000129
130/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000131
Craig Topper76bd9382012-07-18 04:59:16 +0000132static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000133 return (( Value <= 0x000000000000007FULL)||
134 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
135 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
136}
137
138static bool isImmSExti32i8Value(uint64_t Value) {
139 return (( Value <= 0x000000000000007FULL)||
140 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
141 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
142}
143
144static bool isImmZExtu32u8Value(uint64_t Value) {
145 return (Value <= 0x00000000000000FFULL);
146}
147
148static bool isImmSExti64i8Value(uint64_t Value) {
149 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000150 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000151}
152
153static bool isImmSExti64i32Value(uint64_t Value) {
154 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000155 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000156}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000157namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000158
159/// X86Operand - Instances of this class represent a parsed X86 machine
160/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000161struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000162 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000163 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000164 Register,
165 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000166 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000167 } Kind;
168
Chris Lattner29ef9a22010-01-15 18:51:29 +0000169 SMLoc StartLoc, EndLoc;
Chad Rosier5a719fc2012-10-23 17:43:43 +0000170 SMLoc OffsetOfLoc;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000171 bool AddressOf;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000172
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000173 union {
174 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000175 const char *Data;
176 unsigned Length;
177 } Tok;
178
179 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000180 unsigned RegNo;
181 } Reg;
182
183 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000184 const MCExpr *Val;
Chad Rosierefcb3d92012-10-26 18:04:20 +0000185 bool NeedAsmRewrite;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000186 } Imm;
187
188 struct {
189 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000190 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000191 unsigned BaseReg;
192 unsigned IndexReg;
193 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000194 unsigned Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000195 bool NeedSizeDir;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000196 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000197 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000198
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000199 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000200 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000201
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000202 /// getStartLoc - Get the location of the first token of this operand.
203 SMLoc getStartLoc() const { return StartLoc; }
204 /// getEndLoc - Get the location of the last token of this operand.
205 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000206 /// getLocRange - Get the range between the first and last token of this
207 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000208 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000209 /// getOffsetOfLoc - Get the location of the offset operator.
210 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000211
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000212 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000213
Daniel Dunbar20927f22009-08-07 08:26:05 +0000214 StringRef getToken() const {
215 assert(Kind == Token && "Invalid access!");
216 return StringRef(Tok.Data, Tok.Length);
217 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000218 void setTokenValue(StringRef Value) {
219 assert(Kind == Token && "Invalid access!");
220 Tok.Data = Value.data();
221 Tok.Length = Value.size();
222 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000223
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000224 unsigned getReg() const {
225 assert(Kind == Register && "Invalid access!");
226 return Reg.RegNo;
227 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000228
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000229 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000230 assert(Kind == Immediate && "Invalid access!");
231 return Imm.Val;
232 }
233
Chad Rosierefcb3d92012-10-26 18:04:20 +0000234 bool needAsmRewrite() const {
235 assert(Kind == Immediate && "Invalid access!");
236 return Imm.NeedAsmRewrite;
237 }
238
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000239 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000240 assert(Kind == Memory && "Invalid access!");
241 return Mem.Disp;
242 }
243 unsigned getMemSegReg() const {
244 assert(Kind == Memory && "Invalid access!");
245 return Mem.SegReg;
246 }
247 unsigned getMemBaseReg() const {
248 assert(Kind == Memory && "Invalid access!");
249 return Mem.BaseReg;
250 }
251 unsigned getMemIndexReg() const {
252 assert(Kind == Memory && "Invalid access!");
253 return Mem.IndexReg;
254 }
255 unsigned getMemScale() const {
256 assert(Kind == Memory && "Invalid access!");
257 return Mem.Scale;
258 }
259
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000260 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000261
262 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000263
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000264 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000265 if (!isImm())
266 return false;
267
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000268 // If this isn't a constant expr, just assume it fits and let relaxation
269 // handle it.
270 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
271 if (!CE)
272 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000273
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000274 // Otherwise, check the value is in a range that makes sense for this
275 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000276 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000277 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000278 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000279 if (!isImm())
280 return false;
281
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000282 // If this isn't a constant expr, just assume it fits and let relaxation
283 // handle it.
284 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
285 if (!CE)
286 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000287
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000288 // Otherwise, check the value is in a range that makes sense for this
289 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000290 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000291 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000292 bool isImmZExtu32u8() const {
293 if (!isImm())
294 return false;
295
296 // If this isn't a constant expr, just assume it fits and let relaxation
297 // handle it.
298 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
299 if (!CE)
300 return true;
301
302 // Otherwise, check the value is in a range that makes sense for this
303 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000304 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000305 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000306 bool isImmSExti64i8() const {
307 if (!isImm())
308 return false;
309
310 // If this isn't a constant expr, just assume it fits and let relaxation
311 // handle it.
312 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
313 if (!CE)
314 return true;
315
316 // Otherwise, check the value is in a range that makes sense for this
317 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000318 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000319 }
320 bool isImmSExti64i32() const {
321 if (!isImm())
322 return false;
323
324 // If this isn't a constant expr, just assume it fits and let relaxation
325 // handle it.
326 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
327 if (!CE)
328 return true;
329
330 // Otherwise, check the value is in a range that makes sense for this
331 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000332 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000333 }
334
Chad Rosier96d58e62012-10-19 20:57:14 +0000335 unsigned getMemSize() const {
336 assert(Kind == Memory && "Invalid access!");
337 return Mem.Size;
338 }
339
Chad Rosiera703fb92012-10-22 19:50:35 +0000340 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000341 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000342 }
343
Chad Rosierc1ec2072013-01-10 22:10:27 +0000344 bool needAddressOf() const {
345 return AddressOf;
346 }
347
Chad Rosier96d58e62012-10-19 20:57:14 +0000348 bool needSizeDirective() const {
349 assert(Kind == Memory && "Invalid access!");
350 return Mem.NeedSizeDir;
351 }
352
Daniel Dunbar20927f22009-08-07 08:26:05 +0000353 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000354 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000355 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000356 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000357 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000358 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000359 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000360 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000361 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000362 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000363 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000364 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000365 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000366 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000367 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000368 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000369 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000370 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000371 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000372 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000373 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000374 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000375
Craig Topper75dc33a2012-07-18 04:11:12 +0000376 bool isMemVX32() const {
377 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
378 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
379 }
380 bool isMemVY32() const {
381 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
382 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
383 }
384 bool isMemVX64() const {
385 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
386 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
387 }
388 bool isMemVY64() const {
389 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
390 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
391 }
392
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000393 bool isAbsMem() const {
394 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000395 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000396 }
397
Daniel Dunbar20927f22009-08-07 08:26:05 +0000398 bool isReg() const { return Kind == Register; }
399
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000400 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
401 // Add as immediates when possible.
402 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
403 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
404 else
405 Inst.addOperand(MCOperand::CreateExpr(Expr));
406 }
407
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000408 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000409 assert(N == 1 && "Invalid number of operands!");
410 Inst.addOperand(MCOperand::CreateReg(getReg()));
411 }
412
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000413 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000414 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000415 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000416 }
417
Chad Rosier36b8fed2012-06-27 22:34:28 +0000418 void addMem8Operands(MCInst &Inst, unsigned N) const {
419 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000420 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000421 void addMem16Operands(MCInst &Inst, unsigned N) const {
422 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000423 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000424 void addMem32Operands(MCInst &Inst, unsigned N) const {
425 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000426 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000427 void addMem64Operands(MCInst &Inst, unsigned N) const {
428 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000429 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000430 void addMem80Operands(MCInst &Inst, unsigned N) const {
431 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000432 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000433 void addMem128Operands(MCInst &Inst, unsigned N) const {
434 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000435 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000436 void addMem256Operands(MCInst &Inst, unsigned N) const {
437 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000438 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000439 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
440 addMemOperands(Inst, N);
441 }
442 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
443 addMemOperands(Inst, N);
444 }
445 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
446 addMemOperands(Inst, N);
447 }
448 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
449 addMemOperands(Inst, N);
450 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000451
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000452 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000453 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000454 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
455 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
456 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000457 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000458 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
459 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000460
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000461 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
462 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000463 // Add as immediates when possible.
464 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
465 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
466 else
467 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000468 }
469
Chris Lattnerb4307b32010-01-15 19:28:38 +0000470 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000471 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000472 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000473 Res->Tok.Data = Str.data();
474 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000475 return Res;
476 }
477
Chad Rosierc0a14b82012-10-24 17:22:29 +0000478 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +0000479 bool AddressOf = false,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000480 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000481 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000482 Res->Reg.RegNo = RegNo;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000483 Res->AddressOf = AddressOf;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000484 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000485 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000486 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000487
Chad Rosierefcb3d92012-10-26 18:04:20 +0000488 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc,
489 bool NeedRewrite = true){
Chris Lattnerb4307b32010-01-15 19:28:38 +0000490 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000491 Res->Imm.Val = Val;
Chad Rosierefcb3d92012-10-26 18:04:20 +0000492 Res->Imm.NeedAsmRewrite = NeedRewrite;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000493 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000494 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000495
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000496 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000497 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier7109fbe2013-01-10 23:39:07 +0000498 unsigned Size = 0, bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000499 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
500 Res->Mem.SegReg = 0;
501 Res->Mem.Disp = Disp;
502 Res->Mem.BaseReg = 0;
503 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000504 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000505 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000506 Res->Mem.NeedSizeDir = NeedSizeDir;
Chad Rosier7109fbe2013-01-10 23:39:07 +0000507 Res->AddressOf = false;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000508 return Res;
509 }
510
511 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000512 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
513 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000514 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000515 unsigned Size = 0, bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000516 // We should never just have a displacement, that should be parsed as an
517 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000518 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
519
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000520 // The scale should always be one of {1,2,4,8}.
521 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000522 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000523 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000524 Res->Mem.SegReg = SegReg;
525 Res->Mem.Disp = Disp;
526 Res->Mem.BaseReg = BaseReg;
527 Res->Mem.IndexReg = IndexReg;
528 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000529 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000530 Res->Mem.NeedSizeDir = NeedSizeDir;
NAKAMURA Takumib789b942013-01-11 01:13:54 +0000531 Res->AddressOf = false;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000532 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000533 }
534};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000535
Chris Lattner37dfdec2009-07-29 06:33:53 +0000536} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000537
Devang Pateldd929fc2012-01-12 18:03:40 +0000538bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000539 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000540
541 return (Op.isMem() &&
542 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
543 isa<MCConstantExpr>(Op.Mem.Disp) &&
544 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
545 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
546}
547
Devang Pateldd929fc2012-01-12 18:03:40 +0000548bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000549 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000550
Chad Rosier36b8fed2012-06-27 22:34:28 +0000551 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000552 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000553 isa<MCConstantExpr>(Op.Mem.Disp) &&
554 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
555 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
556}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000557
Devang Pateldd929fc2012-01-12 18:03:40 +0000558bool X86AsmParser::ParseRegister(unsigned &RegNo,
559 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000560 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000561 const AsmToken &PercentTok = Parser.getTok();
562 StartLoc = PercentTok.getLoc();
563
564 // If we encounter a %, ignore it. This code handles registers with and
565 // without the prefix, unprefixed registers can occur in cfi directives.
566 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000567 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000568
Sean Callanan18b83232010-01-19 21:44:56 +0000569 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000570 EndLoc = Tok.getEndLoc();
571
Devang Patel1aea4302012-01-20 22:32:05 +0000572 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000573 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000574 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000575 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000576 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000577
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000578 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000579
Chris Lattner33d60d52010-09-22 04:11:10 +0000580 // If the match failed, try the register name as lowercase.
581 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000582 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000583
Evan Cheng5de728c2011-07-27 23:22:03 +0000584 if (!is64BitMode()) {
585 // FIXME: This should be done using Requires<In32BitMode> and
586 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
587 // checked.
588 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
589 // REX prefix.
590 if (RegNo == X86::RIZ ||
591 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
592 X86II::isX86_64NonExtLowByteReg(RegNo) ||
593 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000594 return Error(StartLoc, "register %"
595 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000596 SMRange(StartLoc, EndLoc));
Evan Cheng5de728c2011-07-27 23:22:03 +0000597 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000598
Chris Lattner33d60d52010-09-22 04:11:10 +0000599 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
600 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000601 RegNo = X86::ST0;
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000602 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000603
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000604 // Check to see if we have '(4)' after %st.
605 if (getLexer().isNot(AsmToken::LParen))
606 return false;
607 // Lex the paren.
608 getParser().Lex();
609
610 const AsmToken &IntTok = Parser.getTok();
611 if (IntTok.isNot(AsmToken::Integer))
612 return Error(IntTok.getLoc(), "expected stack index");
613 switch (IntTok.getIntVal()) {
614 case 0: RegNo = X86::ST0; break;
615 case 1: RegNo = X86::ST1; break;
616 case 2: RegNo = X86::ST2; break;
617 case 3: RegNo = X86::ST3; break;
618 case 4: RegNo = X86::ST4; break;
619 case 5: RegNo = X86::ST5; break;
620 case 6: RegNo = X86::ST6; break;
621 case 7: RegNo = X86::ST7; break;
622 default: return Error(IntTok.getLoc(), "invalid stack index");
623 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000624
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000625 if (getParser().Lex().isNot(AsmToken::RParen))
626 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000627
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000628 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000629 Parser.Lex(); // Eat ')'
630 return false;
631 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000632
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000633 EndLoc = Parser.getTok().getEndLoc();
634
Chris Lattner645b2092010-06-24 07:29:18 +0000635 // If this is "db[0-7]", match it as an alias
636 // for dr[0-7].
637 if (RegNo == 0 && Tok.getString().size() == 3 &&
638 Tok.getString().startswith("db")) {
639 switch (Tok.getString()[2]) {
640 case '0': RegNo = X86::DR0; break;
641 case '1': RegNo = X86::DR1; break;
642 case '2': RegNo = X86::DR2; break;
643 case '3': RegNo = X86::DR3; break;
644 case '4': RegNo = X86::DR4; break;
645 case '5': RegNo = X86::DR5; break;
646 case '6': RegNo = X86::DR6; break;
647 case '7': RegNo = X86::DR7; break;
648 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000649
Chris Lattner645b2092010-06-24 07:29:18 +0000650 if (RegNo != 0) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000651 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner645b2092010-06-24 07:29:18 +0000652 Parser.Lex(); // Eat it.
653 return false;
654 }
655 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000656
Devang Patel1aea4302012-01-20 22:32:05 +0000657 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000658 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000659 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000660 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000661 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000662
Sean Callananb9a25b72010-01-19 20:27:46 +0000663 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000664 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000665}
666
Devang Pateldd929fc2012-01-12 18:03:40 +0000667X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000668 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000669 return ParseIntelOperand();
670 return ParseATTOperand();
671}
672
Devang Pateld37ad242012-01-17 18:00:18 +0000673/// getIntelMemOperandSize - Return intel memory operand size.
674static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000675 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000676 .Cases("BYTE", "byte", 8)
677 .Cases("WORD", "word", 16)
678 .Cases("DWORD", "dword", 32)
679 .Cases("QWORD", "qword", 64)
680 .Cases("XWORD", "xword", 80)
681 .Cases("XMMWORD", "xmmword", 128)
682 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000683 .Default(0);
684 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000685}
686
Chad Rosierdd2e8952013-01-14 22:31:35 +0000687enum IntelBracExprState {
688 IBES_START,
689 IBES_LBRAC,
690 IBES_RBRAC,
691 IBES_REGISTER,
692 IBES_REGISTER_STAR,
693 IBES_REGISTER_STAR_INTEGER,
694 IBES_INTEGER,
695 IBES_INTEGER_STAR,
696 IBES_INDEX_REGISTER,
697 IBES_IDENTIFIER,
698 IBES_DISP_EXPR,
699 IBES_MINUS,
700 IBES_ERROR
701};
702
703class IntelBracExprStateMachine {
704 IntelBracExprState State;
705 unsigned BaseReg, IndexReg, Scale;
706 int64_t Disp;
707
708 unsigned TmpReg;
709 int64_t TmpInteger;
710
711 bool isPlus;
712
713public:
714 IntelBracExprStateMachine(MCAsmParser &parser) :
715 State(IBES_START), BaseReg(0), IndexReg(0), Scale(1), Disp(0),
716 TmpReg(0), TmpInteger(0), isPlus(true) {}
717
718 unsigned getBaseReg() { return BaseReg; }
719 unsigned getIndexReg() { return IndexReg; }
720 unsigned getScale() { return Scale; }
721 int64_t getDisp() { return Disp; }
722 bool isValidEndState() { return State == IBES_RBRAC; }
723
724 void onPlus() {
725 switch (State) {
726 default:
727 State = IBES_ERROR;
728 break;
729 case IBES_INTEGER:
730 State = IBES_START;
731 if (isPlus)
732 Disp += TmpInteger;
733 else
734 Disp -= TmpInteger;
735 break;
736 case IBES_REGISTER:
737 State = IBES_START;
738 // If we already have a BaseReg, then assume this is the IndexReg with a
739 // scale of 1.
740 if (!BaseReg) {
741 BaseReg = TmpReg;
742 } else {
743 assert (!IndexReg && "BaseReg/IndexReg already set!");
744 IndexReg = TmpReg;
745 Scale = 1;
746 }
747 break;
748 case IBES_INDEX_REGISTER:
749 State = IBES_START;
750 break;
751 }
752 isPlus = true;
753 }
754 void onMinus() {
755 switch (State) {
756 default:
757 State = IBES_ERROR;
758 break;
759 case IBES_START:
760 State = IBES_MINUS;
761 break;
762 case IBES_INTEGER:
763 State = IBES_START;
764 if (isPlus)
765 Disp += TmpInteger;
766 else
767 Disp -= TmpInteger;
768 break;
769 case IBES_REGISTER:
770 State = IBES_START;
771 // If we already have a BaseReg, then assume this is the IndexReg with a
772 // scale of 1.
773 if (!BaseReg) {
774 BaseReg = TmpReg;
775 } else {
776 assert (!IndexReg && "BaseReg/IndexReg already set!");
777 IndexReg = TmpReg;
778 Scale = 1;
779 }
780 break;
781 case IBES_INDEX_REGISTER:
782 State = IBES_START;
783 break;
784 }
785 isPlus = false;
786 }
787 void onRegister(unsigned Reg) {
788 switch (State) {
789 default:
790 State = IBES_ERROR;
791 break;
792 case IBES_START:
793 State = IBES_REGISTER;
794 TmpReg = Reg;
795 break;
796 case IBES_INTEGER_STAR:
797 assert (!IndexReg && "IndexReg already set!");
798 State = IBES_INDEX_REGISTER;
799 IndexReg = Reg;
800 Scale = TmpInteger;
801 break;
802 }
803 }
804 void onDispExpr() {
805 switch (State) {
806 default:
807 State = IBES_ERROR;
808 break;
809 case IBES_START:
810 State = IBES_DISP_EXPR;
811 break;
812 }
813 }
814 void onInteger(int64_t TmpInt) {
815 switch (State) {
816 default:
817 State = IBES_ERROR;
818 break;
819 case IBES_START:
820 State = IBES_INTEGER;
821 TmpInteger = TmpInt;
822 break;
823 case IBES_MINUS:
824 State = IBES_INTEGER;
825 TmpInteger = TmpInt;
826 break;
827 case IBES_REGISTER_STAR:
828 assert (!IndexReg && "IndexReg already set!");
829 State = IBES_INDEX_REGISTER;
830 IndexReg = TmpReg;
831 Scale = TmpInt;
832 break;
833 }
834 }
835 void onStar() {
836 switch (State) {
837 default:
838 State = IBES_ERROR;
839 break;
840 case IBES_INTEGER:
841 State = IBES_INTEGER_STAR;
842 break;
843 case IBES_REGISTER:
844 State = IBES_REGISTER_STAR;
845 break;
846 }
847 }
848 void onLBrac() {
849 switch (State) {
850 default:
851 State = IBES_ERROR;
852 break;
853 case IBES_RBRAC:
854 State = IBES_START;
855 isPlus = true;
856 break;
857 }
858 }
859 void onRBrac() {
860 switch (State) {
861 default:
862 State = IBES_ERROR;
863 break;
864 case IBES_DISP_EXPR:
865 State = IBES_RBRAC;
866 break;
867 case IBES_INTEGER:
868 State = IBES_RBRAC;
869 if (isPlus)
870 Disp += TmpInteger;
871 else
872 Disp -= TmpInteger;
873 break;
874 case IBES_REGISTER:
875 State = IBES_RBRAC;
876 // If we already have a BaseReg, then assume this is the IndexReg with a
877 // scale of 1.
878 if (!BaseReg) {
879 BaseReg = TmpReg;
880 } else {
881 assert (!IndexReg && "BaseReg/IndexReg already set!");
882 IndexReg = TmpReg;
883 Scale = 1;
884 }
885 break;
886 case IBES_INDEX_REGISTER:
887 State = IBES_RBRAC;
888 break;
889 }
890 }
891};
892
Chad Rosier65c88922012-10-22 19:42:52 +0000893X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000894 unsigned Size) {
Chad Rosier4284e172012-10-24 22:13:37 +0000895 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000896 SMLoc Start = Tok.getLoc(), End = Tok.getEndLoc();
Devang Patel0a338862012-01-12 01:36:43 +0000897
Devang Pateld37ad242012-01-17 18:00:18 +0000898 // Eat '['
899 if (getLexer().isNot(AsmToken::LBrac))
900 return ErrorOperand(Start, "Expected '[' token!");
901 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000902
Chad Rosierdd2e8952013-01-14 22:31:35 +0000903 unsigned TmpReg = 0;
904
905 // Try to handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000906 if (getLexer().is(AsmToken::Identifier)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +0000907 if (ParseRegister(TmpReg, Start, End)) {
908 const MCExpr *Disp;
909 if (getParser().ParseExpression(Disp, End))
910 return 0;
911
Devang Pateld37ad242012-01-17 18:00:18 +0000912 if (getLexer().isNot(AsmToken::RBrac))
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000913 return ErrorOperand(Parser.getTok().getLoc(), "Expected ']' token!");
Chad Rosier4fb25b72013-02-15 21:58:13 +0000914 // Adjust the EndLoc due to the ']'.
915 End = SMLoc::getFromPointer(Parser.getTok().getEndLoc().getPointer()-1);
Devang Pateld37ad242012-01-17 18:00:18 +0000916 Parser.Lex();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000917 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000918 }
Devang Pateld37ad242012-01-17 18:00:18 +0000919 }
920
Chad Rosierdd2e8952013-01-14 22:31:35 +0000921 // Parse [ BaseReg + Scale*IndexReg + Disp ].
922 bool Done = false;
923 IntelBracExprStateMachine SM(Parser);
Chad Rosier2fbc2392012-10-29 18:01:54 +0000924
Chad Rosierdd2e8952013-01-14 22:31:35 +0000925 // If we parsed a register, then the end loc has already been set and
926 // the identifier has already been lexed. We also need to update the
927 // state.
928 if (TmpReg)
929 SM.onRegister(TmpReg);
930
931 const MCExpr *Disp = 0;
932 while (!Done) {
933 bool UpdateLocLex = true;
934
935 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
936 // identifier. Don't try an parse it as a register.
937 if (Tok.getString().startswith("."))
938 break;
939
940 switch (getLexer().getKind()) {
941 default: {
942 if (SM.isValidEndState()) {
943 Done = true;
944 break;
945 }
946 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
947 }
948 case AsmToken::Identifier: {
949 // This could be a register or a displacement expression.
950 if(!ParseRegister(TmpReg, Start, End)) {
951 SM.onRegister(TmpReg);
952 UpdateLocLex = false;
953 break;
954 } else if (!getParser().ParseExpression(Disp, End)) {
955 SM.onDispExpr();
956 UpdateLocLex = false;
957 break;
958 }
959 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
960 }
961 case AsmToken::Integer: {
Chad Rosier4284e172012-10-24 22:13:37 +0000962 int64_t Val = Tok.getIntVal();
Chad Rosierdd2e8952013-01-14 22:31:35 +0000963 SM.onInteger(Val);
964 break;
965 }
966 case AsmToken::Plus: SM.onPlus(); break;
967 case AsmToken::Minus: SM.onMinus(); break;
968 case AsmToken::Star: SM.onStar(); break;
969 case AsmToken::LBrac: SM.onLBrac(); break;
970 case AsmToken::RBrac: SM.onRBrac(); break;
971 }
972 if (!Done && UpdateLocLex) {
973 End = Tok.getLoc();
974 Parser.Lex(); // Consume the token.
Devang Patelf2d21372012-01-23 22:35:25 +0000975 }
Devang Pateld37ad242012-01-17 18:00:18 +0000976 }
977
Chad Rosierdd2e8952013-01-14 22:31:35 +0000978 if (!Disp)
979 Disp = MCConstantExpr::Create(SM.getDisp(), getContext());
Devang Patelfdd3b302012-01-20 21:21:01 +0000980
Chad Rosierddb53ef2012-10-26 22:01:25 +0000981 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000982 if (Tok.getString().startswith(".")) {
983 SmallString<64> Err;
984 const MCExpr *NewDisp;
985 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
986 return ErrorOperand(Tok.getLoc(), Err);
987
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000988 End = Parser.getTok().getEndLoc();
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000989 Parser.Lex(); // Eat the field.
990 Disp = NewDisp;
991 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000992
Chad Rosierdd2e8952013-01-14 22:31:35 +0000993 int BaseReg = SM.getBaseReg();
994 int IndexReg = SM.getIndexReg();
Devang Patelfdd3b302012-01-20 21:21:01 +0000995
Chad Rosierdd2e8952013-01-14 22:31:35 +0000996 // handle [-42]
997 if (!BaseReg && !IndexReg) {
998 if (!SegReg)
999 return X86Operand::CreateMem(Disp, Start, End);
1000 else
1001 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1002 }
1003
1004 int Scale = SM.getScale();
Devang Pateld37ad242012-01-17 18:00:18 +00001005 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +00001006 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +00001007}
1008
1009/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001010X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +00001011 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001012 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +00001013
1014 unsigned Size = getIntelMemOperandSize(Tok.getString());
1015 if (Size) {
1016 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +00001017 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
1018 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +00001019 Parser.Lex();
1020 }
1021
Chad Rosierc0a14b82012-10-24 17:22:29 +00001022 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +00001023 return ParseIntelBracExpression(SegReg, Size);
1024
1025 if (!ParseRegister(SegReg, Start, End)) {
1026 // Handel SegReg : [ ... ]
1027 if (getLexer().isNot(AsmToken::Colon))
1028 return ErrorOperand(Start, "Expected ':' token!");
1029 Parser.Lex(); // Eat :
1030 if (getLexer().isNot(AsmToken::LBrac))
1031 return ErrorOperand(Start, "Expected '[' token!");
1032 return ParseIntelBracExpression(SegReg, Size);
1033 }
Devang Pateld37ad242012-01-17 18:00:18 +00001034
1035 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001036 if (getParser().ParseExpression(Disp, End))
1037 return 0;
Chad Rosier96d58e62012-10-19 20:57:14 +00001038
1039 bool NeedSizeDir = false;
Chad Rosierc1ec2072013-01-10 22:10:27 +00001040 bool IsVarDecl = false;
1041 if (isParsingInlineAsm()) {
Chad Rosier96d58e62012-10-19 20:57:14 +00001042 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
1043 const MCSymbol &Sym = SymRef->getSymbol();
1044 // FIXME: The SemaLookup will fail if the name is anything other then an
1045 // identifier.
1046 // FIXME: Pass a valid SMLoc.
Chad Rosier505bca32013-01-17 19:21:48 +00001047 unsigned tLength, tSize, tType;
1048 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, tLength,
1049 tSize, tType, IsVarDecl);
Chad Rosierc1ec2072013-01-10 22:10:27 +00001050 if (!Size)
Chad Rosier505bca32013-01-17 19:21:48 +00001051 Size = tType * 8; // Size is in terms of bits in this context.
Chad Rosier96d58e62012-10-19 20:57:14 +00001052 NeedSizeDir = Size > 0;
1053 }
1054 }
Chad Rosier2a784132012-10-23 23:31:33 +00001055 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +00001056 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosierc1ec2072013-01-10 22:10:27 +00001057 else {
1058 // If this is not a VarDecl then assume it is a FuncDecl or some other label
1059 // reference. We need an 'r' constraint here, so we need to create register
1060 // operand to ensure proper matching. Just pick a GPR based on the size of
1061 // a pointer.
1062 if (!IsVarDecl) {
1063 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
1064 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true);
1065 }
1066
Chad Rosierd4d96ac2012-10-23 23:34:28 +00001067 // When parsing inline assembly we set the base register to a non-zero value
1068 // as we don't know the actual value at this time. This is necessary to
1069 // get the matching correct in some cases.
Chad Rosier2a784132012-10-23 23:31:33 +00001070 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
Chad Rosierc0a14b82012-10-24 17:22:29 +00001071 /*Scale*/1, Start, End, Size, NeedSizeDir);
Chad Rosierc1ec2072013-01-10 22:10:27 +00001072 }
Chad Rosierc0a14b82012-10-24 17:22:29 +00001073}
1074
Chad Rosier22f441a2012-10-24 22:21:50 +00001075/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001076bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1077 const MCExpr **NewDisp,
1078 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +00001079 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001080 uint64_t OrigDispVal, DotDispVal;
1081
1082 // FIXME: Handle non-constant expressions.
1083 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
1084 OrigDispVal = OrigDisp->getValue();
1085 } else {
1086 Err = "Non-constant offsets are not supported!";
1087 return true;
1088 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001089
1090 // Drop the '.'.
1091 StringRef DotDispStr = Tok.getString().drop_front(1);
1092
Chad Rosier22f441a2012-10-24 22:21:50 +00001093 // .Imm gets lexed as a real.
1094 if (Tok.is(AsmToken::Real)) {
1095 APInt DotDisp;
1096 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001097 DotDispVal = DotDisp.getZExtValue();
Chad Rosierec130222012-10-25 21:51:10 +00001098 } else if (Tok.is(AsmToken::Identifier)) {
1099 // We should only see an identifier when parsing the original inline asm.
1100 // The front-end should rewrite this in terms of immediates.
1101 assert (isParsingInlineAsm() && "Unexpected field name!");
1102
1103 unsigned DotDisp;
1104 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1105 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
1106 DotDisp)) {
1107 Err = "Unable to lookup field reference!";
1108 return true;
1109 }
1110 DotDispVal = DotDisp;
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001111 } else {
1112 Err = "Unexpected token type!";
1113 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +00001114 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001115
Chad Rosierec130222012-10-25 21:51:10 +00001116 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1117 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1118 unsigned Len = DotDispStr.size();
1119 unsigned Val = OrigDispVal + DotDispVal;
1120 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1121 Val));
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001122 }
1123
1124 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1125 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +00001126}
1127
Chad Rosierc0a14b82012-10-24 17:22:29 +00001128/// Parse the 'offset' operator. This operator is used to specify the
1129/// location rather then the content of a variable.
1130X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
1131 SMLoc OffsetOfLoc = Start;
1132 Parser.Lex(); // Eat offset.
1133 Start = Parser.getTok().getLoc();
1134 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1135
Chad Rosier6e431572012-10-26 16:09:20 +00001136 SMLoc End;
Chad Rosierc0a14b82012-10-24 17:22:29 +00001137 const MCExpr *Val;
1138 if (getParser().ParseExpression(Val, End))
Chad Rosier7ab21c72012-10-26 18:32:44 +00001139 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosierc0a14b82012-10-24 17:22:29 +00001140
Chad Rosier6e431572012-10-26 16:09:20 +00001141 // Don't emit the offset operator.
1142 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1143
Chad Rosierc0a14b82012-10-24 17:22:29 +00001144 // The offset operator will have an 'r' constraint, thus we need to create
1145 // register operand to ensure proper matching. Just pick a GPR based on
1146 // the size of a pointer.
1147 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosierc1ec2072013-01-10 22:10:27 +00001148 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
1149 OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +00001150}
1151
Chad Rosier505bca32013-01-17 19:21:48 +00001152enum IntelOperatorKind {
1153 IOK_LENGTH,
1154 IOK_SIZE,
1155 IOK_TYPE
1156};
1157
1158/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1159/// returns the number of elements in an array. It returns the value 1 for
1160/// non-array variables. The SIZE operator returns the size of a C or C++
1161/// variable. A variable's size is the product of its LENGTH and TYPE. The
1162/// TYPE operator returns the size of a C or C++ type or variable. If the
1163/// variable is an array, TYPE returns the size of a single element.
1164X86Operand *X86AsmParser::ParseIntelOperator(SMLoc Start, unsigned OpKind) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00001165 SMLoc TypeLoc = Start;
1166 Parser.Lex(); // Eat offset.
1167 Start = Parser.getTok().getLoc();
1168 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1169
1170 SMLoc End;
1171 const MCExpr *Val;
1172 if (getParser().ParseExpression(Val, End))
1173 return 0;
1174
Chad Rosier505bca32013-01-17 19:21:48 +00001175 unsigned Length = 0, Size = 0, Type = 0;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001176 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
1177 const MCSymbol &Sym = SymRef->getSymbol();
1178 // FIXME: The SemaLookup will fail if the name is anything other then an
1179 // identifier.
1180 // FIXME: Pass a valid SMLoc.
Chad Rosierc1ec2072013-01-10 22:10:27 +00001181 bool IsVarDecl;
Chad Rosier505bca32013-01-17 19:21:48 +00001182 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Length,
1183 Size, Type, IsVarDecl))
Chad Rosier3da67ca2013-01-18 00:50:59 +00001184 return ErrorOperand(Start, "Unable to lookup expr!");
Chad Rosier505bca32013-01-17 19:21:48 +00001185 }
1186 unsigned CVal;
1187 switch(OpKind) {
1188 default: llvm_unreachable("Unexpected operand kind!");
1189 case IOK_LENGTH: CVal = Length; break;
1190 case IOK_SIZE: CVal = Size; break;
1191 case IOK_TYPE: CVal = Type; break;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001192 }
1193
1194 // Rewrite the type operator and the C or C++ type or variable in terms of an
1195 // immediate. E.g. TYPE foo -> $$4
1196 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosier505bca32013-01-17 19:21:48 +00001197 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosierefcb3d92012-10-26 18:04:20 +00001198
Chad Rosier505bca32013-01-17 19:21:48 +00001199 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosierefcb3d92012-10-26 18:04:20 +00001200 return X86Operand::CreateImm(Imm, Start, End, /*NeedAsmRewrite*/false);
1201}
1202
Devang Pateld37ad242012-01-17 18:00:18 +00001203X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +00001204 SMLoc Start = Parser.getTok().getLoc(), End;
Chad Rosier7ab21c72012-10-26 18:32:44 +00001205 StringRef AsmTokStr = Parser.getTok().getString();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001206
Chad Rosier505bca32013-01-17 19:21:48 +00001207 // Offset, length, type and size operators.
1208 if (isParsingInlineAsm()) {
1209 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
1210 return ParseIntelOffsetOfOperator(Start);
1211 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
1212 return ParseIntelOperator(Start, IOK_LENGTH);
1213 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
1214 return ParseIntelOperator(Start, IOK_SIZE);
1215 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
1216 return ParseIntelOperator(Start, IOK_TYPE);
1217 }
Chad Rosierefcb3d92012-10-26 18:04:20 +00001218
Chad Rosier505bca32013-01-17 19:21:48 +00001219 // Immediate.
Devang Pateld37ad242012-01-17 18:00:18 +00001220 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
1221 getLexer().is(AsmToken::Minus)) {
1222 const MCExpr *Val;
1223 if (!getParser().ParseExpression(Val, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +00001224 return X86Operand::CreateImm(Val, Start, End);
1225 }
1226 }
1227
Chad Rosier505bca32013-01-17 19:21:48 +00001228 // Register.
Devang Patel1aea4302012-01-20 22:32:05 +00001229 unsigned RegNo = 0;
1230 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001231 // If this is a segment register followed by a ':', then this is the start
1232 // of a memory reference, otherwise this is a normal register reference.
1233 if (getLexer().isNot(AsmToken::Colon))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001234 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001235
1236 getParser().Lex(); // Eat the colon.
1237 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001238 }
1239
Chad Rosier505bca32013-01-17 19:21:48 +00001240 // Memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001241 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001242}
1243
Devang Pateldd929fc2012-01-12 18:03:40 +00001244X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001245 switch (getLexer().getKind()) {
1246 default:
Chris Lattnereef6d782010-04-17 18:56:34 +00001247 // Parse a memory operand with no segment register.
1248 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +00001249 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +00001250 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +00001251 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +00001252 SMLoc Start, End;
1253 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001254 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001255 Error(Start, "%eiz and %riz can only be used as index registers",
1256 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001257 return 0;
1258 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001259
Chris Lattnereef6d782010-04-17 18:56:34 +00001260 // If this is a segment register followed by a ':', then this is the start
1261 // of a memory reference, otherwise this is a normal register reference.
1262 if (getLexer().isNot(AsmToken::Colon))
1263 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001264
1265
Chris Lattnereef6d782010-04-17 18:56:34 +00001266 getParser().Lex(); // Eat the colon.
1267 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +00001268 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001269 case AsmToken::Dollar: {
1270 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +00001271 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +00001272 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001273 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +00001274 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +00001275 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001276 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001277 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001278 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +00001279}
1280
Chris Lattnereef6d782010-04-17 18:56:34 +00001281/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1282/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +00001283X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001284
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001285 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1286 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +00001287 // only way to do this without lookahead is to eat the '(' and see what is
1288 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001289 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001290 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +00001291 SMLoc ExprEnd;
1292 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001293
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001294 // After parsing the base expression we could either have a parenthesized
1295 // memory address or not. If not, return now. If so, eat the (.
1296 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001297 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001298 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001299 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001300 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001301 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001302
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001303 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001304 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001305 } else {
1306 // Okay, we have a '('. We don't know if this is an expression or not, but
1307 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +00001308 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001309 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001310
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001311 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001312 // Nothing to do here, fall into the code below with the '(' part of the
1313 // memory operand consumed.
1314 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001315 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001316
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001317 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001318 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +00001319 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001320
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001321 // After parsing the base expression we could either have a parenthesized
1322 // memory address or not. If not, return now. If so, eat the (.
1323 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001324 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001325 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001326 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001327 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001328 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001329
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001330 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001331 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001332 }
1333 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001334
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001335 // If we reached here, then we just ate the ( of the memory operand. Process
1336 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001337 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001338 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001339
Chris Lattner29ef9a22010-01-15 18:51:29 +00001340 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001341 SMLoc StartLoc, EndLoc;
1342 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001343 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001344 Error(StartLoc, "eiz and riz can only be used as index registers",
1345 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001346 return 0;
1347 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001348 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001349
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001350 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001351 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001352 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001353
1354 // Following the comma we should have either an index register, or a scale
1355 // value. We don't support the later form, but we want to parse it
1356 // correctly.
1357 //
1358 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001359 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001360 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001361 SMLoc L;
1362 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001363
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001364 if (getLexer().isNot(AsmToken::RParen)) {
1365 // Parse the scale amount:
1366 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001367 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001368 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001369 "expected comma in scale expression");
1370 return 0;
1371 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001372 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001373
1374 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001375 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001376
1377 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001378 if (getParser().ParseAbsoluteExpression(ScaleVal)){
1379 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001380 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001381 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001382
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001383 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001384 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1385 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1386 return 0;
1387 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001388 Scale = (unsigned)ScaleVal;
1389 }
1390 }
1391 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001392 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001393 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001394 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001395
1396 int64_t Value;
1397 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001398 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001399
Daniel Dunbaree910252010-08-24 19:13:38 +00001400 if (Value != 1)
1401 Warning(Loc, "scale factor without index register is ignored");
1402 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001403 }
1404 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001405
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001406 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001407 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001408 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001409 return 0;
1410 }
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001411 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001412 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001413
Kevin Enderby84faf652012-03-12 21:32:09 +00001414 // If we have both a base register and an index register make sure they are
1415 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001416 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001417 if (BaseReg != 0 && IndexReg != 0) {
1418 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001419 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1420 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001421 IndexReg != X86::RIZ) {
1422 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1423 return 0;
1424 }
1425 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001426 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1427 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001428 IndexReg != X86::EIZ){
1429 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1430 return 0;
1431 }
1432 }
1433
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001434 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1435 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001436}
1437
Devang Pateldd929fc2012-01-12 18:03:40 +00001438bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001439ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001440 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001441 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001442 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001443
Chris Lattnerd8f71792010-11-28 20:23:50 +00001444 // FIXME: Hack to recognize setneb as setne.
1445 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1446 PatchedName != "setb" && PatchedName != "setnb")
1447 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001448
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001449 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1450 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001451 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001452 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1453 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001454 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001455 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001456 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001457 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001458 .Case("eq", 0x00)
1459 .Case("lt", 0x01)
1460 .Case("le", 0x02)
1461 .Case("unord", 0x03)
1462 .Case("neq", 0x04)
1463 .Case("nlt", 0x05)
1464 .Case("nle", 0x06)
1465 .Case("ord", 0x07)
1466 /* AVX only from here */
1467 .Case("eq_uq", 0x08)
1468 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001469 .Case("ngt", 0x0A)
1470 .Case("false", 0x0B)
1471 .Case("neq_oq", 0x0C)
1472 .Case("ge", 0x0D)
1473 .Case("gt", 0x0E)
1474 .Case("true", 0x0F)
1475 .Case("eq_os", 0x10)
1476 .Case("lt_oq", 0x11)
1477 .Case("le_oq", 0x12)
1478 .Case("unord_s", 0x13)
1479 .Case("neq_us", 0x14)
1480 .Case("nlt_uq", 0x15)
1481 .Case("nle_uq", 0x16)
1482 .Case("ord_s", 0x17)
1483 .Case("eq_us", 0x18)
1484 .Case("nge_uq", 0x19)
1485 .Case("ngt_uq", 0x1A)
1486 .Case("false_os", 0x1B)
1487 .Case("neq_os", 0x1C)
1488 .Case("ge_oq", 0x1D)
1489 .Case("gt_oq", 0x1E)
1490 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001491 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001492 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001493 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1494 getParser().getContext());
1495 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001496 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001497 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001498 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001499 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001500 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001501 } else {
1502 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001503 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001504 }
1505 }
1506 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001507
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001508 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001509
Devang Patel885f65b2012-01-30 22:47:12 +00001510 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001511 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001512
Chris Lattner2544f422010-09-08 05:17:37 +00001513 // Determine whether this is an instruction prefix.
1514 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001515 Name == "lock" || Name == "rep" ||
1516 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001517 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001518 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001519
1520
Chris Lattner2544f422010-09-08 05:17:37 +00001521 // This does the actual operand parsing. Don't parse any more if we have a
1522 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1523 // just want to parse the "lock" as the first instruction and the "incl" as
1524 // the next one.
1525 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001526
1527 // Parse '*' modifier.
1528 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001529 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001530 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001531 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001532 }
1533
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001534 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001535 if (X86Operand *Op = ParseOperand())
1536 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001537 else {
1538 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001539 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001540 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001541
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001542 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001543 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001544
1545 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001546 if (X86Operand *Op = ParseOperand())
1547 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001548 else {
1549 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001550 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001551 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001552 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001553
Chris Lattnercbf8a982010-09-11 16:18:25 +00001554 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001555 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001556 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001557 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001558 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001559 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001560
Chris Lattner2544f422010-09-08 05:17:37 +00001561 if (getLexer().is(AsmToken::EndOfStatement))
1562 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001563 else if (isPrefix && getLexer().is(AsmToken::Slash))
1564 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001565
Devang Patel885f65b2012-01-30 22:47:12 +00001566 if (ExtraImmOp && isParsingIntelSyntax())
1567 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1568
Chris Lattner98c870f2010-11-06 19:25:43 +00001569 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1570 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1571 // documented form in various unofficial manuals, so a lot of code uses it.
1572 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1573 Operands.size() == 3) {
1574 X86Operand &Op = *(X86Operand*)Operands.back();
1575 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1576 isa<MCConstantExpr>(Op.Mem.Disp) &&
1577 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1578 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1579 SMLoc Loc = Op.getEndLoc();
1580 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1581 delete &Op;
1582 }
1583 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001584 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1585 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1586 Operands.size() == 3) {
1587 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1588 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1589 isa<MCConstantExpr>(Op.Mem.Disp) &&
1590 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1591 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1592 SMLoc Loc = Op.getEndLoc();
1593 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1594 delete &Op;
1595 }
1596 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001597 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1598 if (Name.startswith("ins") && Operands.size() == 3 &&
1599 (Name == "insb" || Name == "insw" || Name == "insl")) {
1600 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1601 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1602 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1603 Operands.pop_back();
1604 Operands.pop_back();
1605 delete &Op;
1606 delete &Op2;
1607 }
1608 }
1609
1610 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1611 if (Name.startswith("outs") && Operands.size() == 3 &&
1612 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1613 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1614 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1615 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1616 Operands.pop_back();
1617 Operands.pop_back();
1618 delete &Op;
1619 delete &Op2;
1620 }
1621 }
1622
1623 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1624 if (Name.startswith("movs") && Operands.size() == 3 &&
1625 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001626 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001627 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1628 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1629 if (isSrcOp(Op) && isDstOp(Op2)) {
1630 Operands.pop_back();
1631 Operands.pop_back();
1632 delete &Op;
1633 delete &Op2;
1634 }
1635 }
1636 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1637 if (Name.startswith("lods") && Operands.size() == 3 &&
1638 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001639 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001640 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1641 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1642 if (isSrcOp(*Op1) && Op2->isReg()) {
1643 const char *ins;
1644 unsigned reg = Op2->getReg();
1645 bool isLods = Name == "lods";
1646 if (reg == X86::AL && (isLods || Name == "lodsb"))
1647 ins = "lodsb";
1648 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1649 ins = "lodsw";
1650 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1651 ins = "lodsl";
1652 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1653 ins = "lodsq";
1654 else
1655 ins = NULL;
1656 if (ins != NULL) {
1657 Operands.pop_back();
1658 Operands.pop_back();
1659 delete Op1;
1660 delete Op2;
1661 if (Name != ins)
1662 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1663 }
1664 }
1665 }
1666 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1667 if (Name.startswith("stos") && Operands.size() == 3 &&
1668 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001669 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001670 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1671 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1672 if (isDstOp(*Op2) && Op1->isReg()) {
1673 const char *ins;
1674 unsigned reg = Op1->getReg();
1675 bool isStos = Name == "stos";
1676 if (reg == X86::AL && (isStos || Name == "stosb"))
1677 ins = "stosb";
1678 else if (reg == X86::AX && (isStos || Name == "stosw"))
1679 ins = "stosw";
1680 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1681 ins = "stosl";
1682 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1683 ins = "stosq";
1684 else
1685 ins = NULL;
1686 if (ins != NULL) {
1687 Operands.pop_back();
1688 Operands.pop_back();
1689 delete Op1;
1690 delete Op2;
1691 if (Name != ins)
1692 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1693 }
1694 }
1695 }
1696
Chris Lattnere9e16a32010-09-15 04:33:27 +00001697 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001698 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001699 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001700 Name.startswith("shl") || Name.startswith("sal") ||
1701 Name.startswith("rcl") || Name.startswith("rcr") ||
1702 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001703 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001704 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001705 // Intel syntax
1706 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1707 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001708 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1709 delete Operands[2];
1710 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001711 }
1712 } else {
1713 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1714 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001715 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1716 delete Operands[1];
1717 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001718 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001719 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001720 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001721
Chris Lattner15f89512011-04-09 19:41:05 +00001722 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1723 // instalias with an immediate operand yet.
1724 if (Name == "int" && Operands.size() == 2) {
1725 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1726 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1727 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1728 delete Operands[1];
1729 Operands.erase(Operands.begin() + 1);
1730 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1731 }
1732 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001733
Chris Lattner98986712010-01-14 22:21:20 +00001734 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001735}
1736
Devang Pateldd929fc2012-01-12 18:03:40 +00001737bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001738processInstruction(MCInst &Inst,
1739 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1740 switch (Inst.getOpcode()) {
1741 default: return false;
1742 case X86::AND16i16: {
1743 if (!Inst.getOperand(0).isImm() ||
1744 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1745 return false;
1746
1747 MCInst TmpInst;
1748 TmpInst.setOpcode(X86::AND16ri8);
1749 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1750 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1751 TmpInst.addOperand(Inst.getOperand(0));
1752 Inst = TmpInst;
1753 return true;
1754 }
1755 case X86::AND32i32: {
1756 if (!Inst.getOperand(0).isImm() ||
1757 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1758 return false;
1759
1760 MCInst TmpInst;
1761 TmpInst.setOpcode(X86::AND32ri8);
1762 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1763 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1764 TmpInst.addOperand(Inst.getOperand(0));
1765 Inst = TmpInst;
1766 return true;
1767 }
1768 case X86::AND64i32: {
1769 if (!Inst.getOperand(0).isImm() ||
1770 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1771 return false;
1772
1773 MCInst TmpInst;
1774 TmpInst.setOpcode(X86::AND64ri8);
1775 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1776 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1777 TmpInst.addOperand(Inst.getOperand(0));
1778 Inst = TmpInst;
1779 return true;
1780 }
Devang Patelac0f0482012-01-19 17:53:25 +00001781 case X86::XOR16i16: {
1782 if (!Inst.getOperand(0).isImm() ||
1783 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1784 return false;
1785
1786 MCInst TmpInst;
1787 TmpInst.setOpcode(X86::XOR16ri8);
1788 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1789 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1790 TmpInst.addOperand(Inst.getOperand(0));
1791 Inst = TmpInst;
1792 return true;
1793 }
1794 case X86::XOR32i32: {
1795 if (!Inst.getOperand(0).isImm() ||
1796 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1797 return false;
1798
1799 MCInst TmpInst;
1800 TmpInst.setOpcode(X86::XOR32ri8);
1801 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1802 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1803 TmpInst.addOperand(Inst.getOperand(0));
1804 Inst = TmpInst;
1805 return true;
1806 }
1807 case X86::XOR64i32: {
1808 if (!Inst.getOperand(0).isImm() ||
1809 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1810 return false;
1811
1812 MCInst TmpInst;
1813 TmpInst.setOpcode(X86::XOR64ri8);
1814 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1815 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1816 TmpInst.addOperand(Inst.getOperand(0));
1817 Inst = TmpInst;
1818 return true;
1819 }
1820 case X86::OR16i16: {
1821 if (!Inst.getOperand(0).isImm() ||
1822 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1823 return false;
1824
1825 MCInst TmpInst;
1826 TmpInst.setOpcode(X86::OR16ri8);
1827 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1828 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1829 TmpInst.addOperand(Inst.getOperand(0));
1830 Inst = TmpInst;
1831 return true;
1832 }
1833 case X86::OR32i32: {
1834 if (!Inst.getOperand(0).isImm() ||
1835 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1836 return false;
1837
1838 MCInst TmpInst;
1839 TmpInst.setOpcode(X86::OR32ri8);
1840 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1841 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1842 TmpInst.addOperand(Inst.getOperand(0));
1843 Inst = TmpInst;
1844 return true;
1845 }
1846 case X86::OR64i32: {
1847 if (!Inst.getOperand(0).isImm() ||
1848 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1849 return false;
1850
1851 MCInst TmpInst;
1852 TmpInst.setOpcode(X86::OR64ri8);
1853 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1854 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1855 TmpInst.addOperand(Inst.getOperand(0));
1856 Inst = TmpInst;
1857 return true;
1858 }
1859 case X86::CMP16i16: {
1860 if (!Inst.getOperand(0).isImm() ||
1861 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1862 return false;
1863
1864 MCInst TmpInst;
1865 TmpInst.setOpcode(X86::CMP16ri8);
1866 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1867 TmpInst.addOperand(Inst.getOperand(0));
1868 Inst = TmpInst;
1869 return true;
1870 }
1871 case X86::CMP32i32: {
1872 if (!Inst.getOperand(0).isImm() ||
1873 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1874 return false;
1875
1876 MCInst TmpInst;
1877 TmpInst.setOpcode(X86::CMP32ri8);
1878 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1879 TmpInst.addOperand(Inst.getOperand(0));
1880 Inst = TmpInst;
1881 return true;
1882 }
1883 case X86::CMP64i32: {
1884 if (!Inst.getOperand(0).isImm() ||
1885 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1886 return false;
1887
1888 MCInst TmpInst;
1889 TmpInst.setOpcode(X86::CMP64ri8);
1890 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1891 TmpInst.addOperand(Inst.getOperand(0));
1892 Inst = TmpInst;
1893 return true;
1894 }
Devang Patela951f772012-01-19 18:40:55 +00001895 case X86::ADD16i16: {
1896 if (!Inst.getOperand(0).isImm() ||
1897 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1898 return false;
1899
1900 MCInst TmpInst;
1901 TmpInst.setOpcode(X86::ADD16ri8);
1902 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1903 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1904 TmpInst.addOperand(Inst.getOperand(0));
1905 Inst = TmpInst;
1906 return true;
1907 }
1908 case X86::ADD32i32: {
1909 if (!Inst.getOperand(0).isImm() ||
1910 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1911 return false;
1912
1913 MCInst TmpInst;
1914 TmpInst.setOpcode(X86::ADD32ri8);
1915 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1916 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1917 TmpInst.addOperand(Inst.getOperand(0));
1918 Inst = TmpInst;
1919 return true;
1920 }
1921 case X86::ADD64i32: {
1922 if (!Inst.getOperand(0).isImm() ||
1923 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1924 return false;
1925
1926 MCInst TmpInst;
1927 TmpInst.setOpcode(X86::ADD64ri8);
1928 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1929 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1930 TmpInst.addOperand(Inst.getOperand(0));
1931 Inst = TmpInst;
1932 return true;
1933 }
1934 case X86::SUB16i16: {
1935 if (!Inst.getOperand(0).isImm() ||
1936 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1937 return false;
1938
1939 MCInst TmpInst;
1940 TmpInst.setOpcode(X86::SUB16ri8);
1941 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1942 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1943 TmpInst.addOperand(Inst.getOperand(0));
1944 Inst = TmpInst;
1945 return true;
1946 }
1947 case X86::SUB32i32: {
1948 if (!Inst.getOperand(0).isImm() ||
1949 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1950 return false;
1951
1952 MCInst TmpInst;
1953 TmpInst.setOpcode(X86::SUB32ri8);
1954 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1955 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1956 TmpInst.addOperand(Inst.getOperand(0));
1957 Inst = TmpInst;
1958 return true;
1959 }
1960 case X86::SUB64i32: {
1961 if (!Inst.getOperand(0).isImm() ||
1962 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1963 return false;
1964
1965 MCInst TmpInst;
1966 TmpInst.setOpcode(X86::SUB64ri8);
1967 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1968 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1969 TmpInst.addOperand(Inst.getOperand(0));
1970 Inst = TmpInst;
1971 return true;
1972 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001973 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001974}
1975
Jim Grosbach3ca63822012-11-14 18:04:47 +00001976static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001977bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001978MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001979 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001980 MCStreamer &Out, unsigned &ErrorInfo,
1981 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001982 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001983 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1984 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001985 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001986
Chris Lattner7c51a312010-09-29 01:50:45 +00001987 // First, handle aliases that expand to multiple instructions.
1988 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001989 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001990 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001991 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001992 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001993 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001994 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001995 MCInst Inst;
1996 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001997 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001998 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001999 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00002000
Chris Lattner0bb83a82010-09-30 16:39:29 +00002001 const char *Repl =
2002 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00002003 .Case("finit", "fninit")
2004 .Case("fsave", "fnsave")
2005 .Case("fstcw", "fnstcw")
2006 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00002007 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00002008 .Case("fstsw", "fnstsw")
2009 .Case("fstsww", "fnstsw")
2010 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00002011 .Default(0);
2012 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00002013 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00002014 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00002015 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002016
Chris Lattnera008e8a2010-09-06 21:54:15 +00002017 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00002018 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002019
Daniel Dunbarc918d602010-05-04 16:12:42 +00002020 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00002021 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00002022 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00002023 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002024 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00002025 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00002026 // Some instructions need post-processing to, for example, tweak which
2027 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00002028 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00002029 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00002030 while (processInstruction(Inst, Operands))
2031 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00002032
Jim Grosbachcb5dca32012-01-27 00:51:27 +00002033 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00002034 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00002035 Out.EmitInstruction(Inst);
2036 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00002037 return false;
Jim Grosbach3ca63822012-11-14 18:04:47 +00002038 case Match_MissingFeature: {
2039 assert(ErrorInfo && "Unknown missing feature!");
2040 // Special case the error message for the very common case where only
2041 // a single subtarget feature is missing.
2042 std::string Msg = "instruction requires:";
2043 unsigned Mask = 1;
2044 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
2045 if (ErrorInfo & Mask) {
2046 Msg += " ";
2047 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
2048 }
2049 Mask <<= 1;
2050 }
2051 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
2052 }
Chris Lattnera008e8a2010-09-06 21:54:15 +00002053 case Match_InvalidOperand:
2054 WasOriginallyInvalidOperand = true;
2055 break;
2056 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00002057 break;
2058 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00002059
Daniel Dunbarc918d602010-05-04 16:12:42 +00002060 // FIXME: Ideally, we would only attempt suffix matches for things which are
2061 // valid prefixes, and we could just infer the right unambiguous
2062 // type. However, that requires substantially more matcher support than the
2063 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002064
Daniel Dunbarc918d602010-05-04 16:12:42 +00002065 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00002066 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00002067 SmallString<16> Tmp;
2068 Tmp += Base;
2069 Tmp += ' ';
2070 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00002071
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002072 // If this instruction starts with an 'f', then it is a floating point stack
2073 // instruction. These come in up to three forms for 32-bit, 64-bit, and
2074 // 80-bit floating point, which use the suffixes s,l,t respectively.
2075 //
2076 // Otherwise, we assume that this may be an integer instruction, which comes
2077 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
2078 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00002079
Daniel Dunbarc918d602010-05-04 16:12:42 +00002080 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002081 Tmp[Base.size()] = Suffixes[0];
2082 unsigned ErrorInfoIgnore;
Jim Grosbach3ca63822012-11-14 18:04:47 +00002083 unsigned ErrorInfoMissingFeature;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00002084 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002085
Chad Rosier6e006d32012-10-12 22:53:36 +00002086 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2087 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00002088 // If this returned as a missing feature failure, remember that.
2089 if (Match1 == Match_MissingFeature)
2090 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002091 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00002092 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2093 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00002094 // If this returned as a missing feature failure, remember that.
2095 if (Match2 == Match_MissingFeature)
2096 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002097 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00002098 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2099 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00002100 // If this returned as a missing feature failure, remember that.
2101 if (Match3 == Match_MissingFeature)
2102 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002103 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00002104 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
2105 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00002106 // If this returned as a missing feature failure, remember that.
2107 if (Match4 == Match_MissingFeature)
2108 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbarc918d602010-05-04 16:12:42 +00002109
2110 // Restore the old token.
2111 Op->setTokenValue(Base);
2112
2113 // If exactly one matched, then we treat that as a successful match (and the
2114 // instruction will already have been filled in correctly, since the failing
2115 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00002116 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002117 (Match1 == Match_Success) + (Match2 == Match_Success) +
2118 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00002119 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00002120 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00002121 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00002122 Out.EmitInstruction(Inst);
2123 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00002124 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00002125 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00002126
Chris Lattnerec6789f2010-09-06 20:08:02 +00002127 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00002128
Daniel Dunbar09062b12010-08-12 00:55:42 +00002129 // If we had multiple suffix matches, then identify this as an ambiguous
2130 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00002131 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00002132 char MatchChars[4];
2133 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002134 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2135 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2136 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2137 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00002138
2139 SmallString<126> Msg;
2140 raw_svector_ostream OS(Msg);
2141 OS << "ambiguous instructions require an explicit suffix (could be ";
2142 for (unsigned i = 0; i != NumMatches; ++i) {
2143 if (i != 0)
2144 OS << ", ";
2145 if (i + 1 == NumMatches)
2146 OS << "or ";
2147 OS << "'" << Base << MatchChars[i] << "'";
2148 }
2149 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00002150 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00002151 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00002152 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002153
Chris Lattnera008e8a2010-09-06 21:54:15 +00002154 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002155
Chris Lattnera008e8a2010-09-06 21:54:15 +00002156 // If all of the instructions reported an invalid mnemonic, then the original
2157 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002158 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2159 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00002160 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00002161 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00002162 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00002163 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002164 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00002165 }
2166
2167 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00002168 if (ErrorInfo != ~0U) {
2169 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00002170 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002171 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002172
Chad Rosier84125ca2012-10-13 00:26:04 +00002173 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002174 if (Operand->getStartLoc().isValid()) {
2175 SMRange OperandRange = Operand->getLocRange();
2176 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002177 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002178 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00002179 }
2180
Chad Rosierb4fdade2012-08-21 19:36:59 +00002181 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002182 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002183 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002184
Chris Lattnerec6789f2010-09-06 20:08:02 +00002185 // If one instruction matched with a missing feature, report this as a
2186 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002187 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2188 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach3ca63822012-11-14 18:04:47 +00002189 std::string Msg = "instruction requires:";
2190 unsigned Mask = 1;
2191 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2192 if (ErrorInfoMissingFeature & Mask) {
2193 Msg += " ";
2194 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2195 }
2196 Mask <<= 1;
2197 }
2198 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00002199 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002200
Chris Lattnera008e8a2010-09-06 21:54:15 +00002201 // If one instruction matched with an invalid operand, report this as an
2202 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002203 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2204 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00002205 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002206 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002207 return true;
2208 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002209
Chris Lattnerec6789f2010-09-06 20:08:02 +00002210 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00002211 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002212 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00002213 return true;
2214}
2215
2216
Devang Pateldd929fc2012-01-12 18:03:40 +00002217bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00002218 StringRef IDVal = DirectiveID.getIdentifier();
2219 if (IDVal == ".word")
2220 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00002221 else if (IDVal.startswith(".code"))
2222 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00002223 else if (IDVal.startswith(".att_syntax")) {
2224 getParser().setAssemblerDialect(0);
2225 return false;
2226 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00002227 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00002228 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2229 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00002230 // FIXME : Handle noprefix
2231 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00002232 } else
Craig Topper76bd9382012-07-18 04:59:16 +00002233 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00002234 }
2235 return false;
2236 }
Chris Lattner537ca842010-10-30 17:38:55 +00002237 return true;
2238}
2239
2240/// ParseDirectiveWord
2241/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00002242bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00002243 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2244 for (;;) {
2245 const MCExpr *Value;
2246 if (getParser().ParseExpression(Value))
2247 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002248
Eric Christopher1ced2082013-01-09 03:52:05 +00002249 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier36b8fed2012-06-27 22:34:28 +00002250
Chris Lattner537ca842010-10-30 17:38:55 +00002251 if (getLexer().is(AsmToken::EndOfStatement))
2252 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002253
Chris Lattner537ca842010-10-30 17:38:55 +00002254 // FIXME: Improve diagnostic.
2255 if (getLexer().isNot(AsmToken::Comma))
2256 return Error(L, "unexpected token in directive");
2257 Parser.Lex();
2258 }
2259 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00002260
Chris Lattner537ca842010-10-30 17:38:55 +00002261 Parser.Lex();
2262 return false;
2263}
2264
Evan Chengbd27f5a2011-07-27 00:38:12 +00002265/// ParseDirectiveCode
2266/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00002267bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00002268 if (IDVal == ".code32") {
2269 Parser.Lex();
2270 if (is64BitMode()) {
2271 SwitchMode();
2272 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2273 }
2274 } else if (IDVal == ".code64") {
2275 Parser.Lex();
2276 if (!is64BitMode()) {
2277 SwitchMode();
2278 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2279 }
2280 } else {
2281 return Error(L, "unexpected directive " + IDVal);
2282 }
Chris Lattner537ca842010-10-30 17:38:55 +00002283
Evan Chengbd27f5a2011-07-27 00:38:12 +00002284 return false;
2285}
Chris Lattner537ca842010-10-30 17:38:55 +00002286
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002287// Force static initialization.
2288extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00002289 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2290 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002291}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002292
Chris Lattner0692ee62010-09-06 19:11:01 +00002293#define GET_REGISTER_MATCHER
2294#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach3ca63822012-11-14 18:04:47 +00002295#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002296#include "X86GenAsmMatcher.inc"