blob: 8545a56cafa5d70175ce173b3ddceaef6c1f979d [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 Rosierefcb3d92012-10-26 18:04:20 +000060 X86Operand *ParseIntelTypeOperator(SMLoc StartLoc);
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;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000531 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000532 }
533};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000534
Chris Lattner37dfdec2009-07-29 06:33:53 +0000535} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000536
Devang Pateldd929fc2012-01-12 18:03:40 +0000537bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000538 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000539
540 return (Op.isMem() &&
541 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
542 isa<MCConstantExpr>(Op.Mem.Disp) &&
543 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
544 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
545}
546
Devang Pateldd929fc2012-01-12 18:03:40 +0000547bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000548 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000549
Chad Rosier36b8fed2012-06-27 22:34:28 +0000550 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000551 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000552 isa<MCConstantExpr>(Op.Mem.Disp) &&
553 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
554 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
555}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000556
Devang Pateldd929fc2012-01-12 18:03:40 +0000557bool X86AsmParser::ParseRegister(unsigned &RegNo,
558 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000559 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000560 const AsmToken &PercentTok = Parser.getTok();
561 StartLoc = PercentTok.getLoc();
562
563 // If we encounter a %, ignore it. This code handles registers with and
564 // without the prefix, unprefixed registers can occur in cfi directives.
565 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000566 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000567
Sean Callanan18b83232010-01-19 21:44:56 +0000568 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000569 EndLoc = Tok.getEndLoc();
570
Devang Patel1aea4302012-01-20 22:32:05 +0000571 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000572 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000573 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000574 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000575 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000576
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000577 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000578
Chris Lattner33d60d52010-09-22 04:11:10 +0000579 // If the match failed, try the register name as lowercase.
580 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000581 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000582
Evan Cheng5de728c2011-07-27 23:22:03 +0000583 if (!is64BitMode()) {
584 // FIXME: This should be done using Requires<In32BitMode> and
585 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
586 // checked.
587 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
588 // REX prefix.
589 if (RegNo == X86::RIZ ||
590 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
591 X86II::isX86_64NonExtLowByteReg(RegNo) ||
592 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000593 return Error(StartLoc, "register %"
594 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000595 SMRange(StartLoc, EndLoc));
Evan Cheng5de728c2011-07-27 23:22:03 +0000596 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000597
Chris Lattner33d60d52010-09-22 04:11:10 +0000598 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
599 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000600 RegNo = X86::ST0;
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000601 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000602
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000603 // Check to see if we have '(4)' after %st.
604 if (getLexer().isNot(AsmToken::LParen))
605 return false;
606 // Lex the paren.
607 getParser().Lex();
608
609 const AsmToken &IntTok = Parser.getTok();
610 if (IntTok.isNot(AsmToken::Integer))
611 return Error(IntTok.getLoc(), "expected stack index");
612 switch (IntTok.getIntVal()) {
613 case 0: RegNo = X86::ST0; break;
614 case 1: RegNo = X86::ST1; break;
615 case 2: RegNo = X86::ST2; break;
616 case 3: RegNo = X86::ST3; break;
617 case 4: RegNo = X86::ST4; break;
618 case 5: RegNo = X86::ST5; break;
619 case 6: RegNo = X86::ST6; break;
620 case 7: RegNo = X86::ST7; break;
621 default: return Error(IntTok.getLoc(), "invalid stack index");
622 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000623
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000624 if (getParser().Lex().isNot(AsmToken::RParen))
625 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000626
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000627 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000628 Parser.Lex(); // Eat ')'
629 return false;
630 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000631
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000632 EndLoc = Parser.getTok().getEndLoc();
633
Chris Lattner645b2092010-06-24 07:29:18 +0000634 // If this is "db[0-7]", match it as an alias
635 // for dr[0-7].
636 if (RegNo == 0 && Tok.getString().size() == 3 &&
637 Tok.getString().startswith("db")) {
638 switch (Tok.getString()[2]) {
639 case '0': RegNo = X86::DR0; break;
640 case '1': RegNo = X86::DR1; break;
641 case '2': RegNo = X86::DR2; break;
642 case '3': RegNo = X86::DR3; break;
643 case '4': RegNo = X86::DR4; break;
644 case '5': RegNo = X86::DR5; break;
645 case '6': RegNo = X86::DR6; break;
646 case '7': RegNo = X86::DR7; break;
647 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000648
Chris Lattner645b2092010-06-24 07:29:18 +0000649 if (RegNo != 0) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000650 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner645b2092010-06-24 07:29:18 +0000651 Parser.Lex(); // Eat it.
652 return false;
653 }
654 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000655
Devang Patel1aea4302012-01-20 22:32:05 +0000656 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000657 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000658 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000659 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000660 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000661
Sean Callananb9a25b72010-01-19 20:27:46 +0000662 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000663 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000664}
665
Devang Pateldd929fc2012-01-12 18:03:40 +0000666X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000667 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000668 return ParseIntelOperand();
669 return ParseATTOperand();
670}
671
Devang Pateld37ad242012-01-17 18:00:18 +0000672/// getIntelMemOperandSize - Return intel memory operand size.
673static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000674 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000675 .Cases("BYTE", "byte", 8)
676 .Cases("WORD", "word", 16)
677 .Cases("DWORD", "dword", 32)
678 .Cases("QWORD", "qword", 64)
679 .Cases("XWORD", "xword", 80)
680 .Cases("XMMWORD", "xmmword", 128)
681 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000682 .Default(0);
683 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000684}
685
Chad Rosier65c88922012-10-22 19:42:52 +0000686X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000687 unsigned Size) {
688 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Chad Rosier4284e172012-10-24 22:13:37 +0000689 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000690 SMLoc Start = Tok.getLoc(), End = Tok.getEndLoc();
Devang Patel0a338862012-01-12 01:36:43 +0000691
Chad Rosier4284e172012-10-24 22:13:37 +0000692 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000693 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
694
695 // Eat '['
696 if (getLexer().isNot(AsmToken::LBrac))
697 return ErrorOperand(Start, "Expected '[' token!");
698 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000699
Devang Pateld37ad242012-01-17 18:00:18 +0000700 if (getLexer().is(AsmToken::Identifier)) {
701 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000702 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000703 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000704 if (getParser().ParseExpression(Disp, End)) return 0;
705 if (getLexer().isNot(AsmToken::RBrac))
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000706 return ErrorOperand(Parser.getTok().getLoc(), "Expected ']' token!");
707 End = Parser.getTok().getEndLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000708 Parser.Lex();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000709 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000710 }
711 } else if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000712 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000713 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000714 SMLoc Loc = Tok.getLoc();
Devang Patel3e081312012-01-23 20:20:06 +0000715 if (getLexer().is(AsmToken::RBrac)) {
716 // Handle '[' number ']'
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000717 End = Parser.getTok().getEndLoc();
Devang Patel3e081312012-01-23 20:20:06 +0000718 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000719 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
720 if (SegReg)
721 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000722 Start, End, Size);
723 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000724 } else if (getLexer().is(AsmToken::Star)) {
725 // Handle '[' Scale*IndexReg ']'
726 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000727 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000728 if (ParseRegister(IndexReg, IdxRegLoc, End))
729 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000730 Scale = Val;
731 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000732 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000733 }
734
Chad Rosier2fbc2392012-10-29 18:01:54 +0000735 // Parse ][ as a plus.
736 bool ExpectRBrac = true;
737 if (getLexer().is(AsmToken::RBrac)) {
738 ExpectRBrac = false;
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000739 End = Parser.getTok().getEndLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000740 Parser.Lex();
Chad Rosier2fbc2392012-10-29 18:01:54 +0000741 }
742
743 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus) ||
744 getLexer().is(AsmToken::LBrac)) {
745 ExpectRBrac = true;
746 bool isPlus = getLexer().is(AsmToken::Plus) ||
747 getLexer().is(AsmToken::LBrac);
748 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000749 SMLoc PlusLoc = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000750 if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000751 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000752 Parser.Lex();
753 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000754 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000755 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000756 if (ParseRegister(IndexReg, IdxRegLoc, End))
757 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000758 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000759 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000760 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000761 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000762 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000763 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000764 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000765 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000766 if (!IndexReg)
767 ParseRegister(IndexReg, Start, End);
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000768 else if (getParser().ParseExpression(Disp, End))
769 return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000770 }
Devang Pateld37ad242012-01-17 18:00:18 +0000771 }
Chad Rosier2fbc2392012-10-29 18:01:54 +0000772
773 // Parse ][ as a plus.
774 if (getLexer().is(AsmToken::RBrac)) {
775 ExpectRBrac = false;
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000776 End = Parser.getTok().getEndLoc();
Chad Rosier2fbc2392012-10-29 18:01:54 +0000777 Parser.Lex();
Chad Rosier2fbc2392012-10-29 18:01:54 +0000778 if (getLexer().is(AsmToken::LBrac)) {
779 ExpectRBrac = true;
780 Parser.Lex();
781 if (getParser().ParseExpression(Disp, End))
782 return 0;
783 }
784 } else if (ExpectRBrac) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000785 if (getParser().ParseExpression(Disp, End))
786 return 0;
Chad Rosier2fbc2392012-10-29 18:01:54 +0000787 }
Devang Pateld37ad242012-01-17 18:00:18 +0000788
Chad Rosier2fbc2392012-10-29 18:01:54 +0000789 if (ExpectRBrac) {
790 if (getLexer().isNot(AsmToken::RBrac))
791 return ErrorOperand(End, "expected ']' token!");
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000792 End = Parser.getTok().getEndLoc();
Chad Rosier2fbc2392012-10-29 18:01:54 +0000793 Parser.Lex();
Chad Rosier2fbc2392012-10-29 18:01:54 +0000794 }
Devang Patelfdd3b302012-01-20 21:21:01 +0000795
Chad Rosierddb53ef2012-10-26 22:01:25 +0000796 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000797 if (Tok.getString().startswith(".")) {
798 SmallString<64> Err;
799 const MCExpr *NewDisp;
800 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
801 return ErrorOperand(Tok.getLoc(), Err);
802
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000803 End = Parser.getTok().getEndLoc();
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000804 Parser.Lex(); // Eat the field.
805 Disp = NewDisp;
806 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000807
Devang Patelfdd3b302012-01-20 21:21:01 +0000808 // handle [-42]
809 if (!BaseReg && !IndexReg)
Chad Rosierc0a14b82012-10-24 17:22:29 +0000810 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patelfdd3b302012-01-20 21:21:01 +0000811
Devang Pateld37ad242012-01-17 18:00:18 +0000812 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000813 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000814}
815
816/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000817X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000818 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000819 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000820
821 unsigned Size = getIntelMemOperandSize(Tok.getString());
822 if (Size) {
823 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000824 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
825 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000826 Parser.Lex();
827 }
828
Chad Rosierc0a14b82012-10-24 17:22:29 +0000829 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000830 return ParseIntelBracExpression(SegReg, Size);
831
832 if (!ParseRegister(SegReg, Start, End)) {
833 // Handel SegReg : [ ... ]
834 if (getLexer().isNot(AsmToken::Colon))
835 return ErrorOperand(Start, "Expected ':' token!");
836 Parser.Lex(); // Eat :
837 if (getLexer().isNot(AsmToken::LBrac))
838 return ErrorOperand(Start, "Expected '[' token!");
839 return ParseIntelBracExpression(SegReg, Size);
840 }
Devang Pateld37ad242012-01-17 18:00:18 +0000841
842 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000843 if (getParser().ParseExpression(Disp, End))
844 return 0;
Chad Rosier96d58e62012-10-19 20:57:14 +0000845
846 bool NeedSizeDir = false;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000847 bool IsVarDecl = false;
848 if (isParsingInlineAsm()) {
Chad Rosier96d58e62012-10-19 20:57:14 +0000849 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
850 const MCSymbol &Sym = SymRef->getSymbol();
851 // FIXME: The SemaLookup will fail if the name is anything other then an
852 // identifier.
853 // FIXME: Pass a valid SMLoc.
Chad Rosierc1ec2072013-01-10 22:10:27 +0000854 unsigned tSize;
855 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, tSize,
856 IsVarDecl);
857 if (!Size)
858 Size = tSize;
Chad Rosier96d58e62012-10-19 20:57:14 +0000859 NeedSizeDir = Size > 0;
860 }
861 }
Chad Rosier2a784132012-10-23 23:31:33 +0000862 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +0000863 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosierc1ec2072013-01-10 22:10:27 +0000864 else {
865 // If this is not a VarDecl then assume it is a FuncDecl or some other label
866 // reference. We need an 'r' constraint here, so we need to create register
867 // operand to ensure proper matching. Just pick a GPR based on the size of
868 // a pointer.
869 if (!IsVarDecl) {
870 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
871 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true);
872 }
873
Chad Rosierd4d96ac2012-10-23 23:34:28 +0000874 // When parsing inline assembly we set the base register to a non-zero value
875 // as we don't know the actual value at this time. This is necessary to
876 // get the matching correct in some cases.
Chad Rosier2a784132012-10-23 23:31:33 +0000877 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000878 /*Scale*/1, Start, End, Size, NeedSizeDir);
Chad Rosierc1ec2072013-01-10 22:10:27 +0000879 }
Chad Rosierc0a14b82012-10-24 17:22:29 +0000880}
881
Chad Rosier22f441a2012-10-24 22:21:50 +0000882/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000883bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
884 const MCExpr **NewDisp,
885 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +0000886 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000887 uint64_t OrigDispVal, DotDispVal;
888
889 // FIXME: Handle non-constant expressions.
890 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
891 OrigDispVal = OrigDisp->getValue();
892 } else {
893 Err = "Non-constant offsets are not supported!";
894 return true;
895 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000896
897 // Drop the '.'.
898 StringRef DotDispStr = Tok.getString().drop_front(1);
899
Chad Rosier22f441a2012-10-24 22:21:50 +0000900 // .Imm gets lexed as a real.
901 if (Tok.is(AsmToken::Real)) {
902 APInt DotDisp;
903 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000904 DotDispVal = DotDisp.getZExtValue();
Chad Rosierec130222012-10-25 21:51:10 +0000905 } else if (Tok.is(AsmToken::Identifier)) {
906 // We should only see an identifier when parsing the original inline asm.
907 // The front-end should rewrite this in terms of immediates.
908 assert (isParsingInlineAsm() && "Unexpected field name!");
909
910 unsigned DotDisp;
911 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
912 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
913 DotDisp)) {
914 Err = "Unable to lookup field reference!";
915 return true;
916 }
917 DotDispVal = DotDisp;
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000918 } else {
919 Err = "Unexpected token type!";
920 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +0000921 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000922
Chad Rosierec130222012-10-25 21:51:10 +0000923 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
924 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
925 unsigned Len = DotDispStr.size();
926 unsigned Val = OrigDispVal + DotDispVal;
927 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
928 Val));
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000929 }
930
931 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
932 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +0000933}
934
Chad Rosierc0a14b82012-10-24 17:22:29 +0000935/// Parse the 'offset' operator. This operator is used to specify the
936/// location rather then the content of a variable.
937X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
938 SMLoc OffsetOfLoc = Start;
939 Parser.Lex(); // Eat offset.
940 Start = Parser.getTok().getLoc();
941 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
942
Chad Rosier6e431572012-10-26 16:09:20 +0000943 SMLoc End;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000944 const MCExpr *Val;
945 if (getParser().ParseExpression(Val, End))
Chad Rosier7ab21c72012-10-26 18:32:44 +0000946 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosierc0a14b82012-10-24 17:22:29 +0000947
Chad Rosier6e431572012-10-26 16:09:20 +0000948 // Don't emit the offset operator.
949 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
950
Chad Rosierc0a14b82012-10-24 17:22:29 +0000951 // The offset operator will have an 'r' constraint, thus we need to create
952 // register operand to ensure proper matching. Just pick a GPR based on
953 // the size of a pointer.
954 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000955 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
956 OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +0000957}
958
Chad Rosierefcb3d92012-10-26 18:04:20 +0000959/// Parse the 'TYPE' operator. The TYPE operator returns the size of a C or
960/// C++ type or variable. If the variable is an array, TYPE returns the size of
961/// a single element of the array.
962X86Operand *X86AsmParser::ParseIntelTypeOperator(SMLoc Start) {
963 SMLoc TypeLoc = Start;
964 Parser.Lex(); // Eat offset.
965 Start = Parser.getTok().getLoc();
966 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
967
968 SMLoc End;
969 const MCExpr *Val;
970 if (getParser().ParseExpression(Val, End))
971 return 0;
972
Chad Rosierefcb3d92012-10-26 18:04:20 +0000973 unsigned Size = 0;
974 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
975 const MCSymbol &Sym = SymRef->getSymbol();
976 // FIXME: The SemaLookup will fail if the name is anything other then an
977 // identifier.
978 // FIXME: Pass a valid SMLoc.
Chad Rosierc1ec2072013-01-10 22:10:27 +0000979 bool IsVarDecl;
980 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Size,
981 IsVarDecl))
Chad Rosier7ab21c72012-10-26 18:32:44 +0000982 return ErrorOperand(Start, "Unable to lookup TYPE of expr!");
Chad Rosierefcb3d92012-10-26 18:04:20 +0000983
984 Size /= 8; // Size is in terms of bits, but we want bytes in the context.
985 }
986
987 // Rewrite the type operator and the C or C++ type or variable in terms of an
988 // immediate. E.g. TYPE foo -> $$4
989 unsigned Len = End.getPointer() - TypeLoc.getPointer();
990 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, Size));
991
992 const MCExpr *Imm = MCConstantExpr::Create(Size, getContext());
993 return X86Operand::CreateImm(Imm, Start, End, /*NeedAsmRewrite*/false);
994}
995
Devang Pateld37ad242012-01-17 18:00:18 +0000996X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000997 SMLoc Start = Parser.getTok().getLoc(), End;
998
Chad Rosierc0a14b82012-10-24 17:22:29 +0000999 // offset operator.
Chad Rosier7ab21c72012-10-26 18:32:44 +00001000 StringRef AsmTokStr = Parser.getTok().getString();
1001 if ((AsmTokStr == "offset" || AsmTokStr == "OFFSET") &&
Chad Rosierc0a14b82012-10-24 17:22:29 +00001002 isParsingInlineAsm())
1003 return ParseIntelOffsetOfOperator(Start);
1004
Chad Rosierefcb3d92012-10-26 18:04:20 +00001005 // Type directive.
Chad Rosier7ab21c72012-10-26 18:32:44 +00001006 if ((AsmTokStr == "type" || AsmTokStr == "TYPE") &&
Chad Rosierefcb3d92012-10-26 18:04:20 +00001007 isParsingInlineAsm())
1008 return ParseIntelTypeOperator(Start);
1009
Chad Rosier7ab21c72012-10-26 18:32:44 +00001010 // Unsupported directives.
1011 if (isParsingIntelSyntax() &&
1012 (AsmTokStr == "size" || AsmTokStr == "SIZE" ||
1013 AsmTokStr == "length" || AsmTokStr == "LENGTH"))
1014 return ErrorOperand(Start, "Unsupported directive!");
1015
Devang Pateld37ad242012-01-17 18:00:18 +00001016 // immediate.
1017 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
1018 getLexer().is(AsmToken::Minus)) {
1019 const MCExpr *Val;
1020 if (!getParser().ParseExpression(Val, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +00001021 return X86Operand::CreateImm(Val, Start, End);
1022 }
1023 }
1024
Devang Patel0a338862012-01-12 01:36:43 +00001025 // register
Devang Patel1aea4302012-01-20 22:32:05 +00001026 unsigned RegNo = 0;
1027 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001028 // If this is a segment register followed by a ':', then this is the start
1029 // of a memory reference, otherwise this is a normal register reference.
1030 if (getLexer().isNot(AsmToken::Colon))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001031 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001032
1033 getParser().Lex(); // Eat the colon.
1034 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001035 }
1036
1037 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001038 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001039}
1040
Devang Pateldd929fc2012-01-12 18:03:40 +00001041X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001042 switch (getLexer().getKind()) {
1043 default:
Chris Lattnereef6d782010-04-17 18:56:34 +00001044 // Parse a memory operand with no segment register.
1045 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +00001046 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +00001047 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +00001048 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +00001049 SMLoc Start, End;
1050 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001051 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001052 Error(Start, "%eiz and %riz can only be used as index registers",
1053 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001054 return 0;
1055 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001056
Chris Lattnereef6d782010-04-17 18:56:34 +00001057 // If this is a segment register followed by a ':', then this is the start
1058 // of a memory reference, otherwise this is a normal register reference.
1059 if (getLexer().isNot(AsmToken::Colon))
1060 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001061
1062
Chris Lattnereef6d782010-04-17 18:56:34 +00001063 getParser().Lex(); // Eat the colon.
1064 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +00001065 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001066 case AsmToken::Dollar: {
1067 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +00001068 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +00001069 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001070 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +00001071 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +00001072 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001073 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001074 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001075 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +00001076}
1077
Chris Lattnereef6d782010-04-17 18:56:34 +00001078/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1079/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +00001080X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001081
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001082 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1083 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +00001084 // only way to do this without lookahead is to eat the '(' and see what is
1085 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001086 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001087 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +00001088 SMLoc ExprEnd;
1089 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001090
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001091 // After parsing the base expression we could either have a parenthesized
1092 // memory address or not. If not, return now. If so, eat the (.
1093 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001094 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001095 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001096 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001097 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001098 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001099
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001100 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001101 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001102 } else {
1103 // Okay, we have a '('. We don't know if this is an expression or not, but
1104 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +00001105 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001106 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001107
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001108 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001109 // Nothing to do here, fall into the code below with the '(' part of the
1110 // memory operand consumed.
1111 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001112 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001113
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001114 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001115 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +00001116 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001117
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001118 // After parsing the base expression we could either have a parenthesized
1119 // memory address or not. If not, return now. If so, eat the (.
1120 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001121 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001122 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001123 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001124 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001125 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001126
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001127 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001128 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001129 }
1130 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001131
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001132 // If we reached here, then we just ate the ( of the memory operand. Process
1133 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001134 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001135 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001136
Chris Lattner29ef9a22010-01-15 18:51:29 +00001137 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001138 SMLoc StartLoc, EndLoc;
1139 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001140 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001141 Error(StartLoc, "eiz and riz can only be used as index registers",
1142 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001143 return 0;
1144 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001145 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001146
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001147 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001148 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001149 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001150
1151 // Following the comma we should have either an index register, or a scale
1152 // value. We don't support the later form, but we want to parse it
1153 // correctly.
1154 //
1155 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001156 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001157 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001158 SMLoc L;
1159 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001160
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001161 if (getLexer().isNot(AsmToken::RParen)) {
1162 // Parse the scale amount:
1163 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001164 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001165 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001166 "expected comma in scale expression");
1167 return 0;
1168 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001169 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001170
1171 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001172 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001173
1174 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001175 if (getParser().ParseAbsoluteExpression(ScaleVal)){
1176 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001177 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001178 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001179
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001180 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001181 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1182 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1183 return 0;
1184 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001185 Scale = (unsigned)ScaleVal;
1186 }
1187 }
1188 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001189 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001190 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001191 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001192
1193 int64_t Value;
1194 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001195 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001196
Daniel Dunbaree910252010-08-24 19:13:38 +00001197 if (Value != 1)
1198 Warning(Loc, "scale factor without index register is ignored");
1199 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001200 }
1201 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001202
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001203 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001204 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001205 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001206 return 0;
1207 }
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001208 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001209 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001210
Kevin Enderby84faf652012-03-12 21:32:09 +00001211 // If we have both a base register and an index register make sure they are
1212 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001213 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001214 if (BaseReg != 0 && IndexReg != 0) {
1215 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001216 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1217 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001218 IndexReg != X86::RIZ) {
1219 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1220 return 0;
1221 }
1222 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001223 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1224 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001225 IndexReg != X86::EIZ){
1226 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1227 return 0;
1228 }
1229 }
1230
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001231 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1232 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001233}
1234
Devang Pateldd929fc2012-01-12 18:03:40 +00001235bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001236ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001237 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001238 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001239 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001240
Chris Lattnerd8f71792010-11-28 20:23:50 +00001241 // FIXME: Hack to recognize setneb as setne.
1242 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1243 PatchedName != "setb" && PatchedName != "setnb")
1244 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001245
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001246 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1247 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001248 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001249 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1250 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001251 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001252 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001253 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001254 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001255 .Case("eq", 0x00)
1256 .Case("lt", 0x01)
1257 .Case("le", 0x02)
1258 .Case("unord", 0x03)
1259 .Case("neq", 0x04)
1260 .Case("nlt", 0x05)
1261 .Case("nle", 0x06)
1262 .Case("ord", 0x07)
1263 /* AVX only from here */
1264 .Case("eq_uq", 0x08)
1265 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001266 .Case("ngt", 0x0A)
1267 .Case("false", 0x0B)
1268 .Case("neq_oq", 0x0C)
1269 .Case("ge", 0x0D)
1270 .Case("gt", 0x0E)
1271 .Case("true", 0x0F)
1272 .Case("eq_os", 0x10)
1273 .Case("lt_oq", 0x11)
1274 .Case("le_oq", 0x12)
1275 .Case("unord_s", 0x13)
1276 .Case("neq_us", 0x14)
1277 .Case("nlt_uq", 0x15)
1278 .Case("nle_uq", 0x16)
1279 .Case("ord_s", 0x17)
1280 .Case("eq_us", 0x18)
1281 .Case("nge_uq", 0x19)
1282 .Case("ngt_uq", 0x1A)
1283 .Case("false_os", 0x1B)
1284 .Case("neq_os", 0x1C)
1285 .Case("ge_oq", 0x1D)
1286 .Case("gt_oq", 0x1E)
1287 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001288 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001289 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001290 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1291 getParser().getContext());
1292 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001293 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001294 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001295 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001296 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001297 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001298 } else {
1299 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001300 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001301 }
1302 }
1303 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001304
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001305 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001306
Devang Patel885f65b2012-01-30 22:47:12 +00001307 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001308 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001309
Chris Lattner2544f422010-09-08 05:17:37 +00001310 // Determine whether this is an instruction prefix.
1311 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001312 Name == "lock" || Name == "rep" ||
1313 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001314 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001315 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001316
1317
Chris Lattner2544f422010-09-08 05:17:37 +00001318 // This does the actual operand parsing. Don't parse any more if we have a
1319 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1320 // just want to parse the "lock" as the first instruction and the "incl" as
1321 // the next one.
1322 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001323
1324 // Parse '*' modifier.
1325 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001326 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001327 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001328 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001329 }
1330
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001331 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001332 if (X86Operand *Op = ParseOperand())
1333 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001334 else {
1335 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001336 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001337 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001338
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001339 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001340 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001341
1342 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001343 if (X86Operand *Op = ParseOperand())
1344 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001345 else {
1346 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001347 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001348 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001349 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001350
Chris Lattnercbf8a982010-09-11 16:18:25 +00001351 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001352 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001353 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001354 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001355 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001356 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001357
Chris Lattner2544f422010-09-08 05:17:37 +00001358 if (getLexer().is(AsmToken::EndOfStatement))
1359 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001360 else if (isPrefix && getLexer().is(AsmToken::Slash))
1361 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001362
Devang Patel885f65b2012-01-30 22:47:12 +00001363 if (ExtraImmOp && isParsingIntelSyntax())
1364 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1365
Chris Lattner98c870f2010-11-06 19:25:43 +00001366 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1367 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1368 // documented form in various unofficial manuals, so a lot of code uses it.
1369 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1370 Operands.size() == 3) {
1371 X86Operand &Op = *(X86Operand*)Operands.back();
1372 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1373 isa<MCConstantExpr>(Op.Mem.Disp) &&
1374 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1375 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1376 SMLoc Loc = Op.getEndLoc();
1377 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1378 delete &Op;
1379 }
1380 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001381 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1382 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1383 Operands.size() == 3) {
1384 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1385 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1386 isa<MCConstantExpr>(Op.Mem.Disp) &&
1387 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1388 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1389 SMLoc Loc = Op.getEndLoc();
1390 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1391 delete &Op;
1392 }
1393 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001394 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1395 if (Name.startswith("ins") && Operands.size() == 3 &&
1396 (Name == "insb" || Name == "insw" || Name == "insl")) {
1397 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1398 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1399 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1400 Operands.pop_back();
1401 Operands.pop_back();
1402 delete &Op;
1403 delete &Op2;
1404 }
1405 }
1406
1407 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1408 if (Name.startswith("outs") && Operands.size() == 3 &&
1409 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1410 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1411 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1412 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1413 Operands.pop_back();
1414 Operands.pop_back();
1415 delete &Op;
1416 delete &Op2;
1417 }
1418 }
1419
1420 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1421 if (Name.startswith("movs") && Operands.size() == 3 &&
1422 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001423 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001424 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1425 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1426 if (isSrcOp(Op) && isDstOp(Op2)) {
1427 Operands.pop_back();
1428 Operands.pop_back();
1429 delete &Op;
1430 delete &Op2;
1431 }
1432 }
1433 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1434 if (Name.startswith("lods") && Operands.size() == 3 &&
1435 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001436 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001437 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1438 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1439 if (isSrcOp(*Op1) && Op2->isReg()) {
1440 const char *ins;
1441 unsigned reg = Op2->getReg();
1442 bool isLods = Name == "lods";
1443 if (reg == X86::AL && (isLods || Name == "lodsb"))
1444 ins = "lodsb";
1445 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1446 ins = "lodsw";
1447 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1448 ins = "lodsl";
1449 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1450 ins = "lodsq";
1451 else
1452 ins = NULL;
1453 if (ins != NULL) {
1454 Operands.pop_back();
1455 Operands.pop_back();
1456 delete Op1;
1457 delete Op2;
1458 if (Name != ins)
1459 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1460 }
1461 }
1462 }
1463 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1464 if (Name.startswith("stos") && Operands.size() == 3 &&
1465 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001466 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001467 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1468 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1469 if (isDstOp(*Op2) && Op1->isReg()) {
1470 const char *ins;
1471 unsigned reg = Op1->getReg();
1472 bool isStos = Name == "stos";
1473 if (reg == X86::AL && (isStos || Name == "stosb"))
1474 ins = "stosb";
1475 else if (reg == X86::AX && (isStos || Name == "stosw"))
1476 ins = "stosw";
1477 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1478 ins = "stosl";
1479 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1480 ins = "stosq";
1481 else
1482 ins = NULL;
1483 if (ins != NULL) {
1484 Operands.pop_back();
1485 Operands.pop_back();
1486 delete Op1;
1487 delete Op2;
1488 if (Name != ins)
1489 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1490 }
1491 }
1492 }
1493
Chris Lattnere9e16a32010-09-15 04:33:27 +00001494 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001495 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001496 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001497 Name.startswith("shl") || Name.startswith("sal") ||
1498 Name.startswith("rcl") || Name.startswith("rcr") ||
1499 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001500 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001501 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001502 // Intel syntax
1503 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1504 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001505 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1506 delete Operands[2];
1507 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001508 }
1509 } else {
1510 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1511 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001512 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1513 delete Operands[1];
1514 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001515 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001516 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001517 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001518
Chris Lattner15f89512011-04-09 19:41:05 +00001519 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1520 // instalias with an immediate operand yet.
1521 if (Name == "int" && Operands.size() == 2) {
1522 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1523 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1524 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1525 delete Operands[1];
1526 Operands.erase(Operands.begin() + 1);
1527 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1528 }
1529 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001530
Chris Lattner98986712010-01-14 22:21:20 +00001531 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001532}
1533
Devang Pateldd929fc2012-01-12 18:03:40 +00001534bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001535processInstruction(MCInst &Inst,
1536 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1537 switch (Inst.getOpcode()) {
1538 default: return false;
1539 case X86::AND16i16: {
1540 if (!Inst.getOperand(0).isImm() ||
1541 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1542 return false;
1543
1544 MCInst TmpInst;
1545 TmpInst.setOpcode(X86::AND16ri8);
1546 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1547 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1548 TmpInst.addOperand(Inst.getOperand(0));
1549 Inst = TmpInst;
1550 return true;
1551 }
1552 case X86::AND32i32: {
1553 if (!Inst.getOperand(0).isImm() ||
1554 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1555 return false;
1556
1557 MCInst TmpInst;
1558 TmpInst.setOpcode(X86::AND32ri8);
1559 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1560 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1561 TmpInst.addOperand(Inst.getOperand(0));
1562 Inst = TmpInst;
1563 return true;
1564 }
1565 case X86::AND64i32: {
1566 if (!Inst.getOperand(0).isImm() ||
1567 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1568 return false;
1569
1570 MCInst TmpInst;
1571 TmpInst.setOpcode(X86::AND64ri8);
1572 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1573 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1574 TmpInst.addOperand(Inst.getOperand(0));
1575 Inst = TmpInst;
1576 return true;
1577 }
Devang Patelac0f0482012-01-19 17:53:25 +00001578 case X86::XOR16i16: {
1579 if (!Inst.getOperand(0).isImm() ||
1580 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1581 return false;
1582
1583 MCInst TmpInst;
1584 TmpInst.setOpcode(X86::XOR16ri8);
1585 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1586 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1587 TmpInst.addOperand(Inst.getOperand(0));
1588 Inst = TmpInst;
1589 return true;
1590 }
1591 case X86::XOR32i32: {
1592 if (!Inst.getOperand(0).isImm() ||
1593 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1594 return false;
1595
1596 MCInst TmpInst;
1597 TmpInst.setOpcode(X86::XOR32ri8);
1598 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1599 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1600 TmpInst.addOperand(Inst.getOperand(0));
1601 Inst = TmpInst;
1602 return true;
1603 }
1604 case X86::XOR64i32: {
1605 if (!Inst.getOperand(0).isImm() ||
1606 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1607 return false;
1608
1609 MCInst TmpInst;
1610 TmpInst.setOpcode(X86::XOR64ri8);
1611 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1612 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1613 TmpInst.addOperand(Inst.getOperand(0));
1614 Inst = TmpInst;
1615 return true;
1616 }
1617 case X86::OR16i16: {
1618 if (!Inst.getOperand(0).isImm() ||
1619 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1620 return false;
1621
1622 MCInst TmpInst;
1623 TmpInst.setOpcode(X86::OR16ri8);
1624 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1625 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1626 TmpInst.addOperand(Inst.getOperand(0));
1627 Inst = TmpInst;
1628 return true;
1629 }
1630 case X86::OR32i32: {
1631 if (!Inst.getOperand(0).isImm() ||
1632 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1633 return false;
1634
1635 MCInst TmpInst;
1636 TmpInst.setOpcode(X86::OR32ri8);
1637 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1638 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1639 TmpInst.addOperand(Inst.getOperand(0));
1640 Inst = TmpInst;
1641 return true;
1642 }
1643 case X86::OR64i32: {
1644 if (!Inst.getOperand(0).isImm() ||
1645 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1646 return false;
1647
1648 MCInst TmpInst;
1649 TmpInst.setOpcode(X86::OR64ri8);
1650 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1651 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1652 TmpInst.addOperand(Inst.getOperand(0));
1653 Inst = TmpInst;
1654 return true;
1655 }
1656 case X86::CMP16i16: {
1657 if (!Inst.getOperand(0).isImm() ||
1658 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1659 return false;
1660
1661 MCInst TmpInst;
1662 TmpInst.setOpcode(X86::CMP16ri8);
1663 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1664 TmpInst.addOperand(Inst.getOperand(0));
1665 Inst = TmpInst;
1666 return true;
1667 }
1668 case X86::CMP32i32: {
1669 if (!Inst.getOperand(0).isImm() ||
1670 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1671 return false;
1672
1673 MCInst TmpInst;
1674 TmpInst.setOpcode(X86::CMP32ri8);
1675 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1676 TmpInst.addOperand(Inst.getOperand(0));
1677 Inst = TmpInst;
1678 return true;
1679 }
1680 case X86::CMP64i32: {
1681 if (!Inst.getOperand(0).isImm() ||
1682 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1683 return false;
1684
1685 MCInst TmpInst;
1686 TmpInst.setOpcode(X86::CMP64ri8);
1687 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1688 TmpInst.addOperand(Inst.getOperand(0));
1689 Inst = TmpInst;
1690 return true;
1691 }
Devang Patela951f772012-01-19 18:40:55 +00001692 case X86::ADD16i16: {
1693 if (!Inst.getOperand(0).isImm() ||
1694 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1695 return false;
1696
1697 MCInst TmpInst;
1698 TmpInst.setOpcode(X86::ADD16ri8);
1699 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1700 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1701 TmpInst.addOperand(Inst.getOperand(0));
1702 Inst = TmpInst;
1703 return true;
1704 }
1705 case X86::ADD32i32: {
1706 if (!Inst.getOperand(0).isImm() ||
1707 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1708 return false;
1709
1710 MCInst TmpInst;
1711 TmpInst.setOpcode(X86::ADD32ri8);
1712 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1713 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1714 TmpInst.addOperand(Inst.getOperand(0));
1715 Inst = TmpInst;
1716 return true;
1717 }
1718 case X86::ADD64i32: {
1719 if (!Inst.getOperand(0).isImm() ||
1720 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1721 return false;
1722
1723 MCInst TmpInst;
1724 TmpInst.setOpcode(X86::ADD64ri8);
1725 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1726 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1727 TmpInst.addOperand(Inst.getOperand(0));
1728 Inst = TmpInst;
1729 return true;
1730 }
1731 case X86::SUB16i16: {
1732 if (!Inst.getOperand(0).isImm() ||
1733 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1734 return false;
1735
1736 MCInst TmpInst;
1737 TmpInst.setOpcode(X86::SUB16ri8);
1738 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1739 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1740 TmpInst.addOperand(Inst.getOperand(0));
1741 Inst = TmpInst;
1742 return true;
1743 }
1744 case X86::SUB32i32: {
1745 if (!Inst.getOperand(0).isImm() ||
1746 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1747 return false;
1748
1749 MCInst TmpInst;
1750 TmpInst.setOpcode(X86::SUB32ri8);
1751 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1752 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1753 TmpInst.addOperand(Inst.getOperand(0));
1754 Inst = TmpInst;
1755 return true;
1756 }
1757 case X86::SUB64i32: {
1758 if (!Inst.getOperand(0).isImm() ||
1759 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1760 return false;
1761
1762 MCInst TmpInst;
1763 TmpInst.setOpcode(X86::SUB64ri8);
1764 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1765 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1766 TmpInst.addOperand(Inst.getOperand(0));
1767 Inst = TmpInst;
1768 return true;
1769 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001770 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001771}
1772
Jim Grosbach3ca63822012-11-14 18:04:47 +00001773static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001774bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001775MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001776 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001777 MCStreamer &Out, unsigned &ErrorInfo,
1778 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001779 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001780 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1781 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001782 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001783
Chris Lattner7c51a312010-09-29 01:50:45 +00001784 // First, handle aliases that expand to multiple instructions.
1785 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001786 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001787 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001788 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001789 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001790 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001791 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001792 MCInst Inst;
1793 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001794 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001795 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001796 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001797
Chris Lattner0bb83a82010-09-30 16:39:29 +00001798 const char *Repl =
1799 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001800 .Case("finit", "fninit")
1801 .Case("fsave", "fnsave")
1802 .Case("fstcw", "fnstcw")
1803 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001804 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001805 .Case("fstsw", "fnstsw")
1806 .Case("fstsww", "fnstsw")
1807 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001808 .Default(0);
1809 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001810 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001811 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001812 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001813
Chris Lattnera008e8a2010-09-06 21:54:15 +00001814 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001815 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001816
Daniel Dunbarc918d602010-05-04 16:12:42 +00001817 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001818 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001819 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001820 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001821 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001822 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001823 // Some instructions need post-processing to, for example, tweak which
1824 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001825 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001826 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001827 while (processInstruction(Inst, Operands))
1828 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001829
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001830 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001831 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001832 Out.EmitInstruction(Inst);
1833 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001834 return false;
Jim Grosbach3ca63822012-11-14 18:04:47 +00001835 case Match_MissingFeature: {
1836 assert(ErrorInfo && "Unknown missing feature!");
1837 // Special case the error message for the very common case where only
1838 // a single subtarget feature is missing.
1839 std::string Msg = "instruction requires:";
1840 unsigned Mask = 1;
1841 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
1842 if (ErrorInfo & Mask) {
1843 Msg += " ";
1844 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
1845 }
1846 Mask <<= 1;
1847 }
1848 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
1849 }
Chris Lattnera008e8a2010-09-06 21:54:15 +00001850 case Match_InvalidOperand:
1851 WasOriginallyInvalidOperand = true;
1852 break;
1853 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001854 break;
1855 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001856
Daniel Dunbarc918d602010-05-04 16:12:42 +00001857 // FIXME: Ideally, we would only attempt suffix matches for things which are
1858 // valid prefixes, and we could just infer the right unambiguous
1859 // type. However, that requires substantially more matcher support than the
1860 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001861
Daniel Dunbarc918d602010-05-04 16:12:42 +00001862 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001863 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001864 SmallString<16> Tmp;
1865 Tmp += Base;
1866 Tmp += ' ';
1867 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001868
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001869 // If this instruction starts with an 'f', then it is a floating point stack
1870 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1871 // 80-bit floating point, which use the suffixes s,l,t respectively.
1872 //
1873 // Otherwise, we assume that this may be an integer instruction, which comes
1874 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1875 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001876
Daniel Dunbarc918d602010-05-04 16:12:42 +00001877 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001878 Tmp[Base.size()] = Suffixes[0];
1879 unsigned ErrorInfoIgnore;
Jim Grosbach3ca63822012-11-14 18:04:47 +00001880 unsigned ErrorInfoMissingFeature;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001881 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001882
Chad Rosier6e006d32012-10-12 22:53:36 +00001883 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1884 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001885 // If this returned as a missing feature failure, remember that.
1886 if (Match1 == Match_MissingFeature)
1887 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001888 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001889 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1890 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001891 // If this returned as a missing feature failure, remember that.
1892 if (Match2 == Match_MissingFeature)
1893 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001894 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001895 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1896 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001897 // If this returned as a missing feature failure, remember that.
1898 if (Match3 == Match_MissingFeature)
1899 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001900 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001901 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1902 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001903 // If this returned as a missing feature failure, remember that.
1904 if (Match4 == Match_MissingFeature)
1905 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbarc918d602010-05-04 16:12:42 +00001906
1907 // Restore the old token.
1908 Op->setTokenValue(Base);
1909
1910 // If exactly one matched, then we treat that as a successful match (and the
1911 // instruction will already have been filled in correctly, since the failing
1912 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001913 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001914 (Match1 == Match_Success) + (Match2 == Match_Success) +
1915 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001916 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001917 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001918 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001919 Out.EmitInstruction(Inst);
1920 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001921 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001922 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001923
Chris Lattnerec6789f2010-09-06 20:08:02 +00001924 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001925
Daniel Dunbar09062b12010-08-12 00:55:42 +00001926 // If we had multiple suffix matches, then identify this as an ambiguous
1927 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001928 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001929 char MatchChars[4];
1930 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001931 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1932 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1933 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1934 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001935
1936 SmallString<126> Msg;
1937 raw_svector_ostream OS(Msg);
1938 OS << "ambiguous instructions require an explicit suffix (could be ";
1939 for (unsigned i = 0; i != NumMatches; ++i) {
1940 if (i != 0)
1941 OS << ", ";
1942 if (i + 1 == NumMatches)
1943 OS << "or ";
1944 OS << "'" << Base << MatchChars[i] << "'";
1945 }
1946 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001947 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001948 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001949 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001950
Chris Lattnera008e8a2010-09-06 21:54:15 +00001951 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001952
Chris Lattnera008e8a2010-09-06 21:54:15 +00001953 // If all of the instructions reported an invalid mnemonic, then the original
1954 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001955 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1956 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001957 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001958 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001959 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001960 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001961 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001962 }
1963
1964 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001965 if (ErrorInfo != ~0U) {
1966 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001967 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001968 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001969
Chad Rosier84125ca2012-10-13 00:26:04 +00001970 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001971 if (Operand->getStartLoc().isValid()) {
1972 SMRange OperandRange = Operand->getLocRange();
1973 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001974 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001975 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001976 }
1977
Chad Rosierb4fdade2012-08-21 19:36:59 +00001978 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001979 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001980 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001981
Chris Lattnerec6789f2010-09-06 20:08:02 +00001982 // If one instruction matched with a missing feature, report this as a
1983 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001984 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1985 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach3ca63822012-11-14 18:04:47 +00001986 std::string Msg = "instruction requires:";
1987 unsigned Mask = 1;
1988 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
1989 if (ErrorInfoMissingFeature & Mask) {
1990 Msg += " ";
1991 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
1992 }
1993 Mask <<= 1;
1994 }
1995 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001996 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001997
Chris Lattnera008e8a2010-09-06 21:54:15 +00001998 // If one instruction matched with an invalid operand, report this as an
1999 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002000 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2001 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00002002 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002003 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002004 return true;
2005 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002006
Chris Lattnerec6789f2010-09-06 20:08:02 +00002007 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00002008 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002009 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00002010 return true;
2011}
2012
2013
Devang Pateldd929fc2012-01-12 18:03:40 +00002014bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00002015 StringRef IDVal = DirectiveID.getIdentifier();
2016 if (IDVal == ".word")
2017 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00002018 else if (IDVal.startswith(".code"))
2019 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00002020 else if (IDVal.startswith(".att_syntax")) {
2021 getParser().setAssemblerDialect(0);
2022 return false;
2023 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00002024 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00002025 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2026 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00002027 // FIXME : Handle noprefix
2028 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00002029 } else
Craig Topper76bd9382012-07-18 04:59:16 +00002030 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00002031 }
2032 return false;
2033 }
Chris Lattner537ca842010-10-30 17:38:55 +00002034 return true;
2035}
2036
2037/// ParseDirectiveWord
2038/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00002039bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00002040 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2041 for (;;) {
2042 const MCExpr *Value;
2043 if (getParser().ParseExpression(Value))
2044 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002045
Eric Christopher1ced2082013-01-09 03:52:05 +00002046 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier36b8fed2012-06-27 22:34:28 +00002047
Chris Lattner537ca842010-10-30 17:38:55 +00002048 if (getLexer().is(AsmToken::EndOfStatement))
2049 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002050
Chris Lattner537ca842010-10-30 17:38:55 +00002051 // FIXME: Improve diagnostic.
2052 if (getLexer().isNot(AsmToken::Comma))
2053 return Error(L, "unexpected token in directive");
2054 Parser.Lex();
2055 }
2056 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00002057
Chris Lattner537ca842010-10-30 17:38:55 +00002058 Parser.Lex();
2059 return false;
2060}
2061
Evan Chengbd27f5a2011-07-27 00:38:12 +00002062/// ParseDirectiveCode
2063/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00002064bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00002065 if (IDVal == ".code32") {
2066 Parser.Lex();
2067 if (is64BitMode()) {
2068 SwitchMode();
2069 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2070 }
2071 } else if (IDVal == ".code64") {
2072 Parser.Lex();
2073 if (!is64BitMode()) {
2074 SwitchMode();
2075 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2076 }
2077 } else {
2078 return Error(L, "unexpected directive " + IDVal);
2079 }
Chris Lattner537ca842010-10-30 17:38:55 +00002080
Evan Chengbd27f5a2011-07-27 00:38:12 +00002081 return false;
2082}
Chris Lattner537ca842010-10-30 17:38:55 +00002083
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002084// Force static initialization.
2085extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00002086 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2087 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002088}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002089
Chris Lattner0692ee62010-09-06 19:11:01 +00002090#define GET_REGISTER_MATCHER
2091#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach3ca63822012-11-14 18:04:47 +00002092#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002093#include "X86GenAsmMatcher.inc"