blob: 75d26f55c3e10efa0ab70abf6518812c49214eae [file] [log] [blame]
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
Chad Rosier4284e172012-10-24 22:13:37 +000011#include "llvm/ADT/APFloat.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000012#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000014#include "llvm/ADT/StringSwitch.h"
15#include "llvm/ADT/Twine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
18#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
21#include "llvm/MC/MCRegisterInfo.h"
22#include "llvm/MC/MCStreamer.h"
23#include "llvm/MC/MCSubtargetInfo.h"
24#include "llvm/MC/MCSymbol.h"
25#include "llvm/MC/MCTargetAsmParser.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000028#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000029
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000030using namespace llvm;
31
32namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000033struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000034
Devang Pateldd929fc2012-01-12 18:03:40 +000035class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000036 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &Parser;
Chad Rosier6a020a72012-10-25 20:41:34 +000038 ParseInstructionInfo *InstInfo;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000040 MCAsmParser &getParser() const { return Parser; }
41
42 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000044 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000045 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000046 bool MatchingInlineAsm = false) {
47 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000048 return Parser.Error(L, Msg, Ranges);
49 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000050
Devang Pateld37ad242012-01-17 18:00:18 +000051 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
52 Error(Loc, Msg);
53 return 0;
54 }
55
Chris Lattner309264d2010-01-15 18:44:13 +000056 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000057 X86Operand *ParseATTOperand();
58 X86Operand *ParseIntelOperand();
Chad Rosierc0a14b82012-10-24 17:22:29 +000059 X86Operand *ParseIntelOffsetOfOperator(SMLoc StartLoc);
Chad Rosier505bca32013-01-17 19:21:48 +000060 X86Operand *ParseIntelOperator(SMLoc StartLoc, unsigned OpKind);
Chad Rosierdd40e8c2013-03-27 21:49:56 +000061 X86Operand *ParseIntelMemOperand(unsigned SegReg, uint64_t ImmDisp,
62 SMLoc StartLoc);
63 X86Operand *ParseIntelBracExpression(unsigned SegReg, uint64_t ImmDisp,
64 unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000065 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000066
Chad Rosierd3e74162013-03-19 21:11:56 +000067 X86Operand *CreateMemForInlineAsm(const MCExpr *Disp, SMLoc Start, SMLoc End,
68 SMLoc SizeDirLoc, unsigned Size);
69
Chad Rosier5e6b37f2012-10-25 17:37:43 +000070 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr **NewDisp,
71 SmallString<64> &Err);
Chad Rosier22f441a2012-10-24 22:21:50 +000072
Kevin Enderby9c656452009-09-10 20:51:44 +000073 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000074 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000075
Devang Patelb8ba13f2012-01-18 22:42:29 +000076 bool processInstruction(MCInst &Inst,
77 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
78
Chad Rosier84125ca2012-10-13 00:26:04 +000079 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000080 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000081 MCStreamer &Out, unsigned &ErrorInfo,
82 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000083
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000084 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000085 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000086 bool isSrcOp(X86Operand &Op);
87
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000088 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
89 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000090 bool isDstOp(X86Operand &Op);
91
Evan Cheng59ee62d2011-07-11 03:57:24 +000092 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000093 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000094 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000095 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000096 void SwitchMode() {
97 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
98 setAvailableFeatures(FB);
99 }
Evan Chengebdeeab2011-07-08 01:53:10 +0000100
Daniel Dunbar54074b52010-07-19 05:44:09 +0000101 /// @name Auto-generated Matcher Functions
102 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000103
Chris Lattner0692ee62010-09-06 19:11:01 +0000104#define GET_ASSEMBLER_HEADER
105#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000106
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000107 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000108
109public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000110 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosier6a020a72012-10-25 20:41:34 +0000111 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000112
Daniel Dunbar54074b52010-07-19 05:44:09 +0000113 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000114 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000115 }
Roman Divackybf755322011-01-27 17:14:22 +0000116 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000117
Chad Rosier6a020a72012-10-25 20:41:34 +0000118 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
119 SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000120 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000121
122 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000123
124 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000125 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000126 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000127};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000128} // end anonymous namespace
129
Sean Callanane9b466d2010-01-23 00:40:33 +0000130/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000131/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000132
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000133static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000134
135/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000136
Craig Topper76bd9382012-07-18 04:59:16 +0000137static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000138 return (( Value <= 0x000000000000007FULL)||
139 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
140 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
141}
142
143static bool isImmSExti32i8Value(uint64_t Value) {
144 return (( Value <= 0x000000000000007FULL)||
145 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
146 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
147}
148
149static bool isImmZExtu32u8Value(uint64_t Value) {
150 return (Value <= 0x00000000000000FFULL);
151}
152
153static bool isImmSExti64i8Value(uint64_t Value) {
154 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000155 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000156}
157
158static bool isImmSExti64i32Value(uint64_t Value) {
159 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000160 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000161}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000162namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000163
164/// X86Operand - Instances of this class represent a parsed X86 machine
165/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000166struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000167 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000168 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000169 Register,
170 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000171 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000172 } Kind;
173
Chris Lattner29ef9a22010-01-15 18:51:29 +0000174 SMLoc StartLoc, EndLoc;
Chad Rosier5a719fc2012-10-23 17:43:43 +0000175 SMLoc OffsetOfLoc;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000176 bool AddressOf;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000177
Eric Christophera286fc02013-03-15 00:42:55 +0000178 struct TokOp {
179 const char *Data;
180 unsigned Length;
181 };
182
183 struct RegOp {
184 unsigned RegNo;
185 };
186
187 struct ImmOp {
188 const MCExpr *Val;
Eric Christophera286fc02013-03-15 00:42:55 +0000189 };
190
191 struct MemOp {
192 unsigned SegReg;
193 const MCExpr *Disp;
194 unsigned BaseReg;
195 unsigned IndexReg;
196 unsigned Scale;
197 unsigned Size;
Eric Christophera286fc02013-03-15 00:42:55 +0000198 };
199
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000200 union {
Eric Christophera286fc02013-03-15 00:42:55 +0000201 struct TokOp Tok;
202 struct RegOp Reg;
203 struct ImmOp Imm;
204 struct MemOp Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000205 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000206
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000207 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000208 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000209
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000210 /// getStartLoc - Get the location of the first token of this operand.
211 SMLoc getStartLoc() const { return StartLoc; }
212 /// getEndLoc - Get the location of the last token of this operand.
213 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000214 /// getLocRange - Get the range between the first and last token of this
215 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000216 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000217 /// getOffsetOfLoc - Get the location of the offset operator.
218 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000219
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000220 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000221
Daniel Dunbar20927f22009-08-07 08:26:05 +0000222 StringRef getToken() const {
223 assert(Kind == Token && "Invalid access!");
224 return StringRef(Tok.Data, Tok.Length);
225 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000226 void setTokenValue(StringRef Value) {
227 assert(Kind == Token && "Invalid access!");
228 Tok.Data = Value.data();
229 Tok.Length = Value.size();
230 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000231
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000232 unsigned getReg() const {
233 assert(Kind == Register && "Invalid access!");
234 return Reg.RegNo;
235 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000236
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000237 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000238 assert(Kind == Immediate && "Invalid access!");
239 return Imm.Val;
240 }
241
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000242 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000243 assert(Kind == Memory && "Invalid access!");
244 return Mem.Disp;
245 }
246 unsigned getMemSegReg() const {
247 assert(Kind == Memory && "Invalid access!");
248 return Mem.SegReg;
249 }
250 unsigned getMemBaseReg() const {
251 assert(Kind == Memory && "Invalid access!");
252 return Mem.BaseReg;
253 }
254 unsigned getMemIndexReg() const {
255 assert(Kind == Memory && "Invalid access!");
256 return Mem.IndexReg;
257 }
258 unsigned getMemScale() const {
259 assert(Kind == Memory && "Invalid access!");
260 return Mem.Scale;
261 }
262
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000263 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000264
265 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000266
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000267 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000268 if (!isImm())
269 return false;
270
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000271 // If this isn't a constant expr, just assume it fits and let relaxation
272 // handle it.
273 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
274 if (!CE)
275 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000276
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000277 // Otherwise, check the value is in a range that makes sense for this
278 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000279 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000280 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000281 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000282 if (!isImm())
283 return false;
284
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000285 // If this isn't a constant expr, just assume it fits and let relaxation
286 // handle it.
287 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
288 if (!CE)
289 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000290
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000291 // Otherwise, check the value is in a range that makes sense for this
292 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000293 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000294 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000295 bool isImmZExtu32u8() const {
296 if (!isImm())
297 return false;
298
299 // If this isn't a constant expr, just assume it fits and let relaxation
300 // handle it.
301 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
302 if (!CE)
303 return true;
304
305 // Otherwise, check the value is in a range that makes sense for this
306 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000307 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000308 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000309 bool isImmSExti64i8() const {
310 if (!isImm())
311 return false;
312
313 // If this isn't a constant expr, just assume it fits and let relaxation
314 // handle it.
315 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
316 if (!CE)
317 return true;
318
319 // Otherwise, check the value is in a range that makes sense for this
320 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000321 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000322 }
323 bool isImmSExti64i32() const {
324 if (!isImm())
325 return false;
326
327 // If this isn't a constant expr, just assume it fits and let relaxation
328 // handle it.
329 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
330 if (!CE)
331 return true;
332
333 // Otherwise, check the value is in a range that makes sense for this
334 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000335 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000336 }
337
Chad Rosiera703fb92012-10-22 19:50:35 +0000338 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000339 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000340 }
341
Chad Rosierc1ec2072013-01-10 22:10:27 +0000342 bool needAddressOf() const {
343 return AddressOf;
344 }
345
Daniel Dunbar20927f22009-08-07 08:26:05 +0000346 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000347 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000348 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000349 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000350 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000351 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000352 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000353 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000354 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000355 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000356 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000357 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000358 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000359 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000360 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000361 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000362 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000363 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000364 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000365 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000366 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000367 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000368
Craig Topper75dc33a2012-07-18 04:11:12 +0000369 bool isMemVX32() const {
370 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
371 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
372 }
373 bool isMemVY32() const {
374 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
375 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
376 }
377 bool isMemVX64() const {
378 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
379 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
380 }
381 bool isMemVY64() const {
382 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
383 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
384 }
385
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000386 bool isAbsMem() const {
387 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000388 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000389 }
390
Daniel Dunbar20927f22009-08-07 08:26:05 +0000391 bool isReg() const { return Kind == Register; }
392
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000393 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
394 // Add as immediates when possible.
395 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
396 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
397 else
398 Inst.addOperand(MCOperand::CreateExpr(Expr));
399 }
400
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000401 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000402 assert(N == 1 && "Invalid number of operands!");
403 Inst.addOperand(MCOperand::CreateReg(getReg()));
404 }
405
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000406 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000407 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000408 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000409 }
410
Chad Rosier36b8fed2012-06-27 22:34:28 +0000411 void addMem8Operands(MCInst &Inst, unsigned N) const {
412 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000413 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000414 void addMem16Operands(MCInst &Inst, unsigned N) const {
415 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000416 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000417 void addMem32Operands(MCInst &Inst, unsigned N) const {
418 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000419 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000420 void addMem64Operands(MCInst &Inst, unsigned N) const {
421 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000422 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000423 void addMem80Operands(MCInst &Inst, unsigned N) const {
424 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000425 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000426 void addMem128Operands(MCInst &Inst, unsigned N) const {
427 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000428 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000429 void addMem256Operands(MCInst &Inst, unsigned N) const {
430 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000431 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000432 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
433 addMemOperands(Inst, N);
434 }
435 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
436 addMemOperands(Inst, N);
437 }
438 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
439 addMemOperands(Inst, N);
440 }
441 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
442 addMemOperands(Inst, N);
443 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000444
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000445 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000446 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000447 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
448 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
449 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000450 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000451 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
452 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000453
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000454 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
455 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000456 // Add as immediates when possible.
457 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
458 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
459 else
460 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000461 }
462
Chris Lattnerb4307b32010-01-15 19:28:38 +0000463 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000464 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size());
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000465 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000466 Res->Tok.Data = Str.data();
467 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000468 return Res;
469 }
470
Chad Rosierc0a14b82012-10-24 17:22:29 +0000471 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc1ec2072013-01-10 22:10:27 +0000472 bool AddressOf = false,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000473 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000474 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000475 Res->Reg.RegNo = RegNo;
Chad Rosierc1ec2072013-01-10 22:10:27 +0000476 Res->AddressOf = AddressOf;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000477 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000478 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000479 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000480
Chad Rosier811ddf62013-03-19 21:58:18 +0000481 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
Chris Lattnerb4307b32010-01-15 19:28:38 +0000482 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000483 Res->Imm.Val = Val;
484 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000485 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000486
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000487 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000488 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier023c8802013-03-19 17:32:17 +0000489 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000490 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
491 Res->Mem.SegReg = 0;
492 Res->Mem.Disp = Disp;
493 Res->Mem.BaseReg = 0;
494 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000495 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000496 Res->Mem.Size = Size;
Chad Rosier7109fbe2013-01-10 23:39:07 +0000497 Res->AddressOf = false;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000498 return Res;
499 }
500
501 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000502 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
503 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000504 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosier023c8802013-03-19 17:32:17 +0000505 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000506 // We should never just have a displacement, that should be parsed as an
507 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000508 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
509
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000510 // The scale should always be one of {1,2,4,8}.
511 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000512 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000513 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000514 Res->Mem.SegReg = SegReg;
515 Res->Mem.Disp = Disp;
516 Res->Mem.BaseReg = BaseReg;
517 Res->Mem.IndexReg = IndexReg;
518 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000519 Res->Mem.Size = Size;
NAKAMURA Takumib789b942013-01-11 01:13:54 +0000520 Res->AddressOf = false;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000521 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000522 }
523};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000524
Chris Lattner37dfdec2009-07-29 06:33:53 +0000525} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000526
Devang Pateldd929fc2012-01-12 18:03:40 +0000527bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000528 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000529
530 return (Op.isMem() &&
531 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
532 isa<MCConstantExpr>(Op.Mem.Disp) &&
533 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
534 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
535}
536
Devang Pateldd929fc2012-01-12 18:03:40 +0000537bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000538 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000539
Chad Rosier36b8fed2012-06-27 22:34:28 +0000540 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000541 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000542 isa<MCConstantExpr>(Op.Mem.Disp) &&
543 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
544 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
545}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000546
Devang Pateldd929fc2012-01-12 18:03:40 +0000547bool X86AsmParser::ParseRegister(unsigned &RegNo,
548 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000549 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000550 const AsmToken &PercentTok = Parser.getTok();
551 StartLoc = PercentTok.getLoc();
552
553 // If we encounter a %, ignore it. This code handles registers with and
554 // without the prefix, unprefixed registers can occur in cfi directives.
555 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000556 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000557
Sean Callanan18b83232010-01-19 21:44:56 +0000558 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000559 EndLoc = Tok.getEndLoc();
560
Devang Patel1aea4302012-01-20 22:32:05 +0000561 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000562 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000563 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000564 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000565 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000566
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000567 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000568
Chris Lattner33d60d52010-09-22 04:11:10 +0000569 // If the match failed, try the register name as lowercase.
570 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000571 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000572
Evan Cheng5de728c2011-07-27 23:22:03 +0000573 if (!is64BitMode()) {
574 // FIXME: This should be done using Requires<In32BitMode> and
575 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
576 // checked.
577 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
578 // REX prefix.
579 if (RegNo == X86::RIZ ||
580 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
581 X86II::isX86_64NonExtLowByteReg(RegNo) ||
582 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000583 return Error(StartLoc, "register %"
584 + Tok.getString() + " is only available in 64-bit mode",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000585 SMRange(StartLoc, EndLoc));
Evan Cheng5de728c2011-07-27 23:22:03 +0000586 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000587
Chris Lattner33d60d52010-09-22 04:11:10 +0000588 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
589 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000590 RegNo = X86::ST0;
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000591 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000592
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000593 // Check to see if we have '(4)' after %st.
594 if (getLexer().isNot(AsmToken::LParen))
595 return false;
596 // Lex the paren.
597 getParser().Lex();
598
599 const AsmToken &IntTok = Parser.getTok();
600 if (IntTok.isNot(AsmToken::Integer))
601 return Error(IntTok.getLoc(), "expected stack index");
602 switch (IntTok.getIntVal()) {
603 case 0: RegNo = X86::ST0; break;
604 case 1: RegNo = X86::ST1; break;
605 case 2: RegNo = X86::ST2; break;
606 case 3: RegNo = X86::ST3; break;
607 case 4: RegNo = X86::ST4; break;
608 case 5: RegNo = X86::ST5; break;
609 case 6: RegNo = X86::ST6; break;
610 case 7: RegNo = X86::ST7; break;
611 default: return Error(IntTok.getLoc(), "invalid stack index");
612 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000613
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000614 if (getParser().Lex().isNot(AsmToken::RParen))
615 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000616
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000617 EndLoc = Parser.getTok().getEndLoc();
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000618 Parser.Lex(); // Eat ')'
619 return false;
620 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000621
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000622 EndLoc = Parser.getTok().getEndLoc();
623
Chris Lattner645b2092010-06-24 07:29:18 +0000624 // If this is "db[0-7]", match it as an alias
625 // for dr[0-7].
626 if (RegNo == 0 && Tok.getString().size() == 3 &&
627 Tok.getString().startswith("db")) {
628 switch (Tok.getString()[2]) {
629 case '0': RegNo = X86::DR0; break;
630 case '1': RegNo = X86::DR1; break;
631 case '2': RegNo = X86::DR2; break;
632 case '3': RegNo = X86::DR3; break;
633 case '4': RegNo = X86::DR4; break;
634 case '5': RegNo = X86::DR5; break;
635 case '6': RegNo = X86::DR6; break;
636 case '7': RegNo = X86::DR7; break;
637 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000638
Chris Lattner645b2092010-06-24 07:29:18 +0000639 if (RegNo != 0) {
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000640 EndLoc = Parser.getTok().getEndLoc();
Chris Lattner645b2092010-06-24 07:29:18 +0000641 Parser.Lex(); // Eat it.
642 return false;
643 }
644 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000645
Devang Patel1aea4302012-01-20 22:32:05 +0000646 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000647 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000648 return Error(StartLoc, "invalid register name",
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000649 SMRange(StartLoc, EndLoc));
Devang Patel1aea4302012-01-20 22:32:05 +0000650 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000651
Sean Callananb9a25b72010-01-19 20:27:46 +0000652 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000653 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000654}
655
Devang Pateldd929fc2012-01-12 18:03:40 +0000656X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000657 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000658 return ParseIntelOperand();
659 return ParseATTOperand();
660}
661
Devang Pateld37ad242012-01-17 18:00:18 +0000662/// getIntelMemOperandSize - Return intel memory operand size.
663static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000664 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000665 .Cases("BYTE", "byte", 8)
666 .Cases("WORD", "word", 16)
667 .Cases("DWORD", "dword", 32)
668 .Cases("QWORD", "qword", 64)
669 .Cases("XWORD", "xword", 80)
670 .Cases("XMMWORD", "xmmword", 128)
671 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000672 .Default(0);
673 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000674}
675
Chad Rosierdd2e8952013-01-14 22:31:35 +0000676enum IntelBracExprState {
677 IBES_START,
678 IBES_LBRAC,
679 IBES_RBRAC,
680 IBES_REGISTER,
681 IBES_REGISTER_STAR,
682 IBES_REGISTER_STAR_INTEGER,
683 IBES_INTEGER,
684 IBES_INTEGER_STAR,
685 IBES_INDEX_REGISTER,
686 IBES_IDENTIFIER,
687 IBES_DISP_EXPR,
688 IBES_MINUS,
689 IBES_ERROR
690};
691
692class IntelBracExprStateMachine {
693 IntelBracExprState State;
694 unsigned BaseReg, IndexReg, Scale;
695 int64_t Disp;
696
697 unsigned TmpReg;
698 int64_t TmpInteger;
699
700 bool isPlus;
701
702public:
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000703 IntelBracExprStateMachine(MCAsmParser &parser, int64_t disp) :
704 State(IBES_START), BaseReg(0), IndexReg(0), Scale(1), Disp(disp),
Chad Rosierdd2e8952013-01-14 22:31:35 +0000705 TmpReg(0), TmpInteger(0), isPlus(true) {}
706
707 unsigned getBaseReg() { return BaseReg; }
708 unsigned getIndexReg() { return IndexReg; }
709 unsigned getScale() { return Scale; }
710 int64_t getDisp() { return Disp; }
711 bool isValidEndState() { return State == IBES_RBRAC; }
712
713 void onPlus() {
714 switch (State) {
715 default:
716 State = IBES_ERROR;
717 break;
718 case IBES_INTEGER:
719 State = IBES_START;
720 if (isPlus)
721 Disp += TmpInteger;
722 else
723 Disp -= TmpInteger;
724 break;
725 case IBES_REGISTER:
726 State = IBES_START;
727 // If we already have a BaseReg, then assume this is the IndexReg with a
728 // scale of 1.
729 if (!BaseReg) {
730 BaseReg = TmpReg;
731 } else {
732 assert (!IndexReg && "BaseReg/IndexReg already set!");
733 IndexReg = TmpReg;
734 Scale = 1;
735 }
736 break;
737 case IBES_INDEX_REGISTER:
738 State = IBES_START;
739 break;
740 }
741 isPlus = true;
742 }
743 void onMinus() {
744 switch (State) {
745 default:
746 State = IBES_ERROR;
747 break;
748 case IBES_START:
749 State = IBES_MINUS;
750 break;
751 case IBES_INTEGER:
752 State = IBES_START;
753 if (isPlus)
754 Disp += TmpInteger;
755 else
756 Disp -= TmpInteger;
757 break;
758 case IBES_REGISTER:
759 State = IBES_START;
760 // If we already have a BaseReg, then assume this is the IndexReg with a
761 // scale of 1.
762 if (!BaseReg) {
763 BaseReg = TmpReg;
764 } else {
765 assert (!IndexReg && "BaseReg/IndexReg already set!");
766 IndexReg = TmpReg;
767 Scale = 1;
768 }
769 break;
770 case IBES_INDEX_REGISTER:
771 State = IBES_START;
772 break;
773 }
774 isPlus = false;
775 }
776 void onRegister(unsigned Reg) {
777 switch (State) {
778 default:
779 State = IBES_ERROR;
780 break;
781 case IBES_START:
782 State = IBES_REGISTER;
783 TmpReg = Reg;
784 break;
785 case IBES_INTEGER_STAR:
786 assert (!IndexReg && "IndexReg already set!");
787 State = IBES_INDEX_REGISTER;
788 IndexReg = Reg;
789 Scale = TmpInteger;
790 break;
791 }
792 }
793 void onDispExpr() {
794 switch (State) {
795 default:
796 State = IBES_ERROR;
797 break;
798 case IBES_START:
799 State = IBES_DISP_EXPR;
800 break;
801 }
802 }
803 void onInteger(int64_t TmpInt) {
804 switch (State) {
805 default:
806 State = IBES_ERROR;
807 break;
808 case IBES_START:
809 State = IBES_INTEGER;
810 TmpInteger = TmpInt;
811 break;
812 case IBES_MINUS:
813 State = IBES_INTEGER;
814 TmpInteger = TmpInt;
815 break;
816 case IBES_REGISTER_STAR:
817 assert (!IndexReg && "IndexReg already set!");
818 State = IBES_INDEX_REGISTER;
819 IndexReg = TmpReg;
820 Scale = TmpInt;
821 break;
822 }
823 }
824 void onStar() {
825 switch (State) {
826 default:
827 State = IBES_ERROR;
828 break;
829 case IBES_INTEGER:
830 State = IBES_INTEGER_STAR;
831 break;
832 case IBES_REGISTER:
833 State = IBES_REGISTER_STAR;
834 break;
835 }
836 }
837 void onLBrac() {
838 switch (State) {
839 default:
840 State = IBES_ERROR;
841 break;
842 case IBES_RBRAC:
843 State = IBES_START;
844 isPlus = true;
845 break;
846 }
847 }
848 void onRBrac() {
849 switch (State) {
850 default:
851 State = IBES_ERROR;
852 break;
853 case IBES_DISP_EXPR:
854 State = IBES_RBRAC;
855 break;
856 case IBES_INTEGER:
857 State = IBES_RBRAC;
858 if (isPlus)
859 Disp += TmpInteger;
860 else
861 Disp -= TmpInteger;
862 break;
863 case IBES_REGISTER:
864 State = IBES_RBRAC;
865 // If we already have a BaseReg, then assume this is the IndexReg with a
866 // scale of 1.
867 if (!BaseReg) {
868 BaseReg = TmpReg;
869 } else {
870 assert (!IndexReg && "BaseReg/IndexReg already set!");
871 IndexReg = TmpReg;
872 Scale = 1;
873 }
874 break;
875 case IBES_INDEX_REGISTER:
876 State = IBES_RBRAC;
877 break;
878 }
879 }
880};
881
Chad Rosierd3e74162013-03-19 21:11:56 +0000882X86Operand *X86AsmParser::CreateMemForInlineAsm(const MCExpr *Disp, SMLoc Start,
883 SMLoc End, SMLoc SizeDirLoc,
884 unsigned Size) {
885 bool NeedSizeDir = false;
886 bool IsVarDecl = false;
887 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
888 const MCSymbol &Sym = SymRef->getSymbol();
889 // FIXME: The SemaLookup will fail if the name is anything other then an
890 // identifier.
891 // FIXME: Pass a valid SMLoc.
892 unsigned tLength, tSize, tType;
893 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, tLength,
894 tSize, tType, IsVarDecl);
895 if (!Size) {
896 Size = tType * 8; // Size is in terms of bits in this context.
897 NeedSizeDir = Size > 0;
898 }
899 }
900
901 // If this is not a VarDecl then assume it is a FuncDecl or some other label
902 // reference. We need an 'r' constraint here, so we need to create register
903 // operand to ensure proper matching. Just pick a GPR based on the size of
904 // a pointer.
905 if (!IsVarDecl) {
906 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
907 return X86Operand::CreateReg(RegNo, Start, End, /*AddressOf=*/true);
908 }
909
910 if (NeedSizeDir)
911 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_SizeDirective, SizeDirLoc,
912 /*Len*/0, Size));
913
914 // When parsing inline assembly we set the base register to a non-zero value
915 // as we don't know the actual value at this time. This is necessary to
916 // get the matching correct in some cases.
917 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
918 /*Scale*/1, Start, End, Size);
919}
920
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000921X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
922 uint64_t ImmDisp,
Devang Patel7c64fe62012-01-23 18:31:58 +0000923 unsigned Size) {
Chad Rosier4284e172012-10-24 22:13:37 +0000924 const AsmToken &Tok = Parser.getTok();
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000925 SMLoc Start = Tok.getLoc(), End = Tok.getEndLoc();
Devang Patel0a338862012-01-12 01:36:43 +0000926
Devang Pateld37ad242012-01-17 18:00:18 +0000927 // Eat '['
928 if (getLexer().isNot(AsmToken::LBrac))
929 return ErrorOperand(Start, "Expected '[' token!");
930 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000931
Chad Rosierdd2e8952013-01-14 22:31:35 +0000932 unsigned TmpReg = 0;
933
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000934 // Try to handle '[' 'Symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000935 if (getLexer().is(AsmToken::Identifier)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +0000936 if (ParseRegister(TmpReg, Start, End)) {
937 const MCExpr *Disp;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000938 if (getParser().parseExpression(Disp, End))
Chad Rosierdd2e8952013-01-14 22:31:35 +0000939 return 0;
940
Devang Pateld37ad242012-01-17 18:00:18 +0000941 if (getLexer().isNot(AsmToken::RBrac))
Jordan Rose3ebe59c2013-01-07 19:00:49 +0000942 return ErrorOperand(Parser.getTok().getLoc(), "Expected ']' token!");
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000943
944 // FIXME: We don't handle 'ImmDisp' '[' 'Symbol' ']'.
945 if (ImmDisp)
946 return ErrorOperand(Start, "Unsupported immediate displacement!");
947
Chad Rosier4fb25b72013-02-15 21:58:13 +0000948 // Adjust the EndLoc due to the ']'.
949 End = SMLoc::getFromPointer(Parser.getTok().getEndLoc().getPointer()-1);
Devang Pateld37ad242012-01-17 18:00:18 +0000950 Parser.Lex();
Chad Rosierd3e74162013-03-19 21:11:56 +0000951 if (!isParsingInlineAsm())
952 return X86Operand::CreateMem(Disp, Start, End, Size);
953
954 // We want the size directive before the '['.
955 SMLoc SizeDirLoc = SMLoc::getFromPointer(Start.getPointer()-1);
956 return CreateMemForInlineAsm(Disp, Start, End, SizeDirLoc, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000957 }
Devang Pateld37ad242012-01-17 18:00:18 +0000958 }
959
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000960 // Parse [ BaseReg + Scale*IndexReg + Disp ]. We may have already parsed an
961 // immediate displacement before the bracketed expression.
Chad Rosierdd2e8952013-01-14 22:31:35 +0000962 bool Done = false;
Chad Rosierdd40e8c2013-03-27 21:49:56 +0000963 IntelBracExprStateMachine SM(Parser, ImmDisp);
Chad Rosier2fbc2392012-10-29 18:01:54 +0000964
Chad Rosierdd2e8952013-01-14 22:31:35 +0000965 // If we parsed a register, then the end loc has already been set and
966 // the identifier has already been lexed. We also need to update the
967 // state.
968 if (TmpReg)
969 SM.onRegister(TmpReg);
970
971 const MCExpr *Disp = 0;
972 while (!Done) {
973 bool UpdateLocLex = true;
974
975 // The period in the dot operator (e.g., [ebx].foo.bar) is parsed as an
976 // identifier. Don't try an parse it as a register.
977 if (Tok.getString().startswith("."))
978 break;
979
980 switch (getLexer().getKind()) {
981 default: {
982 if (SM.isValidEndState()) {
983 Done = true;
984 break;
985 }
986 return ErrorOperand(Tok.getLoc(), "Unexpected token!");
987 }
988 case AsmToken::Identifier: {
989 // This could be a register or a displacement expression.
990 if(!ParseRegister(TmpReg, Start, End)) {
991 SM.onRegister(TmpReg);
992 UpdateLocLex = false;
993 break;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +0000994 } else if (!getParser().parseExpression(Disp, End)) {
Chad Rosierdd2e8952013-01-14 22:31:35 +0000995 SM.onDispExpr();
996 UpdateLocLex = false;
997 break;
998 }
999 return ErrorOperand(Tok.getLoc(), "Unexpected identifier!");
1000 }
1001 case AsmToken::Integer: {
Chad Rosier4284e172012-10-24 22:13:37 +00001002 int64_t Val = Tok.getIntVal();
Chad Rosierdd2e8952013-01-14 22:31:35 +00001003 SM.onInteger(Val);
1004 break;
1005 }
1006 case AsmToken::Plus: SM.onPlus(); break;
1007 case AsmToken::Minus: SM.onMinus(); break;
1008 case AsmToken::Star: SM.onStar(); break;
1009 case AsmToken::LBrac: SM.onLBrac(); break;
1010 case AsmToken::RBrac: SM.onRBrac(); break;
1011 }
1012 if (!Done && UpdateLocLex) {
1013 End = Tok.getLoc();
1014 Parser.Lex(); // Consume the token.
Devang Patelf2d21372012-01-23 22:35:25 +00001015 }
Devang Pateld37ad242012-01-17 18:00:18 +00001016 }
1017
Chad Rosierdd2e8952013-01-14 22:31:35 +00001018 if (!Disp)
1019 Disp = MCConstantExpr::Create(SM.getDisp(), getContext());
Devang Patelfdd3b302012-01-20 21:21:01 +00001020
Chad Rosierddb53ef2012-10-26 22:01:25 +00001021 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001022 if (Tok.getString().startswith(".")) {
1023 SmallString<64> Err;
1024 const MCExpr *NewDisp;
1025 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
1026 return ErrorOperand(Tok.getLoc(), Err);
1027
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001028 End = Parser.getTok().getEndLoc();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001029 Parser.Lex(); // Eat the field.
1030 Disp = NewDisp;
1031 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001032
Chad Rosierdd2e8952013-01-14 22:31:35 +00001033 int BaseReg = SM.getBaseReg();
1034 int IndexReg = SM.getIndexReg();
Devang Patelfdd3b302012-01-20 21:21:01 +00001035
Chad Rosierdd2e8952013-01-14 22:31:35 +00001036 // handle [-42]
1037 if (!BaseReg && !IndexReg) {
1038 if (!SegReg)
1039 return X86Operand::CreateMem(Disp, Start, End);
1040 else
1041 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, Start, End, Size);
1042 }
1043
1044 int Scale = SM.getScale();
Devang Pateld37ad242012-01-17 18:00:18 +00001045 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +00001046 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +00001047}
1048
1049/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001050X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg,
1051 uint64_t ImmDisp,
1052 SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +00001053 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001054 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +00001055
1056 unsigned Size = getIntelMemOperandSize(Tok.getString());
1057 if (Size) {
1058 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +00001059 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
1060 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +00001061 Parser.Lex();
1062 }
1063
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001064 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1065 if (getLexer().is(AsmToken::Integer)) {
1066 const AsmToken &IntTok = Parser.getTok();
1067 if (isParsingInlineAsm())
1068 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix,
1069 IntTok.getLoc()));
1070 uint64_t ImmDisp = IntTok.getIntVal();
1071 Parser.Lex(); // Eat the integer.
1072 if (getLexer().isNot(AsmToken::LBrac))
1073 return ErrorOperand(Start, "Expected '[' token!");
1074 return ParseIntelBracExpression(SegReg, ImmDisp, Size);
1075 }
1076
Chad Rosierc0a14b82012-10-24 17:22:29 +00001077 if (getLexer().is(AsmToken::LBrac))
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001078 return ParseIntelBracExpression(SegReg, ImmDisp, Size);
Devang Patel7c64fe62012-01-23 18:31:58 +00001079
1080 if (!ParseRegister(SegReg, Start, End)) {
1081 // Handel SegReg : [ ... ]
1082 if (getLexer().isNot(AsmToken::Colon))
1083 return ErrorOperand(Start, "Expected ':' token!");
1084 Parser.Lex(); // Eat :
1085 if (getLexer().isNot(AsmToken::LBrac))
1086 return ErrorOperand(Start, "Expected '[' token!");
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001087 return ParseIntelBracExpression(SegReg, ImmDisp, Size);
Devang Patel7c64fe62012-01-23 18:31:58 +00001088 }
Devang Pateld37ad242012-01-17 18:00:18 +00001089
1090 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001091 if (getParser().parseExpression(Disp, End))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001092 return 0;
Chad Rosier96d58e62012-10-19 20:57:14 +00001093
Chad Rosier2a784132012-10-23 23:31:33 +00001094 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +00001095 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosierd3e74162013-03-19 21:11:56 +00001096 return CreateMemForInlineAsm(Disp, Start, End, Start, Size);
Chad Rosierc0a14b82012-10-24 17:22:29 +00001097}
1098
Chad Rosier22f441a2012-10-24 22:21:50 +00001099/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001100bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
1101 const MCExpr **NewDisp,
1102 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +00001103 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001104 uint64_t OrigDispVal, DotDispVal;
1105
1106 // FIXME: Handle non-constant expressions.
1107 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
1108 OrigDispVal = OrigDisp->getValue();
1109 } else {
1110 Err = "Non-constant offsets are not supported!";
1111 return true;
1112 }
Chad Rosier22f441a2012-10-24 22:21:50 +00001113
1114 // Drop the '.'.
1115 StringRef DotDispStr = Tok.getString().drop_front(1);
1116
Chad Rosier22f441a2012-10-24 22:21:50 +00001117 // .Imm gets lexed as a real.
1118 if (Tok.is(AsmToken::Real)) {
1119 APInt DotDisp;
1120 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001121 DotDispVal = DotDisp.getZExtValue();
Chad Rosierec130222012-10-25 21:51:10 +00001122 } else if (Tok.is(AsmToken::Identifier)) {
1123 // We should only see an identifier when parsing the original inline asm.
1124 // The front-end should rewrite this in terms of immediates.
1125 assert (isParsingInlineAsm() && "Unexpected field name!");
1126
1127 unsigned DotDisp;
1128 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
1129 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
1130 DotDisp)) {
1131 Err = "Unable to lookup field reference!";
1132 return true;
1133 }
1134 DotDispVal = DotDisp;
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001135 } else {
1136 Err = "Unexpected token type!";
1137 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +00001138 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001139
Chad Rosierec130222012-10-25 21:51:10 +00001140 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
1141 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
1142 unsigned Len = DotDispStr.size();
1143 unsigned Val = OrigDispVal + DotDispVal;
1144 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
1145 Val));
Chad Rosier5e6b37f2012-10-25 17:37:43 +00001146 }
1147
1148 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
1149 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +00001150}
1151
Chad Rosierc0a14b82012-10-24 17:22:29 +00001152/// Parse the 'offset' operator. This operator is used to specify the
1153/// location rather then the content of a variable.
1154X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
1155 SMLoc OffsetOfLoc = Start;
1156 Parser.Lex(); // Eat offset.
1157 Start = Parser.getTok().getLoc();
1158 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1159
Chad Rosier6e431572012-10-26 16:09:20 +00001160 SMLoc End;
Chad Rosierc0a14b82012-10-24 17:22:29 +00001161 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001162 if (getParser().parseExpression(Val, End))
Chad Rosier7ab21c72012-10-26 18:32:44 +00001163 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosierc0a14b82012-10-24 17:22:29 +00001164
Chad Rosier6e431572012-10-26 16:09:20 +00001165 // Don't emit the offset operator.
1166 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
1167
Chad Rosierc0a14b82012-10-24 17:22:29 +00001168 // The offset operator will have an 'r' constraint, thus we need to create
1169 // register operand to ensure proper matching. Just pick a GPR based on
1170 // the size of a pointer.
1171 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
Chad Rosierc1ec2072013-01-10 22:10:27 +00001172 return X86Operand::CreateReg(RegNo, Start, End, /*GetAddress=*/true,
1173 OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +00001174}
1175
Chad Rosier505bca32013-01-17 19:21:48 +00001176enum IntelOperatorKind {
1177 IOK_LENGTH,
1178 IOK_SIZE,
1179 IOK_TYPE
1180};
1181
1182/// Parse the 'LENGTH', 'TYPE' and 'SIZE' operators. The LENGTH operator
1183/// returns the number of elements in an array. It returns the value 1 for
1184/// non-array variables. The SIZE operator returns the size of a C or C++
1185/// variable. A variable's size is the product of its LENGTH and TYPE. The
1186/// TYPE operator returns the size of a C or C++ type or variable. If the
1187/// variable is an array, TYPE returns the size of a single element.
1188X86Operand *X86AsmParser::ParseIntelOperator(SMLoc Start, unsigned OpKind) {
Chad Rosierefcb3d92012-10-26 18:04:20 +00001189 SMLoc TypeLoc = Start;
1190 Parser.Lex(); // Eat offset.
1191 Start = Parser.getTok().getLoc();
1192 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
1193
1194 SMLoc End;
1195 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001196 if (getParser().parseExpression(Val, End))
Chad Rosierefcb3d92012-10-26 18:04:20 +00001197 return 0;
1198
Chad Rosier505bca32013-01-17 19:21:48 +00001199 unsigned Length = 0, Size = 0, Type = 0;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001200 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
1201 const MCSymbol &Sym = SymRef->getSymbol();
1202 // FIXME: The SemaLookup will fail if the name is anything other then an
1203 // identifier.
1204 // FIXME: Pass a valid SMLoc.
Chad Rosierc1ec2072013-01-10 22:10:27 +00001205 bool IsVarDecl;
Chad Rosier505bca32013-01-17 19:21:48 +00001206 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Length,
1207 Size, Type, IsVarDecl))
Chad Rosier3da67ca2013-01-18 00:50:59 +00001208 return ErrorOperand(Start, "Unable to lookup expr!");
Chad Rosier505bca32013-01-17 19:21:48 +00001209 }
1210 unsigned CVal;
1211 switch(OpKind) {
1212 default: llvm_unreachable("Unexpected operand kind!");
1213 case IOK_LENGTH: CVal = Length; break;
1214 case IOK_SIZE: CVal = Size; break;
1215 case IOK_TYPE: CVal = Type; break;
Chad Rosierefcb3d92012-10-26 18:04:20 +00001216 }
1217
1218 // Rewrite the type operator and the C or C++ type or variable in terms of an
1219 // immediate. E.g. TYPE foo -> $$4
1220 unsigned Len = End.getPointer() - TypeLoc.getPointer();
Chad Rosier505bca32013-01-17 19:21:48 +00001221 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, CVal));
Chad Rosierefcb3d92012-10-26 18:04:20 +00001222
Chad Rosier505bca32013-01-17 19:21:48 +00001223 const MCExpr *Imm = MCConstantExpr::Create(CVal, getContext());
Chad Rosier811ddf62013-03-19 21:58:18 +00001224 return X86Operand::CreateImm(Imm, Start, End);
Chad Rosierefcb3d92012-10-26 18:04:20 +00001225}
1226
Devang Pateld37ad242012-01-17 18:00:18 +00001227X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +00001228 SMLoc Start = Parser.getTok().getLoc(), End;
Chad Rosier7ab21c72012-10-26 18:32:44 +00001229 StringRef AsmTokStr = Parser.getTok().getString();
Chad Rosierc0a14b82012-10-24 17:22:29 +00001230
Chad Rosier505bca32013-01-17 19:21:48 +00001231 // Offset, length, type and size operators.
1232 if (isParsingInlineAsm()) {
1233 if (AsmTokStr == "offset" || AsmTokStr == "OFFSET")
1234 return ParseIntelOffsetOfOperator(Start);
1235 if (AsmTokStr == "length" || AsmTokStr == "LENGTH")
1236 return ParseIntelOperator(Start, IOK_LENGTH);
1237 if (AsmTokStr == "size" || AsmTokStr == "SIZE")
1238 return ParseIntelOperator(Start, IOK_SIZE);
1239 if (AsmTokStr == "type" || AsmTokStr == "TYPE")
1240 return ParseIntelOperator(Start, IOK_TYPE);
1241 }
Chad Rosierefcb3d92012-10-26 18:04:20 +00001242
Chad Rosier505bca32013-01-17 19:21:48 +00001243 // Immediate.
Devang Pateld37ad242012-01-17 18:00:18 +00001244 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
1245 getLexer().is(AsmToken::Minus)) {
1246 const MCExpr *Val;
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001247 bool isInteger = getLexer().is(AsmToken::Integer);
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001248 if (!getParser().parseExpression(Val, End)) {
Chad Rosier811ddf62013-03-19 21:58:18 +00001249 if (isParsingInlineAsm())
1250 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_ImmPrefix, Start));
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001251 // Immediate.
1252 if (getLexer().isNot(AsmToken::LBrac))
1253 return X86Operand::CreateImm(Val, Start, End);
1254
1255 // Only positive immediates are valid.
1256 if (!isInteger) {
1257 Error(Parser.getTok().getLoc(), "expected a positive immediate "
1258 "displacement before bracketed expr.");
1259 return 0;
1260 }
1261
1262 // Parse ImmDisp [ BaseReg + Scale*IndexReg + Disp ].
1263 if (uint64_t ImmDisp = dyn_cast<MCConstantExpr>(Val)->getValue())
1264 return ParseIntelMemOperand(/*SegReg=*/0, ImmDisp, Start);
Devang Pateld37ad242012-01-17 18:00:18 +00001265 }
1266 }
1267
Chad Rosier505bca32013-01-17 19:21:48 +00001268 // Register.
Devang Patel1aea4302012-01-20 22:32:05 +00001269 unsigned RegNo = 0;
1270 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001271 // If this is a segment register followed by a ':', then this is the start
1272 // of a memory reference, otherwise this is a normal register reference.
1273 if (getLexer().isNot(AsmToken::Colon))
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001274 return X86Operand::CreateReg(RegNo, Start, End);
Chad Rosier5b0f1b32012-10-04 23:59:38 +00001275
1276 getParser().Lex(); // Eat the colon.
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001277 return ParseIntelMemOperand(/*SegReg=*/RegNo, /*Disp=*/0, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001278 }
1279
Chad Rosier505bca32013-01-17 19:21:48 +00001280 // Memory operand.
Chad Rosierdd40e8c2013-03-27 21:49:56 +00001281 return ParseIntelMemOperand(/*SegReg=*/0, /*Disp=*/0, Start);
Devang Patel0a338862012-01-12 01:36:43 +00001282}
1283
Devang Pateldd929fc2012-01-12 18:03:40 +00001284X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001285 switch (getLexer().getKind()) {
1286 default:
Chris Lattnereef6d782010-04-17 18:56:34 +00001287 // Parse a memory operand with no segment register.
1288 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +00001289 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +00001290 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +00001291 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +00001292 SMLoc Start, End;
1293 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001294 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001295 Error(Start, "%eiz and %riz can only be used as index registers",
1296 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001297 return 0;
1298 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001299
Chris Lattnereef6d782010-04-17 18:56:34 +00001300 // If this is a segment register followed by a ':', then this is the start
1301 // of a memory reference, otherwise this is a normal register reference.
1302 if (getLexer().isNot(AsmToken::Colon))
1303 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001304
Chris Lattnereef6d782010-04-17 18:56:34 +00001305 getParser().Lex(); // Eat the colon.
1306 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +00001307 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001308 case AsmToken::Dollar: {
1309 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +00001310 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +00001311 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001312 const MCExpr *Val;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001313 if (getParser().parseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +00001314 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001315 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001316 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001317 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +00001318}
1319
Chris Lattnereef6d782010-04-17 18:56:34 +00001320/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1321/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +00001322X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001323
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001324 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1325 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +00001326 // only way to do this without lookahead is to eat the '(' and see what is
1327 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001328 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001329 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +00001330 SMLoc ExprEnd;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001331 if (getParser().parseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001332
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001333 // After parsing the base expression we could either have a parenthesized
1334 // memory address or not. If not, return now. If so, eat the (.
1335 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001336 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001337 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001338 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001339 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001340 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001341
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001342 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001343 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001344 } else {
1345 // Okay, we have a '('. We don't know if this is an expression or not, but
1346 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +00001347 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001348 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001349
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001350 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001351 // Nothing to do here, fall into the code below with the '(' part of the
1352 // memory operand consumed.
1353 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001354 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001355
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001356 // It must be an parenthesized expression, parse it now.
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001357 if (getParser().parseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +00001358 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001359
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001360 // After parsing the base expression we could either have a parenthesized
1361 // memory address or not. If not, return now. If so, eat the (.
1362 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001363 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001364 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001365 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001366 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001367 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001368
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001369 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001370 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001371 }
1372 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001373
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001374 // If we reached here, then we just ate the ( of the memory operand. Process
1375 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001376 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001377 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001378
Chris Lattner29ef9a22010-01-15 18:51:29 +00001379 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001380 SMLoc StartLoc, EndLoc;
1381 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001382 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001383 Error(StartLoc, "eiz and riz can only be used as index registers",
1384 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001385 return 0;
1386 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001387 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001388
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001389 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001390 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001391 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001392
1393 // Following the comma we should have either an index register, or a scale
1394 // value. We don't support the later form, but we want to parse it
1395 // correctly.
1396 //
1397 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001398 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001399 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001400 SMLoc L;
1401 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001402
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001403 if (getLexer().isNot(AsmToken::RParen)) {
1404 // Parse the scale amount:
1405 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001406 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001407 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001408 "expected comma in scale expression");
1409 return 0;
1410 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001411 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001412
1413 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001414 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001415
1416 int64_t ScaleVal;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001417 if (getParser().parseAbsoluteExpression(ScaleVal)){
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001418 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001419 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001420 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001421
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001422 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001423 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1424 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1425 return 0;
1426 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001427 Scale = (unsigned)ScaleVal;
1428 }
1429 }
1430 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001431 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001432 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001433 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001434
1435 int64_t Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001436 if (getParser().parseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001437 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001438
Daniel Dunbaree910252010-08-24 19:13:38 +00001439 if (Value != 1)
1440 Warning(Loc, "scale factor without index register is ignored");
1441 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001442 }
1443 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001444
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001445 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001446 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001447 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001448 return 0;
1449 }
Jordan Rose3ebe59c2013-01-07 19:00:49 +00001450 SMLoc MemEnd = Parser.getTok().getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001451 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001452
Kevin Enderby84faf652012-03-12 21:32:09 +00001453 // If we have both a base register and an index register make sure they are
1454 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001455 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001456 if (BaseReg != 0 && IndexReg != 0) {
1457 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001458 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1459 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001460 IndexReg != X86::RIZ) {
1461 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1462 return 0;
1463 }
1464 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001465 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1466 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001467 IndexReg != X86::EIZ){
1468 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1469 return 0;
1470 }
1471 }
1472
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001473 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1474 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001475}
1476
Devang Pateldd929fc2012-01-12 18:03:40 +00001477bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001478ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001479 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001480 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001481 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001482
Chris Lattnerd8f71792010-11-28 20:23:50 +00001483 // FIXME: Hack to recognize setneb as setne.
1484 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1485 PatchedName != "setb" && PatchedName != "setnb")
1486 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001487
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001488 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1489 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001490 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001491 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1492 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001493 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001494 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001495 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001496 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001497 .Case("eq", 0x00)
1498 .Case("lt", 0x01)
1499 .Case("le", 0x02)
1500 .Case("unord", 0x03)
1501 .Case("neq", 0x04)
1502 .Case("nlt", 0x05)
1503 .Case("nle", 0x06)
1504 .Case("ord", 0x07)
1505 /* AVX only from here */
1506 .Case("eq_uq", 0x08)
1507 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001508 .Case("ngt", 0x0A)
1509 .Case("false", 0x0B)
1510 .Case("neq_oq", 0x0C)
1511 .Case("ge", 0x0D)
1512 .Case("gt", 0x0E)
1513 .Case("true", 0x0F)
1514 .Case("eq_os", 0x10)
1515 .Case("lt_oq", 0x11)
1516 .Case("le_oq", 0x12)
1517 .Case("unord_s", 0x13)
1518 .Case("neq_us", 0x14)
1519 .Case("nlt_uq", 0x15)
1520 .Case("nle_uq", 0x16)
1521 .Case("ord_s", 0x17)
1522 .Case("eq_us", 0x18)
1523 .Case("nge_uq", 0x19)
1524 .Case("ngt_uq", 0x1A)
1525 .Case("false_os", 0x1B)
1526 .Case("neq_os", 0x1C)
1527 .Case("ge_oq", 0x1D)
1528 .Case("gt_oq", 0x1E)
1529 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001530 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001531 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001532 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1533 getParser().getContext());
1534 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001535 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001536 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001537 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001538 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001539 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001540 } else {
1541 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001542 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001543 }
1544 }
1545 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001546
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001547 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001548
Devang Patel885f65b2012-01-30 22:47:12 +00001549 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001550 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001551
Chris Lattner2544f422010-09-08 05:17:37 +00001552 // Determine whether this is an instruction prefix.
1553 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001554 Name == "lock" || Name == "rep" ||
1555 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001556 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001557 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001558
1559
Chris Lattner2544f422010-09-08 05:17:37 +00001560 // This does the actual operand parsing. Don't parse any more if we have a
1561 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1562 // just want to parse the "lock" as the first instruction and the "incl" as
1563 // the next one.
1564 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001565
1566 // Parse '*' modifier.
1567 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001568 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001569 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001570 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001571 }
1572
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001573 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001574 if (X86Operand *Op = ParseOperand())
1575 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001576 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001577 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001578 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001579 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001580
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001581 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001582 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001583
1584 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001585 if (X86Operand *Op = ParseOperand())
1586 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001587 else {
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001588 Parser.eatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001589 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001590 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001591 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001592
Chris Lattnercbf8a982010-09-11 16:18:25 +00001593 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001594 SMLoc Loc = getLexer().getLoc();
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00001595 Parser.eatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001596 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001597 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001598 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001599
Chris Lattner2544f422010-09-08 05:17:37 +00001600 if (getLexer().is(AsmToken::EndOfStatement))
1601 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001602 else if (isPrefix && getLexer().is(AsmToken::Slash))
1603 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001604
Devang Patel885f65b2012-01-30 22:47:12 +00001605 if (ExtraImmOp && isParsingIntelSyntax())
1606 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1607
Chris Lattner98c870f2010-11-06 19:25:43 +00001608 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1609 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1610 // documented form in various unofficial manuals, so a lot of code uses it.
1611 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1612 Operands.size() == 3) {
1613 X86Operand &Op = *(X86Operand*)Operands.back();
1614 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1615 isa<MCConstantExpr>(Op.Mem.Disp) &&
1616 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1617 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1618 SMLoc Loc = Op.getEndLoc();
1619 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1620 delete &Op;
1621 }
1622 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001623 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1624 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1625 Operands.size() == 3) {
1626 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1627 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1628 isa<MCConstantExpr>(Op.Mem.Disp) &&
1629 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1630 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1631 SMLoc Loc = Op.getEndLoc();
1632 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1633 delete &Op;
1634 }
1635 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001636 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1637 if (Name.startswith("ins") && Operands.size() == 3 &&
1638 (Name == "insb" || Name == "insw" || Name == "insl")) {
1639 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1640 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1641 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1642 Operands.pop_back();
1643 Operands.pop_back();
1644 delete &Op;
1645 delete &Op2;
1646 }
1647 }
1648
1649 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1650 if (Name.startswith("outs") && Operands.size() == 3 &&
1651 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1652 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1653 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1654 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1655 Operands.pop_back();
1656 Operands.pop_back();
1657 delete &Op;
1658 delete &Op2;
1659 }
1660 }
1661
1662 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1663 if (Name.startswith("movs") && Operands.size() == 3 &&
1664 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001665 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001666 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1667 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1668 if (isSrcOp(Op) && isDstOp(Op2)) {
1669 Operands.pop_back();
1670 Operands.pop_back();
1671 delete &Op;
1672 delete &Op2;
1673 }
1674 }
1675 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1676 if (Name.startswith("lods") && Operands.size() == 3 &&
1677 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001678 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001679 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1680 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1681 if (isSrcOp(*Op1) && Op2->isReg()) {
1682 const char *ins;
1683 unsigned reg = Op2->getReg();
1684 bool isLods = Name == "lods";
1685 if (reg == X86::AL && (isLods || Name == "lodsb"))
1686 ins = "lodsb";
1687 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1688 ins = "lodsw";
1689 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1690 ins = "lodsl";
1691 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1692 ins = "lodsq";
1693 else
1694 ins = NULL;
1695 if (ins != NULL) {
1696 Operands.pop_back();
1697 Operands.pop_back();
1698 delete Op1;
1699 delete Op2;
1700 if (Name != ins)
1701 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1702 }
1703 }
1704 }
1705 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1706 if (Name.startswith("stos") && Operands.size() == 3 &&
1707 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001708 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001709 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1710 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1711 if (isDstOp(*Op2) && Op1->isReg()) {
1712 const char *ins;
1713 unsigned reg = Op1->getReg();
1714 bool isStos = Name == "stos";
1715 if (reg == X86::AL && (isStos || Name == "stosb"))
1716 ins = "stosb";
1717 else if (reg == X86::AX && (isStos || Name == "stosw"))
1718 ins = "stosw";
1719 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1720 ins = "stosl";
1721 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1722 ins = "stosq";
1723 else
1724 ins = NULL;
1725 if (ins != NULL) {
1726 Operands.pop_back();
1727 Operands.pop_back();
1728 delete Op1;
1729 delete Op2;
1730 if (Name != ins)
1731 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1732 }
1733 }
1734 }
1735
Chris Lattnere9e16a32010-09-15 04:33:27 +00001736 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001737 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001738 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001739 Name.startswith("shl") || Name.startswith("sal") ||
1740 Name.startswith("rcl") || Name.startswith("rcr") ||
1741 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001742 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001743 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001744 // Intel syntax
1745 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1746 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001747 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1748 delete Operands[2];
1749 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001750 }
1751 } else {
1752 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1753 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001754 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1755 delete Operands[1];
1756 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001757 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001758 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001759 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001760
Chris Lattner15f89512011-04-09 19:41:05 +00001761 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1762 // instalias with an immediate operand yet.
1763 if (Name == "int" && Operands.size() == 2) {
1764 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1765 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1766 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1767 delete Operands[1];
1768 Operands.erase(Operands.begin() + 1);
1769 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1770 }
1771 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001772
Chris Lattner98986712010-01-14 22:21:20 +00001773 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001774}
1775
Craig Topper4bef9612013-03-18 02:53:34 +00001776static bool convertToSExti8(MCInst &Inst, unsigned Opcode, unsigned Reg,
1777 bool isCmp) {
1778 MCInst TmpInst;
1779 TmpInst.setOpcode(Opcode);
1780 if (!isCmp)
1781 TmpInst.addOperand(MCOperand::CreateReg(Reg));
1782 TmpInst.addOperand(MCOperand::CreateReg(Reg));
1783 TmpInst.addOperand(Inst.getOperand(0));
1784 Inst = TmpInst;
1785 return true;
1786}
1787
1788static bool convert16i16to16ri8(MCInst &Inst, unsigned Opcode,
1789 bool isCmp = false) {
1790 if (!Inst.getOperand(0).isImm() ||
1791 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1792 return false;
1793
1794 return convertToSExti8(Inst, Opcode, X86::AX, isCmp);
1795}
1796
1797static bool convert32i32to32ri8(MCInst &Inst, unsigned Opcode,
1798 bool isCmp = false) {
1799 if (!Inst.getOperand(0).isImm() ||
1800 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1801 return false;
1802
1803 return convertToSExti8(Inst, Opcode, X86::EAX, isCmp);
1804}
1805
1806static bool convert64i32to64ri8(MCInst &Inst, unsigned Opcode,
1807 bool isCmp = false) {
1808 if (!Inst.getOperand(0).isImm() ||
1809 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1810 return false;
1811
1812 return convertToSExti8(Inst, Opcode, X86::RAX, isCmp);
1813}
1814
Devang Pateldd929fc2012-01-12 18:03:40 +00001815bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001816processInstruction(MCInst &Inst,
1817 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1818 switch (Inst.getOpcode()) {
1819 default: return false;
Craig Topper4bef9612013-03-18 02:53:34 +00001820 case X86::AND16i16: return convert16i16to16ri8(Inst, X86::AND16ri8);
1821 case X86::AND32i32: return convert32i32to32ri8(Inst, X86::AND32ri8);
1822 case X86::AND64i32: return convert64i32to64ri8(Inst, X86::AND64ri8);
1823 case X86::XOR16i16: return convert16i16to16ri8(Inst, X86::XOR16ri8);
1824 case X86::XOR32i32: return convert32i32to32ri8(Inst, X86::XOR32ri8);
1825 case X86::XOR64i32: return convert64i32to64ri8(Inst, X86::XOR64ri8);
1826 case X86::OR16i16: return convert16i16to16ri8(Inst, X86::OR16ri8);
1827 case X86::OR32i32: return convert32i32to32ri8(Inst, X86::OR32ri8);
1828 case X86::OR64i32: return convert64i32to64ri8(Inst, X86::OR64ri8);
1829 case X86::CMP16i16: return convert16i16to16ri8(Inst, X86::CMP16ri8, true);
1830 case X86::CMP32i32: return convert32i32to32ri8(Inst, X86::CMP32ri8, true);
1831 case X86::CMP64i32: return convert64i32to64ri8(Inst, X86::CMP64ri8, true);
1832 case X86::ADD16i16: return convert16i16to16ri8(Inst, X86::ADD16ri8);
1833 case X86::ADD32i32: return convert32i32to32ri8(Inst, X86::ADD32ri8);
1834 case X86::ADD64i32: return convert64i32to64ri8(Inst, X86::ADD64ri8);
1835 case X86::SUB16i16: return convert16i16to16ri8(Inst, X86::SUB16ri8);
1836 case X86::SUB32i32: return convert32i32to32ri8(Inst, X86::SUB32ri8);
1837 case X86::SUB64i32: return convert64i32to64ri8(Inst, X86::SUB64ri8);
Craig Topper8ee1c1c2013-03-18 03:34:55 +00001838 case X86::ADC16i16: return convert16i16to16ri8(Inst, X86::ADC16ri8);
1839 case X86::ADC32i32: return convert32i32to32ri8(Inst, X86::ADC32ri8);
1840 case X86::ADC64i32: return convert64i32to64ri8(Inst, X86::ADC64ri8);
1841 case X86::SBB16i16: return convert16i16to16ri8(Inst, X86::SBB16ri8);
1842 case X86::SBB32i32: return convert32i32to32ri8(Inst, X86::SBB32ri8);
1843 case X86::SBB64i32: return convert64i32to64ri8(Inst, X86::SBB64ri8);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001844 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001845}
1846
Jim Grosbach3ca63822012-11-14 18:04:47 +00001847static const char *getSubtargetFeatureName(unsigned Val);
Devang Patelb8ba13f2012-01-18 22:42:29 +00001848bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001849MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001850 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001851 MCStreamer &Out, unsigned &ErrorInfo,
1852 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001853 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001854 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1855 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001856 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001857
Chris Lattner7c51a312010-09-29 01:50:45 +00001858 // First, handle aliases that expand to multiple instructions.
1859 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001860 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001861 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001862 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001863 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001864 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001865 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001866 MCInst Inst;
1867 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001868 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001869 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001870 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001871
Chris Lattner0bb83a82010-09-30 16:39:29 +00001872 const char *Repl =
1873 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001874 .Case("finit", "fninit")
1875 .Case("fsave", "fnsave")
1876 .Case("fstcw", "fnstcw")
1877 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001878 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001879 .Case("fstsw", "fnstsw")
1880 .Case("fstsww", "fnstsw")
1881 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001882 .Default(0);
1883 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001884 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001885 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001886 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001887
Chris Lattnera008e8a2010-09-06 21:54:15 +00001888 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001889 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001890
Daniel Dunbarc918d602010-05-04 16:12:42 +00001891 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001892 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001893 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001894 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001895 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001896 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001897 // Some instructions need post-processing to, for example, tweak which
1898 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001899 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001900 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001901 while (processInstruction(Inst, Operands))
1902 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001903
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001904 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001905 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001906 Out.EmitInstruction(Inst);
1907 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001908 return false;
Jim Grosbach3ca63822012-11-14 18:04:47 +00001909 case Match_MissingFeature: {
1910 assert(ErrorInfo && "Unknown missing feature!");
1911 // Special case the error message for the very common case where only
1912 // a single subtarget feature is missing.
1913 std::string Msg = "instruction requires:";
1914 unsigned Mask = 1;
1915 for (unsigned i = 0; i < (sizeof(ErrorInfo)*8-1); ++i) {
1916 if (ErrorInfo & Mask) {
1917 Msg += " ";
1918 Msg += getSubtargetFeatureName(ErrorInfo & Mask);
1919 }
1920 Mask <<= 1;
1921 }
1922 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
1923 }
Chris Lattnera008e8a2010-09-06 21:54:15 +00001924 case Match_InvalidOperand:
1925 WasOriginallyInvalidOperand = true;
1926 break;
1927 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001928 break;
1929 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001930
Daniel Dunbarc918d602010-05-04 16:12:42 +00001931 // FIXME: Ideally, we would only attempt suffix matches for things which are
1932 // valid prefixes, and we could just infer the right unambiguous
1933 // type. However, that requires substantially more matcher support than the
1934 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001935
Daniel Dunbarc918d602010-05-04 16:12:42 +00001936 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001937 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001938 SmallString<16> Tmp;
1939 Tmp += Base;
1940 Tmp += ' ';
1941 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001942
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001943 // If this instruction starts with an 'f', then it is a floating point stack
1944 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1945 // 80-bit floating point, which use the suffixes s,l,t respectively.
1946 //
1947 // Otherwise, we assume that this may be an integer instruction, which comes
1948 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1949 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001950
Daniel Dunbarc918d602010-05-04 16:12:42 +00001951 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001952 Tmp[Base.size()] = Suffixes[0];
1953 unsigned ErrorInfoIgnore;
Duncan Sands4d9b7c22013-03-01 09:46:03 +00001954 unsigned ErrorInfoMissingFeature = 0; // Init suppresses compiler warnings.
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001955 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001956
Chad Rosier6e006d32012-10-12 22:53:36 +00001957 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1958 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001959 // If this returned as a missing feature failure, remember that.
1960 if (Match1 == Match_MissingFeature)
1961 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001962 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001963 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1964 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001965 // If this returned as a missing feature failure, remember that.
1966 if (Match2 == Match_MissingFeature)
1967 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001968 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001969 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1970 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001971 // If this returned as a missing feature failure, remember that.
1972 if (Match3 == Match_MissingFeature)
1973 ErrorInfoMissingFeature = ErrorInfoIgnore;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001974 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001975 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1976 isParsingIntelSyntax());
Jim Grosbach3ca63822012-11-14 18:04:47 +00001977 // If this returned as a missing feature failure, remember that.
1978 if (Match4 == Match_MissingFeature)
1979 ErrorInfoMissingFeature = ErrorInfoIgnore;
Daniel Dunbarc918d602010-05-04 16:12:42 +00001980
1981 // Restore the old token.
1982 Op->setTokenValue(Base);
1983
1984 // If exactly one matched, then we treat that as a successful match (and the
1985 // instruction will already have been filled in correctly, since the failing
1986 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001987 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001988 (Match1 == Match_Success) + (Match2 == Match_Success) +
1989 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001990 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001991 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001992 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001993 Out.EmitInstruction(Inst);
1994 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001995 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001996 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001997
Chris Lattnerec6789f2010-09-06 20:08:02 +00001998 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001999
Daniel Dunbar09062b12010-08-12 00:55:42 +00002000 // If we had multiple suffix matches, then identify this as an ambiguous
2001 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00002002 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00002003 char MatchChars[4];
2004 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002005 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
2006 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
2007 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
2008 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00002009
2010 SmallString<126> Msg;
2011 raw_svector_ostream OS(Msg);
2012 OS << "ambiguous instructions require an explicit suffix (could be ";
2013 for (unsigned i = 0; i != NumMatches; ++i) {
2014 if (i != 0)
2015 OS << ", ";
2016 if (i + 1 == NumMatches)
2017 OS << "or ";
2018 OS << "'" << Base << MatchChars[i] << "'";
2019 }
2020 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00002021 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00002022 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00002023 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002024
Chris Lattnera008e8a2010-09-06 21:54:15 +00002025 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002026
Chris Lattnera008e8a2010-09-06 21:54:15 +00002027 // If all of the instructions reported an invalid mnemonic, then the original
2028 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002029 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
2030 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00002031 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00002032 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00002033 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00002034 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002035 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00002036 }
2037
2038 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00002039 if (ErrorInfo != ~0U) {
2040 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00002041 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002042 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002043
Chad Rosier84125ca2012-10-13 00:26:04 +00002044 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002045 if (Operand->getStartLoc().isValid()) {
2046 SMRange OperandRange = Operand->getLocRange();
2047 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002048 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00002049 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00002050 }
2051
Chad Rosierb4fdade2012-08-21 19:36:59 +00002052 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002053 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002054 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002055
Chris Lattnerec6789f2010-09-06 20:08:02 +00002056 // If one instruction matched with a missing feature, report this as a
2057 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002058 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
2059 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Jim Grosbach3ca63822012-11-14 18:04:47 +00002060 std::string Msg = "instruction requires:";
2061 unsigned Mask = 1;
2062 for (unsigned i = 0; i < (sizeof(ErrorInfoMissingFeature)*8-1); ++i) {
2063 if (ErrorInfoMissingFeature & Mask) {
2064 Msg += " ";
2065 Msg += getSubtargetFeatureName(ErrorInfoMissingFeature & Mask);
2066 }
2067 Mask <<= 1;
2068 }
2069 return Error(IDLoc, Msg, EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00002070 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002071
Chris Lattnera008e8a2010-09-06 21:54:15 +00002072 // If one instruction matched with an invalid operand, report this as an
2073 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00002074 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
2075 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00002076 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00002077 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00002078 return true;
2079 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00002080
Chris Lattnerec6789f2010-09-06 20:08:02 +00002081 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00002082 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00002083 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00002084 return true;
2085}
2086
2087
Devang Pateldd929fc2012-01-12 18:03:40 +00002088bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00002089 StringRef IDVal = DirectiveID.getIdentifier();
2090 if (IDVal == ".word")
2091 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00002092 else if (IDVal.startswith(".code"))
2093 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00002094 else if (IDVal.startswith(".att_syntax")) {
2095 getParser().setAssemblerDialect(0);
2096 return false;
2097 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00002098 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00002099 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2100 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00002101 // FIXME : Handle noprefix
2102 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00002103 } else
Craig Topper76bd9382012-07-18 04:59:16 +00002104 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00002105 }
2106 return false;
2107 }
Chris Lattner537ca842010-10-30 17:38:55 +00002108 return true;
2109}
2110
2111/// ParseDirectiveWord
2112/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00002113bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00002114 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2115 for (;;) {
2116 const MCExpr *Value;
Jim Grosbachcb2ae3d2013-02-20 22:21:35 +00002117 if (getParser().parseExpression(Value))
Chris Lattner537ca842010-10-30 17:38:55 +00002118 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002119
Eric Christopher1ced2082013-01-09 03:52:05 +00002120 getParser().getStreamer().EmitValue(Value, Size);
Chad Rosier36b8fed2012-06-27 22:34:28 +00002121
Chris Lattner537ca842010-10-30 17:38:55 +00002122 if (getLexer().is(AsmToken::EndOfStatement))
2123 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00002124
Chris Lattner537ca842010-10-30 17:38:55 +00002125 // FIXME: Improve diagnostic.
2126 if (getLexer().isNot(AsmToken::Comma))
2127 return Error(L, "unexpected token in directive");
2128 Parser.Lex();
2129 }
2130 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00002131
Chris Lattner537ca842010-10-30 17:38:55 +00002132 Parser.Lex();
2133 return false;
2134}
2135
Evan Chengbd27f5a2011-07-27 00:38:12 +00002136/// ParseDirectiveCode
2137/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00002138bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00002139 if (IDVal == ".code32") {
2140 Parser.Lex();
2141 if (is64BitMode()) {
2142 SwitchMode();
2143 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2144 }
2145 } else if (IDVal == ".code64") {
2146 Parser.Lex();
2147 if (!is64BitMode()) {
2148 SwitchMode();
2149 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
2150 }
2151 } else {
2152 return Error(L, "unexpected directive " + IDVal);
2153 }
Chris Lattner537ca842010-10-30 17:38:55 +00002154
Evan Chengbd27f5a2011-07-27 00:38:12 +00002155 return false;
2156}
Chris Lattner537ca842010-10-30 17:38:55 +00002157
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002158// Force static initialization.
2159extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00002160 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2161 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002162}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002163
Chris Lattner0692ee62010-09-06 19:11:01 +00002164#define GET_REGISTER_MATCHER
2165#define GET_MATCHER_IMPLEMENTATION
Jim Grosbach3ca63822012-11-14 18:04:47 +00002166#define GET_SUBTARGET_FEATURE_NAME
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002167#include "X86GenAsmMatcher.inc"