blob: 0e8ab2982206303c56eb678c5e6924e84d1174eb [file] [log] [blame]
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001//===-- X86AsmParser.cpp - Parse X86 assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Evan Cheng94b95502011-07-26 00:24:13 +000010#include "MCTargetDesc/X86BaseInfo.h"
11#include "llvm/MC/MCTargetAsmParser.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000012#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000013#include "llvm/MC/MCExpr.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000014#include "llvm/MC/MCInst.h"
Evan Cheng5de728c2011-07-27 23:22:03 +000015#include "llvm/MC/MCRegisterInfo.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000016#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000017#include "llvm/MC/MCParser/MCAsmLexer.h"
18#include "llvm/MC/MCParser/MCAsmParser.h"
19#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Benjamin Kramer75ca4b92011-07-08 21:06:23 +000020#include "llvm/ADT/OwningPtr.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000021#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/SmallVector.h"
Chris Lattner33d60d52010-09-22 04:11:10 +000023#include "llvm/ADT/StringSwitch.h"
24#include "llvm/ADT/Twine.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000025#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000026#include "llvm/Support/TargetRegistry.h"
Daniel Dunbar09062b12010-08-12 00:55:42 +000027#include "llvm/Support/raw_ostream.h"
Evan Chengebdeeab2011-07-08 01:53:10 +000028
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000029using namespace llvm;
30
31namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000032struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000033
Devang Pateldd929fc2012-01-12 18:03:40 +000034class X86AsmParser : public MCTargetAsmParser {
Evan Chengffc0e732011-07-09 05:47:46 +000035 MCSubtargetInfo &STI;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036 MCAsmParser &Parser;
Devang Patelbe3e3102012-01-30 20:02:42 +000037 bool IntelSyntax;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000039 MCAsmParser &getParser() const { return Parser; }
40
41 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
42
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000043 bool Error(SMLoc L, const Twine &Msg,
44 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) {
45 return Parser.Error(L, Msg, Ranges);
46 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000047
Devang Pateld37ad242012-01-17 18:00:18 +000048 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
49 Error(Loc, Msg);
50 return 0;
51 }
52
Chris Lattner309264d2010-01-15 18:44:13 +000053 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000054 X86Operand *ParseATTOperand();
55 X86Operand *ParseIntelOperand();
Devang Pateld37ad242012-01-17 18:00:18 +000056 X86Operand *ParseIntelMemOperand();
Devang Patel7c64fe62012-01-23 18:31:58 +000057 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000058 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000059
60 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000061 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000062
Devang Patelb8ba13f2012-01-18 22:42:29 +000063 bool processInstruction(MCInst &Inst,
64 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
65
Chris Lattner7036f8b2010-09-29 01:42:58 +000066 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000067 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000068 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000069
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000070 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
71 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
72 bool isSrcOp(X86Operand &Op);
73
74 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
75 /// or %es:(%edi) in 32bit mode.
76 bool isDstOp(X86Operand &Op);
77
Evan Cheng59ee62d2011-07-11 03:57:24 +000078 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000079 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000080 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000081 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000082 void SwitchMode() {
83 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
84 setAvailableFeatures(FB);
85 }
Evan Chengebdeeab2011-07-08 01:53:10 +000086
Daniel Dunbar54074b52010-07-19 05:44:09 +000087 /// @name Auto-generated Matcher Functions
88 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000089
Chris Lattner0692ee62010-09-06 19:11:01 +000090#define GET_ASSEMBLER_HEADER
91#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000092
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000093 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000094
95public:
Devang Pateldd929fc2012-01-12 18:03:40 +000096 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patelbe3e3102012-01-30 20:02:42 +000097 : MCTargetAsmParser(), STI(sti), Parser(parser), IntelSyntax(false) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000098
Daniel Dunbar54074b52010-07-19 05:44:09 +000099 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +0000100 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000101 }
Roman Divackybf755322011-01-27 17:14:22 +0000102 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000103
Benjamin Kramer38e59892010-07-14 22:38:02 +0000104 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000105 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000106
107 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000108
109 bool isParsingIntelSyntax() {
110 return IntelSyntax || getParser().getAssemblerDialect();
111 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000112};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000113} // end anonymous namespace
114
Sean Callanane9b466d2010-01-23 00:40:33 +0000115/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000116/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000117
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000118static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000119
120/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000121
Devang Patelb8ba13f2012-01-18 22:42:29 +0000122static bool isImmSExti16i8Value(uint64_t Value) {
123 return (( Value <= 0x000000000000007FULL)||
124 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
125 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
126}
127
128static bool isImmSExti32i8Value(uint64_t Value) {
129 return (( Value <= 0x000000000000007FULL)||
130 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
131 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
132}
133
134static bool isImmZExtu32u8Value(uint64_t Value) {
135 return (Value <= 0x00000000000000FFULL);
136}
137
138static bool isImmSExti64i8Value(uint64_t Value) {
139 return (( Value <= 0x000000000000007FULL)||
140 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
141}
142
143static bool isImmSExti64i32Value(uint64_t Value) {
144 return (( Value <= 0x000000007FFFFFFFULL)||
145 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
146}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000147namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000148
149/// X86Operand - Instances of this class represent a parsed X86 machine
150/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000151struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000152 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000153 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000154 Register,
155 Immediate,
156 Memory
157 } Kind;
158
Chris Lattner29ef9a22010-01-15 18:51:29 +0000159 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000160
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000161 union {
162 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000163 const char *Data;
164 unsigned Length;
165 } Tok;
166
167 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000168 unsigned RegNo;
169 } Reg;
170
171 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000172 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000173 } Imm;
174
175 struct {
176 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000177 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000178 unsigned BaseReg;
179 unsigned IndexReg;
180 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000181 unsigned Size;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000182 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000183 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000184
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000185 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000186 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000187
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000188 /// getStartLoc - Get the location of the first token of this operand.
189 SMLoc getStartLoc() const { return StartLoc; }
190 /// getEndLoc - Get the location of the last token of this operand.
191 SMLoc getEndLoc() const { return EndLoc; }
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000192
193 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000194
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000195 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000196
Daniel Dunbar20927f22009-08-07 08:26:05 +0000197 StringRef getToken() const {
198 assert(Kind == Token && "Invalid access!");
199 return StringRef(Tok.Data, Tok.Length);
200 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000201 void setTokenValue(StringRef Value) {
202 assert(Kind == Token && "Invalid access!");
203 Tok.Data = Value.data();
204 Tok.Length = Value.size();
205 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000206
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000207 unsigned getReg() const {
208 assert(Kind == Register && "Invalid access!");
209 return Reg.RegNo;
210 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000211
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000212 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000213 assert(Kind == Immediate && "Invalid access!");
214 return Imm.Val;
215 }
216
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000217 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000218 assert(Kind == Memory && "Invalid access!");
219 return Mem.Disp;
220 }
221 unsigned getMemSegReg() const {
222 assert(Kind == Memory && "Invalid access!");
223 return Mem.SegReg;
224 }
225 unsigned getMemBaseReg() const {
226 assert(Kind == Memory && "Invalid access!");
227 return Mem.BaseReg;
228 }
229 unsigned getMemIndexReg() const {
230 assert(Kind == Memory && "Invalid access!");
231 return Mem.IndexReg;
232 }
233 unsigned getMemScale() const {
234 assert(Kind == Memory && "Invalid access!");
235 return Mem.Scale;
236 }
237
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000238 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000239
240 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000241
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000242 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000243 if (!isImm())
244 return false;
245
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000246 // If this isn't a constant expr, just assume it fits and let relaxation
247 // handle it.
248 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
249 if (!CE)
250 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000251
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000252 // Otherwise, check the value is in a range that makes sense for this
253 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000254 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000255 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000256 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +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 Dunbar1fe591d2010-05-20 20:20:39 +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 isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000269 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000270 bool isImmZExtu32u8() const {
271 if (!isImm())
272 return false;
273
274 // 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;
279
280 // Otherwise, check the value is in a range that makes sense for this
281 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000282 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000283 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000284 bool isImmSExti64i8() 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 isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000297 }
298 bool isImmSExti64i32() 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 isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000311 }
312
Daniel Dunbar20927f22009-08-07 08:26:05 +0000313 bool isMem() const { return Kind == Memory; }
Devang Patelc59d9df2012-01-12 01:51:42 +0000314 bool isMem8() const {
315 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
316 }
317 bool isMem16() const {
318 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
319 }
320 bool isMem32() const {
321 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
322 }
323 bool isMem64() const {
324 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
325 }
326 bool isMem80() const {
327 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
328 }
329 bool isMem128() const {
330 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
331 }
332 bool isMem256() const {
333 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
334 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000335
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000336 bool isAbsMem() const {
337 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000338 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000339 }
340
Daniel Dunbar20927f22009-08-07 08:26:05 +0000341 bool isReg() const { return Kind == Register; }
342
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000343 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
344 // Add as immediates when possible.
345 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
346 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
347 else
348 Inst.addOperand(MCOperand::CreateExpr(Expr));
349 }
350
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000351 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000352 assert(N == 1 && "Invalid number of operands!");
353 Inst.addOperand(MCOperand::CreateReg(getReg()));
354 }
355
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000356 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000357 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000358 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000359 }
360
Devang Patelc59d9df2012-01-12 01:51:42 +0000361 void addMem8Operands(MCInst &Inst, unsigned N) const {
362 addMemOperands(Inst, N);
363 }
364 void addMem16Operands(MCInst &Inst, unsigned N) const {
365 addMemOperands(Inst, N);
366 }
367 void addMem32Operands(MCInst &Inst, unsigned N) const {
368 addMemOperands(Inst, N);
369 }
370 void addMem64Operands(MCInst &Inst, unsigned N) const {
371 addMemOperands(Inst, N);
372 }
373 void addMem80Operands(MCInst &Inst, unsigned N) const {
374 addMemOperands(Inst, N);
375 }
376 void addMem128Operands(MCInst &Inst, unsigned N) const {
377 addMemOperands(Inst, N);
378 }
379 void addMem256Operands(MCInst &Inst, unsigned N) const {
380 addMemOperands(Inst, N);
381 }
382
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000383 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000384 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000385 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
386 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
387 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000388 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000389 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
390 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000391
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000392 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
393 assert((N == 1) && "Invalid number of operands!");
394 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
395 }
396
Chris Lattnerb4307b32010-01-15 19:28:38 +0000397 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000398 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
399 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000400 Res->Tok.Data = Str.data();
401 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000402 return Res;
403 }
404
Chris Lattner29ef9a22010-01-15 18:51:29 +0000405 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000406 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000407 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000408 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000409 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000410
Chris Lattnerb4307b32010-01-15 19:28:38 +0000411 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
412 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000413 Res->Imm.Val = Val;
414 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000415 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000416
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000417 /// Create an absolute memory operand.
418 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000419 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000420 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
421 Res->Mem.SegReg = 0;
422 Res->Mem.Disp = Disp;
423 Res->Mem.BaseReg = 0;
424 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000425 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000426 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000427 return Res;
428 }
429
430 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000431 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
432 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000433 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
434 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000435 // We should never just have a displacement, that should be parsed as an
436 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000437 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
438
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000439 // The scale should always be one of {1,2,4,8}.
440 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000441 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000442 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000443 Res->Mem.SegReg = SegReg;
444 Res->Mem.Disp = Disp;
445 Res->Mem.BaseReg = BaseReg;
446 Res->Mem.IndexReg = IndexReg;
447 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000448 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000449 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000450 }
451};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000452
Chris Lattner37dfdec2009-07-29 06:33:53 +0000453} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000454
Devang Pateldd929fc2012-01-12 18:03:40 +0000455bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000456 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000457
458 return (Op.isMem() &&
459 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
460 isa<MCConstantExpr>(Op.Mem.Disp) &&
461 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
462 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
463}
464
Devang Pateldd929fc2012-01-12 18:03:40 +0000465bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000466 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000467
468 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
469 isa<MCConstantExpr>(Op.Mem.Disp) &&
470 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
471 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
472}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000473
Devang Pateldd929fc2012-01-12 18:03:40 +0000474bool X86AsmParser::ParseRegister(unsigned &RegNo,
475 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000476 RegNo = 0;
Devang Patelbe3e3102012-01-30 20:02:42 +0000477 if (!isParsingIntelSyntax()) {
Devang Patel1aea4302012-01-20 22:32:05 +0000478 const AsmToken &TokPercent = Parser.getTok();
Devang Pateld37ad242012-01-17 18:00:18 +0000479 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
480 StartLoc = TokPercent.getLoc();
481 Parser.Lex(); // Eat percent token.
482 }
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000483
Sean Callanan18b83232010-01-19 21:44:56 +0000484 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000485 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000486 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000487 return Error(StartLoc, "invalid register name",
488 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000489 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000490
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000491 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000492
Chris Lattner33d60d52010-09-22 04:11:10 +0000493 // If the match failed, try the register name as lowercase.
494 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000495 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000496
Evan Cheng5de728c2011-07-27 23:22:03 +0000497 if (!is64BitMode()) {
498 // FIXME: This should be done using Requires<In32BitMode> and
499 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
500 // checked.
501 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
502 // REX prefix.
503 if (RegNo == X86::RIZ ||
504 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
505 X86II::isX86_64NonExtLowByteReg(RegNo) ||
506 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000507 return Error(StartLoc, "register %"
508 + Tok.getString() + " is only available in 64-bit mode",
509 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000510 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000511
Chris Lattner33d60d52010-09-22 04:11:10 +0000512 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
513 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000514 RegNo = X86::ST0;
515 EndLoc = Tok.getLoc();
516 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000517
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000518 // Check to see if we have '(4)' after %st.
519 if (getLexer().isNot(AsmToken::LParen))
520 return false;
521 // Lex the paren.
522 getParser().Lex();
523
524 const AsmToken &IntTok = Parser.getTok();
525 if (IntTok.isNot(AsmToken::Integer))
526 return Error(IntTok.getLoc(), "expected stack index");
527 switch (IntTok.getIntVal()) {
528 case 0: RegNo = X86::ST0; break;
529 case 1: RegNo = X86::ST1; break;
530 case 2: RegNo = X86::ST2; break;
531 case 3: RegNo = X86::ST3; break;
532 case 4: RegNo = X86::ST4; break;
533 case 5: RegNo = X86::ST5; break;
534 case 6: RegNo = X86::ST6; break;
535 case 7: RegNo = X86::ST7; break;
536 default: return Error(IntTok.getLoc(), "invalid stack index");
537 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000538
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000539 if (getParser().Lex().isNot(AsmToken::RParen))
540 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000541
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000542 EndLoc = Tok.getLoc();
543 Parser.Lex(); // Eat ')'
544 return false;
545 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000546
Chris Lattner645b2092010-06-24 07:29:18 +0000547 // If this is "db[0-7]", match it as an alias
548 // for dr[0-7].
549 if (RegNo == 0 && Tok.getString().size() == 3 &&
550 Tok.getString().startswith("db")) {
551 switch (Tok.getString()[2]) {
552 case '0': RegNo = X86::DR0; break;
553 case '1': RegNo = X86::DR1; break;
554 case '2': RegNo = X86::DR2; break;
555 case '3': RegNo = X86::DR3; break;
556 case '4': RegNo = X86::DR4; break;
557 case '5': RegNo = X86::DR5; break;
558 case '6': RegNo = X86::DR6; break;
559 case '7': RegNo = X86::DR7; break;
560 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000561
Chris Lattner645b2092010-06-24 07:29:18 +0000562 if (RegNo != 0) {
563 EndLoc = Tok.getLoc();
564 Parser.Lex(); // Eat it.
565 return false;
566 }
567 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000568
Devang Patel1aea4302012-01-20 22:32:05 +0000569 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000570 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000571 return Error(StartLoc, "invalid register name",
572 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000573 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000574
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000575 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000576 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000577 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000578}
579
Devang Pateldd929fc2012-01-12 18:03:40 +0000580X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000581 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000582 return ParseIntelOperand();
583 return ParseATTOperand();
584}
585
Devang Pateld37ad242012-01-17 18:00:18 +0000586/// getIntelMemOperandSize - Return intel memory operand size.
587static unsigned getIntelMemOperandSize(StringRef OpStr) {
588 unsigned Size = 0;
Devang Patel0a338862012-01-12 01:36:43 +0000589 if (OpStr == "BYTE") Size = 8;
590 if (OpStr == "WORD") Size = 16;
591 if (OpStr == "DWORD") Size = 32;
592 if (OpStr == "QWORD") Size = 64;
593 if (OpStr == "XWORD") Size = 80;
594 if (OpStr == "XMMWORD") Size = 128;
595 if (OpStr == "YMMWORD") Size = 256;
Devang Pateld37ad242012-01-17 18:00:18 +0000596 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000597}
598
Devang Patel7c64fe62012-01-23 18:31:58 +0000599X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
600 unsigned Size) {
601 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000602 SMLoc Start = Parser.getTok().getLoc(), End;
603
Devang Pateld37ad242012-01-17 18:00:18 +0000604 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
605 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
606
607 // Eat '['
608 if (getLexer().isNot(AsmToken::LBrac))
609 return ErrorOperand(Start, "Expected '[' token!");
610 Parser.Lex();
611
612 if (getLexer().is(AsmToken::Identifier)) {
613 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000614 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000615 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000616 if (getParser().ParseExpression(Disp, End)) return 0;
617 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000618 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000619 Parser.Lex();
620 return X86Operand::CreateMem(Disp, Start, End, Size);
621 }
622 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000623 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000624 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000625 SMLoc Loc = Parser.getTok().getLoc();
626 if (getLexer().is(AsmToken::RBrac)) {
627 // Handle '[' number ']'
628 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000629 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
630 if (SegReg)
631 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
632 Start, End, Size);
633 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000634 } else if (getLexer().is(AsmToken::Star)) {
635 // Handle '[' Scale*IndexReg ']'
636 Parser.Lex();
637 SMLoc IdxRegLoc = Parser.getTok().getLoc();
638 if (ParseRegister(IndexReg, IdxRegLoc, End))
639 return ErrorOperand(IdxRegLoc, "Expected register");
640 Scale = Val;
641 } else
642 return ErrorOperand(Loc, "Unepxeted token");
Devang Pateld37ad242012-01-17 18:00:18 +0000643 }
644
645 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
646 bool isPlus = getLexer().is(AsmToken::Plus);
647 Parser.Lex();
648 SMLoc PlusLoc = Parser.getTok().getLoc();
649 if (getLexer().is(AsmToken::Integer)) {
650 int64_t Val = Parser.getTok().getIntVal();
651 Parser.Lex();
652 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000653 Parser.Lex();
654 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Devang Patel1aea4302012-01-20 22:32:05 +0000655 if (ParseRegister(IndexReg, IdxRegLoc, End))
656 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000657 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000658 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000659 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000660 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000661 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000662 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000663 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000664 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000665 End = Parser.getTok().getLoc();
666 if (!IndexReg)
667 ParseRegister(IndexReg, Start, End);
668 else if (getParser().ParseExpression(Disp, End)) return 0;
669 }
Devang Pateld37ad242012-01-17 18:00:18 +0000670 }
671
672 if (getLexer().isNot(AsmToken::RBrac))
673 if (getParser().ParseExpression(Disp, End)) return 0;
674
675 End = Parser.getTok().getLoc();
676 if (getLexer().isNot(AsmToken::RBrac))
677 return ErrorOperand(End, "expected ']' token!");
678 Parser.Lex();
679 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000680
681 // handle [-42]
682 if (!BaseReg && !IndexReg)
683 return X86Operand::CreateMem(Disp, Start, End, Size);
684
Devang Pateld37ad242012-01-17 18:00:18 +0000685 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000686 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000687}
688
689/// ParseIntelMemOperand - Parse intel style memory operand.
690X86Operand *X86AsmParser::ParseIntelMemOperand() {
691 const AsmToken &Tok = Parser.getTok();
692 SMLoc Start = Parser.getTok().getLoc(), End;
Devang Patel7c64fe62012-01-23 18:31:58 +0000693 unsigned SegReg = 0;
Devang Pateld37ad242012-01-17 18:00:18 +0000694
695 unsigned Size = getIntelMemOperandSize(Tok.getString());
696 if (Size) {
697 Parser.Lex();
698 assert (Tok.getString() == "PTR" && "Unexpected token!");
699 Parser.Lex();
700 }
701
702 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000703 return ParseIntelBracExpression(SegReg, Size);
704
705 if (!ParseRegister(SegReg, Start, End)) {
706 // Handel SegReg : [ ... ]
707 if (getLexer().isNot(AsmToken::Colon))
708 return ErrorOperand(Start, "Expected ':' token!");
709 Parser.Lex(); // Eat :
710 if (getLexer().isNot(AsmToken::LBrac))
711 return ErrorOperand(Start, "Expected '[' token!");
712 return ParseIntelBracExpression(SegReg, Size);
713 }
Devang Pateld37ad242012-01-17 18:00:18 +0000714
715 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
716 if (getParser().ParseExpression(Disp, End)) return 0;
717 return X86Operand::CreateMem(Disp, Start, End, Size);
718}
719
720X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000721 SMLoc Start = Parser.getTok().getLoc(), End;
722
723 // immediate.
724 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
725 getLexer().is(AsmToken::Minus)) {
726 const MCExpr *Val;
727 if (!getParser().ParseExpression(Val, End)) {
728 End = Parser.getTok().getLoc();
729 return X86Operand::CreateImm(Val, Start, End);
730 }
731 }
732
Devang Patel0a338862012-01-12 01:36:43 +0000733 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000734 unsigned RegNo = 0;
735 if (!ParseRegister(RegNo, Start, End)) {
Devang Patel0a338862012-01-12 01:36:43 +0000736 End = Parser.getTok().getLoc();
737 return X86Operand::CreateReg(RegNo, Start, End);
738 }
739
740 // mem operand
Devang Pateld37ad242012-01-17 18:00:18 +0000741 return ParseIntelMemOperand();
Devang Patel0a338862012-01-12 01:36:43 +0000742}
743
Devang Pateldd929fc2012-01-12 18:03:40 +0000744X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000745 switch (getLexer().getKind()) {
746 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000747 // Parse a memory operand with no segment register.
748 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000749 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000750 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000751 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000752 SMLoc Start, End;
753 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000754 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000755 Error(Start, "%eiz and %riz can only be used as index registers",
756 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000757 return 0;
758 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000759
Chris Lattnereef6d782010-04-17 18:56:34 +0000760 // If this is a segment register followed by a ':', then this is the start
761 // of a memory reference, otherwise this is a normal register reference.
762 if (getLexer().isNot(AsmToken::Colon))
763 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000764
765
Chris Lattnereef6d782010-04-17 18:56:34 +0000766 getParser().Lex(); // Eat the colon.
767 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000768 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000769 case AsmToken::Dollar: {
770 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000771 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000772 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000773 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000774 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000775 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000776 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000777 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000778 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000779}
780
Chris Lattnereef6d782010-04-17 18:56:34 +0000781/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
782/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000783X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000784
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000785 // We have to disambiguate a parenthesized expression "(4+5)" from the start
786 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000787 // only way to do this without lookahead is to eat the '(' and see what is
788 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000789 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000790 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000791 SMLoc ExprEnd;
792 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000793
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000794 // After parsing the base expression we could either have a parenthesized
795 // memory address or not. If not, return now. If so, eat the (.
796 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000797 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000798 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000799 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000800 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000801 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000802
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000803 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000804 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000805 } else {
806 // Okay, we have a '('. We don't know if this is an expression or not, but
807 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000808 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000809 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000810
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000811 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000812 // Nothing to do here, fall into the code below with the '(' part of the
813 // memory operand consumed.
814 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000815 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000816
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000817 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000818 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000819 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000820
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000821 // After parsing the base expression we could either have a parenthesized
822 // memory address or not. If not, return now. If so, eat the (.
823 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000824 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000825 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000826 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000827 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000828 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000829
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000830 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000831 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000832 }
833 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000834
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000835 // If we reached here, then we just ate the ( of the memory operand. Process
836 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000837 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000838
Chris Lattner29ef9a22010-01-15 18:51:29 +0000839 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000840 SMLoc StartLoc, EndLoc;
841 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000842 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000843 Error(StartLoc, "eiz and riz can only be used as index registers",
844 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000845 return 0;
846 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000847 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000848
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000849 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000850 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000851
852 // Following the comma we should have either an index register, or a scale
853 // value. We don't support the later form, but we want to parse it
854 // correctly.
855 //
856 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000857 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000858 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000859 SMLoc L;
860 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000861
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000862 if (getLexer().isNot(AsmToken::RParen)) {
863 // Parse the scale amount:
864 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000865 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000866 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000867 "expected comma in scale expression");
868 return 0;
869 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000870 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000871
872 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000873 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000874
875 int64_t ScaleVal;
876 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000877 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000878
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000879 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000880 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
881 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
882 return 0;
883 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000884 Scale = (unsigned)ScaleVal;
885 }
886 }
887 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000888 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000889 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000890 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000891
892 int64_t Value;
893 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000894 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000895
Daniel Dunbaree910252010-08-24 19:13:38 +0000896 if (Value != 1)
897 Warning(Loc, "scale factor without index register is ignored");
898 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000899 }
900 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000901
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000902 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000903 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000904 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000905 return 0;
906 }
Sean Callanan18b83232010-01-19 21:44:56 +0000907 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000908 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000909
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000910 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
911 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000912}
913
Devang Pateldd929fc2012-01-12 18:03:40 +0000914bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000915ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000916 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000917 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000918
Chris Lattnerd8f71792010-11-28 20:23:50 +0000919 // FIXME: Hack to recognize setneb as setne.
920 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
921 PatchedName != "setb" && PatchedName != "setnb")
922 PatchedName = PatchedName.substr(0, Name.size()-1);
923
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000924 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
925 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000926 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000927 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
928 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000929 bool IsVCMP = PatchedName.startswith("vcmp");
930 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000931 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000932 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000933 .Case("eq", 0)
934 .Case("lt", 1)
935 .Case("le", 2)
936 .Case("unord", 3)
937 .Case("neq", 4)
938 .Case("nlt", 5)
939 .Case("nle", 6)
940 .Case("ord", 7)
941 .Case("eq_uq", 8)
942 .Case("nge", 9)
943 .Case("ngt", 0x0A)
944 .Case("false", 0x0B)
945 .Case("neq_oq", 0x0C)
946 .Case("ge", 0x0D)
947 .Case("gt", 0x0E)
948 .Case("true", 0x0F)
949 .Case("eq_os", 0x10)
950 .Case("lt_oq", 0x11)
951 .Case("le_oq", 0x12)
952 .Case("unord_s", 0x13)
953 .Case("neq_us", 0x14)
954 .Case("nlt_uq", 0x15)
955 .Case("nle_uq", 0x16)
956 .Case("ord_s", 0x17)
957 .Case("eq_us", 0x18)
958 .Case("nge_uq", 0x19)
959 .Case("ngt_uq", 0x1A)
960 .Case("false_os", 0x1B)
961 .Case("neq_os", 0x1C)
962 .Case("ge_oq", 0x1D)
963 .Case("gt_oq", 0x1E)
964 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000965 .Default(~0U);
966 if (SSEComparisonCode != ~0U) {
967 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
968 getParser().getContext());
969 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000970 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000971 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000972 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000973 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000974 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000975 } else {
976 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000977 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000978 }
979 }
980 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000981
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000982 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000983
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000984 if (ExtraImmOp)
985 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000986
987
Chris Lattner2544f422010-09-08 05:17:37 +0000988 // Determine whether this is an instruction prefix.
989 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000990 Name == "lock" || Name == "rep" ||
991 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000992 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000993 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000994
995
Chris Lattner2544f422010-09-08 05:17:37 +0000996 // This does the actual operand parsing. Don't parse any more if we have a
997 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
998 // just want to parse the "lock" as the first instruction and the "incl" as
999 // the next one.
1000 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001001
1002 // Parse '*' modifier.
1003 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001004 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001005 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001006 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001007 }
1008
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001009 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001010 if (X86Operand *Op = ParseOperand())
1011 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001012 else {
1013 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001014 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001015 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001016
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001017 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001018 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001019
1020 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001021 if (X86Operand *Op = ParseOperand())
1022 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001023 else {
1024 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001025 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001026 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001027 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001028
Chris Lattnercbf8a982010-09-11 16:18:25 +00001029 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001030 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001031 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001032 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001033 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001034 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001035
Chris Lattner2544f422010-09-08 05:17:37 +00001036 if (getLexer().is(AsmToken::EndOfStatement))
1037 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001038 else if (isPrefix && getLexer().is(AsmToken::Slash))
1039 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001040
Chris Lattner98c870f2010-11-06 19:25:43 +00001041 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1042 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1043 // documented form in various unofficial manuals, so a lot of code uses it.
1044 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1045 Operands.size() == 3) {
1046 X86Operand &Op = *(X86Operand*)Operands.back();
1047 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1048 isa<MCConstantExpr>(Op.Mem.Disp) &&
1049 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1050 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1051 SMLoc Loc = Op.getEndLoc();
1052 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1053 delete &Op;
1054 }
1055 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001056 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1057 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1058 Operands.size() == 3) {
1059 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1060 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1061 isa<MCConstantExpr>(Op.Mem.Disp) &&
1062 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1063 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1064 SMLoc Loc = Op.getEndLoc();
1065 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1066 delete &Op;
1067 }
1068 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001069 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1070 if (Name.startswith("ins") && Operands.size() == 3 &&
1071 (Name == "insb" || Name == "insw" || Name == "insl")) {
1072 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1073 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1074 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1075 Operands.pop_back();
1076 Operands.pop_back();
1077 delete &Op;
1078 delete &Op2;
1079 }
1080 }
1081
1082 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1083 if (Name.startswith("outs") && Operands.size() == 3 &&
1084 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1085 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1086 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1087 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1088 Operands.pop_back();
1089 Operands.pop_back();
1090 delete &Op;
1091 delete &Op2;
1092 }
1093 }
1094
1095 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1096 if (Name.startswith("movs") && Operands.size() == 3 &&
1097 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001098 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001099 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1100 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1101 if (isSrcOp(Op) && isDstOp(Op2)) {
1102 Operands.pop_back();
1103 Operands.pop_back();
1104 delete &Op;
1105 delete &Op2;
1106 }
1107 }
1108 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1109 if (Name.startswith("lods") && Operands.size() == 3 &&
1110 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001111 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001112 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1113 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1114 if (isSrcOp(*Op1) && Op2->isReg()) {
1115 const char *ins;
1116 unsigned reg = Op2->getReg();
1117 bool isLods = Name == "lods";
1118 if (reg == X86::AL && (isLods || Name == "lodsb"))
1119 ins = "lodsb";
1120 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1121 ins = "lodsw";
1122 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1123 ins = "lodsl";
1124 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1125 ins = "lodsq";
1126 else
1127 ins = NULL;
1128 if (ins != NULL) {
1129 Operands.pop_back();
1130 Operands.pop_back();
1131 delete Op1;
1132 delete Op2;
1133 if (Name != ins)
1134 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1135 }
1136 }
1137 }
1138 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1139 if (Name.startswith("stos") && Operands.size() == 3 &&
1140 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001141 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001142 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1143 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1144 if (isDstOp(*Op2) && Op1->isReg()) {
1145 const char *ins;
1146 unsigned reg = Op1->getReg();
1147 bool isStos = Name == "stos";
1148 if (reg == X86::AL && (isStos || Name == "stosb"))
1149 ins = "stosb";
1150 else if (reg == X86::AX && (isStos || Name == "stosw"))
1151 ins = "stosw";
1152 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1153 ins = "stosl";
1154 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1155 ins = "stosq";
1156 else
1157 ins = NULL;
1158 if (ins != NULL) {
1159 Operands.pop_back();
1160 Operands.pop_back();
1161 delete Op1;
1162 delete Op2;
1163 if (Name != ins)
1164 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1165 }
1166 }
1167 }
1168
Chris Lattnere9e16a32010-09-15 04:33:27 +00001169 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001170 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001171 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001172 Name.startswith("shl") || Name.startswith("sal") ||
1173 Name.startswith("rcl") || Name.startswith("rcr") ||
1174 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001175 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001176 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001177 // Intel syntax
1178 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1179 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1180 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1181 delete Operands[2];
1182 Operands.pop_back();
1183 }
1184 } else {
1185 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1186 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1187 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1188 delete Operands[1];
1189 Operands.erase(Operands.begin() + 1);
1190 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001191 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001192 }
Chris Lattner15f89512011-04-09 19:41:05 +00001193
1194 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1195 // instalias with an immediate operand yet.
1196 if (Name == "int" && Operands.size() == 2) {
1197 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1198 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1199 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1200 delete Operands[1];
1201 Operands.erase(Operands.begin() + 1);
1202 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1203 }
1204 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001205
Chris Lattner98986712010-01-14 22:21:20 +00001206 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001207}
1208
Devang Pateldd929fc2012-01-12 18:03:40 +00001209bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001210processInstruction(MCInst &Inst,
1211 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1212 switch (Inst.getOpcode()) {
1213 default: return false;
1214 case X86::AND16i16: {
1215 if (!Inst.getOperand(0).isImm() ||
1216 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1217 return false;
1218
1219 MCInst TmpInst;
1220 TmpInst.setOpcode(X86::AND16ri8);
1221 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1222 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1223 TmpInst.addOperand(Inst.getOperand(0));
1224 Inst = TmpInst;
1225 return true;
1226 }
1227 case X86::AND32i32: {
1228 if (!Inst.getOperand(0).isImm() ||
1229 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1230 return false;
1231
1232 MCInst TmpInst;
1233 TmpInst.setOpcode(X86::AND32ri8);
1234 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1235 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1236 TmpInst.addOperand(Inst.getOperand(0));
1237 Inst = TmpInst;
1238 return true;
1239 }
1240 case X86::AND64i32: {
1241 if (!Inst.getOperand(0).isImm() ||
1242 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1243 return false;
1244
1245 MCInst TmpInst;
1246 TmpInst.setOpcode(X86::AND64ri8);
1247 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1248 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1249 TmpInst.addOperand(Inst.getOperand(0));
1250 Inst = TmpInst;
1251 return true;
1252 }
Devang Patelac0f0482012-01-19 17:53:25 +00001253 case X86::XOR16i16: {
1254 if (!Inst.getOperand(0).isImm() ||
1255 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1256 return false;
1257
1258 MCInst TmpInst;
1259 TmpInst.setOpcode(X86::XOR16ri8);
1260 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1261 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1262 TmpInst.addOperand(Inst.getOperand(0));
1263 Inst = TmpInst;
1264 return true;
1265 }
1266 case X86::XOR32i32: {
1267 if (!Inst.getOperand(0).isImm() ||
1268 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1269 return false;
1270
1271 MCInst TmpInst;
1272 TmpInst.setOpcode(X86::XOR32ri8);
1273 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1274 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1275 TmpInst.addOperand(Inst.getOperand(0));
1276 Inst = TmpInst;
1277 return true;
1278 }
1279 case X86::XOR64i32: {
1280 if (!Inst.getOperand(0).isImm() ||
1281 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1282 return false;
1283
1284 MCInst TmpInst;
1285 TmpInst.setOpcode(X86::XOR64ri8);
1286 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1287 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1288 TmpInst.addOperand(Inst.getOperand(0));
1289 Inst = TmpInst;
1290 return true;
1291 }
1292 case X86::OR16i16: {
1293 if (!Inst.getOperand(0).isImm() ||
1294 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1295 return false;
1296
1297 MCInst TmpInst;
1298 TmpInst.setOpcode(X86::OR16ri8);
1299 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1300 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1301 TmpInst.addOperand(Inst.getOperand(0));
1302 Inst = TmpInst;
1303 return true;
1304 }
1305 case X86::OR32i32: {
1306 if (!Inst.getOperand(0).isImm() ||
1307 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1308 return false;
1309
1310 MCInst TmpInst;
1311 TmpInst.setOpcode(X86::OR32ri8);
1312 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1313 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1314 TmpInst.addOperand(Inst.getOperand(0));
1315 Inst = TmpInst;
1316 return true;
1317 }
1318 case X86::OR64i32: {
1319 if (!Inst.getOperand(0).isImm() ||
1320 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1321 return false;
1322
1323 MCInst TmpInst;
1324 TmpInst.setOpcode(X86::OR64ri8);
1325 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1326 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1327 TmpInst.addOperand(Inst.getOperand(0));
1328 Inst = TmpInst;
1329 return true;
1330 }
1331 case X86::CMP16i16: {
1332 if (!Inst.getOperand(0).isImm() ||
1333 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1334 return false;
1335
1336 MCInst TmpInst;
1337 TmpInst.setOpcode(X86::CMP16ri8);
1338 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1339 TmpInst.addOperand(Inst.getOperand(0));
1340 Inst = TmpInst;
1341 return true;
1342 }
1343 case X86::CMP32i32: {
1344 if (!Inst.getOperand(0).isImm() ||
1345 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1346 return false;
1347
1348 MCInst TmpInst;
1349 TmpInst.setOpcode(X86::CMP32ri8);
1350 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1351 TmpInst.addOperand(Inst.getOperand(0));
1352 Inst = TmpInst;
1353 return true;
1354 }
1355 case X86::CMP64i32: {
1356 if (!Inst.getOperand(0).isImm() ||
1357 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1358 return false;
1359
1360 MCInst TmpInst;
1361 TmpInst.setOpcode(X86::CMP64ri8);
1362 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1363 TmpInst.addOperand(Inst.getOperand(0));
1364 Inst = TmpInst;
1365 return true;
1366 }
Devang Patela951f772012-01-19 18:40:55 +00001367 case X86::ADD16i16: {
1368 if (!Inst.getOperand(0).isImm() ||
1369 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1370 return false;
1371
1372 MCInst TmpInst;
1373 TmpInst.setOpcode(X86::ADD16ri8);
1374 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1375 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1376 TmpInst.addOperand(Inst.getOperand(0));
1377 Inst = TmpInst;
1378 return true;
1379 }
1380 case X86::ADD32i32: {
1381 if (!Inst.getOperand(0).isImm() ||
1382 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1383 return false;
1384
1385 MCInst TmpInst;
1386 TmpInst.setOpcode(X86::ADD32ri8);
1387 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1388 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1389 TmpInst.addOperand(Inst.getOperand(0));
1390 Inst = TmpInst;
1391 return true;
1392 }
1393 case X86::ADD64i32: {
1394 if (!Inst.getOperand(0).isImm() ||
1395 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1396 return false;
1397
1398 MCInst TmpInst;
1399 TmpInst.setOpcode(X86::ADD64ri8);
1400 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1401 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1402 TmpInst.addOperand(Inst.getOperand(0));
1403 Inst = TmpInst;
1404 return true;
1405 }
1406 case X86::SUB16i16: {
1407 if (!Inst.getOperand(0).isImm() ||
1408 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1409 return false;
1410
1411 MCInst TmpInst;
1412 TmpInst.setOpcode(X86::SUB16ri8);
1413 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1414 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1415 TmpInst.addOperand(Inst.getOperand(0));
1416 Inst = TmpInst;
1417 return true;
1418 }
1419 case X86::SUB32i32: {
1420 if (!Inst.getOperand(0).isImm() ||
1421 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1422 return false;
1423
1424 MCInst TmpInst;
1425 TmpInst.setOpcode(X86::SUB32ri8);
1426 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1427 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1428 TmpInst.addOperand(Inst.getOperand(0));
1429 Inst = TmpInst;
1430 return true;
1431 }
1432 case X86::SUB64i32: {
1433 if (!Inst.getOperand(0).isImm() ||
1434 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1435 return false;
1436
1437 MCInst TmpInst;
1438 TmpInst.setOpcode(X86::SUB64ri8);
1439 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1440 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1441 TmpInst.addOperand(Inst.getOperand(0));
1442 Inst = TmpInst;
1443 return true;
1444 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001445 }
1446 return false;
1447}
1448
1449bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001450MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001451 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001452 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001453 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001454 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1455 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001456
Chris Lattner7c51a312010-09-29 01:50:45 +00001457 // First, handle aliases that expand to multiple instructions.
1458 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +00001459 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
1460 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001461 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001462 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001463 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001464 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001465 MCInst Inst;
1466 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001467 Inst.setLoc(IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001468 Out.EmitInstruction(Inst);
1469
Chris Lattner0bb83a82010-09-30 16:39:29 +00001470 const char *Repl =
1471 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001472 .Case("finit", "fninit")
1473 .Case("fsave", "fnsave")
1474 .Case("fstcw", "fnstcw")
1475 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001476 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001477 .Case("fstsw", "fnstsw")
1478 .Case("fstsww", "fnstsw")
1479 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001480 .Default(0);
1481 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001482 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001483 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001484 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001485
Chris Lattnera008e8a2010-09-06 21:54:15 +00001486 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +00001487 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001488 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001489
Daniel Dunbarc918d602010-05-04 16:12:42 +00001490 // First, try a direct match.
Devang Patelbe3e3102012-01-30 20:02:42 +00001491 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo,
1492 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001493 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001494 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001495 // Some instructions need post-processing to, for example, tweak which
1496 // encoding is selected. Loop on it while changes happen so the
1497 // individual transformations can chain off each other.
1498 while (processInstruction(Inst, Operands))
1499 ;
1500
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001501 Inst.setLoc(IDLoc);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001502 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001503 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001504 case Match_MissingFeature:
1505 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1506 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +00001507 case Match_ConversionFail:
1508 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001509 case Match_InvalidOperand:
1510 WasOriginallyInvalidOperand = true;
1511 break;
1512 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001513 break;
1514 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001515
Daniel Dunbarc918d602010-05-04 16:12:42 +00001516 // FIXME: Ideally, we would only attempt suffix matches for things which are
1517 // valid prefixes, and we could just infer the right unambiguous
1518 // type. However, that requires substantially more matcher support than the
1519 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001520
Daniel Dunbarc918d602010-05-04 16:12:42 +00001521 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001522 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001523 SmallString<16> Tmp;
1524 Tmp += Base;
1525 Tmp += ' ';
1526 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001527
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001528 // If this instruction starts with an 'f', then it is a floating point stack
1529 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1530 // 80-bit floating point, which use the suffixes s,l,t respectively.
1531 //
1532 // Otherwise, we assume that this may be an integer instruction, which comes
1533 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1534 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
1535
Daniel Dunbarc918d602010-05-04 16:12:42 +00001536 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001537 Tmp[Base.size()] = Suffixes[0];
1538 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001539 unsigned Match1, Match2, Match3, Match4;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001540
1541 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1542 Tmp[Base.size()] = Suffixes[1];
1543 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1544 Tmp[Base.size()] = Suffixes[2];
1545 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1546 Tmp[Base.size()] = Suffixes[3];
1547 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001548
1549 // Restore the old token.
1550 Op->setTokenValue(Base);
1551
1552 // If exactly one matched, then we treat that as a successful match (and the
1553 // instruction will already have been filled in correctly, since the failing
1554 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001555 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001556 (Match1 == Match_Success) + (Match2 == Match_Success) +
1557 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001558 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001559 Inst.setLoc(IDLoc);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001560 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001561 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001562 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001563
Chris Lattnerec6789f2010-09-06 20:08:02 +00001564 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001565
Daniel Dunbar09062b12010-08-12 00:55:42 +00001566 // If we had multiple suffix matches, then identify this as an ambiguous
1567 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001568 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001569 char MatchChars[4];
1570 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001571 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1572 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1573 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1574 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001575
1576 SmallString<126> Msg;
1577 raw_svector_ostream OS(Msg);
1578 OS << "ambiguous instructions require an explicit suffix (could be ";
1579 for (unsigned i = 0; i != NumMatches; ++i) {
1580 if (i != 0)
1581 OS << ", ";
1582 if (i + 1 == NumMatches)
1583 OS << "or ";
1584 OS << "'" << Base << MatchChars[i] << "'";
1585 }
1586 OS << ")";
1587 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001588 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001589 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001590
Chris Lattnera008e8a2010-09-06 21:54:15 +00001591 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001592
Chris Lattnera008e8a2010-09-06 21:54:15 +00001593 // If all of the instructions reported an invalid mnemonic, then the original
1594 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001595 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1596 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001597 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001598 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
1599 Op->getLocRange());
Chris Lattnerce4a3352010-09-06 22:11:18 +00001600 }
1601
1602 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001603 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001604 if (OrigErrorInfo >= Operands.size())
1605 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001606
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001607 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1608 if (Operand->getStartLoc().isValid()) {
1609 SMRange OperandRange = Operand->getLocRange();
1610 return Error(Operand->getStartLoc(), "invalid operand for instruction",
1611 OperandRange);
1612 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001613 }
1614
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001615 return Error(IDLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001616 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001617
Chris Lattnerec6789f2010-09-06 20:08:02 +00001618 // If one instruction matched with a missing feature, report this as a
1619 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001620 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1621 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001622 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1623 return true;
1624 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001625
Chris Lattnera008e8a2010-09-06 21:54:15 +00001626 // If one instruction matched with an invalid operand, report this as an
1627 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001628 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1629 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001630 Error(IDLoc, "invalid operand for instruction");
1631 return true;
1632 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001633
Chris Lattnerec6789f2010-09-06 20:08:02 +00001634 // If all of these were an outright failure, report it in a useless way.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001635 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001636 return true;
1637}
1638
1639
Devang Pateldd929fc2012-01-12 18:03:40 +00001640bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001641 StringRef IDVal = DirectiveID.getIdentifier();
1642 if (IDVal == ".word")
1643 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001644 else if (IDVal.startswith(".code"))
1645 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Devang Patelbe3e3102012-01-30 20:02:42 +00001646 else if (IDVal.startswith(".intel_syntax")) {
1647 IntelSyntax = true;
1648 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1649 if(Parser.getTok().getString() == "noprefix") {
1650 // FIXME : Handle noprefix
1651 Parser.Lex();
1652 } else
1653 return true;
1654 }
1655 return false;
1656 }
Chris Lattner537ca842010-10-30 17:38:55 +00001657 return true;
1658}
1659
1660/// ParseDirectiveWord
1661/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001662bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001663 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1664 for (;;) {
1665 const MCExpr *Value;
1666 if (getParser().ParseExpression(Value))
1667 return true;
1668
1669 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1670
1671 if (getLexer().is(AsmToken::EndOfStatement))
1672 break;
1673
1674 // FIXME: Improve diagnostic.
1675 if (getLexer().isNot(AsmToken::Comma))
1676 return Error(L, "unexpected token in directive");
1677 Parser.Lex();
1678 }
1679 }
1680
1681 Parser.Lex();
1682 return false;
1683}
1684
Evan Chengbd27f5a2011-07-27 00:38:12 +00001685/// ParseDirectiveCode
1686/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001687bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001688 if (IDVal == ".code32") {
1689 Parser.Lex();
1690 if (is64BitMode()) {
1691 SwitchMode();
1692 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1693 }
1694 } else if (IDVal == ".code64") {
1695 Parser.Lex();
1696 if (!is64BitMode()) {
1697 SwitchMode();
1698 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1699 }
1700 } else {
1701 return Error(L, "unexpected directive " + IDVal);
1702 }
Chris Lattner537ca842010-10-30 17:38:55 +00001703
Evan Chengbd27f5a2011-07-27 00:38:12 +00001704 return false;
1705}
Chris Lattner537ca842010-10-30 17:38:55 +00001706
1707
Sean Callanane88f5522010-01-23 02:43:15 +00001708extern "C" void LLVMInitializeX86AsmLexer();
1709
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001710// Force static initialization.
1711extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001712 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1713 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001714 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001715}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001716
Chris Lattner0692ee62010-09-06 19:11:01 +00001717#define GET_REGISTER_MATCHER
1718#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001719#include "X86GenAsmMatcher.inc"