blob: 4bd9db9837976474c6d59f5774a98af4598edadb [file] [log] [blame]
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
11#include "llvm/MC/MCTargetAsmParser.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000012#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000013#include "llvm/MC/MCExpr.h"
Chad Rosier96d58e62012-10-19 20:57:14 +000014#include "llvm/MC/MCSymbol.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000015#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000016#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000017#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Chad Rosier4284e172012-10-24 22:13:37 +000021#include "llvm/ADT/APFloat.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000022#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000024#include "llvm/ADT/StringSwitch.h"
25#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000028#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000029
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000030using namespace llvm;
31
32namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000033struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000034
Devang Pateldd929fc2012-01-12 18:03:40 +000035class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000036 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &Parser;
Chad Rosier6a020a72012-10-25 20:41:34 +000038 ParseInstructionInfo *InstInfo;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000040 MCAsmParser &getParser() const { return Parser; }
41
42 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000044 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000045 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000046 bool MatchingInlineAsm = false) {
47 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000048 return Parser.Error(L, Msg, Ranges);
49 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000050
Devang Pateld37ad242012-01-17 18:00:18 +000051 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
52 Error(Loc, Msg);
53 return 0;
54 }
55
Chris Lattner309264d2010-01-15 18:44:13 +000056 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000057 X86Operand *ParseATTOperand();
58 X86Operand *ParseIntelOperand();
Chad Rosierc0a14b82012-10-24 17:22:29 +000059 X86Operand *ParseIntelOffsetOfOperator(SMLoc StartLoc);
Chad Rosierefcb3d92012-10-26 18:04:20 +000060 X86Operand *ParseIntelTypeOperator(SMLoc StartLoc);
Chad Rosier5b0f1b32012-10-04 23:59:38 +000061 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000062 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000063 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000064
Chad Rosier5e6b37f2012-10-25 17:37:43 +000065 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr **NewDisp,
66 SmallString<64> &Err);
Chad Rosier22f441a2012-10-24 22:21:50 +000067
Kevin Enderby9c656452009-09-10 20:51:44 +000068 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000069 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000070
Devang Patelb8ba13f2012-01-18 22:42:29 +000071 bool processInstruction(MCInst &Inst,
72 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
73
Chad Rosier84125ca2012-10-13 00:26:04 +000074 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000075 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000076 MCStreamer &Out, unsigned &ErrorInfo,
77 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000078
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000079 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000080 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000081 bool isSrcOp(X86Operand &Op);
82
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000083 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
84 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000085 bool isDstOp(X86Operand &Op);
86
Evan Cheng59ee62d2011-07-11 03:57:24 +000087 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000088 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000089 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000090 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000091 void SwitchMode() {
92 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
93 setAvailableFeatures(FB);
94 }
Evan Chengebdeeab2011-07-08 01:53:10 +000095
Daniel Dunbar54074b52010-07-19 05:44:09 +000096 /// @name Auto-generated Matcher Functions
97 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000098
Chris Lattner0692ee62010-09-06 19:11:01 +000099#define GET_ASSEMBLER_HEADER
100#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000101
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000102 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000103
104public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000105 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosier6a020a72012-10-25 20:41:34 +0000106 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000107
Daniel Dunbar54074b52010-07-19 05:44:09 +0000108 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000109 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000110 }
Roman Divackybf755322011-01-27 17:14:22 +0000111 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000112
Chad Rosier6a020a72012-10-25 20:41:34 +0000113 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
114 SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000115 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000116
117 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000118
119 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000120 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000121 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000122};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000123} // end anonymous namespace
124
Sean Callanane9b466d2010-01-23 00:40:33 +0000125/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000126/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000127
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000128static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000129
130/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000131
Craig Topper76bd9382012-07-18 04:59:16 +0000132static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000133 return (( Value <= 0x000000000000007FULL)||
134 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
135 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
136}
137
138static bool isImmSExti32i8Value(uint64_t Value) {
139 return (( Value <= 0x000000000000007FULL)||
140 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
141 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
142}
143
144static bool isImmZExtu32u8Value(uint64_t Value) {
145 return (Value <= 0x00000000000000FFULL);
146}
147
148static bool isImmSExti64i8Value(uint64_t Value) {
149 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000150 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000151}
152
153static bool isImmSExti64i32Value(uint64_t Value) {
154 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000155 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000156}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000157namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000158
159/// X86Operand - Instances of this class represent a parsed X86 machine
160/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000161struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000162 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000163 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000164 Register,
165 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000166 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000167 } Kind;
168
Chris Lattner29ef9a22010-01-15 18:51:29 +0000169 SMLoc StartLoc, EndLoc;
Chad Rosier5a719fc2012-10-23 17:43:43 +0000170 SMLoc OffsetOfLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000171
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000172 union {
173 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000174 const char *Data;
175 unsigned Length;
176 } Tok;
177
178 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000179 unsigned RegNo;
180 } Reg;
181
182 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000183 const MCExpr *Val;
Chad Rosierefcb3d92012-10-26 18:04:20 +0000184 bool NeedAsmRewrite;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000185 } Imm;
186
187 struct {
188 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000189 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000190 unsigned BaseReg;
191 unsigned IndexReg;
192 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000193 unsigned Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000194 bool NeedSizeDir;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000195 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000196 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000197
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000198 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000199 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000200
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000201 /// getStartLoc - Get the location of the first token of this operand.
202 SMLoc getStartLoc() const { return StartLoc; }
203 /// getEndLoc - Get the location of the last token of this operand.
204 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000205 /// getLocRange - Get the range between the first and last token of this
206 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000207 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000208 /// getOffsetOfLoc - Get the location of the offset operator.
209 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000210
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000211 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000212
Daniel Dunbar20927f22009-08-07 08:26:05 +0000213 StringRef getToken() const {
214 assert(Kind == Token && "Invalid access!");
215 return StringRef(Tok.Data, Tok.Length);
216 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000217 void setTokenValue(StringRef Value) {
218 assert(Kind == Token && "Invalid access!");
219 Tok.Data = Value.data();
220 Tok.Length = Value.size();
221 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000222
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000223 unsigned getReg() const {
224 assert(Kind == Register && "Invalid access!");
225 return Reg.RegNo;
226 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000227
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000228 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000229 assert(Kind == Immediate && "Invalid access!");
230 return Imm.Val;
231 }
232
Chad Rosierefcb3d92012-10-26 18:04:20 +0000233 bool needAsmRewrite() const {
234 assert(Kind == Immediate && "Invalid access!");
235 return Imm.NeedAsmRewrite;
236 }
237
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000238 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000239 assert(Kind == Memory && "Invalid access!");
240 return Mem.Disp;
241 }
242 unsigned getMemSegReg() const {
243 assert(Kind == Memory && "Invalid access!");
244 return Mem.SegReg;
245 }
246 unsigned getMemBaseReg() const {
247 assert(Kind == Memory && "Invalid access!");
248 return Mem.BaseReg;
249 }
250 unsigned getMemIndexReg() const {
251 assert(Kind == Memory && "Invalid access!");
252 return Mem.IndexReg;
253 }
254 unsigned getMemScale() const {
255 assert(Kind == Memory && "Invalid access!");
256 return Mem.Scale;
257 }
258
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000259 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000260
261 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000262
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000263 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000264 if (!isImm())
265 return false;
266
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000267 // If this isn't a constant expr, just assume it fits and let relaxation
268 // handle it.
269 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
270 if (!CE)
271 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000272
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000273 // Otherwise, check the value is in a range that makes sense for this
274 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000275 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000276 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000277 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000278 if (!isImm())
279 return false;
280
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000281 // If this isn't a constant expr, just assume it fits and let relaxation
282 // handle it.
283 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
284 if (!CE)
285 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000286
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000287 // Otherwise, check the value is in a range that makes sense for this
288 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000289 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000290 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000291 bool isImmZExtu32u8() const {
292 if (!isImm())
293 return false;
294
295 // If this isn't a constant expr, just assume it fits and let relaxation
296 // handle it.
297 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
298 if (!CE)
299 return true;
300
301 // Otherwise, check the value is in a range that makes sense for this
302 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000303 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000304 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000305 bool isImmSExti64i8() const {
306 if (!isImm())
307 return false;
308
309 // If this isn't a constant expr, just assume it fits and let relaxation
310 // handle it.
311 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
312 if (!CE)
313 return true;
314
315 // Otherwise, check the value is in a range that makes sense for this
316 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000317 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000318 }
319 bool isImmSExti64i32() const {
320 if (!isImm())
321 return false;
322
323 // If this isn't a constant expr, just assume it fits and let relaxation
324 // handle it.
325 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
326 if (!CE)
327 return true;
328
329 // Otherwise, check the value is in a range that makes sense for this
330 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000331 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000332 }
333
Chad Rosier96d58e62012-10-19 20:57:14 +0000334 unsigned getMemSize() const {
335 assert(Kind == Memory && "Invalid access!");
336 return Mem.Size;
337 }
338
Chad Rosiera703fb92012-10-22 19:50:35 +0000339 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000340 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000341 }
342
Chad Rosier96d58e62012-10-19 20:57:14 +0000343 bool needSizeDirective() const {
344 assert(Kind == Memory && "Invalid access!");
345 return Mem.NeedSizeDir;
346 }
347
Daniel Dunbar20927f22009-08-07 08:26:05 +0000348 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000349 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000350 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000351 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000352 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000353 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000354 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000355 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000356 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000357 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000358 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000359 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000360 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000361 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000362 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000363 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000364 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000365 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000366 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000367 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000368 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000369 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000370
Craig Topper75dc33a2012-07-18 04:11:12 +0000371 bool isMemVX32() const {
372 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
373 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
374 }
375 bool isMemVY32() const {
376 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
377 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
378 }
379 bool isMemVX64() const {
380 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
381 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
382 }
383 bool isMemVY64() const {
384 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
385 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
386 }
387
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000388 bool isAbsMem() const {
389 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000390 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000391 }
392
Daniel Dunbar20927f22009-08-07 08:26:05 +0000393 bool isReg() const { return Kind == Register; }
394
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000395 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
396 // Add as immediates when possible.
397 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
398 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
399 else
400 Inst.addOperand(MCOperand::CreateExpr(Expr));
401 }
402
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000403 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000404 assert(N == 1 && "Invalid number of operands!");
405 Inst.addOperand(MCOperand::CreateReg(getReg()));
406 }
407
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000408 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000409 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000410 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000411 }
412
Chad Rosier36b8fed2012-06-27 22:34:28 +0000413 void addMem8Operands(MCInst &Inst, unsigned N) const {
414 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000415 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000416 void addMem16Operands(MCInst &Inst, unsigned N) const {
417 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000418 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000419 void addMem32Operands(MCInst &Inst, unsigned N) const {
420 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000421 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000422 void addMem64Operands(MCInst &Inst, unsigned N) const {
423 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000424 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000425 void addMem80Operands(MCInst &Inst, unsigned N) const {
426 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000427 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000428 void addMem128Operands(MCInst &Inst, unsigned N) const {
429 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000430 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000431 void addMem256Operands(MCInst &Inst, unsigned N) const {
432 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000433 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000434 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
435 addMemOperands(Inst, N);
436 }
437 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
438 addMemOperands(Inst, N);
439 }
440 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
441 addMemOperands(Inst, N);
442 }
443 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
444 addMemOperands(Inst, N);
445 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000446
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000447 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000448 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000449 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
450 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
451 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000452 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000453 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
454 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000455
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000456 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
457 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000458 // Add as immediates when possible.
459 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
460 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
461 else
462 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000463 }
464
Chris Lattnerb4307b32010-01-15 19:28:38 +0000465 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000466 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
467 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000468 Res->Tok.Data = Str.data();
469 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000470 return Res;
471 }
472
Chad Rosierc0a14b82012-10-24 17:22:29 +0000473 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
474 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000475 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000476 Res->Reg.RegNo = RegNo;
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 Rosierefcb3d92012-10-26 18:04:20 +0000481 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc,
482 bool NeedRewrite = true){
Chris Lattnerb4307b32010-01-15 19:28:38 +0000483 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000484 Res->Imm.Val = Val;
Chad Rosierefcb3d92012-10-26 18:04:20 +0000485 Res->Imm.NeedAsmRewrite = NeedRewrite;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000486 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000487 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000488
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000489 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000490 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
491 unsigned Size = 0, bool NeedSizeDir = false){
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000492 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
493 Res->Mem.SegReg = 0;
494 Res->Mem.Disp = Disp;
495 Res->Mem.BaseReg = 0;
496 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000497 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000498 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000499 Res->Mem.NeedSizeDir = NeedSizeDir;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000500 return Res;
501 }
502
503 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000504 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
505 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000506 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000507 unsigned Size = 0, bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000508 // We should never just have a displacement, that should be parsed as an
509 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000510 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
511
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000512 // The scale should always be one of {1,2,4,8}.
513 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000514 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000515 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000516 Res->Mem.SegReg = SegReg;
517 Res->Mem.Disp = Disp;
518 Res->Mem.BaseReg = BaseReg;
519 Res->Mem.IndexReg = IndexReg;
520 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000521 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000522 Res->Mem.NeedSizeDir = NeedSizeDir;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000523 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000524 }
525};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000526
Chris Lattner37dfdec2009-07-29 06:33:53 +0000527} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000528
Devang Pateldd929fc2012-01-12 18:03:40 +0000529bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000530 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000531
532 return (Op.isMem() &&
533 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
534 isa<MCConstantExpr>(Op.Mem.Disp) &&
535 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
536 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
537}
538
Devang Pateldd929fc2012-01-12 18:03:40 +0000539bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000540 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000541
Chad Rosier36b8fed2012-06-27 22:34:28 +0000542 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000543 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000544 isa<MCConstantExpr>(Op.Mem.Disp) &&
545 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
546 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
547}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000548
Devang Pateldd929fc2012-01-12 18:03:40 +0000549bool X86AsmParser::ParseRegister(unsigned &RegNo,
550 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000551 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000552 const AsmToken &PercentTok = Parser.getTok();
553 StartLoc = PercentTok.getLoc();
554
555 // If we encounter a %, ignore it. This code handles registers with and
556 // without the prefix, unprefixed registers can occur in cfi directives.
557 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000558 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000559
Sean Callanan18b83232010-01-19 21:44:56 +0000560 const AsmToken &Tok = Parser.getTok();
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",
564 SMRange(StartLoc, Tok.getEndLoc()));
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",
585 SMRange(StartLoc, Tok.getEndLoc()));
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;
591 EndLoc = Tok.getLoc();
592 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000593
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000594 // Check to see if we have '(4)' after %st.
595 if (getLexer().isNot(AsmToken::LParen))
596 return false;
597 // Lex the paren.
598 getParser().Lex();
599
600 const AsmToken &IntTok = Parser.getTok();
601 if (IntTok.isNot(AsmToken::Integer))
602 return Error(IntTok.getLoc(), "expected stack index");
603 switch (IntTok.getIntVal()) {
604 case 0: RegNo = X86::ST0; break;
605 case 1: RegNo = X86::ST1; break;
606 case 2: RegNo = X86::ST2; break;
607 case 3: RegNo = X86::ST3; break;
608 case 4: RegNo = X86::ST4; break;
609 case 5: RegNo = X86::ST5; break;
610 case 6: RegNo = X86::ST6; break;
611 case 7: RegNo = X86::ST7; break;
612 default: return Error(IntTok.getLoc(), "invalid stack index");
613 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000614
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000615 if (getParser().Lex().isNot(AsmToken::RParen))
616 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000617
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000618 EndLoc = Tok.getLoc();
619 Parser.Lex(); // Eat ')'
620 return false;
621 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000622
Chris Lattner645b2092010-06-24 07:29:18 +0000623 // If this is "db[0-7]", match it as an alias
624 // for dr[0-7].
625 if (RegNo == 0 && Tok.getString().size() == 3 &&
626 Tok.getString().startswith("db")) {
627 switch (Tok.getString()[2]) {
628 case '0': RegNo = X86::DR0; break;
629 case '1': RegNo = X86::DR1; break;
630 case '2': RegNo = X86::DR2; break;
631 case '3': RegNo = X86::DR3; break;
632 case '4': RegNo = X86::DR4; break;
633 case '5': RegNo = X86::DR5; break;
634 case '6': RegNo = X86::DR6; break;
635 case '7': RegNo = X86::DR7; break;
636 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000637
Chris Lattner645b2092010-06-24 07:29:18 +0000638 if (RegNo != 0) {
639 EndLoc = Tok.getLoc();
640 Parser.Lex(); // Eat it.
641 return false;
642 }
643 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000644
Devang Patel1aea4302012-01-20 22:32:05 +0000645 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000646 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000647 return Error(StartLoc, "invalid register name",
648 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000649 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000650
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000651 EndLoc = Tok.getEndLoc();
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 Rosier65c88922012-10-22 19:42:52 +0000676X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000677 unsigned Size) {
678 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Chad Rosier4284e172012-10-24 22:13:37 +0000679 const AsmToken &Tok = Parser.getTok();
680 SMLoc Start = Tok.getLoc(), End;
Devang Patel0a338862012-01-12 01:36:43 +0000681
Chad Rosier4284e172012-10-24 22:13:37 +0000682 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000683 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
684
685 // Eat '['
686 if (getLexer().isNot(AsmToken::LBrac))
687 return ErrorOperand(Start, "Expected '[' token!");
688 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000689
Devang Pateld37ad242012-01-17 18:00:18 +0000690 if (getLexer().is(AsmToken::Identifier)) {
691 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000692 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000693 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000694 if (getParser().ParseExpression(Disp, End)) return 0;
695 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000696 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000697 Parser.Lex();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000698 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000699 }
700 } else if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000701 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000702 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000703 SMLoc Loc = Tok.getLoc();
Devang Patel3e081312012-01-23 20:20:06 +0000704 if (getLexer().is(AsmToken::RBrac)) {
705 // Handle '[' number ']'
706 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000707 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
708 if (SegReg)
709 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000710 Start, End, Size);
711 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000712 } else if (getLexer().is(AsmToken::Star)) {
713 // Handle '[' Scale*IndexReg ']'
714 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000715 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000716 if (ParseRegister(IndexReg, IdxRegLoc, End))
717 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000718 Scale = Val;
719 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000720 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000721 }
722
723 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
724 bool isPlus = getLexer().is(AsmToken::Plus);
725 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000726 SMLoc PlusLoc = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000727 if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000728 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000729 Parser.Lex();
730 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000731 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000732 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000733 if (ParseRegister(IndexReg, IdxRegLoc, End))
734 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000735 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000736 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000737 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000738 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000739 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000740 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000741 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000742 // This could be an index register or a displacement expression.
Chad Rosier4284e172012-10-24 22:13:37 +0000743 End = Tok.getLoc();
Devang Patelf2d21372012-01-23 22:35:25 +0000744 if (!IndexReg)
745 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000746 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000747 }
Devang Pateld37ad242012-01-17 18:00:18 +0000748 }
749
750 if (getLexer().isNot(AsmToken::RBrac))
751 if (getParser().ParseExpression(Disp, End)) return 0;
752
Chad Rosier4284e172012-10-24 22:13:37 +0000753 End = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000754 if (getLexer().isNot(AsmToken::RBrac))
755 return ErrorOperand(End, "expected ']' token!");
756 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000757 End = Tok.getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000758
Chad Rosierddb53ef2012-10-26 22:01:25 +0000759 // Parse the dot operator (e.g., [ebx].foo.bar).
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000760 if (Tok.getString().startswith(".")) {
761 SmallString<64> Err;
762 const MCExpr *NewDisp;
763 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
764 return ErrorOperand(Tok.getLoc(), Err);
765
766 Parser.Lex(); // Eat the field.
767 Disp = NewDisp;
768 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000769
770 End = Tok.getLoc();
771
Devang Patelfdd3b302012-01-20 21:21:01 +0000772 // handle [-42]
773 if (!BaseReg && !IndexReg)
Chad Rosierc0a14b82012-10-24 17:22:29 +0000774 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patelfdd3b302012-01-20 21:21:01 +0000775
Devang Pateld37ad242012-01-17 18:00:18 +0000776 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000777 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000778}
779
780/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000781X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000782 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000783 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000784
785 unsigned Size = getIntelMemOperandSize(Tok.getString());
786 if (Size) {
787 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000788 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
789 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000790 Parser.Lex();
791 }
792
Chad Rosierc0a14b82012-10-24 17:22:29 +0000793 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000794 return ParseIntelBracExpression(SegReg, Size);
795
796 if (!ParseRegister(SegReg, Start, End)) {
797 // Handel SegReg : [ ... ]
798 if (getLexer().isNot(AsmToken::Colon))
799 return ErrorOperand(Start, "Expected ':' token!");
800 Parser.Lex(); // Eat :
801 if (getLexer().isNot(AsmToken::LBrac))
802 return ErrorOperand(Start, "Expected '[' token!");
803 return ParseIntelBracExpression(SegReg, Size);
804 }
Devang Pateld37ad242012-01-17 18:00:18 +0000805
806 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
807 if (getParser().ParseExpression(Disp, End)) return 0;
Chad Rosierce353b32012-10-15 17:26:38 +0000808 End = Parser.getTok().getLoc();
Chad Rosier96d58e62012-10-19 20:57:14 +0000809
810 bool NeedSizeDir = false;
811 if (!Size && isParsingInlineAsm()) {
812 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
813 const MCSymbol &Sym = SymRef->getSymbol();
814 // FIXME: The SemaLookup will fail if the name is anything other then an
815 // identifier.
816 // FIXME: Pass a valid SMLoc.
817 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Size);
818 NeedSizeDir = Size > 0;
819 }
820 }
Chad Rosier2a784132012-10-23 23:31:33 +0000821 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +0000822 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier2a784132012-10-23 23:31:33 +0000823 else
Chad Rosierd4d96ac2012-10-23 23:34:28 +0000824 // When parsing inline assembly we set the base register to a non-zero value
825 // as we don't know the actual value at this time. This is necessary to
826 // get the matching correct in some cases.
Chad Rosier2a784132012-10-23 23:31:33 +0000827 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000828 /*Scale*/1, Start, End, Size, NeedSizeDir);
829}
830
Chad Rosier22f441a2012-10-24 22:21:50 +0000831/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000832bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
833 const MCExpr **NewDisp,
834 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +0000835 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000836 uint64_t OrigDispVal, DotDispVal;
837
838 // FIXME: Handle non-constant expressions.
839 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
840 OrigDispVal = OrigDisp->getValue();
841 } else {
842 Err = "Non-constant offsets are not supported!";
843 return true;
844 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000845
846 // Drop the '.'.
847 StringRef DotDispStr = Tok.getString().drop_front(1);
848
Chad Rosier22f441a2012-10-24 22:21:50 +0000849 // .Imm gets lexed as a real.
850 if (Tok.is(AsmToken::Real)) {
851 APInt DotDisp;
852 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000853 DotDispVal = DotDisp.getZExtValue();
Chad Rosierec130222012-10-25 21:51:10 +0000854 } else if (Tok.is(AsmToken::Identifier)) {
855 // We should only see an identifier when parsing the original inline asm.
856 // The front-end should rewrite this in terms of immediates.
857 assert (isParsingInlineAsm() && "Unexpected field name!");
858
859 unsigned DotDisp;
860 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
861 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
862 DotDisp)) {
863 Err = "Unable to lookup field reference!";
864 return true;
865 }
866 DotDispVal = DotDisp;
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000867 } else {
868 Err = "Unexpected token type!";
869 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +0000870 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000871
Chad Rosierec130222012-10-25 21:51:10 +0000872 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
873 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
874 unsigned Len = DotDispStr.size();
875 unsigned Val = OrigDispVal + DotDispVal;
876 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
877 Val));
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000878 }
879
880 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
881 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +0000882}
883
Chad Rosierc0a14b82012-10-24 17:22:29 +0000884/// Parse the 'offset' operator. This operator is used to specify the
885/// location rather then the content of a variable.
886X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
887 SMLoc OffsetOfLoc = Start;
888 Parser.Lex(); // Eat offset.
889 Start = Parser.getTok().getLoc();
890 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
891
Chad Rosier6e431572012-10-26 16:09:20 +0000892 SMLoc End;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000893 const MCExpr *Val;
894 if (getParser().ParseExpression(Val, End))
Chad Rosier7ab21c72012-10-26 18:32:44 +0000895 return ErrorOperand(Start, "Unable to parse expression!");
Chad Rosierc0a14b82012-10-24 17:22:29 +0000896
897 End = Parser.getTok().getLoc();
898
Chad Rosier6e431572012-10-26 16:09:20 +0000899 // Don't emit the offset operator.
900 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
901
Chad Rosierc0a14b82012-10-24 17:22:29 +0000902 // The offset operator will have an 'r' constraint, thus we need to create
903 // register operand to ensure proper matching. Just pick a GPR based on
904 // the size of a pointer.
905 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
906 return X86Operand::CreateReg(RegNo, Start, End, OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +0000907}
908
Chad Rosierefcb3d92012-10-26 18:04:20 +0000909/// Parse the 'TYPE' operator. The TYPE operator returns the size of a C or
910/// C++ type or variable. If the variable is an array, TYPE returns the size of
911/// a single element of the array.
912X86Operand *X86AsmParser::ParseIntelTypeOperator(SMLoc Start) {
913 SMLoc TypeLoc = Start;
914 Parser.Lex(); // Eat offset.
915 Start = Parser.getTok().getLoc();
916 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
917
918 SMLoc End;
919 const MCExpr *Val;
920 if (getParser().ParseExpression(Val, End))
921 return 0;
922
923 End = Parser.getTok().getLoc();
924
925 unsigned Size = 0;
926 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Val)) {
927 const MCSymbol &Sym = SymRef->getSymbol();
928 // FIXME: The SemaLookup will fail if the name is anything other then an
929 // identifier.
930 // FIXME: Pass a valid SMLoc.
931 if (!SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Size))
Chad Rosier7ab21c72012-10-26 18:32:44 +0000932 return ErrorOperand(Start, "Unable to lookup TYPE of expr!");
Chad Rosierefcb3d92012-10-26 18:04:20 +0000933
934 Size /= 8; // Size is in terms of bits, but we want bytes in the context.
935 }
936
937 // Rewrite the type operator and the C or C++ type or variable in terms of an
938 // immediate. E.g. TYPE foo -> $$4
939 unsigned Len = End.getPointer() - TypeLoc.getPointer();
940 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Imm, TypeLoc, Len, Size));
941
942 const MCExpr *Imm = MCConstantExpr::Create(Size, getContext());
943 return X86Operand::CreateImm(Imm, Start, End, /*NeedAsmRewrite*/false);
944}
945
Devang Pateld37ad242012-01-17 18:00:18 +0000946X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000947 SMLoc Start = Parser.getTok().getLoc(), End;
948
Chad Rosierc0a14b82012-10-24 17:22:29 +0000949 // offset operator.
Chad Rosier7ab21c72012-10-26 18:32:44 +0000950 StringRef AsmTokStr = Parser.getTok().getString();
951 if ((AsmTokStr == "offset" || AsmTokStr == "OFFSET") &&
Chad Rosierc0a14b82012-10-24 17:22:29 +0000952 isParsingInlineAsm())
953 return ParseIntelOffsetOfOperator(Start);
954
Chad Rosierefcb3d92012-10-26 18:04:20 +0000955 // Type directive.
Chad Rosier7ab21c72012-10-26 18:32:44 +0000956 if ((AsmTokStr == "type" || AsmTokStr == "TYPE") &&
Chad Rosierefcb3d92012-10-26 18:04:20 +0000957 isParsingInlineAsm())
958 return ParseIntelTypeOperator(Start);
959
Chad Rosier7ab21c72012-10-26 18:32:44 +0000960 // Unsupported directives.
961 if (isParsingIntelSyntax() &&
962 (AsmTokStr == "size" || AsmTokStr == "SIZE" ||
963 AsmTokStr == "length" || AsmTokStr == "LENGTH"))
964 return ErrorOperand(Start, "Unsupported directive!");
965
Devang Pateld37ad242012-01-17 18:00:18 +0000966 // immediate.
967 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
968 getLexer().is(AsmToken::Minus)) {
969 const MCExpr *Val;
970 if (!getParser().ParseExpression(Val, End)) {
971 End = Parser.getTok().getLoc();
972 return X86Operand::CreateImm(Val, Start, End);
973 }
974 }
975
Devang Patel0a338862012-01-12 01:36:43 +0000976 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000977 unsigned RegNo = 0;
978 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000979 // If this is a segment register followed by a ':', then this is the start
980 // of a memory reference, otherwise this is a normal register reference.
981 if (getLexer().isNot(AsmToken::Colon))
982 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
983
984 getParser().Lex(); // Eat the colon.
985 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000986 }
987
988 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000989 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000990}
991
Devang Pateldd929fc2012-01-12 18:03:40 +0000992X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000993 switch (getLexer().getKind()) {
994 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000995 // Parse a memory operand with no segment register.
996 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000997 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000998 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000999 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +00001000 SMLoc Start, End;
1001 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001002 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001003 Error(Start, "%eiz and %riz can only be used as index registers",
1004 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001005 return 0;
1006 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001007
Chris Lattnereef6d782010-04-17 18:56:34 +00001008 // If this is a segment register followed by a ':', then this is the start
1009 // of a memory reference, otherwise this is a normal register reference.
1010 if (getLexer().isNot(AsmToken::Colon))
1011 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001012
1013
Chris Lattnereef6d782010-04-17 18:56:34 +00001014 getParser().Lex(); // Eat the colon.
1015 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +00001016 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001017 case AsmToken::Dollar: {
1018 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +00001019 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +00001020 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001021 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +00001022 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +00001023 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +00001024 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001025 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001026 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +00001027}
1028
Chris Lattnereef6d782010-04-17 18:56:34 +00001029/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
1030/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +00001031X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001032
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001033 // We have to disambiguate a parenthesized expression "(4+5)" from the start
1034 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +00001035 // only way to do this without lookahead is to eat the '(' and see what is
1036 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +00001037 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001038 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +00001039 SMLoc ExprEnd;
1040 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001041
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001042 // After parsing the base expression we could either have a parenthesized
1043 // memory address or not. If not, return now. If so, eat the (.
1044 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001045 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001046 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001047 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001048 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001049 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001050
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001051 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001052 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001053 } else {
1054 // Okay, we have a '('. We don't know if this is an expression or not, but
1055 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +00001056 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001057 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001058
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001059 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001060 // Nothing to do here, fall into the code below with the '(' part of the
1061 // memory operand consumed.
1062 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001063 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001064
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001065 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001066 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +00001067 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001068
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001069 // After parsing the base expression we could either have a parenthesized
1070 // memory address or not. If not, return now. If so, eat the (.
1071 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001072 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001073 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001074 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001075 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001076 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001077
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001078 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001079 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001080 }
1081 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001082
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001083 // If we reached here, then we just ate the ( of the memory operand. Process
1084 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001085 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001086 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001087
Chris Lattner29ef9a22010-01-15 18:51:29 +00001088 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001089 SMLoc StartLoc, EndLoc;
1090 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001091 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001092 Error(StartLoc, "eiz and riz can only be used as index registers",
1093 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001094 return 0;
1095 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001096 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001097
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001098 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001099 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001100 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001101
1102 // Following the comma we should have either an index register, or a scale
1103 // value. We don't support the later form, but we want to parse it
1104 // correctly.
1105 //
1106 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001107 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001108 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001109 SMLoc L;
1110 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001111
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001112 if (getLexer().isNot(AsmToken::RParen)) {
1113 // Parse the scale amount:
1114 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001115 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001116 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001117 "expected comma in scale expression");
1118 return 0;
1119 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001120 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001121
1122 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001123 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001124
1125 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001126 if (getParser().ParseAbsoluteExpression(ScaleVal)){
1127 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001128 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001129 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001130
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001131 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001132 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1133 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1134 return 0;
1135 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001136 Scale = (unsigned)ScaleVal;
1137 }
1138 }
1139 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001140 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001141 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001142 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001143
1144 int64_t Value;
1145 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001146 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001147
Daniel Dunbaree910252010-08-24 19:13:38 +00001148 if (Value != 1)
1149 Warning(Loc, "scale factor without index register is ignored");
1150 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001151 }
1152 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001153
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001154 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001155 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001156 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001157 return 0;
1158 }
Sean Callanan18b83232010-01-19 21:44:56 +00001159 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001160 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001161
Kevin Enderby84faf652012-03-12 21:32:09 +00001162 // If we have both a base register and an index register make sure they are
1163 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001164 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001165 if (BaseReg != 0 && IndexReg != 0) {
1166 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001167 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1168 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001169 IndexReg != X86::RIZ) {
1170 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1171 return 0;
1172 }
1173 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001174 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1175 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001176 IndexReg != X86::EIZ){
1177 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1178 return 0;
1179 }
1180 }
1181
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001182 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1183 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001184}
1185
Devang Pateldd929fc2012-01-12 18:03:40 +00001186bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001187ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001188 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001189 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001190 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001191
Chris Lattnerd8f71792010-11-28 20:23:50 +00001192 // FIXME: Hack to recognize setneb as setne.
1193 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1194 PatchedName != "setb" && PatchedName != "setnb")
1195 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001196
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001197 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1198 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001199 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001200 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1201 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001202 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001203 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001204 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001205 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001206 .Case("eq", 0x00)
1207 .Case("lt", 0x01)
1208 .Case("le", 0x02)
1209 .Case("unord", 0x03)
1210 .Case("neq", 0x04)
1211 .Case("nlt", 0x05)
1212 .Case("nle", 0x06)
1213 .Case("ord", 0x07)
1214 /* AVX only from here */
1215 .Case("eq_uq", 0x08)
1216 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001217 .Case("ngt", 0x0A)
1218 .Case("false", 0x0B)
1219 .Case("neq_oq", 0x0C)
1220 .Case("ge", 0x0D)
1221 .Case("gt", 0x0E)
1222 .Case("true", 0x0F)
1223 .Case("eq_os", 0x10)
1224 .Case("lt_oq", 0x11)
1225 .Case("le_oq", 0x12)
1226 .Case("unord_s", 0x13)
1227 .Case("neq_us", 0x14)
1228 .Case("nlt_uq", 0x15)
1229 .Case("nle_uq", 0x16)
1230 .Case("ord_s", 0x17)
1231 .Case("eq_us", 0x18)
1232 .Case("nge_uq", 0x19)
1233 .Case("ngt_uq", 0x1A)
1234 .Case("false_os", 0x1B)
1235 .Case("neq_os", 0x1C)
1236 .Case("ge_oq", 0x1D)
1237 .Case("gt_oq", 0x1E)
1238 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001239 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001240 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001241 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1242 getParser().getContext());
1243 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001244 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001245 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001246 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001247 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001248 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001249 } else {
1250 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001251 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001252 }
1253 }
1254 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001255
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001256 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001257
Devang Patel885f65b2012-01-30 22:47:12 +00001258 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001259 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001260
Chris Lattner2544f422010-09-08 05:17:37 +00001261 // Determine whether this is an instruction prefix.
1262 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001263 Name == "lock" || Name == "rep" ||
1264 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001265 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001266 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001267
1268
Chris Lattner2544f422010-09-08 05:17:37 +00001269 // This does the actual operand parsing. Don't parse any more if we have a
1270 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1271 // just want to parse the "lock" as the first instruction and the "incl" as
1272 // the next one.
1273 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001274
1275 // Parse '*' modifier.
1276 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001277 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001278 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001279 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001280 }
1281
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001282 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001283 if (X86Operand *Op = ParseOperand())
1284 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001285 else {
1286 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001287 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001288 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001289
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001290 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001291 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001292
1293 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001294 if (X86Operand *Op = ParseOperand())
1295 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001296 else {
1297 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001298 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001299 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001300 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001301
Chris Lattnercbf8a982010-09-11 16:18:25 +00001302 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001303 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001304 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001305 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001306 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001307 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001308
Chris Lattner2544f422010-09-08 05:17:37 +00001309 if (getLexer().is(AsmToken::EndOfStatement))
1310 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001311 else if (isPrefix && getLexer().is(AsmToken::Slash))
1312 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001313
Devang Patel885f65b2012-01-30 22:47:12 +00001314 if (ExtraImmOp && isParsingIntelSyntax())
1315 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1316
Chris Lattner98c870f2010-11-06 19:25:43 +00001317 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1318 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1319 // documented form in various unofficial manuals, so a lot of code uses it.
1320 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1321 Operands.size() == 3) {
1322 X86Operand &Op = *(X86Operand*)Operands.back();
1323 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1324 isa<MCConstantExpr>(Op.Mem.Disp) &&
1325 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1326 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1327 SMLoc Loc = Op.getEndLoc();
1328 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1329 delete &Op;
1330 }
1331 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001332 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1333 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1334 Operands.size() == 3) {
1335 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1336 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1337 isa<MCConstantExpr>(Op.Mem.Disp) &&
1338 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1339 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1340 SMLoc Loc = Op.getEndLoc();
1341 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1342 delete &Op;
1343 }
1344 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001345 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1346 if (Name.startswith("ins") && Operands.size() == 3 &&
1347 (Name == "insb" || Name == "insw" || Name == "insl")) {
1348 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1349 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1350 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1351 Operands.pop_back();
1352 Operands.pop_back();
1353 delete &Op;
1354 delete &Op2;
1355 }
1356 }
1357
1358 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1359 if (Name.startswith("outs") && Operands.size() == 3 &&
1360 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1361 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1362 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1363 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1364 Operands.pop_back();
1365 Operands.pop_back();
1366 delete &Op;
1367 delete &Op2;
1368 }
1369 }
1370
1371 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1372 if (Name.startswith("movs") && Operands.size() == 3 &&
1373 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001374 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001375 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1376 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1377 if (isSrcOp(Op) && isDstOp(Op2)) {
1378 Operands.pop_back();
1379 Operands.pop_back();
1380 delete &Op;
1381 delete &Op2;
1382 }
1383 }
1384 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1385 if (Name.startswith("lods") && Operands.size() == 3 &&
1386 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001387 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001388 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1389 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1390 if (isSrcOp(*Op1) && Op2->isReg()) {
1391 const char *ins;
1392 unsigned reg = Op2->getReg();
1393 bool isLods = Name == "lods";
1394 if (reg == X86::AL && (isLods || Name == "lodsb"))
1395 ins = "lodsb";
1396 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1397 ins = "lodsw";
1398 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1399 ins = "lodsl";
1400 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1401 ins = "lodsq";
1402 else
1403 ins = NULL;
1404 if (ins != NULL) {
1405 Operands.pop_back();
1406 Operands.pop_back();
1407 delete Op1;
1408 delete Op2;
1409 if (Name != ins)
1410 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1411 }
1412 }
1413 }
1414 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1415 if (Name.startswith("stos") && Operands.size() == 3 &&
1416 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001417 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001418 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1419 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1420 if (isDstOp(*Op2) && Op1->isReg()) {
1421 const char *ins;
1422 unsigned reg = Op1->getReg();
1423 bool isStos = Name == "stos";
1424 if (reg == X86::AL && (isStos || Name == "stosb"))
1425 ins = "stosb";
1426 else if (reg == X86::AX && (isStos || Name == "stosw"))
1427 ins = "stosw";
1428 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1429 ins = "stosl";
1430 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1431 ins = "stosq";
1432 else
1433 ins = NULL;
1434 if (ins != NULL) {
1435 Operands.pop_back();
1436 Operands.pop_back();
1437 delete Op1;
1438 delete Op2;
1439 if (Name != ins)
1440 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1441 }
1442 }
1443 }
1444
Chris Lattnere9e16a32010-09-15 04:33:27 +00001445 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001446 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001447 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001448 Name.startswith("shl") || Name.startswith("sal") ||
1449 Name.startswith("rcl") || Name.startswith("rcr") ||
1450 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001451 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001452 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001453 // Intel syntax
1454 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1455 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001456 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1457 delete Operands[2];
1458 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001459 }
1460 } else {
1461 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1462 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001463 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1464 delete Operands[1];
1465 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001466 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001467 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001468 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001469
Chris Lattner15f89512011-04-09 19:41:05 +00001470 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1471 // instalias with an immediate operand yet.
1472 if (Name == "int" && Operands.size() == 2) {
1473 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1474 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1475 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1476 delete Operands[1];
1477 Operands.erase(Operands.begin() + 1);
1478 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1479 }
1480 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001481
Chris Lattner98986712010-01-14 22:21:20 +00001482 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001483}
1484
Devang Pateldd929fc2012-01-12 18:03:40 +00001485bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001486processInstruction(MCInst &Inst,
1487 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1488 switch (Inst.getOpcode()) {
1489 default: return false;
1490 case X86::AND16i16: {
1491 if (!Inst.getOperand(0).isImm() ||
1492 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1493 return false;
1494
1495 MCInst TmpInst;
1496 TmpInst.setOpcode(X86::AND16ri8);
1497 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1498 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1499 TmpInst.addOperand(Inst.getOperand(0));
1500 Inst = TmpInst;
1501 return true;
1502 }
1503 case X86::AND32i32: {
1504 if (!Inst.getOperand(0).isImm() ||
1505 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1506 return false;
1507
1508 MCInst TmpInst;
1509 TmpInst.setOpcode(X86::AND32ri8);
1510 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1511 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1512 TmpInst.addOperand(Inst.getOperand(0));
1513 Inst = TmpInst;
1514 return true;
1515 }
1516 case X86::AND64i32: {
1517 if (!Inst.getOperand(0).isImm() ||
1518 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1519 return false;
1520
1521 MCInst TmpInst;
1522 TmpInst.setOpcode(X86::AND64ri8);
1523 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1524 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1525 TmpInst.addOperand(Inst.getOperand(0));
1526 Inst = TmpInst;
1527 return true;
1528 }
Devang Patelac0f0482012-01-19 17:53:25 +00001529 case X86::XOR16i16: {
1530 if (!Inst.getOperand(0).isImm() ||
1531 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1532 return false;
1533
1534 MCInst TmpInst;
1535 TmpInst.setOpcode(X86::XOR16ri8);
1536 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1537 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1538 TmpInst.addOperand(Inst.getOperand(0));
1539 Inst = TmpInst;
1540 return true;
1541 }
1542 case X86::XOR32i32: {
1543 if (!Inst.getOperand(0).isImm() ||
1544 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1545 return false;
1546
1547 MCInst TmpInst;
1548 TmpInst.setOpcode(X86::XOR32ri8);
1549 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1550 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1551 TmpInst.addOperand(Inst.getOperand(0));
1552 Inst = TmpInst;
1553 return true;
1554 }
1555 case X86::XOR64i32: {
1556 if (!Inst.getOperand(0).isImm() ||
1557 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1558 return false;
1559
1560 MCInst TmpInst;
1561 TmpInst.setOpcode(X86::XOR64ri8);
1562 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1563 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1564 TmpInst.addOperand(Inst.getOperand(0));
1565 Inst = TmpInst;
1566 return true;
1567 }
1568 case X86::OR16i16: {
1569 if (!Inst.getOperand(0).isImm() ||
1570 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1571 return false;
1572
1573 MCInst TmpInst;
1574 TmpInst.setOpcode(X86::OR16ri8);
1575 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1576 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1577 TmpInst.addOperand(Inst.getOperand(0));
1578 Inst = TmpInst;
1579 return true;
1580 }
1581 case X86::OR32i32: {
1582 if (!Inst.getOperand(0).isImm() ||
1583 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1584 return false;
1585
1586 MCInst TmpInst;
1587 TmpInst.setOpcode(X86::OR32ri8);
1588 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1589 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1590 TmpInst.addOperand(Inst.getOperand(0));
1591 Inst = TmpInst;
1592 return true;
1593 }
1594 case X86::OR64i32: {
1595 if (!Inst.getOperand(0).isImm() ||
1596 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1597 return false;
1598
1599 MCInst TmpInst;
1600 TmpInst.setOpcode(X86::OR64ri8);
1601 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1602 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1603 TmpInst.addOperand(Inst.getOperand(0));
1604 Inst = TmpInst;
1605 return true;
1606 }
1607 case X86::CMP16i16: {
1608 if (!Inst.getOperand(0).isImm() ||
1609 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1610 return false;
1611
1612 MCInst TmpInst;
1613 TmpInst.setOpcode(X86::CMP16ri8);
1614 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1615 TmpInst.addOperand(Inst.getOperand(0));
1616 Inst = TmpInst;
1617 return true;
1618 }
1619 case X86::CMP32i32: {
1620 if (!Inst.getOperand(0).isImm() ||
1621 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1622 return false;
1623
1624 MCInst TmpInst;
1625 TmpInst.setOpcode(X86::CMP32ri8);
1626 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1627 TmpInst.addOperand(Inst.getOperand(0));
1628 Inst = TmpInst;
1629 return true;
1630 }
1631 case X86::CMP64i32: {
1632 if (!Inst.getOperand(0).isImm() ||
1633 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1634 return false;
1635
1636 MCInst TmpInst;
1637 TmpInst.setOpcode(X86::CMP64ri8);
1638 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1639 TmpInst.addOperand(Inst.getOperand(0));
1640 Inst = TmpInst;
1641 return true;
1642 }
Devang Patela951f772012-01-19 18:40:55 +00001643 case X86::ADD16i16: {
1644 if (!Inst.getOperand(0).isImm() ||
1645 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1646 return false;
1647
1648 MCInst TmpInst;
1649 TmpInst.setOpcode(X86::ADD16ri8);
1650 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1651 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1652 TmpInst.addOperand(Inst.getOperand(0));
1653 Inst = TmpInst;
1654 return true;
1655 }
1656 case X86::ADD32i32: {
1657 if (!Inst.getOperand(0).isImm() ||
1658 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1659 return false;
1660
1661 MCInst TmpInst;
1662 TmpInst.setOpcode(X86::ADD32ri8);
1663 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1664 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1665 TmpInst.addOperand(Inst.getOperand(0));
1666 Inst = TmpInst;
1667 return true;
1668 }
1669 case X86::ADD64i32: {
1670 if (!Inst.getOperand(0).isImm() ||
1671 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1672 return false;
1673
1674 MCInst TmpInst;
1675 TmpInst.setOpcode(X86::ADD64ri8);
1676 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1677 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1678 TmpInst.addOperand(Inst.getOperand(0));
1679 Inst = TmpInst;
1680 return true;
1681 }
1682 case X86::SUB16i16: {
1683 if (!Inst.getOperand(0).isImm() ||
1684 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1685 return false;
1686
1687 MCInst TmpInst;
1688 TmpInst.setOpcode(X86::SUB16ri8);
1689 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1690 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1691 TmpInst.addOperand(Inst.getOperand(0));
1692 Inst = TmpInst;
1693 return true;
1694 }
1695 case X86::SUB32i32: {
1696 if (!Inst.getOperand(0).isImm() ||
1697 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1698 return false;
1699
1700 MCInst TmpInst;
1701 TmpInst.setOpcode(X86::SUB32ri8);
1702 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1703 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1704 TmpInst.addOperand(Inst.getOperand(0));
1705 Inst = TmpInst;
1706 return true;
1707 }
1708 case X86::SUB64i32: {
1709 if (!Inst.getOperand(0).isImm() ||
1710 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1711 return false;
1712
1713 MCInst TmpInst;
1714 TmpInst.setOpcode(X86::SUB64ri8);
1715 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1716 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1717 TmpInst.addOperand(Inst.getOperand(0));
1718 Inst = TmpInst;
1719 return true;
1720 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001721 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001722}
1723
1724bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001725MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001726 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001727 MCStreamer &Out, unsigned &ErrorInfo,
1728 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001729 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001730 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1731 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001732 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001733
Chris Lattner7c51a312010-09-29 01:50:45 +00001734 // First, handle aliases that expand to multiple instructions.
1735 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001736 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001737 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001738 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001739 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001740 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001741 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001742 MCInst Inst;
1743 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001744 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001745 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001746 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001747
Chris Lattner0bb83a82010-09-30 16:39:29 +00001748 const char *Repl =
1749 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001750 .Case("finit", "fninit")
1751 .Case("fsave", "fnsave")
1752 .Case("fstcw", "fnstcw")
1753 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001754 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001755 .Case("fstsw", "fnstsw")
1756 .Case("fstsww", "fnstsw")
1757 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001758 .Default(0);
1759 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001760 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001761 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001762 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001763
Chris Lattnera008e8a2010-09-06 21:54:15 +00001764 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001765 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001766
Daniel Dunbarc918d602010-05-04 16:12:42 +00001767 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001768 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001769 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001770 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001771 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001772 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001773 // Some instructions need post-processing to, for example, tweak which
1774 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001775 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001776 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001777 while (processInstruction(Inst, Operands))
1778 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001779
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001780 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001781 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001782 Out.EmitInstruction(Inst);
1783 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001784 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001785 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001786 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001787 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001788 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001789 case Match_InvalidOperand:
1790 WasOriginallyInvalidOperand = true;
1791 break;
1792 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001793 break;
1794 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001795
Daniel Dunbarc918d602010-05-04 16:12:42 +00001796 // FIXME: Ideally, we would only attempt suffix matches for things which are
1797 // valid prefixes, and we could just infer the right unambiguous
1798 // type. However, that requires substantially more matcher support than the
1799 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001800
Daniel Dunbarc918d602010-05-04 16:12:42 +00001801 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001802 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001803 SmallString<16> Tmp;
1804 Tmp += Base;
1805 Tmp += ' ';
1806 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001807
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001808 // If this instruction starts with an 'f', then it is a floating point stack
1809 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1810 // 80-bit floating point, which use the suffixes s,l,t respectively.
1811 //
1812 // Otherwise, we assume that this may be an integer instruction, which comes
1813 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1814 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001815
Daniel Dunbarc918d602010-05-04 16:12:42 +00001816 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001817 Tmp[Base.size()] = Suffixes[0];
1818 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001819 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001820
Chad Rosier6e006d32012-10-12 22:53:36 +00001821 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1822 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001823 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001824 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1825 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001826 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001827 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1828 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001829 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001830 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1831 isParsingIntelSyntax());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001832
1833 // Restore the old token.
1834 Op->setTokenValue(Base);
1835
1836 // If exactly one matched, then we treat that as a successful match (and the
1837 // instruction will already have been filled in correctly, since the failing
1838 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001839 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001840 (Match1 == Match_Success) + (Match2 == Match_Success) +
1841 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001842 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001843 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001844 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001845 Out.EmitInstruction(Inst);
1846 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001847 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001848 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001849
Chris Lattnerec6789f2010-09-06 20:08:02 +00001850 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001851
Daniel Dunbar09062b12010-08-12 00:55:42 +00001852 // If we had multiple suffix matches, then identify this as an ambiguous
1853 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001854 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001855 char MatchChars[4];
1856 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001857 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1858 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1859 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1860 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001861
1862 SmallString<126> Msg;
1863 raw_svector_ostream OS(Msg);
1864 OS << "ambiguous instructions require an explicit suffix (could be ";
1865 for (unsigned i = 0; i != NumMatches; ++i) {
1866 if (i != 0)
1867 OS << ", ";
1868 if (i + 1 == NumMatches)
1869 OS << "or ";
1870 OS << "'" << Base << MatchChars[i] << "'";
1871 }
1872 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001873 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001874 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001875 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001876
Chris Lattnera008e8a2010-09-06 21:54:15 +00001877 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001878
Chris Lattnera008e8a2010-09-06 21:54:15 +00001879 // If all of the instructions reported an invalid mnemonic, then the original
1880 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001881 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1882 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001883 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001884 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001885 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001886 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001887 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001888 }
1889
1890 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001891 if (ErrorInfo != ~0U) {
1892 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001893 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001894 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001895
Chad Rosier84125ca2012-10-13 00:26:04 +00001896 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001897 if (Operand->getStartLoc().isValid()) {
1898 SMRange OperandRange = Operand->getLocRange();
1899 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001900 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001901 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001902 }
1903
Chad Rosierb4fdade2012-08-21 19:36:59 +00001904 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001905 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001906 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001907
Chris Lattnerec6789f2010-09-06 20:08:02 +00001908 // If one instruction matched with a missing feature, report this as a
1909 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001910 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1911 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001912 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001913 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001914 return true;
1915 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001916
Chris Lattnera008e8a2010-09-06 21:54:15 +00001917 // If one instruction matched with an invalid operand, report this as an
1918 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001919 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1920 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001921 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001922 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001923 return true;
1924 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001925
Chris Lattnerec6789f2010-09-06 20:08:02 +00001926 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001927 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001928 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001929 return true;
1930}
1931
1932
Devang Pateldd929fc2012-01-12 18:03:40 +00001933bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001934 StringRef IDVal = DirectiveID.getIdentifier();
1935 if (IDVal == ".word")
1936 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001937 else if (IDVal.startswith(".code"))
1938 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001939 else if (IDVal.startswith(".att_syntax")) {
1940 getParser().setAssemblerDialect(0);
1941 return false;
1942 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001943 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001944 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1945 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001946 // FIXME : Handle noprefix
1947 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001948 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001949 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001950 }
1951 return false;
1952 }
Chris Lattner537ca842010-10-30 17:38:55 +00001953 return true;
1954}
1955
1956/// ParseDirectiveWord
1957/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001958bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001959 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1960 for (;;) {
1961 const MCExpr *Value;
1962 if (getParser().ParseExpression(Value))
1963 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001964
Chris Lattner537ca842010-10-30 17:38:55 +00001965 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001966
Chris Lattner537ca842010-10-30 17:38:55 +00001967 if (getLexer().is(AsmToken::EndOfStatement))
1968 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001969
Chris Lattner537ca842010-10-30 17:38:55 +00001970 // FIXME: Improve diagnostic.
1971 if (getLexer().isNot(AsmToken::Comma))
1972 return Error(L, "unexpected token in directive");
1973 Parser.Lex();
1974 }
1975 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001976
Chris Lattner537ca842010-10-30 17:38:55 +00001977 Parser.Lex();
1978 return false;
1979}
1980
Evan Chengbd27f5a2011-07-27 00:38:12 +00001981/// ParseDirectiveCode
1982/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001983bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001984 if (IDVal == ".code32") {
1985 Parser.Lex();
1986 if (is64BitMode()) {
1987 SwitchMode();
1988 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1989 }
1990 } else if (IDVal == ".code64") {
1991 Parser.Lex();
1992 if (!is64BitMode()) {
1993 SwitchMode();
1994 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1995 }
1996 } else {
1997 return Error(L, "unexpected directive " + IDVal);
1998 }
Chris Lattner537ca842010-10-30 17:38:55 +00001999
Evan Chengbd27f5a2011-07-27 00:38:12 +00002000 return false;
2001}
Chris Lattner537ca842010-10-30 17:38:55 +00002002
2003
Sean Callanane88f5522010-01-23 02:43:15 +00002004extern "C" void LLVMInitializeX86AsmLexer();
2005
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002006// Force static initialization.
2007extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00002008 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
2009 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00002010 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00002011}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002012
Chris Lattner0692ee62010-09-06 19:11:01 +00002013#define GET_REGISTER_MATCHER
2014#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00002015#include "X86GenAsmMatcher.inc"