blob: fd6efa63276598361a229b6b4a6e2059f37b3318 [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
Devang Patel885f65b2012-01-30 22:47:12 +0000984 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000985 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000986
Chris Lattner2544f422010-09-08 05:17:37 +0000987 // Determine whether this is an instruction prefix.
988 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000989 Name == "lock" || Name == "rep" ||
990 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000991 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000992 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000993
994
Chris Lattner2544f422010-09-08 05:17:37 +0000995 // This does the actual operand parsing. Don't parse any more if we have a
996 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
997 // just want to parse the "lock" as the first instruction and the "incl" as
998 // the next one.
999 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001000
1001 // Parse '*' modifier.
1002 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001003 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001004 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001005 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001006 }
1007
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001008 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001009 if (X86Operand *Op = ParseOperand())
1010 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001011 else {
1012 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001013 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001014 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001015
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001016 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001017 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001018
1019 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001020 if (X86Operand *Op = ParseOperand())
1021 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001022 else {
1023 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001024 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001025 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001026 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001027
Chris Lattnercbf8a982010-09-11 16:18:25 +00001028 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001029 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001030 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001031 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001032 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001033 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001034
Chris Lattner2544f422010-09-08 05:17:37 +00001035 if (getLexer().is(AsmToken::EndOfStatement))
1036 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001037 else if (isPrefix && getLexer().is(AsmToken::Slash))
1038 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001039
Devang Patel885f65b2012-01-30 22:47:12 +00001040 if (ExtraImmOp && isParsingIntelSyntax())
1041 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1042
Chris Lattner98c870f2010-11-06 19:25:43 +00001043 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1044 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1045 // documented form in various unofficial manuals, so a lot of code uses it.
1046 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1047 Operands.size() == 3) {
1048 X86Operand &Op = *(X86Operand*)Operands.back();
1049 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1050 isa<MCConstantExpr>(Op.Mem.Disp) &&
1051 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1052 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1053 SMLoc Loc = Op.getEndLoc();
1054 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1055 delete &Op;
1056 }
1057 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001058 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1059 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1060 Operands.size() == 3) {
1061 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1062 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1063 isa<MCConstantExpr>(Op.Mem.Disp) &&
1064 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1065 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1066 SMLoc Loc = Op.getEndLoc();
1067 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1068 delete &Op;
1069 }
1070 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001071 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1072 if (Name.startswith("ins") && Operands.size() == 3 &&
1073 (Name == "insb" || Name == "insw" || Name == "insl")) {
1074 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1075 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1076 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1077 Operands.pop_back();
1078 Operands.pop_back();
1079 delete &Op;
1080 delete &Op2;
1081 }
1082 }
1083
1084 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1085 if (Name.startswith("outs") && Operands.size() == 3 &&
1086 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1087 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1088 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1089 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1090 Operands.pop_back();
1091 Operands.pop_back();
1092 delete &Op;
1093 delete &Op2;
1094 }
1095 }
1096
1097 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1098 if (Name.startswith("movs") && Operands.size() == 3 &&
1099 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001100 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001101 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1102 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1103 if (isSrcOp(Op) && isDstOp(Op2)) {
1104 Operands.pop_back();
1105 Operands.pop_back();
1106 delete &Op;
1107 delete &Op2;
1108 }
1109 }
1110 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1111 if (Name.startswith("lods") && Operands.size() == 3 &&
1112 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001113 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001114 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1115 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1116 if (isSrcOp(*Op1) && Op2->isReg()) {
1117 const char *ins;
1118 unsigned reg = Op2->getReg();
1119 bool isLods = Name == "lods";
1120 if (reg == X86::AL && (isLods || Name == "lodsb"))
1121 ins = "lodsb";
1122 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1123 ins = "lodsw";
1124 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1125 ins = "lodsl";
1126 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1127 ins = "lodsq";
1128 else
1129 ins = NULL;
1130 if (ins != NULL) {
1131 Operands.pop_back();
1132 Operands.pop_back();
1133 delete Op1;
1134 delete Op2;
1135 if (Name != ins)
1136 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1137 }
1138 }
1139 }
1140 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1141 if (Name.startswith("stos") && Operands.size() == 3 &&
1142 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001143 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001144 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1145 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1146 if (isDstOp(*Op2) && Op1->isReg()) {
1147 const char *ins;
1148 unsigned reg = Op1->getReg();
1149 bool isStos = Name == "stos";
1150 if (reg == X86::AL && (isStos || Name == "stosb"))
1151 ins = "stosb";
1152 else if (reg == X86::AX && (isStos || Name == "stosw"))
1153 ins = "stosw";
1154 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1155 ins = "stosl";
1156 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1157 ins = "stosq";
1158 else
1159 ins = NULL;
1160 if (ins != NULL) {
1161 Operands.pop_back();
1162 Operands.pop_back();
1163 delete Op1;
1164 delete Op2;
1165 if (Name != ins)
1166 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1167 }
1168 }
1169 }
1170
Chris Lattnere9e16a32010-09-15 04:33:27 +00001171 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001172 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001173 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001174 Name.startswith("shl") || Name.startswith("sal") ||
1175 Name.startswith("rcl") || Name.startswith("rcr") ||
1176 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001177 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001178 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001179 // Intel syntax
1180 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1181 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1182 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1183 delete Operands[2];
1184 Operands.pop_back();
1185 }
1186 } else {
1187 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1188 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1189 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1190 delete Operands[1];
1191 Operands.erase(Operands.begin() + 1);
1192 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001193 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001194 }
Chris Lattner15f89512011-04-09 19:41:05 +00001195
1196 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1197 // instalias with an immediate operand yet.
1198 if (Name == "int" && Operands.size() == 2) {
1199 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1200 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1201 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1202 delete Operands[1];
1203 Operands.erase(Operands.begin() + 1);
1204 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1205 }
1206 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001207
Chris Lattner98986712010-01-14 22:21:20 +00001208 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001209}
1210
Devang Pateldd929fc2012-01-12 18:03:40 +00001211bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001212processInstruction(MCInst &Inst,
1213 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1214 switch (Inst.getOpcode()) {
1215 default: return false;
1216 case X86::AND16i16: {
1217 if (!Inst.getOperand(0).isImm() ||
1218 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1219 return false;
1220
1221 MCInst TmpInst;
1222 TmpInst.setOpcode(X86::AND16ri8);
1223 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1224 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1225 TmpInst.addOperand(Inst.getOperand(0));
1226 Inst = TmpInst;
1227 return true;
1228 }
1229 case X86::AND32i32: {
1230 if (!Inst.getOperand(0).isImm() ||
1231 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1232 return false;
1233
1234 MCInst TmpInst;
1235 TmpInst.setOpcode(X86::AND32ri8);
1236 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1237 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1238 TmpInst.addOperand(Inst.getOperand(0));
1239 Inst = TmpInst;
1240 return true;
1241 }
1242 case X86::AND64i32: {
1243 if (!Inst.getOperand(0).isImm() ||
1244 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1245 return false;
1246
1247 MCInst TmpInst;
1248 TmpInst.setOpcode(X86::AND64ri8);
1249 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1250 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1251 TmpInst.addOperand(Inst.getOperand(0));
1252 Inst = TmpInst;
1253 return true;
1254 }
Devang Patelac0f0482012-01-19 17:53:25 +00001255 case X86::XOR16i16: {
1256 if (!Inst.getOperand(0).isImm() ||
1257 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1258 return false;
1259
1260 MCInst TmpInst;
1261 TmpInst.setOpcode(X86::XOR16ri8);
1262 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1263 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1264 TmpInst.addOperand(Inst.getOperand(0));
1265 Inst = TmpInst;
1266 return true;
1267 }
1268 case X86::XOR32i32: {
1269 if (!Inst.getOperand(0).isImm() ||
1270 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1271 return false;
1272
1273 MCInst TmpInst;
1274 TmpInst.setOpcode(X86::XOR32ri8);
1275 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1276 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1277 TmpInst.addOperand(Inst.getOperand(0));
1278 Inst = TmpInst;
1279 return true;
1280 }
1281 case X86::XOR64i32: {
1282 if (!Inst.getOperand(0).isImm() ||
1283 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1284 return false;
1285
1286 MCInst TmpInst;
1287 TmpInst.setOpcode(X86::XOR64ri8);
1288 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1289 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1290 TmpInst.addOperand(Inst.getOperand(0));
1291 Inst = TmpInst;
1292 return true;
1293 }
1294 case X86::OR16i16: {
1295 if (!Inst.getOperand(0).isImm() ||
1296 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1297 return false;
1298
1299 MCInst TmpInst;
1300 TmpInst.setOpcode(X86::OR16ri8);
1301 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1302 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1303 TmpInst.addOperand(Inst.getOperand(0));
1304 Inst = TmpInst;
1305 return true;
1306 }
1307 case X86::OR32i32: {
1308 if (!Inst.getOperand(0).isImm() ||
1309 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1310 return false;
1311
1312 MCInst TmpInst;
1313 TmpInst.setOpcode(X86::OR32ri8);
1314 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1315 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1316 TmpInst.addOperand(Inst.getOperand(0));
1317 Inst = TmpInst;
1318 return true;
1319 }
1320 case X86::OR64i32: {
1321 if (!Inst.getOperand(0).isImm() ||
1322 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1323 return false;
1324
1325 MCInst TmpInst;
1326 TmpInst.setOpcode(X86::OR64ri8);
1327 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1328 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1329 TmpInst.addOperand(Inst.getOperand(0));
1330 Inst = TmpInst;
1331 return true;
1332 }
1333 case X86::CMP16i16: {
1334 if (!Inst.getOperand(0).isImm() ||
1335 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1336 return false;
1337
1338 MCInst TmpInst;
1339 TmpInst.setOpcode(X86::CMP16ri8);
1340 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1341 TmpInst.addOperand(Inst.getOperand(0));
1342 Inst = TmpInst;
1343 return true;
1344 }
1345 case X86::CMP32i32: {
1346 if (!Inst.getOperand(0).isImm() ||
1347 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1348 return false;
1349
1350 MCInst TmpInst;
1351 TmpInst.setOpcode(X86::CMP32ri8);
1352 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1353 TmpInst.addOperand(Inst.getOperand(0));
1354 Inst = TmpInst;
1355 return true;
1356 }
1357 case X86::CMP64i32: {
1358 if (!Inst.getOperand(0).isImm() ||
1359 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1360 return false;
1361
1362 MCInst TmpInst;
1363 TmpInst.setOpcode(X86::CMP64ri8);
1364 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1365 TmpInst.addOperand(Inst.getOperand(0));
1366 Inst = TmpInst;
1367 return true;
1368 }
Devang Patela951f772012-01-19 18:40:55 +00001369 case X86::ADD16i16: {
1370 if (!Inst.getOperand(0).isImm() ||
1371 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1372 return false;
1373
1374 MCInst TmpInst;
1375 TmpInst.setOpcode(X86::ADD16ri8);
1376 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1377 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1378 TmpInst.addOperand(Inst.getOperand(0));
1379 Inst = TmpInst;
1380 return true;
1381 }
1382 case X86::ADD32i32: {
1383 if (!Inst.getOperand(0).isImm() ||
1384 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1385 return false;
1386
1387 MCInst TmpInst;
1388 TmpInst.setOpcode(X86::ADD32ri8);
1389 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1390 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1391 TmpInst.addOperand(Inst.getOperand(0));
1392 Inst = TmpInst;
1393 return true;
1394 }
1395 case X86::ADD64i32: {
1396 if (!Inst.getOperand(0).isImm() ||
1397 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1398 return false;
1399
1400 MCInst TmpInst;
1401 TmpInst.setOpcode(X86::ADD64ri8);
1402 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1403 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1404 TmpInst.addOperand(Inst.getOperand(0));
1405 Inst = TmpInst;
1406 return true;
1407 }
1408 case X86::SUB16i16: {
1409 if (!Inst.getOperand(0).isImm() ||
1410 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1411 return false;
1412
1413 MCInst TmpInst;
1414 TmpInst.setOpcode(X86::SUB16ri8);
1415 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1416 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1417 TmpInst.addOperand(Inst.getOperand(0));
1418 Inst = TmpInst;
1419 return true;
1420 }
1421 case X86::SUB32i32: {
1422 if (!Inst.getOperand(0).isImm() ||
1423 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1424 return false;
1425
1426 MCInst TmpInst;
1427 TmpInst.setOpcode(X86::SUB32ri8);
1428 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1429 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1430 TmpInst.addOperand(Inst.getOperand(0));
1431 Inst = TmpInst;
1432 return true;
1433 }
1434 case X86::SUB64i32: {
1435 if (!Inst.getOperand(0).isImm() ||
1436 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1437 return false;
1438
1439 MCInst TmpInst;
1440 TmpInst.setOpcode(X86::SUB64ri8);
1441 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1442 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1443 TmpInst.addOperand(Inst.getOperand(0));
1444 Inst = TmpInst;
1445 return true;
1446 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001447 }
1448 return false;
1449}
1450
1451bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001452MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001453 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001454 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001455 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001456 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1457 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001458
Chris Lattner7c51a312010-09-29 01:50:45 +00001459 // First, handle aliases that expand to multiple instructions.
1460 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +00001461 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
1462 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001463 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001464 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001465 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001466 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001467 MCInst Inst;
1468 Inst.setOpcode(X86::WAIT);
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001469 Inst.setLoc(IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001470 Out.EmitInstruction(Inst);
1471
Chris Lattner0bb83a82010-09-30 16:39:29 +00001472 const char *Repl =
1473 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001474 .Case("finit", "fninit")
1475 .Case("fsave", "fnsave")
1476 .Case("fstcw", "fnstcw")
1477 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001478 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001479 .Case("fstsw", "fnstsw")
1480 .Case("fstsww", "fnstsw")
1481 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001482 .Default(0);
1483 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001484 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001485 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001486 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001487
Chris Lattnera008e8a2010-09-06 21:54:15 +00001488 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +00001489 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001490 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001491
Daniel Dunbarc918d602010-05-04 16:12:42 +00001492 // First, try a direct match.
Devang Patelbe3e3102012-01-30 20:02:42 +00001493 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo,
1494 isParsingIntelSyntax())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001495 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001496 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001497 // Some instructions need post-processing to, for example, tweak which
1498 // encoding is selected. Loop on it while changes happen so the
1499 // individual transformations can chain off each other.
1500 while (processInstruction(Inst, Operands))
1501 ;
1502
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001503 Inst.setLoc(IDLoc);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001504 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001505 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001506 case Match_MissingFeature:
1507 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1508 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +00001509 case Match_ConversionFail:
1510 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001511 case Match_InvalidOperand:
1512 WasOriginallyInvalidOperand = true;
1513 break;
1514 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001515 break;
1516 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001517
Daniel Dunbarc918d602010-05-04 16:12:42 +00001518 // FIXME: Ideally, we would only attempt suffix matches for things which are
1519 // valid prefixes, and we could just infer the right unambiguous
1520 // type. However, that requires substantially more matcher support than the
1521 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001522
Daniel Dunbarc918d602010-05-04 16:12:42 +00001523 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001524 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001525 SmallString<16> Tmp;
1526 Tmp += Base;
1527 Tmp += ' ';
1528 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001529
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001530 // If this instruction starts with an 'f', then it is a floating point stack
1531 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1532 // 80-bit floating point, which use the suffixes s,l,t respectively.
1533 //
1534 // Otherwise, we assume that this may be an integer instruction, which comes
1535 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1536 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
1537
Daniel Dunbarc918d602010-05-04 16:12:42 +00001538 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001539 Tmp[Base.size()] = Suffixes[0];
1540 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001541 unsigned Match1, Match2, Match3, Match4;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001542
1543 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1544 Tmp[Base.size()] = Suffixes[1];
1545 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1546 Tmp[Base.size()] = Suffixes[2];
1547 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1548 Tmp[Base.size()] = Suffixes[3];
1549 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001550
1551 // Restore the old token.
1552 Op->setTokenValue(Base);
1553
1554 // If exactly one matched, then we treat that as a successful match (and the
1555 // instruction will already have been filled in correctly, since the failing
1556 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001557 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001558 (Match1 == Match_Success) + (Match2 == Match_Success) +
1559 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001560 if (NumSuccessfulMatches == 1) {
Jim Grosbachcb5dca32012-01-27 00:51:27 +00001561 Inst.setLoc(IDLoc);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001562 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001563 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001564 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001565
Chris Lattnerec6789f2010-09-06 20:08:02 +00001566 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001567
Daniel Dunbar09062b12010-08-12 00:55:42 +00001568 // If we had multiple suffix matches, then identify this as an ambiguous
1569 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001570 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001571 char MatchChars[4];
1572 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001573 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1574 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1575 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1576 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001577
1578 SmallString<126> Msg;
1579 raw_svector_ostream OS(Msg);
1580 OS << "ambiguous instructions require an explicit suffix (could be ";
1581 for (unsigned i = 0; i != NumMatches; ++i) {
1582 if (i != 0)
1583 OS << ", ";
1584 if (i + 1 == NumMatches)
1585 OS << "or ";
1586 OS << "'" << Base << MatchChars[i] << "'";
1587 }
1588 OS << ")";
1589 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001590 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001591 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001592
Chris Lattnera008e8a2010-09-06 21:54:15 +00001593 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001594
Chris Lattnera008e8a2010-09-06 21:54:15 +00001595 // If all of the instructions reported an invalid mnemonic, then the original
1596 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001597 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1598 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001599 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001600 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
1601 Op->getLocRange());
Chris Lattnerce4a3352010-09-06 22:11:18 +00001602 }
1603
1604 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001605 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001606 if (OrigErrorInfo >= Operands.size())
1607 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001608
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001609 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1610 if (Operand->getStartLoc().isValid()) {
1611 SMRange OperandRange = Operand->getLocRange();
1612 return Error(Operand->getStartLoc(), "invalid operand for instruction",
1613 OperandRange);
1614 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001615 }
1616
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001617 return Error(IDLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001618 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001619
Chris Lattnerec6789f2010-09-06 20:08:02 +00001620 // If one instruction matched with a missing feature, report this as a
1621 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001622 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1623 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001624 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1625 return true;
1626 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001627
Chris Lattnera008e8a2010-09-06 21:54:15 +00001628 // If one instruction matched with an invalid operand, report this as an
1629 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001630 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1631 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001632 Error(IDLoc, "invalid operand for instruction");
1633 return true;
1634 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001635
Chris Lattnerec6789f2010-09-06 20:08:02 +00001636 // If all of these were an outright failure, report it in a useless way.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001637 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001638 return true;
1639}
1640
1641
Devang Pateldd929fc2012-01-12 18:03:40 +00001642bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001643 StringRef IDVal = DirectiveID.getIdentifier();
1644 if (IDVal == ".word")
1645 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001646 else if (IDVal.startswith(".code"))
1647 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Devang Patelbe3e3102012-01-30 20:02:42 +00001648 else if (IDVal.startswith(".intel_syntax")) {
1649 IntelSyntax = true;
1650 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1651 if(Parser.getTok().getString() == "noprefix") {
1652 // FIXME : Handle noprefix
1653 Parser.Lex();
1654 } else
1655 return true;
1656 }
1657 return false;
1658 }
Chris Lattner537ca842010-10-30 17:38:55 +00001659 return true;
1660}
1661
1662/// ParseDirectiveWord
1663/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001664bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001665 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1666 for (;;) {
1667 const MCExpr *Value;
1668 if (getParser().ParseExpression(Value))
1669 return true;
1670
1671 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1672
1673 if (getLexer().is(AsmToken::EndOfStatement))
1674 break;
1675
1676 // FIXME: Improve diagnostic.
1677 if (getLexer().isNot(AsmToken::Comma))
1678 return Error(L, "unexpected token in directive");
1679 Parser.Lex();
1680 }
1681 }
1682
1683 Parser.Lex();
1684 return false;
1685}
1686
Evan Chengbd27f5a2011-07-27 00:38:12 +00001687/// ParseDirectiveCode
1688/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001689bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001690 if (IDVal == ".code32") {
1691 Parser.Lex();
1692 if (is64BitMode()) {
1693 SwitchMode();
1694 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1695 }
1696 } else if (IDVal == ".code64") {
1697 Parser.Lex();
1698 if (!is64BitMode()) {
1699 SwitchMode();
1700 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1701 }
1702 } else {
1703 return Error(L, "unexpected directive " + IDVal);
1704 }
Chris Lattner537ca842010-10-30 17:38:55 +00001705
Evan Chengbd27f5a2011-07-27 00:38:12 +00001706 return false;
1707}
Chris Lattner537ca842010-10-30 17:38:55 +00001708
1709
Sean Callanane88f5522010-01-23 02:43:15 +00001710extern "C" void LLVMInitializeX86AsmLexer();
1711
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001712// Force static initialization.
1713extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001714 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1715 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001716 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001717}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001718
Chris Lattner0692ee62010-09-06 19:11:01 +00001719#define GET_REGISTER_MATCHER
1720#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001721#include "X86GenAsmMatcher.inc"