blob: 28cf5d3e6a2e54f1608fea838094938943dbd5e9 [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"
Daniel Dunbara027d222009-07-31 02:32:59 +000014#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000015#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000016#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000017#include "llvm/MC/MCParser/MCAsmLexer.h"
18#include "llvm/MC/MCParser/MCAsmParser.h"
19#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000020#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000022#include "llvm/ADT/StringSwitch.h"
23#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000024#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000025#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000026#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000027
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000028using namespace llvm;
29
30namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000031struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000032
Devang Pateldd929fc2012-01-12 18:03:40 +000033class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000034 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000035 MCAsmParser &Parser;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &getParser() const { return Parser; }
38
39 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
40
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000041 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000042 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
43 bool matchingInlineAsm = false) {
44 if (matchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000045 return Parser.Error(L, Msg, Ranges);
46 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000047
Devang Pateld37ad242012-01-17 18:00:18 +000048 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
49 Error(Loc, Msg);
50 return 0;
51 }
52
Chris Lattner309264d2010-01-15 18:44:13 +000053 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000054 X86Operand *ParseATTOperand();
55 X86Operand *ParseIntelOperand();
Devang Pateld37ad242012-01-17 18:00:18 +000056 X86Operand *ParseIntelMemOperand();
Devang Patel7c64fe62012-01-23 18:31:58 +000057 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000058 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000059
60 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000061 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000062
Devang Patelb8ba13f2012-01-18 22:42:29 +000063 bool processInstruction(MCInst &Inst,
64 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
65
Chris Lattner7036f8b2010-09-29 01:42:58 +000066 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000067 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000068 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000069
Chad Rosierc4d25602012-09-03 03:16:09 +000070 bool MatchInstruction(SMLoc IDLoc, unsigned &Kind,
Chad Rosier32461762012-08-09 22:04:55 +000071 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier64bfcbb2012-08-21 18:14:59 +000072 SmallVectorImpl<MCInst> &MCInsts,
Chad Rosierb4fdade2012-08-21 19:36:59 +000073 unsigned &OrigErrorInfo,
74 bool matchingInlineAsm = false);
Chad Rosier32461762012-08-09 22:04:55 +000075
Chad Rosier5d637d72012-09-05 01:15:43 +000076 unsigned getMCInstOperandNum(unsigned Kind, MCInst &Inst,
Chad Rosier038f3e32012-09-03 18:47:45 +000077 const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier2cc97de2012-09-03 20:31:23 +000078 unsigned OperandNum, unsigned &NumMCOperands) {
Chad Rosier5d637d72012-09-05 01:15:43 +000079 return getMCInstOperandNumImpl(Kind, Inst, Operands, OperandNum,
Chad Rosier2cc97de2012-09-03 20:31:23 +000080 NumMCOperands);
Chad Rosier038f3e32012-09-03 18:47:45 +000081 }
82
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000083 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000084 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000085 bool isSrcOp(X86Operand &Op);
86
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000087 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
88 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000089 bool isDstOp(X86Operand &Op);
90
Evan Cheng59ee62d2011-07-11 03:57:24 +000091 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000092 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000093 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000094 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000095 void SwitchMode() {
96 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
97 setAvailableFeatures(FB);
98 }
Evan Chengebdeeab2011-07-08 01:53:10 +000099
Daniel Dunbar54074b52010-07-19 05:44:09 +0000100 /// @name Auto-generated Matcher Functions
101 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000102
Chris Lattner0692ee62010-09-06 19:11:01 +0000103#define GET_ASSEMBLER_HEADER
104#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000105
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000106 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000107
108public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000109 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +0000110 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000111
Daniel Dunbar54074b52010-07-19 05:44:09 +0000112 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000113 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000114 }
Roman Divackybf755322011-01-27 17:14:22 +0000115 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000116
Benjamin Kramer38e59892010-07-14 22:38:02 +0000117 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000118 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000119
120 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000121
122 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000123 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000124 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000125};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000126} // end anonymous namespace
127
Sean Callanane9b466d2010-01-23 00:40:33 +0000128/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000129/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000130
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000131static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000132
133/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000134
Craig Topper76bd9382012-07-18 04:59:16 +0000135static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000136 return (( Value <= 0x000000000000007FULL)||
137 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
138 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
139}
140
141static bool isImmSExti32i8Value(uint64_t Value) {
142 return (( Value <= 0x000000000000007FULL)||
143 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
144 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
145}
146
147static bool isImmZExtu32u8Value(uint64_t Value) {
148 return (Value <= 0x00000000000000FFULL);
149}
150
151static bool isImmSExti64i8Value(uint64_t Value) {
152 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000153 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000154}
155
156static bool isImmSExti64i32Value(uint64_t Value) {
157 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000158 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000159}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000160namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000161
162/// X86Operand - Instances of this class represent a parsed X86 machine
163/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000164struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000165 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000166 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000167 Register,
168 Immediate,
169 Memory
170 } Kind;
171
Chris Lattner29ef9a22010-01-15 18:51:29 +0000172 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000173
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000174 union {
175 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000176 const char *Data;
177 unsigned Length;
178 } Tok;
179
180 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000181 unsigned RegNo;
182 } Reg;
183
184 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000185 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000186 } Imm;
187
188 struct {
189 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000190 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000191 unsigned BaseReg;
192 unsigned IndexReg;
193 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000194 unsigned Size;
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); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000208
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000209 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000210
Daniel Dunbar20927f22009-08-07 08:26:05 +0000211 StringRef getToken() const {
212 assert(Kind == Token && "Invalid access!");
213 return StringRef(Tok.Data, Tok.Length);
214 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000215 void setTokenValue(StringRef Value) {
216 assert(Kind == Token && "Invalid access!");
217 Tok.Data = Value.data();
218 Tok.Length = Value.size();
219 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000220
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000221 unsigned getReg() const {
222 assert(Kind == Register && "Invalid access!");
223 return Reg.RegNo;
224 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000225
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000226 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000227 assert(Kind == Immediate && "Invalid access!");
228 return Imm.Val;
229 }
230
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000231 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000232 assert(Kind == Memory && "Invalid access!");
233 return Mem.Disp;
234 }
235 unsigned getMemSegReg() const {
236 assert(Kind == Memory && "Invalid access!");
237 return Mem.SegReg;
238 }
239 unsigned getMemBaseReg() const {
240 assert(Kind == Memory && "Invalid access!");
241 return Mem.BaseReg;
242 }
243 unsigned getMemIndexReg() const {
244 assert(Kind == Memory && "Invalid access!");
245 return Mem.IndexReg;
246 }
247 unsigned getMemScale() const {
248 assert(Kind == Memory && "Invalid access!");
249 return Mem.Scale;
250 }
251
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000252 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000253
254 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000255
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000256 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000257 if (!isImm())
258 return false;
259
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000260 // If this isn't a constant expr, just assume it fits and let relaxation
261 // handle it.
262 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
263 if (!CE)
264 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000265
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000266 // Otherwise, check the value is in a range that makes sense for this
267 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000268 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000269 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000270 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000271 if (!isImm())
272 return false;
273
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000274 // If this isn't a constant expr, just assume it fits and let relaxation
275 // handle it.
276 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
277 if (!CE)
278 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000279
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000280 // Otherwise, check the value is in a range that makes sense for this
281 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000282 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000283 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000284 bool isImmZExtu32u8() const {
285 if (!isImm())
286 return false;
287
288 // If this isn't a constant expr, just assume it fits and let relaxation
289 // handle it.
290 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
291 if (!CE)
292 return true;
293
294 // Otherwise, check the value is in a range that makes sense for this
295 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000296 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000297 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000298 bool isImmSExti64i8() const {
299 if (!isImm())
300 return false;
301
302 // If this isn't a constant expr, just assume it fits and let relaxation
303 // handle it.
304 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
305 if (!CE)
306 return true;
307
308 // Otherwise, check the value is in a range that makes sense for this
309 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000310 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000311 }
312 bool isImmSExti64i32() const {
313 if (!isImm())
314 return false;
315
316 // If this isn't a constant expr, just assume it fits and let relaxation
317 // handle it.
318 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
319 if (!CE)
320 return true;
321
322 // Otherwise, check the value is in a range that makes sense for this
323 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000324 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000325 }
326
Daniel Dunbar20927f22009-08-07 08:26:05 +0000327 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000328 bool isMem8() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000329 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
330 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000331 bool isMem16() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000332 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
333 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000334 bool isMem32() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000335 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
336 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000337 bool isMem64() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000338 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
339 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000340 bool isMem80() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000341 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
342 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000343 bool isMem128() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000344 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
345 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000346 bool isMem256() const {
Devang Patelc59d9df2012-01-12 01:51:42 +0000347 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
348 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000349
Craig Topper75dc33a2012-07-18 04:11:12 +0000350 bool isMemVX32() const {
351 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
352 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
353 }
354 bool isMemVY32() const {
355 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
356 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
357 }
358 bool isMemVX64() const {
359 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
360 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
361 }
362 bool isMemVY64() const {
363 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
364 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
365 }
366
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000367 bool isAbsMem() const {
368 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000369 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000370 }
371
Daniel Dunbar20927f22009-08-07 08:26:05 +0000372 bool isReg() const { return Kind == Register; }
373
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000374 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
375 // Add as immediates when possible.
376 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
377 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
378 else
379 Inst.addOperand(MCOperand::CreateExpr(Expr));
380 }
381
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000382 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000383 assert(N == 1 && "Invalid number of operands!");
384 Inst.addOperand(MCOperand::CreateReg(getReg()));
385 }
386
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000387 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000388 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000389 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000390 }
391
Chad Rosier36b8fed2012-06-27 22:34:28 +0000392 void addMem8Operands(MCInst &Inst, unsigned N) const {
393 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000394 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000395 void addMem16Operands(MCInst &Inst, unsigned N) const {
396 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000397 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000398 void addMem32Operands(MCInst &Inst, unsigned N) const {
399 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000400 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000401 void addMem64Operands(MCInst &Inst, unsigned N) const {
402 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000403 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000404 void addMem80Operands(MCInst &Inst, unsigned N) const {
405 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000406 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000407 void addMem128Operands(MCInst &Inst, unsigned N) const {
408 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000409 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000410 void addMem256Operands(MCInst &Inst, unsigned N) const {
411 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000412 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000413 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
414 addMemOperands(Inst, N);
415 }
416 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
417 addMemOperands(Inst, N);
418 }
419 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
420 addMemOperands(Inst, N);
421 }
422 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
423 addMemOperands(Inst, N);
424 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000425
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000426 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000427 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000428 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
429 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
430 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000431 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000432 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
433 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000434
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000435 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
436 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000437 // Add as immediates when possible.
438 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
439 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
440 else
441 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000442 }
443
Chris Lattnerb4307b32010-01-15 19:28:38 +0000444 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000445 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
446 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000447 Res->Tok.Data = Str.data();
448 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000449 return Res;
450 }
451
Chris Lattner29ef9a22010-01-15 18:51:29 +0000452 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000453 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000454 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000455 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000456 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000457
Chris Lattnerb4307b32010-01-15 19:28:38 +0000458 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
459 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000460 Res->Imm.Val = Val;
461 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000462 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000463
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000464 /// Create an absolute memory operand.
465 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000466 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000467 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
468 Res->Mem.SegReg = 0;
469 Res->Mem.Disp = Disp;
470 Res->Mem.BaseReg = 0;
471 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000472 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000473 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000474 return Res;
475 }
476
477 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000478 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
479 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000480 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
481 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000482 // We should never just have a displacement, that should be parsed as an
483 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000484 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
485
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000486 // The scale should always be one of {1,2,4,8}.
487 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000488 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000489 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000490 Res->Mem.SegReg = SegReg;
491 Res->Mem.Disp = Disp;
492 Res->Mem.BaseReg = BaseReg;
493 Res->Mem.IndexReg = IndexReg;
494 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000495 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000496 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000497 }
498};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000499
Chris Lattner37dfdec2009-07-29 06:33:53 +0000500} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000501
Devang Pateldd929fc2012-01-12 18:03:40 +0000502bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000503 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000504
505 return (Op.isMem() &&
506 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
507 isa<MCConstantExpr>(Op.Mem.Disp) &&
508 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
509 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
510}
511
Devang Pateldd929fc2012-01-12 18:03:40 +0000512bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000513 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000514
Chad Rosier36b8fed2012-06-27 22:34:28 +0000515 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000516 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000517 isa<MCConstantExpr>(Op.Mem.Disp) &&
518 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
519 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
520}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000521
Devang Pateldd929fc2012-01-12 18:03:40 +0000522bool X86AsmParser::ParseRegister(unsigned &RegNo,
523 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000524 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000525 const AsmToken &PercentTok = Parser.getTok();
526 StartLoc = PercentTok.getLoc();
527
528 // If we encounter a %, ignore it. This code handles registers with and
529 // without the prefix, unprefixed registers can occur in cfi directives.
530 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000531 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000532
Sean Callanan18b83232010-01-19 21:44:56 +0000533 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000534 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000535 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000536 return Error(StartLoc, "invalid register name",
537 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000538 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000539
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000540 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000541
Chris Lattner33d60d52010-09-22 04:11:10 +0000542 // If the match failed, try the register name as lowercase.
543 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000544 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000545
Evan Cheng5de728c2011-07-27 23:22:03 +0000546 if (!is64BitMode()) {
547 // FIXME: This should be done using Requires<In32BitMode> and
548 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
549 // checked.
550 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
551 // REX prefix.
552 if (RegNo == X86::RIZ ||
553 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
554 X86II::isX86_64NonExtLowByteReg(RegNo) ||
555 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000556 return Error(StartLoc, "register %"
557 + Tok.getString() + " is only available in 64-bit mode",
558 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000559 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000560
Chris Lattner33d60d52010-09-22 04:11:10 +0000561 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
562 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000563 RegNo = X86::ST0;
564 EndLoc = Tok.getLoc();
565 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000566
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000567 // Check to see if we have '(4)' after %st.
568 if (getLexer().isNot(AsmToken::LParen))
569 return false;
570 // Lex the paren.
571 getParser().Lex();
572
573 const AsmToken &IntTok = Parser.getTok();
574 if (IntTok.isNot(AsmToken::Integer))
575 return Error(IntTok.getLoc(), "expected stack index");
576 switch (IntTok.getIntVal()) {
577 case 0: RegNo = X86::ST0; break;
578 case 1: RegNo = X86::ST1; break;
579 case 2: RegNo = X86::ST2; break;
580 case 3: RegNo = X86::ST3; break;
581 case 4: RegNo = X86::ST4; break;
582 case 5: RegNo = X86::ST5; break;
583 case 6: RegNo = X86::ST6; break;
584 case 7: RegNo = X86::ST7; break;
585 default: return Error(IntTok.getLoc(), "invalid stack index");
586 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000587
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000588 if (getParser().Lex().isNot(AsmToken::RParen))
589 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000590
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000591 EndLoc = Tok.getLoc();
592 Parser.Lex(); // Eat ')'
593 return false;
594 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000595
Chris Lattner645b2092010-06-24 07:29:18 +0000596 // If this is "db[0-7]", match it as an alias
597 // for dr[0-7].
598 if (RegNo == 0 && Tok.getString().size() == 3 &&
599 Tok.getString().startswith("db")) {
600 switch (Tok.getString()[2]) {
601 case '0': RegNo = X86::DR0; break;
602 case '1': RegNo = X86::DR1; break;
603 case '2': RegNo = X86::DR2; break;
604 case '3': RegNo = X86::DR3; break;
605 case '4': RegNo = X86::DR4; break;
606 case '5': RegNo = X86::DR5; break;
607 case '6': RegNo = X86::DR6; break;
608 case '7': RegNo = X86::DR7; break;
609 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000610
Chris Lattner645b2092010-06-24 07:29:18 +0000611 if (RegNo != 0) {
612 EndLoc = Tok.getLoc();
613 Parser.Lex(); // Eat it.
614 return false;
615 }
616 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000617
Devang Patel1aea4302012-01-20 22:32:05 +0000618 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000619 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000620 return Error(StartLoc, "invalid register name",
621 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000622 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000623
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000624 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000625 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000626 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000627}
628
Devang Pateldd929fc2012-01-12 18:03:40 +0000629X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000630 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000631 return ParseIntelOperand();
632 return ParseATTOperand();
633}
634
Devang Pateld37ad242012-01-17 18:00:18 +0000635/// getIntelMemOperandSize - Return intel memory operand size.
636static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000637 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000638 .Cases("BYTE", "byte", 8)
639 .Cases("WORD", "word", 16)
640 .Cases("DWORD", "dword", 32)
641 .Cases("QWORD", "qword", 64)
642 .Cases("XWORD", "xword", 80)
643 .Cases("XMMWORD", "xmmword", 128)
644 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000645 .Default(0);
646 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000647}
648
Devang Patel7c64fe62012-01-23 18:31:58 +0000649X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
650 unsigned Size) {
651 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000652 SMLoc Start = Parser.getTok().getLoc(), End;
653
Devang Pateld37ad242012-01-17 18:00:18 +0000654 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
655 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
656
657 // Eat '['
658 if (getLexer().isNot(AsmToken::LBrac))
659 return ErrorOperand(Start, "Expected '[' token!");
660 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000661
Devang Pateld37ad242012-01-17 18:00:18 +0000662 if (getLexer().is(AsmToken::Identifier)) {
663 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000664 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000665 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000666 if (getParser().ParseExpression(Disp, End)) return 0;
667 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000668 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000669 Parser.Lex();
670 return X86Operand::CreateMem(Disp, Start, End, Size);
671 }
672 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000673 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000674 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000675 SMLoc Loc = Parser.getTok().getLoc();
676 if (getLexer().is(AsmToken::RBrac)) {
677 // Handle '[' number ']'
678 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000679 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
680 if (SegReg)
681 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
682 Start, End, Size);
683 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000684 } else if (getLexer().is(AsmToken::Star)) {
685 // Handle '[' Scale*IndexReg ']'
686 Parser.Lex();
687 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000688 if (ParseRegister(IndexReg, IdxRegLoc, End))
689 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000690 Scale = Val;
691 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000692 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000693 }
694
695 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
696 bool isPlus = getLexer().is(AsmToken::Plus);
697 Parser.Lex();
698 SMLoc PlusLoc = Parser.getTok().getLoc();
699 if (getLexer().is(AsmToken::Integer)) {
700 int64_t Val = Parser.getTok().getIntVal();
701 Parser.Lex();
702 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000703 Parser.Lex();
704 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000705 if (ParseRegister(IndexReg, IdxRegLoc, End))
706 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000707 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000708 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000709 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000710 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000711 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000712 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000713 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000714 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000715 End = Parser.getTok().getLoc();
716 if (!IndexReg)
717 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000718 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000719 }
Devang Pateld37ad242012-01-17 18:00:18 +0000720 }
721
722 if (getLexer().isNot(AsmToken::RBrac))
723 if (getParser().ParseExpression(Disp, End)) return 0;
724
725 End = Parser.getTok().getLoc();
726 if (getLexer().isNot(AsmToken::RBrac))
727 return ErrorOperand(End, "expected ']' token!");
728 Parser.Lex();
729 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000730
731 // handle [-42]
732 if (!BaseReg && !IndexReg)
733 return X86Operand::CreateMem(Disp, Start, End, Size);
734
Devang Pateld37ad242012-01-17 18:00:18 +0000735 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000736 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000737}
738
739/// ParseIntelMemOperand - Parse intel style memory operand.
740X86Operand *X86AsmParser::ParseIntelMemOperand() {
741 const AsmToken &Tok = Parser.getTok();
742 SMLoc Start = Parser.getTok().getLoc(), End;
Devang Patel7c64fe62012-01-23 18:31:58 +0000743 unsigned SegReg = 0;
Devang Pateld37ad242012-01-17 18:00:18 +0000744
745 unsigned Size = getIntelMemOperandSize(Tok.getString());
746 if (Size) {
747 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000748 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
749 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000750 Parser.Lex();
751 }
752
753 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000754 return ParseIntelBracExpression(SegReg, Size);
755
756 if (!ParseRegister(SegReg, Start, End)) {
757 // Handel SegReg : [ ... ]
758 if (getLexer().isNot(AsmToken::Colon))
759 return ErrorOperand(Start, "Expected ':' token!");
760 Parser.Lex(); // Eat :
761 if (getLexer().isNot(AsmToken::LBrac))
762 return ErrorOperand(Start, "Expected '[' token!");
763 return ParseIntelBracExpression(SegReg, Size);
764 }
Devang Pateld37ad242012-01-17 18:00:18 +0000765
766 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
767 if (getParser().ParseExpression(Disp, End)) return 0;
768 return X86Operand::CreateMem(Disp, Start, End, Size);
769}
770
771X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000772 SMLoc Start = Parser.getTok().getLoc(), End;
773
774 // immediate.
775 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
776 getLexer().is(AsmToken::Minus)) {
777 const MCExpr *Val;
778 if (!getParser().ParseExpression(Val, End)) {
779 End = Parser.getTok().getLoc();
780 return X86Operand::CreateImm(Val, Start, End);
781 }
782 }
783
Devang Patel0a338862012-01-12 01:36:43 +0000784 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000785 unsigned RegNo = 0;
786 if (!ParseRegister(RegNo, Start, End)) {
Devang Patel0a338862012-01-12 01:36:43 +0000787 End = Parser.getTok().getLoc();
788 return X86Operand::CreateReg(RegNo, Start, End);
789 }
790
791 // mem operand
Devang Pateld37ad242012-01-17 18:00:18 +0000792 return ParseIntelMemOperand();
Devang Patel0a338862012-01-12 01:36:43 +0000793}
794
Devang Pateldd929fc2012-01-12 18:03:40 +0000795X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000796 switch (getLexer().getKind()) {
797 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000798 // Parse a memory operand with no segment register.
799 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000800 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000801 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000802 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000803 SMLoc Start, End;
804 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000805 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000806 Error(Start, "%eiz and %riz can only be used as index registers",
807 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000808 return 0;
809 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000810
Chris Lattnereef6d782010-04-17 18:56:34 +0000811 // If this is a segment register followed by a ':', then this is the start
812 // of a memory reference, otherwise this is a normal register reference.
813 if (getLexer().isNot(AsmToken::Colon))
814 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000815
816
Chris Lattnereef6d782010-04-17 18:56:34 +0000817 getParser().Lex(); // Eat the colon.
818 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000819 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000820 case AsmToken::Dollar: {
821 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000822 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000823 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000824 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000825 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000826 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000827 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000828 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000829 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000830}
831
Chris Lattnereef6d782010-04-17 18:56:34 +0000832/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
833/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000834X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000835
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000836 // We have to disambiguate a parenthesized expression "(4+5)" from the start
837 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000838 // only way to do this without lookahead is to eat the '(' and see what is
839 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000840 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000841 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000842 SMLoc ExprEnd;
843 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000844
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000845 // After parsing the base expression we could either have a parenthesized
846 // memory address or not. If not, return now. If so, eat the (.
847 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000848 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000849 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000850 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000851 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000852 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000853
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000854 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000855 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000856 } else {
857 // Okay, we have a '('. We don't know if this is an expression or not, but
858 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000859 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000860 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000861
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000862 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000863 // Nothing to do here, fall into the code below with the '(' part of the
864 // memory operand consumed.
865 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000866 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000867
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000868 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000869 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000870 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000871
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000872 // After parsing the base expression we could either have a parenthesized
873 // memory address or not. If not, return now. If so, eat the (.
874 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000875 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000876 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000877 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000878 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000879 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000880
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000881 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000882 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000883 }
884 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000885
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000886 // If we reached here, then we just ate the ( of the memory operand. Process
887 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000888 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +0000889 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000890
Chris Lattner29ef9a22010-01-15 18:51:29 +0000891 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000892 SMLoc StartLoc, EndLoc;
893 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000894 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000895 Error(StartLoc, "eiz and riz can only be used as index registers",
896 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000897 return 0;
898 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000899 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000900
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000901 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000902 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +0000903 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000904
905 // Following the comma we should have either an index register, or a scale
906 // value. We don't support the later form, but we want to parse it
907 // correctly.
908 //
909 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000910 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000911 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000912 SMLoc L;
913 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000914
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000915 if (getLexer().isNot(AsmToken::RParen)) {
916 // Parse the scale amount:
917 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000918 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000919 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000920 "expected comma in scale expression");
921 return 0;
922 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000923 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000924
925 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000926 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000927
928 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +0000929 if (getParser().ParseAbsoluteExpression(ScaleVal)){
930 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +0000931 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +0000932 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000933
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000934 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000935 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
936 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
937 return 0;
938 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000939 Scale = (unsigned)ScaleVal;
940 }
941 }
942 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000943 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000944 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000945 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000946
947 int64_t Value;
948 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000949 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000950
Daniel Dunbaree910252010-08-24 19:13:38 +0000951 if (Value != 1)
952 Warning(Loc, "scale factor without index register is ignored");
953 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000954 }
955 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000956
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000957 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000958 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000959 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000960 return 0;
961 }
Sean Callanan18b83232010-01-19 21:44:56 +0000962 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000963 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000964
Kevin Enderby84faf652012-03-12 21:32:09 +0000965 // If we have both a base register and an index register make sure they are
966 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +0000967 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +0000968 if (BaseReg != 0 && IndexReg != 0) {
969 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000970 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
971 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000972 IndexReg != X86::RIZ) {
973 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
974 return 0;
975 }
976 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +0000977 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
978 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +0000979 IndexReg != X86::EIZ){
980 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
981 return 0;
982 }
983 }
984
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000985 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
986 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000987}
988
Devang Pateldd929fc2012-01-12 18:03:40 +0000989bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000990ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000991 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000992 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000993
Chris Lattnerd8f71792010-11-28 20:23:50 +0000994 // FIXME: Hack to recognize setneb as setne.
995 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
996 PatchedName != "setb" && PatchedName != "setnb")
997 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000998
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000999 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1000 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001001 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001002 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1003 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001004 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001005 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001006 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001007 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001008 .Case("eq", 0x00)
1009 .Case("lt", 0x01)
1010 .Case("le", 0x02)
1011 .Case("unord", 0x03)
1012 .Case("neq", 0x04)
1013 .Case("nlt", 0x05)
1014 .Case("nle", 0x06)
1015 .Case("ord", 0x07)
1016 /* AVX only from here */
1017 .Case("eq_uq", 0x08)
1018 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001019 .Case("ngt", 0x0A)
1020 .Case("false", 0x0B)
1021 .Case("neq_oq", 0x0C)
1022 .Case("ge", 0x0D)
1023 .Case("gt", 0x0E)
1024 .Case("true", 0x0F)
1025 .Case("eq_os", 0x10)
1026 .Case("lt_oq", 0x11)
1027 .Case("le_oq", 0x12)
1028 .Case("unord_s", 0x13)
1029 .Case("neq_us", 0x14)
1030 .Case("nlt_uq", 0x15)
1031 .Case("nle_uq", 0x16)
1032 .Case("ord_s", 0x17)
1033 .Case("eq_us", 0x18)
1034 .Case("nge_uq", 0x19)
1035 .Case("ngt_uq", 0x1A)
1036 .Case("false_os", 0x1B)
1037 .Case("neq_os", 0x1C)
1038 .Case("ge_oq", 0x1D)
1039 .Case("gt_oq", 0x1E)
1040 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001041 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001042 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001043 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1044 getParser().getContext());
1045 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001046 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001047 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001048 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001049 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001050 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001051 } else {
1052 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001053 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001054 }
1055 }
1056 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001057
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001058 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001059
Devang Patel885f65b2012-01-30 22:47:12 +00001060 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001061 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001062
Chris Lattner2544f422010-09-08 05:17:37 +00001063 // Determine whether this is an instruction prefix.
1064 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001065 Name == "lock" || Name == "rep" ||
1066 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001067 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001068 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001069
1070
Chris Lattner2544f422010-09-08 05:17:37 +00001071 // This does the actual operand parsing. Don't parse any more if we have a
1072 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1073 // just want to parse the "lock" as the first instruction and the "incl" as
1074 // the next one.
1075 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001076
1077 // Parse '*' modifier.
1078 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001079 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001080 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001081 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001082 }
1083
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001084 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001085 if (X86Operand *Op = ParseOperand())
1086 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001087 else {
1088 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001089 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001090 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001091
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001092 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001093 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001094
1095 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001096 if (X86Operand *Op = ParseOperand())
1097 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001098 else {
1099 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001100 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001101 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001102 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001103
Chris Lattnercbf8a982010-09-11 16:18:25 +00001104 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001105 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001106 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001107 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001108 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001109 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001110
Chris Lattner2544f422010-09-08 05:17:37 +00001111 if (getLexer().is(AsmToken::EndOfStatement))
1112 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001113 else if (isPrefix && getLexer().is(AsmToken::Slash))
1114 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001115
Devang Patel885f65b2012-01-30 22:47:12 +00001116 if (ExtraImmOp && isParsingIntelSyntax())
1117 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1118
Chris Lattner98c870f2010-11-06 19:25:43 +00001119 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1120 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1121 // documented form in various unofficial manuals, so a lot of code uses it.
1122 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1123 Operands.size() == 3) {
1124 X86Operand &Op = *(X86Operand*)Operands.back();
1125 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1126 isa<MCConstantExpr>(Op.Mem.Disp) &&
1127 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1128 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1129 SMLoc Loc = Op.getEndLoc();
1130 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1131 delete &Op;
1132 }
1133 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001134 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1135 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1136 Operands.size() == 3) {
1137 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1138 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1139 isa<MCConstantExpr>(Op.Mem.Disp) &&
1140 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1141 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1142 SMLoc Loc = Op.getEndLoc();
1143 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1144 delete &Op;
1145 }
1146 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001147 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1148 if (Name.startswith("ins") && Operands.size() == 3 &&
1149 (Name == "insb" || Name == "insw" || Name == "insl")) {
1150 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1151 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1152 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1153 Operands.pop_back();
1154 Operands.pop_back();
1155 delete &Op;
1156 delete &Op2;
1157 }
1158 }
1159
1160 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1161 if (Name.startswith("outs") && Operands.size() == 3 &&
1162 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1163 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1164 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1165 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1166 Operands.pop_back();
1167 Operands.pop_back();
1168 delete &Op;
1169 delete &Op2;
1170 }
1171 }
1172
1173 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1174 if (Name.startswith("movs") && Operands.size() == 3 &&
1175 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001176 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001177 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1178 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1179 if (isSrcOp(Op) && isDstOp(Op2)) {
1180 Operands.pop_back();
1181 Operands.pop_back();
1182 delete &Op;
1183 delete &Op2;
1184 }
1185 }
1186 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1187 if (Name.startswith("lods") && Operands.size() == 3 &&
1188 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001189 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001190 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1191 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1192 if (isSrcOp(*Op1) && Op2->isReg()) {
1193 const char *ins;
1194 unsigned reg = Op2->getReg();
1195 bool isLods = Name == "lods";
1196 if (reg == X86::AL && (isLods || Name == "lodsb"))
1197 ins = "lodsb";
1198 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1199 ins = "lodsw";
1200 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1201 ins = "lodsl";
1202 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1203 ins = "lodsq";
1204 else
1205 ins = NULL;
1206 if (ins != NULL) {
1207 Operands.pop_back();
1208 Operands.pop_back();
1209 delete Op1;
1210 delete Op2;
1211 if (Name != ins)
1212 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1213 }
1214 }
1215 }
1216 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1217 if (Name.startswith("stos") && Operands.size() == 3 &&
1218 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001219 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001220 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1221 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1222 if (isDstOp(*Op2) && Op1->isReg()) {
1223 const char *ins;
1224 unsigned reg = Op1->getReg();
1225 bool isStos = Name == "stos";
1226 if (reg == X86::AL && (isStos || Name == "stosb"))
1227 ins = "stosb";
1228 else if (reg == X86::AX && (isStos || Name == "stosw"))
1229 ins = "stosw";
1230 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1231 ins = "stosl";
1232 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1233 ins = "stosq";
1234 else
1235 ins = NULL;
1236 if (ins != NULL) {
1237 Operands.pop_back();
1238 Operands.pop_back();
1239 delete Op1;
1240 delete Op2;
1241 if (Name != ins)
1242 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1243 }
1244 }
1245 }
1246
Chris Lattnere9e16a32010-09-15 04:33:27 +00001247 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001248 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001249 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001250 Name.startswith("shl") || Name.startswith("sal") ||
1251 Name.startswith("rcl") || Name.startswith("rcr") ||
1252 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001253 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001254 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001255 // Intel syntax
1256 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1257 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001258 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1259 delete Operands[2];
1260 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001261 }
1262 } else {
1263 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1264 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001265 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1266 delete Operands[1];
1267 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001268 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001269 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001270 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001271
Chris Lattner15f89512011-04-09 19:41:05 +00001272 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1273 // instalias with an immediate operand yet.
1274 if (Name == "int" && Operands.size() == 2) {
1275 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1276 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1277 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1278 delete Operands[1];
1279 Operands.erase(Operands.begin() + 1);
1280 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1281 }
1282 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001283
Chris Lattner98986712010-01-14 22:21:20 +00001284 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001285}
1286
Devang Pateldd929fc2012-01-12 18:03:40 +00001287bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001288processInstruction(MCInst &Inst,
1289 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1290 switch (Inst.getOpcode()) {
1291 default: return false;
1292 case X86::AND16i16: {
1293 if (!Inst.getOperand(0).isImm() ||
1294 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1295 return false;
1296
1297 MCInst TmpInst;
1298 TmpInst.setOpcode(X86::AND16ri8);
1299 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1300 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1301 TmpInst.addOperand(Inst.getOperand(0));
1302 Inst = TmpInst;
1303 return true;
1304 }
1305 case X86::AND32i32: {
1306 if (!Inst.getOperand(0).isImm() ||
1307 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1308 return false;
1309
1310 MCInst TmpInst;
1311 TmpInst.setOpcode(X86::AND32ri8);
1312 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1313 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1314 TmpInst.addOperand(Inst.getOperand(0));
1315 Inst = TmpInst;
1316 return true;
1317 }
1318 case X86::AND64i32: {
1319 if (!Inst.getOperand(0).isImm() ||
1320 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1321 return false;
1322
1323 MCInst TmpInst;
1324 TmpInst.setOpcode(X86::AND64ri8);
1325 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1326 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1327 TmpInst.addOperand(Inst.getOperand(0));
1328 Inst = TmpInst;
1329 return true;
1330 }
Devang Patelac0f0482012-01-19 17:53:25 +00001331 case X86::XOR16i16: {
1332 if (!Inst.getOperand(0).isImm() ||
1333 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1334 return false;
1335
1336 MCInst TmpInst;
1337 TmpInst.setOpcode(X86::XOR16ri8);
1338 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1339 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1340 TmpInst.addOperand(Inst.getOperand(0));
1341 Inst = TmpInst;
1342 return true;
1343 }
1344 case X86::XOR32i32: {
1345 if (!Inst.getOperand(0).isImm() ||
1346 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1347 return false;
1348
1349 MCInst TmpInst;
1350 TmpInst.setOpcode(X86::XOR32ri8);
1351 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1352 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1353 TmpInst.addOperand(Inst.getOperand(0));
1354 Inst = TmpInst;
1355 return true;
1356 }
1357 case X86::XOR64i32: {
1358 if (!Inst.getOperand(0).isImm() ||
1359 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1360 return false;
1361
1362 MCInst TmpInst;
1363 TmpInst.setOpcode(X86::XOR64ri8);
1364 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1365 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1366 TmpInst.addOperand(Inst.getOperand(0));
1367 Inst = TmpInst;
1368 return true;
1369 }
1370 case X86::OR16i16: {
1371 if (!Inst.getOperand(0).isImm() ||
1372 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1373 return false;
1374
1375 MCInst TmpInst;
1376 TmpInst.setOpcode(X86::OR16ri8);
1377 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1378 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1379 TmpInst.addOperand(Inst.getOperand(0));
1380 Inst = TmpInst;
1381 return true;
1382 }
1383 case X86::OR32i32: {
1384 if (!Inst.getOperand(0).isImm() ||
1385 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1386 return false;
1387
1388 MCInst TmpInst;
1389 TmpInst.setOpcode(X86::OR32ri8);
1390 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1391 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1392 TmpInst.addOperand(Inst.getOperand(0));
1393 Inst = TmpInst;
1394 return true;
1395 }
1396 case X86::OR64i32: {
1397 if (!Inst.getOperand(0).isImm() ||
1398 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1399 return false;
1400
1401 MCInst TmpInst;
1402 TmpInst.setOpcode(X86::OR64ri8);
1403 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1404 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1405 TmpInst.addOperand(Inst.getOperand(0));
1406 Inst = TmpInst;
1407 return true;
1408 }
1409 case X86::CMP16i16: {
1410 if (!Inst.getOperand(0).isImm() ||
1411 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1412 return false;
1413
1414 MCInst TmpInst;
1415 TmpInst.setOpcode(X86::CMP16ri8);
1416 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1417 TmpInst.addOperand(Inst.getOperand(0));
1418 Inst = TmpInst;
1419 return true;
1420 }
1421 case X86::CMP32i32: {
1422 if (!Inst.getOperand(0).isImm() ||
1423 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1424 return false;
1425
1426 MCInst TmpInst;
1427 TmpInst.setOpcode(X86::CMP32ri8);
1428 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1429 TmpInst.addOperand(Inst.getOperand(0));
1430 Inst = TmpInst;
1431 return true;
1432 }
1433 case X86::CMP64i32: {
1434 if (!Inst.getOperand(0).isImm() ||
1435 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1436 return false;
1437
1438 MCInst TmpInst;
1439 TmpInst.setOpcode(X86::CMP64ri8);
1440 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1441 TmpInst.addOperand(Inst.getOperand(0));
1442 Inst = TmpInst;
1443 return true;
1444 }
Devang Patela951f772012-01-19 18:40:55 +00001445 case X86::ADD16i16: {
1446 if (!Inst.getOperand(0).isImm() ||
1447 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1448 return false;
1449
1450 MCInst TmpInst;
1451 TmpInst.setOpcode(X86::ADD16ri8);
1452 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1453 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1454 TmpInst.addOperand(Inst.getOperand(0));
1455 Inst = TmpInst;
1456 return true;
1457 }
1458 case X86::ADD32i32: {
1459 if (!Inst.getOperand(0).isImm() ||
1460 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1461 return false;
1462
1463 MCInst TmpInst;
1464 TmpInst.setOpcode(X86::ADD32ri8);
1465 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1466 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1467 TmpInst.addOperand(Inst.getOperand(0));
1468 Inst = TmpInst;
1469 return true;
1470 }
1471 case X86::ADD64i32: {
1472 if (!Inst.getOperand(0).isImm() ||
1473 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1474 return false;
1475
1476 MCInst TmpInst;
1477 TmpInst.setOpcode(X86::ADD64ri8);
1478 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1479 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1480 TmpInst.addOperand(Inst.getOperand(0));
1481 Inst = TmpInst;
1482 return true;
1483 }
1484 case X86::SUB16i16: {
1485 if (!Inst.getOperand(0).isImm() ||
1486 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1487 return false;
1488
1489 MCInst TmpInst;
1490 TmpInst.setOpcode(X86::SUB16ri8);
1491 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1492 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1493 TmpInst.addOperand(Inst.getOperand(0));
1494 Inst = TmpInst;
1495 return true;
1496 }
1497 case X86::SUB32i32: {
1498 if (!Inst.getOperand(0).isImm() ||
1499 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1500 return false;
1501
1502 MCInst TmpInst;
1503 TmpInst.setOpcode(X86::SUB32ri8);
1504 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1505 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1506 TmpInst.addOperand(Inst.getOperand(0));
1507 Inst = TmpInst;
1508 return true;
1509 }
1510 case X86::SUB64i32: {
1511 if (!Inst.getOperand(0).isImm() ||
1512 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1513 return false;
1514
1515 MCInst TmpInst;
1516 TmpInst.setOpcode(X86::SUB64ri8);
1517 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1518 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1519 TmpInst.addOperand(Inst.getOperand(0));
1520 Inst = TmpInst;
1521 return true;
1522 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001523 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001524}
1525
1526bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001527MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001528 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001529 MCStreamer &Out) {
Chad Rosier3a86e132012-09-03 02:06:46 +00001530 unsigned Kind;
Chad Rosier64bfcbb2012-08-21 18:14:59 +00001531 unsigned ErrorInfo;
Chad Rosier3a86e132012-09-03 02:06:46 +00001532 SmallVector<MCInst, 2> Insts;
1533
Chad Rosierc4d25602012-09-03 03:16:09 +00001534 bool Error = MatchInstruction(IDLoc, Kind, Operands, Insts,
Chad Rosier3a86e132012-09-03 02:06:46 +00001535 ErrorInfo);
Chad Rosier32461762012-08-09 22:04:55 +00001536 if (!Error)
1537 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
1538 Out.EmitInstruction(Insts[i]);
1539 return Error;
1540}
1541
1542bool X86AsmParser::
Chad Rosierc4d25602012-09-03 03:16:09 +00001543MatchInstruction(SMLoc IDLoc, unsigned &Kind,
Chad Rosier32461762012-08-09 22:04:55 +00001544 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosierb4fdade2012-08-21 19:36:59 +00001545 SmallVectorImpl<MCInst> &MCInsts, unsigned &OrigErrorInfo,
1546 bool matchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001547 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001548 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1549 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001550 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001551
Chris Lattner7c51a312010-09-29 01:50:45 +00001552 // First, handle aliases that expand to multiple instructions.
1553 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001554 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001555 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001556 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001557 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001558 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001559 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001560 MCInst Inst;
1561 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001562 Inst.setLoc(IDLoc);
Chad Rosier32461762012-08-09 22:04:55 +00001563 MCInsts.push_back(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001564
Chris Lattner0bb83a82010-09-30 16:39:29 +00001565 const char *Repl =
1566 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001567 .Case("finit", "fninit")
1568 .Case("fsave", "fnsave")
1569 .Case("fstcw", "fnstcw")
1570 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001571 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001572 .Case("fstsw", "fnstsw")
1573 .Case("fstsww", "fnstsw")
1574 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001575 .Default(0);
1576 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001577 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001578 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001579 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001580
Chris Lattnera008e8a2010-09-06 21:54:15 +00001581 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001582 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001583
Daniel Dunbarc918d602010-05-04 16:12:42 +00001584 // First, try a direct match.
Chad Rosierc4d25602012-09-03 03:16:09 +00001585 switch (MatchInstructionImpl(Operands, Kind, Inst, OrigErrorInfo,
Devang Patelbe3e3102012-01-30 20:02:42 +00001586 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001587 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001588 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001589 // Some instructions need post-processing to, for example, tweak which
1590 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001591 // individual transformations can chain off each other.
Devang Patelb8ba13f2012-01-18 22:42:29 +00001592 while (processInstruction(Inst, Operands))
1593 ;
1594
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001595 Inst.setLoc(IDLoc);
Chad Rosier32461762012-08-09 22:04:55 +00001596 MCInsts.push_back(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001597 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001598 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001599 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
1600 EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001601 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001602 case Match_InvalidOperand:
1603 WasOriginallyInvalidOperand = true;
1604 break;
1605 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001606 break;
1607 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001608
Daniel Dunbarc918d602010-05-04 16:12:42 +00001609 // FIXME: Ideally, we would only attempt suffix matches for things which are
1610 // valid prefixes, and we could just infer the right unambiguous
1611 // type. However, that requires substantially more matcher support than the
1612 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001613
Daniel Dunbarc918d602010-05-04 16:12:42 +00001614 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001615 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001616 SmallString<16> Tmp;
1617 Tmp += Base;
1618 Tmp += ' ';
1619 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001620
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001621 // If this instruction starts with an 'f', then it is a floating point stack
1622 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1623 // 80-bit floating point, which use the suffixes s,l,t respectively.
1624 //
1625 // Otherwise, we assume that this may be an integer instruction, which comes
1626 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1627 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001628
Daniel Dunbarc918d602010-05-04 16:12:42 +00001629 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001630 Tmp[Base.size()] = Suffixes[0];
1631 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001632 unsigned Match1, Match2, Match3, Match4;
Chad Rosierc4d25602012-09-03 03:16:09 +00001633 unsigned tKind;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001634
Chad Rosierc4d25602012-09-03 03:16:09 +00001635 Match1 = MatchInstructionImpl(Operands, tKind, Inst, ErrorInfoIgnore);
1636 if (Match1 == Match_Success) Kind = tKind;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001637 Tmp[Base.size()] = Suffixes[1];
Chad Rosierc4d25602012-09-03 03:16:09 +00001638 Match2 = MatchInstructionImpl(Operands, tKind, Inst, ErrorInfoIgnore);
1639 if (Match2 == Match_Success) Kind = tKind;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001640 Tmp[Base.size()] = Suffixes[2];
Chad Rosierc4d25602012-09-03 03:16:09 +00001641 Match3 = MatchInstructionImpl(Operands, tKind, Inst, ErrorInfoIgnore);
1642 if (Match3 == Match_Success) Kind = tKind;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001643 Tmp[Base.size()] = Suffixes[3];
Chad Rosierc4d25602012-09-03 03:16:09 +00001644 Match4 = MatchInstructionImpl(Operands, tKind, Inst, ErrorInfoIgnore);
1645 if (Match4 == Match_Success) Kind = tKind;
Daniel Dunbarc918d602010-05-04 16:12:42 +00001646
1647 // Restore the old token.
1648 Op->setTokenValue(Base);
1649
1650 // If exactly one matched, then we treat that as a successful match (and the
1651 // instruction will already have been filled in correctly, since the failing
1652 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001653 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001654 (Match1 == Match_Success) + (Match2 == Match_Success) +
1655 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001656 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001657 Inst.setLoc(IDLoc);
Chad Rosier32461762012-08-09 22:04:55 +00001658 MCInsts.push_back(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001659 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001660 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001661
Chris Lattnerec6789f2010-09-06 20:08:02 +00001662 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001663
Daniel Dunbar09062b12010-08-12 00:55:42 +00001664 // If we had multiple suffix matches, then identify this as an ambiguous
1665 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001666 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001667 char MatchChars[4];
1668 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001669 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1670 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1671 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1672 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001673
1674 SmallString<126> Msg;
1675 raw_svector_ostream OS(Msg);
1676 OS << "ambiguous instructions require an explicit suffix (could be ";
1677 for (unsigned i = 0; i != NumMatches; ++i) {
1678 if (i != 0)
1679 OS << ", ";
1680 if (i + 1 == NumMatches)
1681 OS << "or ";
1682 OS << "'" << Base << MatchChars[i] << "'";
1683 }
1684 OS << ")";
Chad Rosierb4fdade2012-08-21 19:36:59 +00001685 Error(IDLoc, OS.str(), EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001686 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001687 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001688
Chris Lattnera008e8a2010-09-06 21:54:15 +00001689 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001690
Chris Lattnera008e8a2010-09-06 21:54:15 +00001691 // If all of the instructions reported an invalid mnemonic, then the original
1692 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001693 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1694 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001695 if (!WasOriginallyInvalidOperand) {
Chad Rosier674101e2012-08-22 19:14:29 +00001696 ArrayRef<SMRange> Ranges = matchingInlineAsm ? EmptyRanges :
1697 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001698 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier674101e2012-08-22 19:14:29 +00001699 Ranges, matchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001700 }
1701
1702 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001703 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001704 if (OrigErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001705 return Error(IDLoc, "too few operands for instruction",
1706 EmptyRanges, matchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001707
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001708 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1709 if (Operand->getStartLoc().isValid()) {
1710 SMRange OperandRange = Operand->getLocRange();
1711 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosierb4fdade2012-08-21 19:36:59 +00001712 OperandRange, matchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001713 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001714 }
1715
Chad Rosierb4fdade2012-08-21 19:36:59 +00001716 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
1717 matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001718 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001719
Chris Lattnerec6789f2010-09-06 20:08:02 +00001720 // If one instruction matched with a missing feature, report this as a
1721 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001722 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1723 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001724 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
1725 EmptyRanges, matchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001726 return true;
1727 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001728
Chris Lattnera008e8a2010-09-06 21:54:15 +00001729 // If one instruction matched with an invalid operand, report this as an
1730 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001731 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1732 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001733 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
1734 matchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001735 return true;
1736 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001737
Chris Lattnerec6789f2010-09-06 20:08:02 +00001738 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001739 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
1740 EmptyRanges, matchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001741 return true;
1742}
1743
1744
Devang Pateldd929fc2012-01-12 18:03:40 +00001745bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001746 StringRef IDVal = DirectiveID.getIdentifier();
1747 if (IDVal == ".word")
1748 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001749 else if (IDVal.startswith(".code"))
1750 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001751 else if (IDVal.startswith(".att_syntax")) {
1752 getParser().setAssemblerDialect(0);
1753 return false;
1754 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001755 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001756 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1757 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001758 // FIXME : Handle noprefix
1759 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001760 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001761 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001762 }
1763 return false;
1764 }
Chris Lattner537ca842010-10-30 17:38:55 +00001765 return true;
1766}
1767
1768/// ParseDirectiveWord
1769/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001770bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001771 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1772 for (;;) {
1773 const MCExpr *Value;
1774 if (getParser().ParseExpression(Value))
1775 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001776
Chris Lattner537ca842010-10-30 17:38:55 +00001777 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001778
Chris Lattner537ca842010-10-30 17:38:55 +00001779 if (getLexer().is(AsmToken::EndOfStatement))
1780 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001781
Chris Lattner537ca842010-10-30 17:38:55 +00001782 // FIXME: Improve diagnostic.
1783 if (getLexer().isNot(AsmToken::Comma))
1784 return Error(L, "unexpected token in directive");
1785 Parser.Lex();
1786 }
1787 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001788
Chris Lattner537ca842010-10-30 17:38:55 +00001789 Parser.Lex();
1790 return false;
1791}
1792
Evan Chengbd27f5a2011-07-27 00:38:12 +00001793/// ParseDirectiveCode
1794/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001795bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001796 if (IDVal == ".code32") {
1797 Parser.Lex();
1798 if (is64BitMode()) {
1799 SwitchMode();
1800 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1801 }
1802 } else if (IDVal == ".code64") {
1803 Parser.Lex();
1804 if (!is64BitMode()) {
1805 SwitchMode();
1806 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1807 }
1808 } else {
1809 return Error(L, "unexpected directive " + IDVal);
1810 }
Chris Lattner537ca842010-10-30 17:38:55 +00001811
Evan Chengbd27f5a2011-07-27 00:38:12 +00001812 return false;
1813}
Chris Lattner537ca842010-10-30 17:38:55 +00001814
1815
Sean Callanane88f5522010-01-23 02:43:15 +00001816extern "C" void LLVMInitializeX86AsmLexer();
1817
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001818// Force static initialization.
1819extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001820 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1821 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001822 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001823}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001824
Chris Lattner0692ee62010-09-06 19:11:01 +00001825#define GET_REGISTER_MATCHER
1826#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001827#include "X86GenAsmMatcher.inc"