blob: a3e90af71ad9bd84fcd7bc982b10d6254343c514 [file] [log] [blame]
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
11#include "llvm/MC/MCTargetAsmParser.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000012#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000013#include "llvm/MC/MCExpr.h"
Chad Rosier96d58e62012-10-19 20:57:14 +000014#include "llvm/MC/MCSymbol.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000015#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000016#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000017#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Chad Rosier4284e172012-10-24 22:13:37 +000021#include "llvm/ADT/APFloat.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000022#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000024#include "llvm/ADT/StringSwitch.h"
25#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000026#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000027#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000028#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000029
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000030using namespace llvm;
31
32namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000033struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000034
Devang Pateldd929fc2012-01-12 18:03:40 +000035class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000036 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037 MCAsmParser &Parser;
Chad Rosier6a020a72012-10-25 20:41:34 +000038 ParseInstructionInfo *InstInfo;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000040 MCAsmParser &getParser() const { return Parser; }
41
42 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000044 bool Error(SMLoc L, const Twine &Msg,
Chad Rosierb4fdade2012-08-21 19:36:59 +000045 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>(),
Chad Rosier7a2b6242012-10-12 23:09:25 +000046 bool MatchingInlineAsm = false) {
47 if (MatchingInlineAsm) return true;
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000048 return Parser.Error(L, Msg, Ranges);
49 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000050
Devang Pateld37ad242012-01-17 18:00:18 +000051 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
52 Error(Loc, Msg);
53 return 0;
54 }
55
Chris Lattner309264d2010-01-15 18:44:13 +000056 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000057 X86Operand *ParseATTOperand();
58 X86Operand *ParseIntelOperand();
Chad Rosierc0a14b82012-10-24 17:22:29 +000059 X86Operand *ParseIntelOffsetOfOperator(SMLoc StartLoc);
Chad Rosier5b0f1b32012-10-04 23:59:38 +000060 X86Operand *ParseIntelMemOperand(unsigned SegReg, SMLoc StartLoc);
Devang Patel7c64fe62012-01-23 18:31:58 +000061 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000062 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000063
Chad Rosier5e6b37f2012-10-25 17:37:43 +000064 bool ParseIntelDotOperator(const MCExpr *Disp, const MCExpr **NewDisp,
65 SmallString<64> &Err);
Chad Rosier22f441a2012-10-24 22:21:50 +000066
Kevin Enderby9c656452009-09-10 20:51:44 +000067 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000068 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000069
Devang Patelb8ba13f2012-01-18 22:42:29 +000070 bool processInstruction(MCInst &Inst,
71 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
72
Chad Rosier84125ca2012-10-13 00:26:04 +000073 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +000074 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +000075 MCStreamer &Out, unsigned &ErrorInfo,
76 bool MatchingInlineAsm);
Chad Rosier32461762012-08-09 22:04:55 +000077
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000078 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000079 /// in 64bit mode or (%esi) or %es:(%esi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000080 bool isSrcOp(X86Operand &Op);
81
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +000082 /// isDstOp - Returns true if operand is either (%rdi) or %es:(%rdi)
83 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000084 bool isDstOp(X86Operand &Op);
85
Evan Cheng59ee62d2011-07-11 03:57:24 +000086 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000087 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000088 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000089 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000090 void SwitchMode() {
91 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
92 setAvailableFeatures(FB);
93 }
Evan Chengebdeeab2011-07-08 01:53:10 +000094
Daniel Dunbar54074b52010-07-19 05:44:09 +000095 /// @name Auto-generated Matcher Functions
96 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000097
Chris Lattner0692ee62010-09-06 19:11:01 +000098#define GET_ASSEMBLER_HEADER
99#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000100
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000101 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000102
103public:
Devang Pateldd929fc2012-01-12 18:03:40 +0000104 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Chad Rosier6a020a72012-10-25 20:41:34 +0000105 : MCTargetAsmParser(), STI(sti), Parser(parser), InstInfo(0) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000106
Daniel Dunbar54074b52010-07-19 05:44:09 +0000107 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000108 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000109 }
Roman Divackybf755322011-01-27 17:14:22 +0000110 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000111
Chad Rosier6a020a72012-10-25 20:41:34 +0000112 virtual bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name,
113 SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000114 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000115
116 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000117
118 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000119 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000120 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000121};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000122} // end anonymous namespace
123
Sean Callanane9b466d2010-01-23 00:40:33 +0000124/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000125/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000126
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000127static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000128
129/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000130
Craig Topper76bd9382012-07-18 04:59:16 +0000131static bool isImmSExti16i8Value(uint64_t Value) {
Devang Patelb8ba13f2012-01-18 22:42:29 +0000132 return (( Value <= 0x000000000000007FULL)||
133 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
134 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
135}
136
137static bool isImmSExti32i8Value(uint64_t Value) {
138 return (( Value <= 0x000000000000007FULL)||
139 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
140 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
141}
142
143static bool isImmZExtu32u8Value(uint64_t Value) {
144 return (Value <= 0x00000000000000FFULL);
145}
146
147static bool isImmSExti64i8Value(uint64_t Value) {
148 return (( Value <= 0x000000000000007FULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000149 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000150}
151
152static bool isImmSExti64i32Value(uint64_t Value) {
153 return (( Value <= 0x000000007FFFFFFFULL)||
Craig Topper76bd9382012-07-18 04:59:16 +0000154 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
Devang Patelb8ba13f2012-01-18 22:42:29 +0000155}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000156namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000157
158/// X86Operand - Instances of this class represent a parsed X86 machine
159/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000160struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000161 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000162 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000163 Register,
164 Immediate,
Chad Rosierf9e008b2012-10-02 23:38:50 +0000165 Memory
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000166 } Kind;
167
Chris Lattner29ef9a22010-01-15 18:51:29 +0000168 SMLoc StartLoc, EndLoc;
Chad Rosier5a719fc2012-10-23 17:43:43 +0000169 SMLoc OffsetOfLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000170
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000171 union {
172 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000173 const char *Data;
174 unsigned Length;
175 } Tok;
176
177 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000178 unsigned RegNo;
179 } Reg;
180
181 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000182 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000183 } Imm;
184
185 struct {
186 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000187 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000188 unsigned BaseReg;
189 unsigned IndexReg;
190 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000191 unsigned Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000192 bool NeedSizeDir;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000193 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000194 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000195
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000196 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000197 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000198
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000199 /// getStartLoc - Get the location of the first token of this operand.
200 SMLoc getStartLoc() const { return StartLoc; }
201 /// getEndLoc - Get the location of the last token of this operand.
202 SMLoc getEndLoc() const { return EndLoc; }
Chad Rosier7d4e9892012-09-21 21:08:46 +0000203 /// getLocRange - Get the range between the first and last token of this
204 /// operand.
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000205 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chad Rosier5a719fc2012-10-23 17:43:43 +0000206 /// getOffsetOfLoc - Get the location of the offset operator.
207 SMLoc getOffsetOfLoc() const { return OffsetOfLoc; }
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
Chad Rosier96d58e62012-10-19 20:57:14 +0000327 unsigned getMemSize() const {
328 assert(Kind == Memory && "Invalid access!");
329 return Mem.Size;
330 }
331
Chad Rosiera703fb92012-10-22 19:50:35 +0000332 bool isOffsetOf() const {
Chad Rosierc0a14b82012-10-24 17:22:29 +0000333 return OffsetOfLoc.getPointer();
Chad Rosiera703fb92012-10-22 19:50:35 +0000334 }
335
Chad Rosier96d58e62012-10-19 20:57:14 +0000336 bool needSizeDirective() const {
337 assert(Kind == Memory && "Invalid access!");
338 return Mem.NeedSizeDir;
339 }
340
Daniel Dunbar20927f22009-08-07 08:26:05 +0000341 bool isMem() const { return Kind == Memory; }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000342 bool isMem8() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000343 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
Devang Patelc59d9df2012-01-12 01:51:42 +0000344 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000345 bool isMem16() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000346 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
Devang Patelc59d9df2012-01-12 01:51:42 +0000347 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000348 bool isMem32() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000349 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
Devang Patelc59d9df2012-01-12 01:51:42 +0000350 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000351 bool isMem64() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000352 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
Devang Patelc59d9df2012-01-12 01:51:42 +0000353 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000354 bool isMem80() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000355 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
Devang Patelc59d9df2012-01-12 01:51:42 +0000356 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000357 bool isMem128() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000358 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
Devang Patelc59d9df2012-01-12 01:51:42 +0000359 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000360 bool isMem256() const {
Chad Rosierf9e008b2012-10-02 23:38:50 +0000361 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
Devang Patelc59d9df2012-01-12 01:51:42 +0000362 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000363
Craig Topper75dc33a2012-07-18 04:11:12 +0000364 bool isMemVX32() const {
365 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
366 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
367 }
368 bool isMemVY32() const {
369 return Kind == Memory && (!Mem.Size || Mem.Size == 32) &&
370 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
371 }
372 bool isMemVX64() const {
373 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
374 getMemIndexReg() >= X86::XMM0 && getMemIndexReg() <= X86::XMM15;
375 }
376 bool isMemVY64() const {
377 return Kind == Memory && (!Mem.Size || Mem.Size == 64) &&
378 getMemIndexReg() >= X86::YMM0 && getMemIndexReg() <= X86::YMM15;
379 }
380
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000381 bool isAbsMem() const {
382 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000383 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000384 }
385
Daniel Dunbar20927f22009-08-07 08:26:05 +0000386 bool isReg() const { return Kind == Register; }
387
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000388 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
389 // Add as immediates when possible.
390 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
391 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
392 else
393 Inst.addOperand(MCOperand::CreateExpr(Expr));
394 }
395
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000396 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000397 assert(N == 1 && "Invalid number of operands!");
398 Inst.addOperand(MCOperand::CreateReg(getReg()));
399 }
400
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000401 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000402 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000403 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000404 }
405
Chad Rosier36b8fed2012-06-27 22:34:28 +0000406 void addMem8Operands(MCInst &Inst, unsigned N) const {
407 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000408 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000409 void addMem16Operands(MCInst &Inst, unsigned N) const {
410 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000411 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000412 void addMem32Operands(MCInst &Inst, unsigned N) const {
413 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000414 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000415 void addMem64Operands(MCInst &Inst, unsigned N) const {
416 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000417 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000418 void addMem80Operands(MCInst &Inst, unsigned N) const {
419 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000420 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000421 void addMem128Operands(MCInst &Inst, unsigned N) const {
422 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000423 }
Chad Rosier36b8fed2012-06-27 22:34:28 +0000424 void addMem256Operands(MCInst &Inst, unsigned N) const {
425 addMemOperands(Inst, N);
Devang Patelc59d9df2012-01-12 01:51:42 +0000426 }
Craig Topper75dc33a2012-07-18 04:11:12 +0000427 void addMemVX32Operands(MCInst &Inst, unsigned N) const {
428 addMemOperands(Inst, N);
429 }
430 void addMemVY32Operands(MCInst &Inst, unsigned N) const {
431 addMemOperands(Inst, N);
432 }
433 void addMemVX64Operands(MCInst &Inst, unsigned N) const {
434 addMemOperands(Inst, N);
435 }
436 void addMemVY64Operands(MCInst &Inst, unsigned N) const {
437 addMemOperands(Inst, N);
438 }
Devang Patelc59d9df2012-01-12 01:51:42 +0000439
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000440 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000441 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000442 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
443 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
444 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000445 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000446 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
447 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000448
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000449 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
450 assert((N == 1) && "Invalid number of operands!");
Kevin Enderbyb80d5712012-02-23 18:18:17 +0000451 // Add as immediates when possible.
452 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemDisp()))
453 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
454 else
455 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000456 }
457
Chris Lattnerb4307b32010-01-15 19:28:38 +0000458 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000459 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
460 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000461 Res->Tok.Data = Str.data();
462 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000463 return Res;
464 }
465
Chad Rosierc0a14b82012-10-24 17:22:29 +0000466 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc,
467 SMLoc OffsetOfLoc = SMLoc()) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000468 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000469 Res->Reg.RegNo = RegNo;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000470 Res->OffsetOfLoc = OffsetOfLoc;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000471 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000472 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000473
Chris Lattnerb4307b32010-01-15 19:28:38 +0000474 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
475 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000476 Res->Imm.Val = Val;
477 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000478 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000479
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000480 /// Create an absolute memory operand.
Chad Rosier4284e172012-10-24 22:13:37 +0000481 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc, SMLoc EndLoc,
482 unsigned Size = 0, bool NeedSizeDir = false){
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000483 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
484 Res->Mem.SegReg = 0;
485 Res->Mem.Disp = Disp;
486 Res->Mem.BaseReg = 0;
487 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000488 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000489 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000490 Res->Mem.NeedSizeDir = NeedSizeDir;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000491 return Res;
492 }
493
494 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000495 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
496 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000497 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000498 unsigned Size = 0, bool NeedSizeDir = false) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000499 // We should never just have a displacement, that should be parsed as an
500 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000501 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
502
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000503 // The scale should always be one of {1,2,4,8}.
504 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000505 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000506 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000507 Res->Mem.SegReg = SegReg;
508 Res->Mem.Disp = Disp;
509 Res->Mem.BaseReg = BaseReg;
510 Res->Mem.IndexReg = IndexReg;
511 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000512 Res->Mem.Size = Size;
Chad Rosier96d58e62012-10-19 20:57:14 +0000513 Res->Mem.NeedSizeDir = NeedSizeDir;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000514 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000515 }
516};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000517
Chris Lattner37dfdec2009-07-29 06:33:53 +0000518} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000519
Devang Pateldd929fc2012-01-12 18:03:40 +0000520bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000521 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000522
523 return (Op.isMem() &&
524 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
525 isa<MCConstantExpr>(Op.Mem.Disp) &&
526 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
527 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
528}
529
Devang Pateldd929fc2012-01-12 18:03:40 +0000530bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000531 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000532
Chad Rosier36b8fed2012-06-27 22:34:28 +0000533 return Op.isMem() &&
Kevin Enderby0f5ab7c2012-03-13 19:47:55 +0000534 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::ES) &&
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000535 isa<MCConstantExpr>(Op.Mem.Disp) &&
536 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
537 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
538}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000539
Devang Pateldd929fc2012-01-12 18:03:40 +0000540bool X86AsmParser::ParseRegister(unsigned &RegNo,
541 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000542 RegNo = 0;
Benjamin Kramer8e70b552012-09-07 14:51:35 +0000543 const AsmToken &PercentTok = Parser.getTok();
544 StartLoc = PercentTok.getLoc();
545
546 // If we encounter a %, ignore it. This code handles registers with and
547 // without the prefix, unprefixed registers can occur in cfi directives.
548 if (!isParsingIntelSyntax() && PercentTok.is(AsmToken::Percent))
Devang Pateld37ad242012-01-17 18:00:18 +0000549 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000550
Sean Callanan18b83232010-01-19 21:44:56 +0000551 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000552 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000553 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000554 return Error(StartLoc, "invalid register name",
555 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000556 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000557
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000558 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000559
Chris Lattner33d60d52010-09-22 04:11:10 +0000560 // If the match failed, try the register name as lowercase.
561 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000562 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000563
Evan Cheng5de728c2011-07-27 23:22:03 +0000564 if (!is64BitMode()) {
565 // FIXME: This should be done using Requires<In32BitMode> and
566 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
567 // checked.
568 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
569 // REX prefix.
570 if (RegNo == X86::RIZ ||
571 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
572 X86II::isX86_64NonExtLowByteReg(RegNo) ||
573 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000574 return Error(StartLoc, "register %"
575 + Tok.getString() + " is only available in 64-bit mode",
576 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000577 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000578
Chris Lattner33d60d52010-09-22 04:11:10 +0000579 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
580 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000581 RegNo = X86::ST0;
582 EndLoc = Tok.getLoc();
583 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000584
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000585 // Check to see if we have '(4)' after %st.
586 if (getLexer().isNot(AsmToken::LParen))
587 return false;
588 // Lex the paren.
589 getParser().Lex();
590
591 const AsmToken &IntTok = Parser.getTok();
592 if (IntTok.isNot(AsmToken::Integer))
593 return Error(IntTok.getLoc(), "expected stack index");
594 switch (IntTok.getIntVal()) {
595 case 0: RegNo = X86::ST0; break;
596 case 1: RegNo = X86::ST1; break;
597 case 2: RegNo = X86::ST2; break;
598 case 3: RegNo = X86::ST3; break;
599 case 4: RegNo = X86::ST4; break;
600 case 5: RegNo = X86::ST5; break;
601 case 6: RegNo = X86::ST6; break;
602 case 7: RegNo = X86::ST7; break;
603 default: return Error(IntTok.getLoc(), "invalid stack index");
604 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000605
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000606 if (getParser().Lex().isNot(AsmToken::RParen))
607 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000608
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000609 EndLoc = Tok.getLoc();
610 Parser.Lex(); // Eat ')'
611 return false;
612 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000613
Chris Lattner645b2092010-06-24 07:29:18 +0000614 // If this is "db[0-7]", match it as an alias
615 // for dr[0-7].
616 if (RegNo == 0 && Tok.getString().size() == 3 &&
617 Tok.getString().startswith("db")) {
618 switch (Tok.getString()[2]) {
619 case '0': RegNo = X86::DR0; break;
620 case '1': RegNo = X86::DR1; break;
621 case '2': RegNo = X86::DR2; break;
622 case '3': RegNo = X86::DR3; break;
623 case '4': RegNo = X86::DR4; break;
624 case '5': RegNo = X86::DR5; break;
625 case '6': RegNo = X86::DR6; break;
626 case '7': RegNo = X86::DR7; break;
627 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000628
Chris Lattner645b2092010-06-24 07:29:18 +0000629 if (RegNo != 0) {
630 EndLoc = Tok.getLoc();
631 Parser.Lex(); // Eat it.
632 return false;
633 }
634 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000635
Devang Patel1aea4302012-01-20 22:32:05 +0000636 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000637 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000638 return Error(StartLoc, "invalid register name",
639 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000640 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000641
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000642 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000643 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000644 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000645}
646
Devang Pateldd929fc2012-01-12 18:03:40 +0000647X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000648 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000649 return ParseIntelOperand();
650 return ParseATTOperand();
651}
652
Devang Pateld37ad242012-01-17 18:00:18 +0000653/// getIntelMemOperandSize - Return intel memory operand size.
654static unsigned getIntelMemOperandSize(StringRef OpStr) {
Chad Rosier66b64be2012-09-11 21:10:25 +0000655 unsigned Size = StringSwitch<unsigned>(OpStr)
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000656 .Cases("BYTE", "byte", 8)
657 .Cases("WORD", "word", 16)
658 .Cases("DWORD", "dword", 32)
659 .Cases("QWORD", "qword", 64)
660 .Cases("XWORD", "xword", 80)
661 .Cases("XMMWORD", "xmmword", 128)
662 .Cases("YMMWORD", "ymmword", 256)
Chad Rosier66b64be2012-09-11 21:10:25 +0000663 .Default(0);
664 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000665}
666
Chad Rosier65c88922012-10-22 19:42:52 +0000667X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
Devang Patel7c64fe62012-01-23 18:31:58 +0000668 unsigned Size) {
669 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Chad Rosier4284e172012-10-24 22:13:37 +0000670 const AsmToken &Tok = Parser.getTok();
671 SMLoc Start = Tok.getLoc(), End;
Devang Patel0a338862012-01-12 01:36:43 +0000672
Chad Rosier4284e172012-10-24 22:13:37 +0000673 const MCExpr *Disp = MCConstantExpr::Create(0, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000674 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
675
676 // Eat '['
677 if (getLexer().isNot(AsmToken::LBrac))
678 return ErrorOperand(Start, "Expected '[' token!");
679 Parser.Lex();
Chad Rosier36b8fed2012-06-27 22:34:28 +0000680
Devang Pateld37ad242012-01-17 18:00:18 +0000681 if (getLexer().is(AsmToken::Identifier)) {
682 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000683 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000684 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000685 if (getParser().ParseExpression(Disp, End)) return 0;
686 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000687 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000688 Parser.Lex();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000689 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000690 }
691 } else if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000692 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000693 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000694 SMLoc Loc = Tok.getLoc();
Devang Patel3e081312012-01-23 20:20:06 +0000695 if (getLexer().is(AsmToken::RBrac)) {
696 // Handle '[' number ']'
697 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000698 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
699 if (SegReg)
700 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000701 Start, End, Size);
702 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000703 } else if (getLexer().is(AsmToken::Star)) {
704 // Handle '[' Scale*IndexReg ']'
705 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000706 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000707 if (ParseRegister(IndexReg, IdxRegLoc, End))
708 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patel3e081312012-01-23 20:20:06 +0000709 Scale = Val;
710 } else
Craig Topper833d7f82012-07-18 04:36:35 +0000711 return ErrorOperand(Loc, "Unexpected token");
Devang Pateld37ad242012-01-17 18:00:18 +0000712 }
713
714 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
715 bool isPlus = getLexer().is(AsmToken::Plus);
716 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000717 SMLoc PlusLoc = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000718 if (getLexer().is(AsmToken::Integer)) {
Chad Rosier4284e172012-10-24 22:13:37 +0000719 int64_t Val = Tok.getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000720 Parser.Lex();
721 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000722 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000723 SMLoc IdxRegLoc = Tok.getLoc();
Craig Topper833d7f82012-07-18 04:36:35 +0000724 if (ParseRegister(IndexReg, IdxRegLoc, End))
725 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000726 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000727 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000728 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000729 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000730 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000731 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000732 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000733 // This could be an index register or a displacement expression.
Chad Rosier4284e172012-10-24 22:13:37 +0000734 End = Tok.getLoc();
Devang Patelf2d21372012-01-23 22:35:25 +0000735 if (!IndexReg)
736 ParseRegister(IndexReg, Start, End);
Chad Rosier36b8fed2012-06-27 22:34:28 +0000737 else if (getParser().ParseExpression(Disp, End)) return 0;
Devang Patelf2d21372012-01-23 22:35:25 +0000738 }
Devang Pateld37ad242012-01-17 18:00:18 +0000739 }
740
741 if (getLexer().isNot(AsmToken::RBrac))
742 if (getParser().ParseExpression(Disp, End)) return 0;
743
Chad Rosier4284e172012-10-24 22:13:37 +0000744 End = Tok.getLoc();
Devang Pateld37ad242012-01-17 18:00:18 +0000745 if (getLexer().isNot(AsmToken::RBrac))
746 return ErrorOperand(End, "expected ']' token!");
747 Parser.Lex();
Chad Rosier4284e172012-10-24 22:13:37 +0000748 End = Tok.getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000749
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000750 if (Tok.getString().startswith(".")) {
751 SmallString<64> Err;
752 const MCExpr *NewDisp;
753 if (ParseIntelDotOperator(Disp, &NewDisp, Err))
754 return ErrorOperand(Tok.getLoc(), Err);
755
756 Parser.Lex(); // Eat the field.
757 Disp = NewDisp;
758 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000759
760 End = Tok.getLoc();
761
Devang Patelfdd3b302012-01-20 21:21:01 +0000762 // handle [-42]
763 if (!BaseReg && !IndexReg)
Chad Rosierc0a14b82012-10-24 17:22:29 +0000764 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patelfdd3b302012-01-20 21:21:01 +0000765
Devang Pateld37ad242012-01-17 18:00:18 +0000766 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000767 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000768}
769
770/// ParseIntelMemOperand - Parse intel style memory operand.
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000771X86Operand *X86AsmParser::ParseIntelMemOperand(unsigned SegReg, SMLoc Start) {
Devang Pateld37ad242012-01-17 18:00:18 +0000772 const AsmToken &Tok = Parser.getTok();
Chad Rosierc0a14b82012-10-24 17:22:29 +0000773 SMLoc End;
Devang Pateld37ad242012-01-17 18:00:18 +0000774
775 unsigned Size = getIntelMemOperandSize(Tok.getString());
776 if (Size) {
777 Parser.Lex();
Chad Rosierf58ae5d2012-09-12 18:24:26 +0000778 assert ((Tok.getString() == "PTR" || Tok.getString() == "ptr") &&
779 "Unexpected token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000780 Parser.Lex();
781 }
782
Chad Rosierc0a14b82012-10-24 17:22:29 +0000783 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000784 return ParseIntelBracExpression(SegReg, Size);
785
786 if (!ParseRegister(SegReg, Start, End)) {
787 // Handel SegReg : [ ... ]
788 if (getLexer().isNot(AsmToken::Colon))
789 return ErrorOperand(Start, "Expected ':' token!");
790 Parser.Lex(); // Eat :
791 if (getLexer().isNot(AsmToken::LBrac))
792 return ErrorOperand(Start, "Expected '[' token!");
793 return ParseIntelBracExpression(SegReg, Size);
794 }
Devang Pateld37ad242012-01-17 18:00:18 +0000795
796 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
797 if (getParser().ParseExpression(Disp, End)) return 0;
Chad Rosierce353b32012-10-15 17:26:38 +0000798 End = Parser.getTok().getLoc();
Chad Rosier96d58e62012-10-19 20:57:14 +0000799
800 bool NeedSizeDir = false;
801 if (!Size && isParsingInlineAsm()) {
802 if (const MCSymbolRefExpr *SymRef = dyn_cast<MCSymbolRefExpr>(Disp)) {
803 const MCSymbol &Sym = SymRef->getSymbol();
804 // FIXME: The SemaLookup will fail if the name is anything other then an
805 // identifier.
806 // FIXME: Pass a valid SMLoc.
807 SemaCallback->LookupInlineAsmIdentifier(Sym.getName(), NULL, Size);
808 NeedSizeDir = Size > 0;
809 }
810 }
Chad Rosier2a784132012-10-23 23:31:33 +0000811 if (!isParsingInlineAsm())
Chad Rosierc0a14b82012-10-24 17:22:29 +0000812 return X86Operand::CreateMem(Disp, Start, End, Size);
Chad Rosier2a784132012-10-23 23:31:33 +0000813 else
Chad Rosierd4d96ac2012-10-23 23:34:28 +0000814 // When parsing inline assembly we set the base register to a non-zero value
815 // as we don't know the actual value at this time. This is necessary to
816 // get the matching correct in some cases.
Chad Rosier2a784132012-10-23 23:31:33 +0000817 return X86Operand::CreateMem(/*SegReg*/0, Disp, /*BaseReg*/1, /*IndexReg*/0,
Chad Rosierc0a14b82012-10-24 17:22:29 +0000818 /*Scale*/1, Start, End, Size, NeedSizeDir);
819}
820
Chad Rosier22f441a2012-10-24 22:21:50 +0000821/// Parse the '.' operator.
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000822bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
823 const MCExpr **NewDisp,
824 SmallString<64> &Err) {
Chad Rosier22f441a2012-10-24 22:21:50 +0000825 AsmToken Tok = *&Parser.getTok();
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000826 uint64_t OrigDispVal, DotDispVal;
827
828 // FIXME: Handle non-constant expressions.
829 if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp)) {
830 OrigDispVal = OrigDisp->getValue();
831 } else {
832 Err = "Non-constant offsets are not supported!";
833 return true;
834 }
Chad Rosier22f441a2012-10-24 22:21:50 +0000835
836 // Drop the '.'.
837 StringRef DotDispStr = Tok.getString().drop_front(1);
838
Chad Rosier22f441a2012-10-24 22:21:50 +0000839 // .Imm gets lexed as a real.
840 if (Tok.is(AsmToken::Real)) {
841 APInt DotDisp;
842 DotDispStr.getAsInteger(10, DotDisp);
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000843 DotDispVal = DotDisp.getZExtValue();
Chad Rosierec130222012-10-25 21:51:10 +0000844 } else if (Tok.is(AsmToken::Identifier)) {
845 // We should only see an identifier when parsing the original inline asm.
846 // The front-end should rewrite this in terms of immediates.
847 assert (isParsingInlineAsm() && "Unexpected field name!");
848
849 unsigned DotDisp;
850 std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
851 if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
852 DotDisp)) {
853 Err = "Unable to lookup field reference!";
854 return true;
855 }
856 DotDispVal = DotDisp;
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000857 } else {
858 Err = "Unexpected token type!";
859 return true;
Chad Rosier22f441a2012-10-24 22:21:50 +0000860 }
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000861
Chad Rosierec130222012-10-25 21:51:10 +0000862 if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
863 SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
864 unsigned Len = DotDispStr.size();
865 unsigned Val = OrigDispVal + DotDispVal;
866 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_DotOperator, Loc, Len,
867 Val));
Chad Rosier5e6b37f2012-10-25 17:37:43 +0000868 }
869
870 *NewDisp = MCConstantExpr::Create(OrigDispVal + DotDispVal, getContext());
871 return false;
Chad Rosier22f441a2012-10-24 22:21:50 +0000872}
873
Chad Rosierc0a14b82012-10-24 17:22:29 +0000874/// Parse the 'offset' operator. This operator is used to specify the
875/// location rather then the content of a variable.
876X86Operand *X86AsmParser::ParseIntelOffsetOfOperator(SMLoc Start) {
877 SMLoc OffsetOfLoc = Start;
878 Parser.Lex(); // Eat offset.
879 Start = Parser.getTok().getLoc();
880 assert (Parser.getTok().is(AsmToken::Identifier) && "Expected an identifier");
881
Chad Rosier6e431572012-10-26 16:09:20 +0000882 SMLoc End;
Chad Rosierc0a14b82012-10-24 17:22:29 +0000883 const MCExpr *Val;
884 if (getParser().ParseExpression(Val, End))
885 return 0;
886
887 End = Parser.getTok().getLoc();
888
Chad Rosier6e431572012-10-26 16:09:20 +0000889 // Don't emit the offset operator.
890 InstInfo->AsmRewrites->push_back(AsmRewrite(AOK_Skip, OffsetOfLoc, 7));
891
Chad Rosierc0a14b82012-10-24 17:22:29 +0000892 // The offset operator will have an 'r' constraint, thus we need to create
893 // register operand to ensure proper matching. Just pick a GPR based on
894 // the size of a pointer.
895 unsigned RegNo = is64BitMode() ? X86::RBX : X86::EBX;
896 return X86Operand::CreateReg(RegNo, Start, End, OffsetOfLoc);
Devang Pateld37ad242012-01-17 18:00:18 +0000897}
898
899X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000900 SMLoc Start = Parser.getTok().getLoc(), End;
901
Chad Rosierc0a14b82012-10-24 17:22:29 +0000902 // offset operator.
903 const AsmToken &Tok = Parser.getTok();
904 if ((Tok.getString() == "offset" || Tok.getString() == "OFFSET") &&
905 isParsingInlineAsm())
906 return ParseIntelOffsetOfOperator(Start);
907
Devang Pateld37ad242012-01-17 18:00:18 +0000908 // immediate.
909 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
910 getLexer().is(AsmToken::Minus)) {
911 const MCExpr *Val;
912 if (!getParser().ParseExpression(Val, End)) {
913 End = Parser.getTok().getLoc();
914 return X86Operand::CreateImm(Val, Start, End);
915 }
916 }
917
Devang Patel0a338862012-01-12 01:36:43 +0000918 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000919 unsigned RegNo = 0;
920 if (!ParseRegister(RegNo, Start, End)) {
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000921 // If this is a segment register followed by a ':', then this is the start
922 // of a memory reference, otherwise this is a normal register reference.
923 if (getLexer().isNot(AsmToken::Colon))
924 return X86Operand::CreateReg(RegNo, Start, Parser.getTok().getLoc());
925
926 getParser().Lex(); // Eat the colon.
927 return ParseIntelMemOperand(RegNo, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000928 }
929
930 // mem operand
Chad Rosier5b0f1b32012-10-04 23:59:38 +0000931 return ParseIntelMemOperand(0, Start);
Devang Patel0a338862012-01-12 01:36:43 +0000932}
933
Devang Pateldd929fc2012-01-12 18:03:40 +0000934X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000935 switch (getLexer().getKind()) {
936 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000937 // Parse a memory operand with no segment register.
938 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000939 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000940 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000941 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000942 SMLoc Start, End;
943 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000944 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000945 Error(Start, "%eiz and %riz can only be used as index registers",
946 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000947 return 0;
948 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000949
Chris Lattnereef6d782010-04-17 18:56:34 +0000950 // If this is a segment register followed by a ':', then this is the start
951 // of a memory reference, otherwise this is a normal register reference.
952 if (getLexer().isNot(AsmToken::Colon))
953 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000954
955
Chris Lattnereef6d782010-04-17 18:56:34 +0000956 getParser().Lex(); // Eat the colon.
957 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000958 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000959 case AsmToken::Dollar: {
960 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000961 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000962 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000963 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000964 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000965 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000966 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000967 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000968 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000969}
970
Chris Lattnereef6d782010-04-17 18:56:34 +0000971/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
972/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000973X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000974
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000975 // We have to disambiguate a parenthesized expression "(4+5)" from the start
976 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000977 // only way to do this without lookahead is to eat the '(' and see what is
978 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000979 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000980 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000981 SMLoc ExprEnd;
982 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000983
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000984 // After parsing the base expression we could either have a parenthesized
985 // memory address or not. If not, return now. If so, eat the (.
986 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000987 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000988 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000989 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000990 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000991 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000992
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000993 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000994 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000995 } else {
996 // Okay, we have a '('. We don't know if this is an expression or not, but
997 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000998 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000999 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001000
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001001 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001002 // Nothing to do here, fall into the code below with the '(' part of the
1003 // memory operand consumed.
1004 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +00001005 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001006
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001007 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +00001008 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +00001009 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001010
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001011 // After parsing the base expression we could either have a parenthesized
1012 // memory address or not. If not, return now. If so, eat the (.
1013 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +00001014 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +00001015 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +00001016 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001017 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001018 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001019
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001020 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +00001021 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001022 }
1023 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001024
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001025 // If we reached here, then we just ate the ( of the memory operand. Process
1026 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +00001027 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Kevin Enderby84faf652012-03-12 21:32:09 +00001028 SMLoc IndexLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001029
Chris Lattner29ef9a22010-01-15 18:51:29 +00001030 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001031 SMLoc StartLoc, EndLoc;
1032 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001033 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +00001034 Error(StartLoc, "eiz and riz can only be used as index registers",
1035 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001036 return 0;
1037 }
Chris Lattner29ef9a22010-01-15 18:51:29 +00001038 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001039
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001040 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001041 Parser.Lex(); // Eat the comma.
Kevin Enderby84faf652012-03-12 21:32:09 +00001042 IndexLoc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001043
1044 // Following the comma we should have either an index register, or a scale
1045 // value. We don't support the later form, but we want to parse it
1046 // correctly.
1047 //
1048 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +00001049 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +00001050 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +00001051 SMLoc L;
1052 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001053
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001054 if (getLexer().isNot(AsmToken::RParen)) {
1055 // Parse the scale amount:
1056 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +00001057 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001058 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +00001059 "expected comma in scale expression");
1060 return 0;
1061 }
Sean Callananb9a25b72010-01-19 20:27:46 +00001062 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001063
1064 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001065 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001066
1067 int64_t ScaleVal;
Kevin Enderby58dfaa12012-03-09 22:24:10 +00001068 if (getParser().ParseAbsoluteExpression(ScaleVal)){
1069 Error(Loc, "expected scale expression");
Chris Lattner309264d2010-01-15 18:44:13 +00001070 return 0;
Craig Topper76bd9382012-07-18 04:59:16 +00001071 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001072
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001073 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +00001074 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
1075 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
1076 return 0;
1077 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001078 Scale = (unsigned)ScaleVal;
1079 }
1080 }
1081 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +00001082 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001083 // index.
Sean Callanan18b83232010-01-19 21:44:56 +00001084 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001085
1086 int64_t Value;
1087 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +00001088 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001089
Daniel Dunbaree910252010-08-24 19:13:38 +00001090 if (Value != 1)
1091 Warning(Loc, "scale factor without index register is ignored");
1092 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001093 }
1094 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001095
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001096 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +00001097 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001098 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +00001099 return 0;
1100 }
Sean Callanan18b83232010-01-19 21:44:56 +00001101 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001102 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +00001103
Kevin Enderby84faf652012-03-12 21:32:09 +00001104 // If we have both a base register and an index register make sure they are
1105 // both 64-bit or 32-bit registers.
Manman Ren1f7a1b62012-06-26 19:47:59 +00001106 // To support VSIB, IndexReg can be 128-bit or 256-bit registers.
Kevin Enderby84faf652012-03-12 21:32:09 +00001107 if (BaseReg != 0 && IndexReg != 0) {
1108 if (X86MCRegisterClasses[X86::GR64RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001109 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1110 X86MCRegisterClasses[X86::GR32RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001111 IndexReg != X86::RIZ) {
1112 Error(IndexLoc, "index register is 32-bit, but base register is 64-bit");
1113 return 0;
1114 }
1115 if (X86MCRegisterClasses[X86::GR32RegClassID].contains(BaseReg) &&
Manman Ren1f7a1b62012-06-26 19:47:59 +00001116 (X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg) ||
1117 X86MCRegisterClasses[X86::GR64RegClassID].contains(IndexReg)) &&
Kevin Enderby84faf652012-03-12 21:32:09 +00001118 IndexReg != X86::EIZ){
1119 Error(IndexLoc, "index register is 64-bit, but base register is 32-bit");
1120 return 0;
1121 }
1122 }
1123
Chris Lattner0a3c5a52010-01-15 19:33:43 +00001124 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
1125 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001126}
1127
Devang Pateldd929fc2012-01-12 18:03:40 +00001128bool X86AsmParser::
Chad Rosier6a020a72012-10-25 20:41:34 +00001129ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +00001130 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chad Rosier6a020a72012-10-25 20:41:34 +00001131 InstInfo = &Info;
Chris Lattner693173f2010-10-30 19:23:13 +00001132 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001133
Chris Lattnerd8f71792010-11-28 20:23:50 +00001134 // FIXME: Hack to recognize setneb as setne.
1135 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
1136 PatchedName != "setb" && PatchedName != "setnb")
1137 PatchedName = PatchedName.substr(0, Name.size()-1);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001138
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001139 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
1140 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001141 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001142 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
1143 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001144 bool IsVCMP = PatchedName[0] == 'v';
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001145 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001146 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001147 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001148 .Case("eq", 0x00)
1149 .Case("lt", 0x01)
1150 .Case("le", 0x02)
1151 .Case("unord", 0x03)
1152 .Case("neq", 0x04)
1153 .Case("nlt", 0x05)
1154 .Case("nle", 0x06)
1155 .Case("ord", 0x07)
1156 /* AVX only from here */
1157 .Case("eq_uq", 0x08)
1158 .Case("nge", 0x09)
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +00001159 .Case("ngt", 0x0A)
1160 .Case("false", 0x0B)
1161 .Case("neq_oq", 0x0C)
1162 .Case("ge", 0x0D)
1163 .Case("gt", 0x0E)
1164 .Case("true", 0x0F)
1165 .Case("eq_os", 0x10)
1166 .Case("lt_oq", 0x11)
1167 .Case("le_oq", 0x12)
1168 .Case("unord_s", 0x13)
1169 .Case("neq_us", 0x14)
1170 .Case("nlt_uq", 0x15)
1171 .Case("nle_uq", 0x16)
1172 .Case("ord_s", 0x17)
1173 .Case("eq_us", 0x18)
1174 .Case("nge_uq", 0x19)
1175 .Case("ngt_uq", 0x1A)
1176 .Case("false_os", 0x1B)
1177 .Case("neq_os", 0x1C)
1178 .Case("ge_oq", 0x1D)
1179 .Case("gt_oq", 0x1E)
1180 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001181 .Default(~0U);
Craig Topper9e6ddcb2012-03-29 07:11:23 +00001182 if (SSEComparisonCode != ~0U && (IsVCMP || SSEComparisonCode < 8)) {
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001183 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
1184 getParser().getContext());
1185 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001186 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001187 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001188 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001189 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001190 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001191 } else {
1192 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +00001193 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001194 }
1195 }
1196 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +00001197
Daniel Dunbar1b6c0602010-02-10 21:19:28 +00001198 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001199
Devang Patel885f65b2012-01-30 22:47:12 +00001200 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001201 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001202
Chris Lattner2544f422010-09-08 05:17:37 +00001203 // Determine whether this is an instruction prefix.
1204 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +00001205 Name == "lock" || Name == "rep" ||
1206 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +00001207 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +00001208 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001209
1210
Chris Lattner2544f422010-09-08 05:17:37 +00001211 // This does the actual operand parsing. Don't parse any more if we have a
1212 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
1213 // just want to parse the "lock" as the first instruction and the "incl" as
1214 // the next one.
1215 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001216
1217 // Parse '*' modifier.
1218 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001219 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001220 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001221 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001222 }
1223
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001224 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001225 if (X86Operand *Op = ParseOperand())
1226 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001227 else {
1228 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001229 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001230 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001231
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001232 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001233 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001234
1235 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001236 if (X86Operand *Op = ParseOperand())
1237 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001238 else {
1239 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001240 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001241 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001242 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001243
Chris Lattnercbf8a982010-09-11 16:18:25 +00001244 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001245 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001246 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001247 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001248 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001249 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001250
Chris Lattner2544f422010-09-08 05:17:37 +00001251 if (getLexer().is(AsmToken::EndOfStatement))
1252 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001253 else if (isPrefix && getLexer().is(AsmToken::Slash))
1254 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001255
Devang Patel885f65b2012-01-30 22:47:12 +00001256 if (ExtraImmOp && isParsingIntelSyntax())
1257 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1258
Chris Lattner98c870f2010-11-06 19:25:43 +00001259 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1260 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1261 // documented form in various unofficial manuals, so a lot of code uses it.
1262 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1263 Operands.size() == 3) {
1264 X86Operand &Op = *(X86Operand*)Operands.back();
1265 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1266 isa<MCConstantExpr>(Op.Mem.Disp) &&
1267 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1268 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1269 SMLoc Loc = Op.getEndLoc();
1270 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1271 delete &Op;
1272 }
1273 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001274 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1275 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1276 Operands.size() == 3) {
1277 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1278 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1279 isa<MCConstantExpr>(Op.Mem.Disp) &&
1280 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1281 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1282 SMLoc Loc = Op.getEndLoc();
1283 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1284 delete &Op;
1285 }
1286 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001287 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1288 if (Name.startswith("ins") && Operands.size() == 3 &&
1289 (Name == "insb" || Name == "insw" || Name == "insl")) {
1290 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1291 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1292 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1293 Operands.pop_back();
1294 Operands.pop_back();
1295 delete &Op;
1296 delete &Op2;
1297 }
1298 }
1299
1300 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1301 if (Name.startswith("outs") && Operands.size() == 3 &&
1302 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1303 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1304 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1305 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1306 Operands.pop_back();
1307 Operands.pop_back();
1308 delete &Op;
1309 delete &Op2;
1310 }
1311 }
1312
1313 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1314 if (Name.startswith("movs") && Operands.size() == 3 &&
1315 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001316 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001317 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1318 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1319 if (isSrcOp(Op) && isDstOp(Op2)) {
1320 Operands.pop_back();
1321 Operands.pop_back();
1322 delete &Op;
1323 delete &Op2;
1324 }
1325 }
1326 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1327 if (Name.startswith("lods") && Operands.size() == 3 &&
1328 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001329 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001330 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1331 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1332 if (isSrcOp(*Op1) && Op2->isReg()) {
1333 const char *ins;
1334 unsigned reg = Op2->getReg();
1335 bool isLods = Name == "lods";
1336 if (reg == X86::AL && (isLods || Name == "lodsb"))
1337 ins = "lodsb";
1338 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1339 ins = "lodsw";
1340 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1341 ins = "lodsl";
1342 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1343 ins = "lodsq";
1344 else
1345 ins = NULL;
1346 if (ins != NULL) {
1347 Operands.pop_back();
1348 Operands.pop_back();
1349 delete Op1;
1350 delete Op2;
1351 if (Name != ins)
1352 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1353 }
1354 }
1355 }
1356 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1357 if (Name.startswith("stos") && Operands.size() == 3 &&
1358 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001359 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001360 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1361 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1362 if (isDstOp(*Op2) && Op1->isReg()) {
1363 const char *ins;
1364 unsigned reg = Op1->getReg();
1365 bool isStos = Name == "stos";
1366 if (reg == X86::AL && (isStos || Name == "stosb"))
1367 ins = "stosb";
1368 else if (reg == X86::AX && (isStos || Name == "stosw"))
1369 ins = "stosw";
1370 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1371 ins = "stosl";
1372 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1373 ins = "stosq";
1374 else
1375 ins = NULL;
1376 if (ins != NULL) {
1377 Operands.pop_back();
1378 Operands.pop_back();
1379 delete Op1;
1380 delete Op2;
1381 if (Name != ins)
1382 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1383 }
1384 }
1385 }
1386
Chris Lattnere9e16a32010-09-15 04:33:27 +00001387 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001388 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001389 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001390 Name.startswith("shl") || Name.startswith("sal") ||
1391 Name.startswith("rcl") || Name.startswith("rcr") ||
1392 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001393 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001394 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001395 // Intel syntax
1396 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1397 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001398 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1399 delete Operands[2];
1400 Operands.pop_back();
Devang Patel3b96e1f2012-01-24 21:43:36 +00001401 }
1402 } else {
1403 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1404 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
Craig Topper76bd9382012-07-18 04:59:16 +00001405 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1406 delete Operands[1];
1407 Operands.erase(Operands.begin() + 1);
Devang Patel3b96e1f2012-01-24 21:43:36 +00001408 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001409 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001410 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001411
Chris Lattner15f89512011-04-09 19:41:05 +00001412 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1413 // instalias with an immediate operand yet.
1414 if (Name == "int" && Operands.size() == 2) {
1415 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1416 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1417 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1418 delete Operands[1];
1419 Operands.erase(Operands.begin() + 1);
1420 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1421 }
1422 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001423
Chris Lattner98986712010-01-14 22:21:20 +00001424 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001425}
1426
Devang Pateldd929fc2012-01-12 18:03:40 +00001427bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001428processInstruction(MCInst &Inst,
1429 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1430 switch (Inst.getOpcode()) {
1431 default: return false;
1432 case X86::AND16i16: {
1433 if (!Inst.getOperand(0).isImm() ||
1434 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1435 return false;
1436
1437 MCInst TmpInst;
1438 TmpInst.setOpcode(X86::AND16ri8);
1439 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1440 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1441 TmpInst.addOperand(Inst.getOperand(0));
1442 Inst = TmpInst;
1443 return true;
1444 }
1445 case X86::AND32i32: {
1446 if (!Inst.getOperand(0).isImm() ||
1447 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1448 return false;
1449
1450 MCInst TmpInst;
1451 TmpInst.setOpcode(X86::AND32ri8);
1452 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1453 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1454 TmpInst.addOperand(Inst.getOperand(0));
1455 Inst = TmpInst;
1456 return true;
1457 }
1458 case X86::AND64i32: {
1459 if (!Inst.getOperand(0).isImm() ||
1460 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1461 return false;
1462
1463 MCInst TmpInst;
1464 TmpInst.setOpcode(X86::AND64ri8);
1465 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1466 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1467 TmpInst.addOperand(Inst.getOperand(0));
1468 Inst = TmpInst;
1469 return true;
1470 }
Devang Patelac0f0482012-01-19 17:53:25 +00001471 case X86::XOR16i16: {
1472 if (!Inst.getOperand(0).isImm() ||
1473 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1474 return false;
1475
1476 MCInst TmpInst;
1477 TmpInst.setOpcode(X86::XOR16ri8);
1478 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1479 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1480 TmpInst.addOperand(Inst.getOperand(0));
1481 Inst = TmpInst;
1482 return true;
1483 }
1484 case X86::XOR32i32: {
1485 if (!Inst.getOperand(0).isImm() ||
1486 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1487 return false;
1488
1489 MCInst TmpInst;
1490 TmpInst.setOpcode(X86::XOR32ri8);
1491 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1492 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1493 TmpInst.addOperand(Inst.getOperand(0));
1494 Inst = TmpInst;
1495 return true;
1496 }
1497 case X86::XOR64i32: {
1498 if (!Inst.getOperand(0).isImm() ||
1499 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1500 return false;
1501
1502 MCInst TmpInst;
1503 TmpInst.setOpcode(X86::XOR64ri8);
1504 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1505 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1506 TmpInst.addOperand(Inst.getOperand(0));
1507 Inst = TmpInst;
1508 return true;
1509 }
1510 case X86::OR16i16: {
1511 if (!Inst.getOperand(0).isImm() ||
1512 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1513 return false;
1514
1515 MCInst TmpInst;
1516 TmpInst.setOpcode(X86::OR16ri8);
1517 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1518 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1519 TmpInst.addOperand(Inst.getOperand(0));
1520 Inst = TmpInst;
1521 return true;
1522 }
1523 case X86::OR32i32: {
1524 if (!Inst.getOperand(0).isImm() ||
1525 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1526 return false;
1527
1528 MCInst TmpInst;
1529 TmpInst.setOpcode(X86::OR32ri8);
1530 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1531 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1532 TmpInst.addOperand(Inst.getOperand(0));
1533 Inst = TmpInst;
1534 return true;
1535 }
1536 case X86::OR64i32: {
1537 if (!Inst.getOperand(0).isImm() ||
1538 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1539 return false;
1540
1541 MCInst TmpInst;
1542 TmpInst.setOpcode(X86::OR64ri8);
1543 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1544 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1545 TmpInst.addOperand(Inst.getOperand(0));
1546 Inst = TmpInst;
1547 return true;
1548 }
1549 case X86::CMP16i16: {
1550 if (!Inst.getOperand(0).isImm() ||
1551 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1552 return false;
1553
1554 MCInst TmpInst;
1555 TmpInst.setOpcode(X86::CMP16ri8);
1556 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1557 TmpInst.addOperand(Inst.getOperand(0));
1558 Inst = TmpInst;
1559 return true;
1560 }
1561 case X86::CMP32i32: {
1562 if (!Inst.getOperand(0).isImm() ||
1563 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1564 return false;
1565
1566 MCInst TmpInst;
1567 TmpInst.setOpcode(X86::CMP32ri8);
1568 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1569 TmpInst.addOperand(Inst.getOperand(0));
1570 Inst = TmpInst;
1571 return true;
1572 }
1573 case X86::CMP64i32: {
1574 if (!Inst.getOperand(0).isImm() ||
1575 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1576 return false;
1577
1578 MCInst TmpInst;
1579 TmpInst.setOpcode(X86::CMP64ri8);
1580 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1581 TmpInst.addOperand(Inst.getOperand(0));
1582 Inst = TmpInst;
1583 return true;
1584 }
Devang Patela951f772012-01-19 18:40:55 +00001585 case X86::ADD16i16: {
1586 if (!Inst.getOperand(0).isImm() ||
1587 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1588 return false;
1589
1590 MCInst TmpInst;
1591 TmpInst.setOpcode(X86::ADD16ri8);
1592 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1593 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1594 TmpInst.addOperand(Inst.getOperand(0));
1595 Inst = TmpInst;
1596 return true;
1597 }
1598 case X86::ADD32i32: {
1599 if (!Inst.getOperand(0).isImm() ||
1600 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1601 return false;
1602
1603 MCInst TmpInst;
1604 TmpInst.setOpcode(X86::ADD32ri8);
1605 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1606 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1607 TmpInst.addOperand(Inst.getOperand(0));
1608 Inst = TmpInst;
1609 return true;
1610 }
1611 case X86::ADD64i32: {
1612 if (!Inst.getOperand(0).isImm() ||
1613 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1614 return false;
1615
1616 MCInst TmpInst;
1617 TmpInst.setOpcode(X86::ADD64ri8);
1618 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1619 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1620 TmpInst.addOperand(Inst.getOperand(0));
1621 Inst = TmpInst;
1622 return true;
1623 }
1624 case X86::SUB16i16: {
1625 if (!Inst.getOperand(0).isImm() ||
1626 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1627 return false;
1628
1629 MCInst TmpInst;
1630 TmpInst.setOpcode(X86::SUB16ri8);
1631 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1632 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1633 TmpInst.addOperand(Inst.getOperand(0));
1634 Inst = TmpInst;
1635 return true;
1636 }
1637 case X86::SUB32i32: {
1638 if (!Inst.getOperand(0).isImm() ||
1639 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1640 return false;
1641
1642 MCInst TmpInst;
1643 TmpInst.setOpcode(X86::SUB32ri8);
1644 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1645 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1646 TmpInst.addOperand(Inst.getOperand(0));
1647 Inst = TmpInst;
1648 return true;
1649 }
1650 case X86::SUB64i32: {
1651 if (!Inst.getOperand(0).isImm() ||
1652 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1653 return false;
1654
1655 MCInst TmpInst;
1656 TmpInst.setOpcode(X86::SUB64ri8);
1657 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1658 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1659 TmpInst.addOperand(Inst.getOperand(0));
1660 Inst = TmpInst;
1661 return true;
1662 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001663 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001664}
1665
1666bool X86AsmParser::
Chad Rosier84125ca2012-10-13 00:26:04 +00001667MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
Chris Lattner7c51a312010-09-29 01:50:45 +00001668 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chad Rosier84125ca2012-10-13 00:26:04 +00001669 MCStreamer &Out, unsigned &ErrorInfo,
1670 bool MatchingInlineAsm) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001671 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001672 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1673 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Chad Rosierb4fdade2012-08-21 19:36:59 +00001674 ArrayRef<SMRange> EmptyRanges = ArrayRef<SMRange>();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001675
Chris Lattner7c51a312010-09-29 01:50:45 +00001676 // First, handle aliases that expand to multiple instructions.
1677 // FIXME: This should be replaced with a real .td file alias mechanism.
Chad Rosier4ee08082012-08-28 23:57:47 +00001678 // Also, MatchInstructionImpl should actually *do* the EmitInstruction
Chris Lattner90fd7972010-11-06 19:57:21 +00001679 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001680 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001681 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001682 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001683 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001684 MCInst Inst;
1685 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001686 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001687 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001688 Out.EmitInstruction(Inst);
Chris Lattner7c51a312010-09-29 01:50:45 +00001689
Chris Lattner0bb83a82010-09-30 16:39:29 +00001690 const char *Repl =
1691 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001692 .Case("finit", "fninit")
1693 .Case("fsave", "fnsave")
1694 .Case("fstcw", "fnstcw")
1695 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001696 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001697 .Case("fstsw", "fnstsw")
1698 .Case("fstsww", "fnstsw")
1699 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001700 .Default(0);
1701 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001702 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001703 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001704 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001705
Chris Lattnera008e8a2010-09-06 21:54:15 +00001706 bool WasOriginallyInvalidOperand = false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001707 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001708
Daniel Dunbarc918d602010-05-04 16:12:42 +00001709 // First, try a direct match.
Chad Rosier6e006d32012-10-12 22:53:36 +00001710 switch (MatchInstructionImpl(Operands, Inst,
Chad Rosier84125ca2012-10-13 00:26:04 +00001711 ErrorInfo, MatchingInlineAsm,
Devang Patelbe3e3102012-01-30 20:02:42 +00001712 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001713 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001714 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001715 // Some instructions need post-processing to, for example, tweak which
1716 // encoding is selected. Loop on it while changes happen so the
Chad Rosier36b8fed2012-06-27 22:34:28 +00001717 // individual transformations can chain off each other.
Chad Rosier7a2b6242012-10-12 23:09:25 +00001718 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001719 while (processInstruction(Inst, Operands))
1720 ;
Devang Patelb8ba13f2012-01-18 22:42:29 +00001721
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001722 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001723 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001724 Out.EmitInstruction(Inst);
1725 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001726 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001727 case Match_MissingFeature:
Chad Rosierb4fdade2012-08-21 19:36:59 +00001728 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001729 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001730 return true;
Chris Lattnera008e8a2010-09-06 21:54:15 +00001731 case Match_InvalidOperand:
1732 WasOriginallyInvalidOperand = true;
1733 break;
1734 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001735 break;
1736 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001737
Daniel Dunbarc918d602010-05-04 16:12:42 +00001738 // FIXME: Ideally, we would only attempt suffix matches for things which are
1739 // valid prefixes, and we could just infer the right unambiguous
1740 // type. However, that requires substantially more matcher support than the
1741 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001742
Daniel Dunbarc918d602010-05-04 16:12:42 +00001743 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001744 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001745 SmallString<16> Tmp;
1746 Tmp += Base;
1747 Tmp += ' ';
1748 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001749
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001750 // If this instruction starts with an 'f', then it is a floating point stack
1751 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1752 // 80-bit floating point, which use the suffixes s,l,t respectively.
1753 //
1754 // Otherwise, we assume that this may be an integer instruction, which comes
1755 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1756 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
Chad Rosier36b8fed2012-06-27 22:34:28 +00001757
Daniel Dunbarc918d602010-05-04 16:12:42 +00001758 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001759 Tmp[Base.size()] = Suffixes[0];
1760 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001761 unsigned Match1, Match2, Match3, Match4;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001762
Chad Rosier6e006d32012-10-12 22:53:36 +00001763 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1764 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001765 Tmp[Base.size()] = Suffixes[1];
Chad Rosier6e006d32012-10-12 22:53:36 +00001766 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1767 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001768 Tmp[Base.size()] = Suffixes[2];
Chad Rosier6e006d32012-10-12 22:53:36 +00001769 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1770 isParsingIntelSyntax());
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001771 Tmp[Base.size()] = Suffixes[3];
Chad Rosier6e006d32012-10-12 22:53:36 +00001772 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore,
1773 isParsingIntelSyntax());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001774
1775 // Restore the old token.
1776 Op->setTokenValue(Base);
1777
1778 // If exactly one matched, then we treat that as a successful match (and the
1779 // instruction will already have been filled in correctly, since the failing
1780 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001781 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001782 (Match1 == Match_Success) + (Match2 == Match_Success) +
1783 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001784 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001785 Inst.setLoc(IDLoc);
Chad Rosier7a2b6242012-10-12 23:09:25 +00001786 if (!MatchingInlineAsm)
Chad Rosier22685872012-10-01 23:45:51 +00001787 Out.EmitInstruction(Inst);
1788 Opcode = Inst.getOpcode();
Daniel Dunbarc918d602010-05-04 16:12:42 +00001789 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001790 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001791
Chris Lattnerec6789f2010-09-06 20:08:02 +00001792 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001793
Daniel Dunbar09062b12010-08-12 00:55:42 +00001794 // If we had multiple suffix matches, then identify this as an ambiguous
1795 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001796 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001797 char MatchChars[4];
1798 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001799 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1800 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1801 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1802 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001803
1804 SmallString<126> Msg;
1805 raw_svector_ostream OS(Msg);
1806 OS << "ambiguous instructions require an explicit suffix (could be ";
1807 for (unsigned i = 0; i != NumMatches; ++i) {
1808 if (i != 0)
1809 OS << ", ";
1810 if (i + 1 == NumMatches)
1811 OS << "or ";
1812 OS << "'" << Base << MatchChars[i] << "'";
1813 }
1814 OS << ")";
Chad Rosier7a2b6242012-10-12 23:09:25 +00001815 Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001816 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001817 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001818
Chris Lattnera008e8a2010-09-06 21:54:15 +00001819 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001820
Chris Lattnera008e8a2010-09-06 21:54:15 +00001821 // If all of the instructions reported an invalid mnemonic, then the original
1822 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001823 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1824 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001825 if (!WasOriginallyInvalidOperand) {
Chad Rosier7a2b6242012-10-12 23:09:25 +00001826 ArrayRef<SMRange> Ranges = MatchingInlineAsm ? EmptyRanges :
Chad Rosier674101e2012-08-22 19:14:29 +00001827 Op->getLocRange();
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001828 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001829 Ranges, MatchingInlineAsm);
Chris Lattnerce4a3352010-09-06 22:11:18 +00001830 }
1831
1832 // Recover location info for the operand if we know which was the problem.
Chad Rosier84125ca2012-10-13 00:26:04 +00001833 if (ErrorInfo != ~0U) {
1834 if (ErrorInfo >= Operands.size())
Chad Rosierb4fdade2012-08-21 19:36:59 +00001835 return Error(IDLoc, "too few operands for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001836 EmptyRanges, MatchingInlineAsm);
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001837
Chad Rosier84125ca2012-10-13 00:26:04 +00001838 X86Operand *Operand = (X86Operand*)Operands[ErrorInfo];
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001839 if (Operand->getStartLoc().isValid()) {
1840 SMRange OperandRange = Operand->getLocRange();
1841 return Error(Operand->getStartLoc(), "invalid operand for instruction",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001842 OperandRange, MatchingInlineAsm);
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001843 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001844 }
1845
Chad Rosierb4fdade2012-08-21 19:36:59 +00001846 return Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001847 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001848 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001849
Chris Lattnerec6789f2010-09-06 20:08:02 +00001850 // If one instruction matched with a missing feature, report this as a
1851 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001852 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1853 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001854 Error(IDLoc, "instruction requires a CPU feature not currently enabled",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001855 EmptyRanges, MatchingInlineAsm);
Chris Lattnerec6789f2010-09-06 20:08:02 +00001856 return true;
1857 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001858
Chris Lattnera008e8a2010-09-06 21:54:15 +00001859 // If one instruction matched with an invalid operand, report this as an
1860 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001861 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1862 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chad Rosierb4fdade2012-08-21 19:36:59 +00001863 Error(IDLoc, "invalid operand for instruction", EmptyRanges,
Chad Rosier7a2b6242012-10-12 23:09:25 +00001864 MatchingInlineAsm);
Chris Lattnera008e8a2010-09-06 21:54:15 +00001865 return true;
1866 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001867
Chris Lattnerec6789f2010-09-06 20:08:02 +00001868 // If all of these were an outright failure, report it in a useless way.
Chad Rosierb4fdade2012-08-21 19:36:59 +00001869 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix",
Chad Rosier7a2b6242012-10-12 23:09:25 +00001870 EmptyRanges, MatchingInlineAsm);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001871 return true;
1872}
1873
1874
Devang Pateldd929fc2012-01-12 18:03:40 +00001875bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001876 StringRef IDVal = DirectiveID.getIdentifier();
1877 if (IDVal == ".word")
1878 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001879 else if (IDVal.startswith(".code"))
1880 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chad Rosier3c4ecd72012-09-10 20:54:39 +00001881 else if (IDVal.startswith(".att_syntax")) {
1882 getParser().setAssemblerDialect(0);
1883 return false;
1884 } else if (IDVal.startswith(".intel_syntax")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001885 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001886 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1887 if(Parser.getTok().getString() == "noprefix") {
Craig Topper76bd9382012-07-18 04:59:16 +00001888 // FIXME : Handle noprefix
1889 Parser.Lex();
Devang Patelbe3e3102012-01-30 20:02:42 +00001890 } else
Craig Topper76bd9382012-07-18 04:59:16 +00001891 return true;
Devang Patelbe3e3102012-01-30 20:02:42 +00001892 }
1893 return false;
1894 }
Chris Lattner537ca842010-10-30 17:38:55 +00001895 return true;
1896}
1897
1898/// ParseDirectiveWord
1899/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001900bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001901 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1902 for (;;) {
1903 const MCExpr *Value;
1904 if (getParser().ParseExpression(Value))
1905 return true;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001906
Chris Lattner537ca842010-10-30 17:38:55 +00001907 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Chad Rosier36b8fed2012-06-27 22:34:28 +00001908
Chris Lattner537ca842010-10-30 17:38:55 +00001909 if (getLexer().is(AsmToken::EndOfStatement))
1910 break;
Chad Rosier36b8fed2012-06-27 22:34:28 +00001911
Chris Lattner537ca842010-10-30 17:38:55 +00001912 // FIXME: Improve diagnostic.
1913 if (getLexer().isNot(AsmToken::Comma))
1914 return Error(L, "unexpected token in directive");
1915 Parser.Lex();
1916 }
1917 }
Chad Rosier36b8fed2012-06-27 22:34:28 +00001918
Chris Lattner537ca842010-10-30 17:38:55 +00001919 Parser.Lex();
1920 return false;
1921}
1922
Evan Chengbd27f5a2011-07-27 00:38:12 +00001923/// ParseDirectiveCode
1924/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001925bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001926 if (IDVal == ".code32") {
1927 Parser.Lex();
1928 if (is64BitMode()) {
1929 SwitchMode();
1930 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1931 }
1932 } else if (IDVal == ".code64") {
1933 Parser.Lex();
1934 if (!is64BitMode()) {
1935 SwitchMode();
1936 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1937 }
1938 } else {
1939 return Error(L, "unexpected directive " + IDVal);
1940 }
Chris Lattner537ca842010-10-30 17:38:55 +00001941
Evan Chengbd27f5a2011-07-27 00:38:12 +00001942 return false;
1943}
Chris Lattner537ca842010-10-30 17:38:55 +00001944
1945
Sean Callanane88f5522010-01-23 02:43:15 +00001946extern "C" void LLVMInitializeX86AsmLexer();
1947
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001948// Force static initialization.
1949extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001950 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1951 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001952 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001953}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001954
Chris Lattner0692ee62010-09-06 19:11:01 +00001955#define GET_REGISTER_MATCHER
1956#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001957#include "X86GenAsmMatcher.inc"