blob: 05340c17839b037b6edc31d818c437e69fbd45b8 [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;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000037private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000038 MCAsmParser &getParser() const { return Parser; }
39
40 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
41
Chris Lattnerd8b7aa22011-10-16 04:47:35 +000042 bool Error(SMLoc L, const Twine &Msg,
43 ArrayRef<SMRange> Ranges = ArrayRef<SMRange>()) {
44 return Parser.Error(L, Msg, Ranges);
45 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000046
Devang Pateld37ad242012-01-17 18:00:18 +000047 X86Operand *ErrorOperand(SMLoc Loc, StringRef Msg) {
48 Error(Loc, Msg);
49 return 0;
50 }
51
Chris Lattner309264d2010-01-15 18:44:13 +000052 X86Operand *ParseOperand();
Devang Patel0a338862012-01-12 01:36:43 +000053 X86Operand *ParseATTOperand();
54 X86Operand *ParseIntelOperand();
Devang Pateld37ad242012-01-17 18:00:18 +000055 X86Operand *ParseIntelMemOperand();
Devang Patel7c64fe62012-01-23 18:31:58 +000056 X86Operand *ParseIntelBracExpression(unsigned SegReg, unsigned Size);
Chris Lattnereef6d782010-04-17 18:56:34 +000057 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000058
59 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Evan Chengbd27f5a2011-07-27 00:38:12 +000060 bool ParseDirectiveCode(StringRef IDVal, SMLoc L);
Kevin Enderby9c656452009-09-10 20:51:44 +000061
Devang Patelb8ba13f2012-01-18 22:42:29 +000062 bool processInstruction(MCInst &Inst,
63 const SmallVectorImpl<MCParsedAsmOperand*> &Ops);
64
Chris Lattner7036f8b2010-09-29 01:42:58 +000065 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000066 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +000067 MCStreamer &Out);
Daniel Dunbar20927f22009-08-07 08:26:05 +000068
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +000069 /// isSrcOp - Returns true if operand is either (%rsi) or %ds:%(rsi)
70 /// in 64bit mode or (%edi) or %es:(%edi) in 32bit mode.
71 bool isSrcOp(X86Operand &Op);
72
73 /// isDstOp - Returns true if operand is either %es:(%rdi) in 64bit mode
74 /// or %es:(%edi) in 32bit mode.
75 bool isDstOp(X86Operand &Op);
76
Evan Cheng59ee62d2011-07-11 03:57:24 +000077 bool is64BitMode() const {
Evan Chengebdeeab2011-07-08 01:53:10 +000078 // FIXME: Can tablegen auto-generate this?
Evan Chengffc0e732011-07-09 05:47:46 +000079 return (STI.getFeatureBits() & X86::Mode64Bit) != 0;
Evan Chengebdeeab2011-07-08 01:53:10 +000080 }
Evan Chengbd27f5a2011-07-27 00:38:12 +000081 void SwitchMode() {
82 unsigned FB = ComputeAvailableFeatures(STI.ToggleFeature(X86::Mode64Bit));
83 setAvailableFeatures(FB);
84 }
Evan Chengebdeeab2011-07-08 01:53:10 +000085
Daniel Dunbar54074b52010-07-19 05:44:09 +000086 /// @name Auto-generated Matcher Functions
87 /// {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000088
Chris Lattner0692ee62010-09-06 19:11:01 +000089#define GET_ASSEMBLER_HEADER
90#include "X86GenAsmMatcher.inc"
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000091
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000092 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000093
94public:
Devang Pateldd929fc2012-01-12 18:03:40 +000095 X86AsmParser(MCSubtargetInfo &sti, MCAsmParser &parser)
Devang Patel0db58bf2012-01-31 18:14:05 +000096 : MCTargetAsmParser(), STI(sti), Parser(parser) {
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000097
Daniel Dunbar54074b52010-07-19 05:44:09 +000098 // Initialize the set of available features.
Evan Chengffc0e732011-07-09 05:47:46 +000099 setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
Daniel Dunbar54074b52010-07-19 05:44:09 +0000100 }
Roman Divackybf755322011-01-27 17:14:22 +0000101 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000102
Benjamin Kramer38e59892010-07-14 22:38:02 +0000103 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000104 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +0000105
106 virtual bool ParseDirective(AsmToken DirectiveID);
Devang Patelbe3e3102012-01-30 20:02:42 +0000107
108 bool isParsingIntelSyntax() {
Devang Patel0db58bf2012-01-31 18:14:05 +0000109 return getParser().getAssemblerDialect();
Devang Patelbe3e3102012-01-30 20:02:42 +0000110 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000111};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000112} // end anonymous namespace
113
Sean Callanane9b466d2010-01-23 00:40:33 +0000114/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000115/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000116
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000117static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000118
119/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000120
Devang Patelb8ba13f2012-01-18 22:42:29 +0000121static bool isImmSExti16i8Value(uint64_t Value) {
122 return (( Value <= 0x000000000000007FULL)||
123 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
124 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
125}
126
127static bool isImmSExti32i8Value(uint64_t Value) {
128 return (( Value <= 0x000000000000007FULL)||
129 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
130 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
131}
132
133static bool isImmZExtu32u8Value(uint64_t Value) {
134 return (Value <= 0x00000000000000FFULL);
135}
136
137static bool isImmSExti64i8Value(uint64_t Value) {
138 return (( Value <= 0x000000000000007FULL)||
139 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
140}
141
142static bool isImmSExti64i32Value(uint64_t Value) {
143 return (( Value <= 0x000000007FFFFFFFULL)||
144 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
145}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000146namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000147
148/// X86Operand - Instances of this class represent a parsed X86 machine
149/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000150struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000151 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000152 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000153 Register,
154 Immediate,
155 Memory
156 } Kind;
157
Chris Lattner29ef9a22010-01-15 18:51:29 +0000158 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000159
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000160 union {
161 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000162 const char *Data;
163 unsigned Length;
164 } Tok;
165
166 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000167 unsigned RegNo;
168 } Reg;
169
170 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000171 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000172 } Imm;
173
174 struct {
175 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000176 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000177 unsigned BaseReg;
178 unsigned IndexReg;
179 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000180 unsigned Size;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000181 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000182 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000183
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000184 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000185 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000186
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000187 /// getStartLoc - Get the location of the first token of this operand.
188 SMLoc getStartLoc() const { return StartLoc; }
189 /// getEndLoc - Get the location of the last token of this operand.
190 SMLoc getEndLoc() const { return EndLoc; }
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000191
192 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000193
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000194 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000195
Daniel Dunbar20927f22009-08-07 08:26:05 +0000196 StringRef getToken() const {
197 assert(Kind == Token && "Invalid access!");
198 return StringRef(Tok.Data, Tok.Length);
199 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000200 void setTokenValue(StringRef Value) {
201 assert(Kind == Token && "Invalid access!");
202 Tok.Data = Value.data();
203 Tok.Length = Value.size();
204 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000205
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000206 unsigned getReg() const {
207 assert(Kind == Register && "Invalid access!");
208 return Reg.RegNo;
209 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000210
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000211 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000212 assert(Kind == Immediate && "Invalid access!");
213 return Imm.Val;
214 }
215
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000216 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000217 assert(Kind == Memory && "Invalid access!");
218 return Mem.Disp;
219 }
220 unsigned getMemSegReg() const {
221 assert(Kind == Memory && "Invalid access!");
222 return Mem.SegReg;
223 }
224 unsigned getMemBaseReg() const {
225 assert(Kind == Memory && "Invalid access!");
226 return Mem.BaseReg;
227 }
228 unsigned getMemIndexReg() const {
229 assert(Kind == Memory && "Invalid access!");
230 return Mem.IndexReg;
231 }
232 unsigned getMemScale() const {
233 assert(Kind == Memory && "Invalid access!");
234 return Mem.Scale;
235 }
236
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000237 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000238
239 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000240
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000241 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000242 if (!isImm())
243 return false;
244
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000245 // If this isn't a constant expr, just assume it fits and let relaxation
246 // handle it.
247 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
248 if (!CE)
249 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000250
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000251 // Otherwise, check the value is in a range that makes sense for this
252 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000253 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000254 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000255 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000256 if (!isImm())
257 return false;
258
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000259 // If this isn't a constant expr, just assume it fits and let relaxation
260 // handle it.
261 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
262 if (!CE)
263 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000264
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000265 // Otherwise, check the value is in a range that makes sense for this
266 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000267 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000268 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000269 bool isImmZExtu32u8() const {
270 if (!isImm())
271 return false;
272
273 // If this isn't a constant expr, just assume it fits and let relaxation
274 // handle it.
275 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
276 if (!CE)
277 return true;
278
279 // Otherwise, check the value is in a range that makes sense for this
280 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000281 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000282 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000283 bool isImmSExti64i8() const {
284 if (!isImm())
285 return false;
286
287 // If this isn't a constant expr, just assume it fits and let relaxation
288 // handle it.
289 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
290 if (!CE)
291 return true;
292
293 // Otherwise, check the value is in a range that makes sense for this
294 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000295 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000296 }
297 bool isImmSExti64i32() const {
298 if (!isImm())
299 return false;
300
301 // If this isn't a constant expr, just assume it fits and let relaxation
302 // handle it.
303 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
304 if (!CE)
305 return true;
306
307 // Otherwise, check the value is in a range that makes sense for this
308 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000309 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000310 }
311
Daniel Dunbar20927f22009-08-07 08:26:05 +0000312 bool isMem() const { return Kind == Memory; }
Devang Patelc59d9df2012-01-12 01:51:42 +0000313 bool isMem8() const {
314 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
315 }
316 bool isMem16() const {
317 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
318 }
319 bool isMem32() const {
320 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
321 }
322 bool isMem64() const {
323 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
324 }
325 bool isMem80() const {
326 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
327 }
328 bool isMem128() const {
329 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
330 }
331 bool isMem256() const {
332 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
333 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000334
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000335 bool isAbsMem() const {
336 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000337 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000338 }
339
Daniel Dunbar20927f22009-08-07 08:26:05 +0000340 bool isReg() const { return Kind == Register; }
341
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000342 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
343 // Add as immediates when possible.
344 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
345 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
346 else
347 Inst.addOperand(MCOperand::CreateExpr(Expr));
348 }
349
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000350 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000351 assert(N == 1 && "Invalid number of operands!");
352 Inst.addOperand(MCOperand::CreateReg(getReg()));
353 }
354
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000355 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000356 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000357 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000358 }
359
Devang Patelc59d9df2012-01-12 01:51:42 +0000360 void addMem8Operands(MCInst &Inst, unsigned N) const {
361 addMemOperands(Inst, N);
362 }
363 void addMem16Operands(MCInst &Inst, unsigned N) const {
364 addMemOperands(Inst, N);
365 }
366 void addMem32Operands(MCInst &Inst, unsigned N) const {
367 addMemOperands(Inst, N);
368 }
369 void addMem64Operands(MCInst &Inst, unsigned N) const {
370 addMemOperands(Inst, N);
371 }
372 void addMem80Operands(MCInst &Inst, unsigned N) const {
373 addMemOperands(Inst, N);
374 }
375 void addMem128Operands(MCInst &Inst, unsigned N) const {
376 addMemOperands(Inst, N);
377 }
378 void addMem256Operands(MCInst &Inst, unsigned N) const {
379 addMemOperands(Inst, N);
380 }
381
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000382 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000383 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000384 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
385 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
386 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000387 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000388 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
389 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000390
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000391 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
392 assert((N == 1) && "Invalid number of operands!");
393 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
394 }
395
Chris Lattnerb4307b32010-01-15 19:28:38 +0000396 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000397 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
398 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000399 Res->Tok.Data = Str.data();
400 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000401 return Res;
402 }
403
Chris Lattner29ef9a22010-01-15 18:51:29 +0000404 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000405 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000406 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000407 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000408 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000409
Chris Lattnerb4307b32010-01-15 19:28:38 +0000410 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
411 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000412 Res->Imm.Val = Val;
413 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000414 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000415
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000416 /// Create an absolute memory operand.
417 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000418 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000419 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
420 Res->Mem.SegReg = 0;
421 Res->Mem.Disp = Disp;
422 Res->Mem.BaseReg = 0;
423 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000424 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000425 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000426 return Res;
427 }
428
429 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000430 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
431 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000432 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
433 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000434 // We should never just have a displacement, that should be parsed as an
435 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000436 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
437
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000438 // The scale should always be one of {1,2,4,8}.
439 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000440 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000441 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000442 Res->Mem.SegReg = SegReg;
443 Res->Mem.Disp = Disp;
444 Res->Mem.BaseReg = BaseReg;
445 Res->Mem.IndexReg = IndexReg;
446 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000447 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000448 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000449 }
450};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000451
Chris Lattner37dfdec2009-07-29 06:33:53 +0000452} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000453
Devang Pateldd929fc2012-01-12 18:03:40 +0000454bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000455 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000456
457 return (Op.isMem() &&
458 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
459 isa<MCConstantExpr>(Op.Mem.Disp) &&
460 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
461 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
462}
463
Devang Pateldd929fc2012-01-12 18:03:40 +0000464bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000465 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000466
467 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
468 isa<MCConstantExpr>(Op.Mem.Disp) &&
469 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
470 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
471}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000472
Devang Pateldd929fc2012-01-12 18:03:40 +0000473bool X86AsmParser::ParseRegister(unsigned &RegNo,
474 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000475 RegNo = 0;
Devang Patelbe3e3102012-01-30 20:02:42 +0000476 if (!isParsingIntelSyntax()) {
Devang Patel1aea4302012-01-20 22:32:05 +0000477 const AsmToken &TokPercent = Parser.getTok();
Devang Pateld37ad242012-01-17 18:00:18 +0000478 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
479 StartLoc = TokPercent.getLoc();
480 Parser.Lex(); // Eat percent token.
481 }
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000482
Sean Callanan18b83232010-01-19 21:44:56 +0000483 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000484 if (Tok.isNot(AsmToken::Identifier)) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000485 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000486 return Error(StartLoc, "invalid register name",
487 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000488 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000489
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000490 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000491
Chris Lattner33d60d52010-09-22 04:11:10 +0000492 // If the match failed, try the register name as lowercase.
493 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000494 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000495
Evan Cheng5de728c2011-07-27 23:22:03 +0000496 if (!is64BitMode()) {
497 // FIXME: This should be done using Requires<In32BitMode> and
498 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
499 // checked.
500 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
501 // REX prefix.
502 if (RegNo == X86::RIZ ||
503 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
504 X86II::isX86_64NonExtLowByteReg(RegNo) ||
505 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000506 return Error(StartLoc, "register %"
507 + Tok.getString() + " is only available in 64-bit mode",
508 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000509 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000510
Chris Lattner33d60d52010-09-22 04:11:10 +0000511 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
512 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000513 RegNo = X86::ST0;
514 EndLoc = Tok.getLoc();
515 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000516
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000517 // Check to see if we have '(4)' after %st.
518 if (getLexer().isNot(AsmToken::LParen))
519 return false;
520 // Lex the paren.
521 getParser().Lex();
522
523 const AsmToken &IntTok = Parser.getTok();
524 if (IntTok.isNot(AsmToken::Integer))
525 return Error(IntTok.getLoc(), "expected stack index");
526 switch (IntTok.getIntVal()) {
527 case 0: RegNo = X86::ST0; break;
528 case 1: RegNo = X86::ST1; break;
529 case 2: RegNo = X86::ST2; break;
530 case 3: RegNo = X86::ST3; break;
531 case 4: RegNo = X86::ST4; break;
532 case 5: RegNo = X86::ST5; break;
533 case 6: RegNo = X86::ST6; break;
534 case 7: RegNo = X86::ST7; break;
535 default: return Error(IntTok.getLoc(), "invalid stack index");
536 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000537
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000538 if (getParser().Lex().isNot(AsmToken::RParen))
539 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000540
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000541 EndLoc = Tok.getLoc();
542 Parser.Lex(); // Eat ')'
543 return false;
544 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000545
Chris Lattner645b2092010-06-24 07:29:18 +0000546 // If this is "db[0-7]", match it as an alias
547 // for dr[0-7].
548 if (RegNo == 0 && Tok.getString().size() == 3 &&
549 Tok.getString().startswith("db")) {
550 switch (Tok.getString()[2]) {
551 case '0': RegNo = X86::DR0; break;
552 case '1': RegNo = X86::DR1; break;
553 case '2': RegNo = X86::DR2; break;
554 case '3': RegNo = X86::DR3; break;
555 case '4': RegNo = X86::DR4; break;
556 case '5': RegNo = X86::DR5; break;
557 case '6': RegNo = X86::DR6; break;
558 case '7': RegNo = X86::DR7; break;
559 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000560
Chris Lattner645b2092010-06-24 07:29:18 +0000561 if (RegNo != 0) {
562 EndLoc = Tok.getLoc();
563 Parser.Lex(); // Eat it.
564 return false;
565 }
566 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000567
Devang Patel1aea4302012-01-20 22:32:05 +0000568 if (RegNo == 0) {
Devang Patelbe3e3102012-01-30 20:02:42 +0000569 if (isParsingIntelSyntax()) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000570 return Error(StartLoc, "invalid register name",
571 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000572 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000573
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000574 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000575 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000576 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000577}
578
Devang Pateldd929fc2012-01-12 18:03:40 +0000579X86Operand *X86AsmParser::ParseOperand() {
Devang Patelbe3e3102012-01-30 20:02:42 +0000580 if (isParsingIntelSyntax())
Devang Patel0a338862012-01-12 01:36:43 +0000581 return ParseIntelOperand();
582 return ParseATTOperand();
583}
584
Devang Pateld37ad242012-01-17 18:00:18 +0000585/// getIntelMemOperandSize - Return intel memory operand size.
586static unsigned getIntelMemOperandSize(StringRef OpStr) {
587 unsigned Size = 0;
Devang Patel0a338862012-01-12 01:36:43 +0000588 if (OpStr == "BYTE") Size = 8;
589 if (OpStr == "WORD") Size = 16;
590 if (OpStr == "DWORD") Size = 32;
591 if (OpStr == "QWORD") Size = 64;
592 if (OpStr == "XWORD") Size = 80;
593 if (OpStr == "XMMWORD") Size = 128;
594 if (OpStr == "YMMWORD") Size = 256;
Devang Pateld37ad242012-01-17 18:00:18 +0000595 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000596}
597
Devang Patel7c64fe62012-01-23 18:31:58 +0000598X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
599 unsigned Size) {
600 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000601 SMLoc Start = Parser.getTok().getLoc(), End;
602
Devang Pateld37ad242012-01-17 18:00:18 +0000603 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
604 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
605
606 // Eat '['
607 if (getLexer().isNot(AsmToken::LBrac))
608 return ErrorOperand(Start, "Expected '[' token!");
609 Parser.Lex();
610
611 if (getLexer().is(AsmToken::Identifier)) {
612 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000613 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000614 // Handle '[' 'symbol' ']'
Devang Pateld37ad242012-01-17 18:00:18 +0000615 if (getParser().ParseExpression(Disp, End)) return 0;
616 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000617 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000618 Parser.Lex();
619 return X86Operand::CreateMem(Disp, Start, End, Size);
620 }
621 } else if (getLexer().is(AsmToken::Integer)) {
Devang Patel3e081312012-01-23 20:20:06 +0000622 int64_t Val = Parser.getTok().getIntVal();
Devang Pateld37ad242012-01-17 18:00:18 +0000623 Parser.Lex();
Devang Patel3e081312012-01-23 20:20:06 +0000624 SMLoc Loc = Parser.getTok().getLoc();
625 if (getLexer().is(AsmToken::RBrac)) {
626 // Handle '[' number ']'
627 Parser.Lex();
Devang Patela28101e2012-01-27 19:48:28 +0000628 const MCExpr *Disp = MCConstantExpr::Create(Val, getContext());
629 if (SegReg)
630 return X86Operand::CreateMem(SegReg, Disp, 0, 0, Scale,
631 Start, End, Size);
632 return X86Operand::CreateMem(Disp, Start, End, Size);
Devang Patel3e081312012-01-23 20:20:06 +0000633 } else if (getLexer().is(AsmToken::Star)) {
634 // Handle '[' Scale*IndexReg ']'
635 Parser.Lex();
636 SMLoc IdxRegLoc = Parser.getTok().getLoc();
637 if (ParseRegister(IndexReg, IdxRegLoc, End))
638 return ErrorOperand(IdxRegLoc, "Expected register");
639 Scale = Val;
640 } else
641 return ErrorOperand(Loc, "Unepxeted token");
Devang Pateld37ad242012-01-17 18:00:18 +0000642 }
643
644 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
645 bool isPlus = getLexer().is(AsmToken::Plus);
646 Parser.Lex();
647 SMLoc PlusLoc = Parser.getTok().getLoc();
648 if (getLexer().is(AsmToken::Integer)) {
649 int64_t Val = Parser.getTok().getIntVal();
650 Parser.Lex();
651 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000652 Parser.Lex();
653 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Devang Patel1aea4302012-01-20 22:32:05 +0000654 if (ParseRegister(IndexReg, IdxRegLoc, End))
655 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000656 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000657 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000658 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000659 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000660 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000661 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patelf2d21372012-01-23 22:35:25 +0000662 } else if (getLexer().is(AsmToken::Identifier)) {
Devang Patel392ad6d2012-01-23 23:56:33 +0000663 // This could be an index register or a displacement expression.
Devang Patelf2d21372012-01-23 22:35:25 +0000664 End = Parser.getTok().getLoc();
665 if (!IndexReg)
666 ParseRegister(IndexReg, Start, End);
667 else if (getParser().ParseExpression(Disp, End)) return 0;
668 }
Devang Pateld37ad242012-01-17 18:00:18 +0000669 }
670
671 if (getLexer().isNot(AsmToken::RBrac))
672 if (getParser().ParseExpression(Disp, End)) return 0;
673
674 End = Parser.getTok().getLoc();
675 if (getLexer().isNot(AsmToken::RBrac))
676 return ErrorOperand(End, "expected ']' token!");
677 Parser.Lex();
678 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000679
680 // handle [-42]
681 if (!BaseReg && !IndexReg)
682 return X86Operand::CreateMem(Disp, Start, End, Size);
683
Devang Pateld37ad242012-01-17 18:00:18 +0000684 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000685 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000686}
687
688/// ParseIntelMemOperand - Parse intel style memory operand.
689X86Operand *X86AsmParser::ParseIntelMemOperand() {
690 const AsmToken &Tok = Parser.getTok();
691 SMLoc Start = Parser.getTok().getLoc(), End;
Devang Patel7c64fe62012-01-23 18:31:58 +0000692 unsigned SegReg = 0;
Devang Pateld37ad242012-01-17 18:00:18 +0000693
694 unsigned Size = getIntelMemOperandSize(Tok.getString());
695 if (Size) {
696 Parser.Lex();
697 assert (Tok.getString() == "PTR" && "Unexpected token!");
698 Parser.Lex();
699 }
700
701 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000702 return ParseIntelBracExpression(SegReg, Size);
703
704 if (!ParseRegister(SegReg, Start, End)) {
705 // Handel SegReg : [ ... ]
706 if (getLexer().isNot(AsmToken::Colon))
707 return ErrorOperand(Start, "Expected ':' token!");
708 Parser.Lex(); // Eat :
709 if (getLexer().isNot(AsmToken::LBrac))
710 return ErrorOperand(Start, "Expected '[' token!");
711 return ParseIntelBracExpression(SegReg, Size);
712 }
Devang Pateld37ad242012-01-17 18:00:18 +0000713
714 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
715 if (getParser().ParseExpression(Disp, End)) return 0;
716 return X86Operand::CreateMem(Disp, Start, End, Size);
717}
718
719X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000720 SMLoc Start = Parser.getTok().getLoc(), End;
721
722 // immediate.
723 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
724 getLexer().is(AsmToken::Minus)) {
725 const MCExpr *Val;
726 if (!getParser().ParseExpression(Val, End)) {
727 End = Parser.getTok().getLoc();
728 return X86Operand::CreateImm(Val, Start, End);
729 }
730 }
731
Devang Patel0a338862012-01-12 01:36:43 +0000732 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000733 unsigned RegNo = 0;
734 if (!ParseRegister(RegNo, Start, End)) {
Devang Patel0a338862012-01-12 01:36:43 +0000735 End = Parser.getTok().getLoc();
736 return X86Operand::CreateReg(RegNo, Start, End);
737 }
738
739 // mem operand
Devang Pateld37ad242012-01-17 18:00:18 +0000740 return ParseIntelMemOperand();
Devang Patel0a338862012-01-12 01:36:43 +0000741}
742
Devang Pateldd929fc2012-01-12 18:03:40 +0000743X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000744 switch (getLexer().getKind()) {
745 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000746 // Parse a memory operand with no segment register.
747 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000748 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000749 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000750 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000751 SMLoc Start, End;
752 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000753 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000754 Error(Start, "%eiz and %riz can only be used as index registers",
755 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000756 return 0;
757 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000758
Chris Lattnereef6d782010-04-17 18:56:34 +0000759 // If this is a segment register followed by a ':', then this is the start
760 // of a memory reference, otherwise this is a normal register reference.
761 if (getLexer().isNot(AsmToken::Colon))
762 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000763
764
Chris Lattnereef6d782010-04-17 18:56:34 +0000765 getParser().Lex(); // Eat the colon.
766 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000767 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000768 case AsmToken::Dollar: {
769 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000770 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000771 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000772 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000773 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000774 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000775 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000776 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000777 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000778}
779
Chris Lattnereef6d782010-04-17 18:56:34 +0000780/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
781/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000782X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000783
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000784 // We have to disambiguate a parenthesized expression "(4+5)" from the start
785 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000786 // only way to do this without lookahead is to eat the '(' and see what is
787 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000788 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000789 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000790 SMLoc ExprEnd;
791 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000792
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000793 // After parsing the base expression we could either have a parenthesized
794 // memory address or not. If not, return now. If so, eat the (.
795 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000796 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000797 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000798 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000799 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000800 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000801
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000802 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000803 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000804 } else {
805 // Okay, we have a '('. We don't know if this is an expression or not, but
806 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000807 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000808 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000809
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000810 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000811 // Nothing to do here, fall into the code below with the '(' part of the
812 // memory operand consumed.
813 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000814 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000815
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000816 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000817 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000818 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000819
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000820 // After parsing the base expression we could either have a parenthesized
821 // memory address or not. If not, return now. If so, eat the (.
822 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000823 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000824 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000825 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000826 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000827 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000828
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000829 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000830 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000831 }
832 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000833
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000834 // If we reached here, then we just ate the ( of the memory operand. Process
835 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000836 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000837
Chris Lattner29ef9a22010-01-15 18:51:29 +0000838 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000839 SMLoc StartLoc, EndLoc;
840 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000841 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000842 Error(StartLoc, "eiz and riz can only be used as index registers",
843 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000844 return 0;
845 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000846 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000847
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000848 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000849 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000850
851 // Following the comma we should have either an index register, or a scale
852 // value. We don't support the later form, but we want to parse it
853 // correctly.
854 //
855 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000856 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000857 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000858 SMLoc L;
859 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000860
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000861 if (getLexer().isNot(AsmToken::RParen)) {
862 // Parse the scale amount:
863 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000864 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000865 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000866 "expected comma in scale expression");
867 return 0;
868 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000869 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000870
871 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000872 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000873
874 int64_t ScaleVal;
875 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000876 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000877
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000878 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000879 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
880 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
881 return 0;
882 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000883 Scale = (unsigned)ScaleVal;
884 }
885 }
886 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000887 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000888 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000889 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000890
891 int64_t Value;
892 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000893 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000894
Daniel Dunbaree910252010-08-24 19:13:38 +0000895 if (Value != 1)
896 Warning(Loc, "scale factor without index register is ignored");
897 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000898 }
899 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000900
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000901 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000902 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000903 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000904 return 0;
905 }
Sean Callanan18b83232010-01-19 21:44:56 +0000906 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000907 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000908
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000909 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
910 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000911}
912
Devang Pateldd929fc2012-01-12 18:03:40 +0000913bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000914ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000915 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000916 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000917
Chris Lattnerd8f71792010-11-28 20:23:50 +0000918 // FIXME: Hack to recognize setneb as setne.
919 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
920 PatchedName != "setb" && PatchedName != "setnb")
921 PatchedName = PatchedName.substr(0, Name.size()-1);
922
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000923 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
924 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000925 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000926 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
927 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000928 bool IsVCMP = PatchedName.startswith("vcmp");
929 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000930 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000931 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000932 .Case("eq", 0)
933 .Case("lt", 1)
934 .Case("le", 2)
935 .Case("unord", 3)
936 .Case("neq", 4)
937 .Case("nlt", 5)
938 .Case("nle", 6)
939 .Case("ord", 7)
940 .Case("eq_uq", 8)
941 .Case("nge", 9)
942 .Case("ngt", 0x0A)
943 .Case("false", 0x0B)
944 .Case("neq_oq", 0x0C)
945 .Case("ge", 0x0D)
946 .Case("gt", 0x0E)
947 .Case("true", 0x0F)
948 .Case("eq_os", 0x10)
949 .Case("lt_oq", 0x11)
950 .Case("le_oq", 0x12)
951 .Case("unord_s", 0x13)
952 .Case("neq_us", 0x14)
953 .Case("nlt_uq", 0x15)
954 .Case("nle_uq", 0x16)
955 .Case("ord_s", 0x17)
956 .Case("eq_us", 0x18)
957 .Case("nge_uq", 0x19)
958 .Case("ngt_uq", 0x1A)
959 .Case("false_os", 0x1B)
960 .Case("neq_os", 0x1C)
961 .Case("ge_oq", 0x1D)
962 .Case("gt_oq", 0x1E)
963 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000964 .Default(~0U);
965 if (SSEComparisonCode != ~0U) {
966 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
967 getParser().getContext());
968 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000969 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000970 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000971 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000972 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000973 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000974 } else {
975 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000976 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000977 }
978 }
979 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000980
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000981 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000982
Devang Patel885f65b2012-01-30 22:47:12 +0000983 if (ExtraImmOp && !isParsingIntelSyntax())
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000984 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000985
Chris Lattner2544f422010-09-08 05:17:37 +0000986 // Determine whether this is an instruction prefix.
987 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000988 Name == "lock" || Name == "rep" ||
989 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000990 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000991 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000992
993
Chris Lattner2544f422010-09-08 05:17:37 +0000994 // This does the actual operand parsing. Don't parse any more if we have a
995 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
996 // just want to parse the "lock" as the first instruction and the "incl" as
997 // the next one.
998 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000999
1000 // Parse '*' modifier.
1001 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +00001002 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +00001003 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +00001004 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +00001005 }
1006
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001007 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001008 if (X86Operand *Op = ParseOperand())
1009 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001010 else {
1011 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001012 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001013 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +00001014
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001015 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001016 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001017
1018 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001019 if (X86Operand *Op = ParseOperand())
1020 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001021 else {
1022 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001023 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001024 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001025 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001026
Chris Lattnercbf8a982010-09-11 16:18:25 +00001027 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001028 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001029 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001030 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001031 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001032 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001033
Chris Lattner2544f422010-09-08 05:17:37 +00001034 if (getLexer().is(AsmToken::EndOfStatement))
1035 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001036 else if (isPrefix && getLexer().is(AsmToken::Slash))
1037 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001038
Devang Patel885f65b2012-01-30 22:47:12 +00001039 if (ExtraImmOp && isParsingIntelSyntax())
1040 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
1041
Chris Lattner98c870f2010-11-06 19:25:43 +00001042 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1043 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1044 // documented form in various unofficial manuals, so a lot of code uses it.
1045 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1046 Operands.size() == 3) {
1047 X86Operand &Op = *(X86Operand*)Operands.back();
1048 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1049 isa<MCConstantExpr>(Op.Mem.Disp) &&
1050 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1051 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1052 SMLoc Loc = Op.getEndLoc();
1053 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1054 delete &Op;
1055 }
1056 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001057 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1058 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1059 Operands.size() == 3) {
1060 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1061 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1062 isa<MCConstantExpr>(Op.Mem.Disp) &&
1063 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1064 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1065 SMLoc Loc = Op.getEndLoc();
1066 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1067 delete &Op;
1068 }
1069 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001070 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1071 if (Name.startswith("ins") && Operands.size() == 3 &&
1072 (Name == "insb" || Name == "insw" || Name == "insl")) {
1073 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1074 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1075 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1076 Operands.pop_back();
1077 Operands.pop_back();
1078 delete &Op;
1079 delete &Op2;
1080 }
1081 }
1082
1083 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1084 if (Name.startswith("outs") && Operands.size() == 3 &&
1085 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1086 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1087 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1088 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1089 Operands.pop_back();
1090 Operands.pop_back();
1091 delete &Op;
1092 delete &Op2;
1093 }
1094 }
1095
1096 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1097 if (Name.startswith("movs") && Operands.size() == 3 &&
1098 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001099 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001100 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1101 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1102 if (isSrcOp(Op) && isDstOp(Op2)) {
1103 Operands.pop_back();
1104 Operands.pop_back();
1105 delete &Op;
1106 delete &Op2;
1107 }
1108 }
1109 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1110 if (Name.startswith("lods") && Operands.size() == 3 &&
1111 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001112 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001113 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1114 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1115 if (isSrcOp(*Op1) && Op2->isReg()) {
1116 const char *ins;
1117 unsigned reg = Op2->getReg();
1118 bool isLods = Name == "lods";
1119 if (reg == X86::AL && (isLods || Name == "lodsb"))
1120 ins = "lodsb";
1121 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1122 ins = "lodsw";
1123 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1124 ins = "lodsl";
1125 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1126 ins = "lodsq";
1127 else
1128 ins = NULL;
1129 if (ins != NULL) {
1130 Operands.pop_back();
1131 Operands.pop_back();
1132 delete Op1;
1133 delete Op2;
1134 if (Name != ins)
1135 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1136 }
1137 }
1138 }
1139 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1140 if (Name.startswith("stos") && Operands.size() == 3 &&
1141 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001142 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001143 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1144 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1145 if (isDstOp(*Op2) && Op1->isReg()) {
1146 const char *ins;
1147 unsigned reg = Op1->getReg();
1148 bool isStos = Name == "stos";
1149 if (reg == X86::AL && (isStos || Name == "stosb"))
1150 ins = "stosb";
1151 else if (reg == X86::AX && (isStos || Name == "stosw"))
1152 ins = "stosw";
1153 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1154 ins = "stosl";
1155 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1156 ins = "stosq";
1157 else
1158 ins = NULL;
1159 if (ins != NULL) {
1160 Operands.pop_back();
1161 Operands.pop_back();
1162 delete Op1;
1163 delete Op2;
1164 if (Name != ins)
1165 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1166 }
1167 }
1168 }
1169
Chris Lattnere9e16a32010-09-15 04:33:27 +00001170 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001171 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001172 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001173 Name.startswith("shl") || Name.startswith("sal") ||
1174 Name.startswith("rcl") || Name.startswith("rcr") ||
1175 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001176 Operands.size() == 3) {
Devang Patelbe3e3102012-01-30 20:02:42 +00001177 if (isParsingIntelSyntax()) {
Devang Patel3b96e1f2012-01-24 21:43:36 +00001178 // Intel syntax
1179 X86Operand *Op1 = static_cast<X86Operand*>(Operands[2]);
1180 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1181 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1182 delete Operands[2];
1183 Operands.pop_back();
1184 }
1185 } else {
1186 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1187 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1188 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1189 delete Operands[1];
1190 Operands.erase(Operands.begin() + 1);
1191 }
Chris Lattner47ab90b2010-09-06 18:32:06 +00001192 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001193 }
Chris Lattner15f89512011-04-09 19:41:05 +00001194
1195 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1196 // instalias with an immediate operand yet.
1197 if (Name == "int" && Operands.size() == 2) {
1198 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1199 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1200 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1201 delete Operands[1];
1202 Operands.erase(Operands.begin() + 1);
1203 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1204 }
1205 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001206
Chris Lattner98986712010-01-14 22:21:20 +00001207 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001208}
1209
Devang Pateldd929fc2012-01-12 18:03:40 +00001210bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001211processInstruction(MCInst &Inst,
1212 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1213 switch (Inst.getOpcode()) {
1214 default: return false;
1215 case X86::AND16i16: {
1216 if (!Inst.getOperand(0).isImm() ||
1217 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1218 return false;
1219
1220 MCInst TmpInst;
1221 TmpInst.setOpcode(X86::AND16ri8);
1222 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1223 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1224 TmpInst.addOperand(Inst.getOperand(0));
1225 Inst = TmpInst;
1226 return true;
1227 }
1228 case X86::AND32i32: {
1229 if (!Inst.getOperand(0).isImm() ||
1230 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1231 return false;
1232
1233 MCInst TmpInst;
1234 TmpInst.setOpcode(X86::AND32ri8);
1235 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1236 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1237 TmpInst.addOperand(Inst.getOperand(0));
1238 Inst = TmpInst;
1239 return true;
1240 }
1241 case X86::AND64i32: {
1242 if (!Inst.getOperand(0).isImm() ||
1243 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1244 return false;
1245
1246 MCInst TmpInst;
1247 TmpInst.setOpcode(X86::AND64ri8);
1248 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1249 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1250 TmpInst.addOperand(Inst.getOperand(0));
1251 Inst = TmpInst;
1252 return true;
1253 }
Devang Patelac0f0482012-01-19 17:53:25 +00001254 case X86::XOR16i16: {
1255 if (!Inst.getOperand(0).isImm() ||
1256 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1257 return false;
1258
1259 MCInst TmpInst;
1260 TmpInst.setOpcode(X86::XOR16ri8);
1261 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1262 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1263 TmpInst.addOperand(Inst.getOperand(0));
1264 Inst = TmpInst;
1265 return true;
1266 }
1267 case X86::XOR32i32: {
1268 if (!Inst.getOperand(0).isImm() ||
1269 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1270 return false;
1271
1272 MCInst TmpInst;
1273 TmpInst.setOpcode(X86::XOR32ri8);
1274 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1275 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1276 TmpInst.addOperand(Inst.getOperand(0));
1277 Inst = TmpInst;
1278 return true;
1279 }
1280 case X86::XOR64i32: {
1281 if (!Inst.getOperand(0).isImm() ||
1282 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1283 return false;
1284
1285 MCInst TmpInst;
1286 TmpInst.setOpcode(X86::XOR64ri8);
1287 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1288 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1289 TmpInst.addOperand(Inst.getOperand(0));
1290 Inst = TmpInst;
1291 return true;
1292 }
1293 case X86::OR16i16: {
1294 if (!Inst.getOperand(0).isImm() ||
1295 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1296 return false;
1297
1298 MCInst TmpInst;
1299 TmpInst.setOpcode(X86::OR16ri8);
1300 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1301 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1302 TmpInst.addOperand(Inst.getOperand(0));
1303 Inst = TmpInst;
1304 return true;
1305 }
1306 case X86::OR32i32: {
1307 if (!Inst.getOperand(0).isImm() ||
1308 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1309 return false;
1310
1311 MCInst TmpInst;
1312 TmpInst.setOpcode(X86::OR32ri8);
1313 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1314 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1315 TmpInst.addOperand(Inst.getOperand(0));
1316 Inst = TmpInst;
1317 return true;
1318 }
1319 case X86::OR64i32: {
1320 if (!Inst.getOperand(0).isImm() ||
1321 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1322 return false;
1323
1324 MCInst TmpInst;
1325 TmpInst.setOpcode(X86::OR64ri8);
1326 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1327 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1328 TmpInst.addOperand(Inst.getOperand(0));
1329 Inst = TmpInst;
1330 return true;
1331 }
1332 case X86::CMP16i16: {
1333 if (!Inst.getOperand(0).isImm() ||
1334 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1335 return false;
1336
1337 MCInst TmpInst;
1338 TmpInst.setOpcode(X86::CMP16ri8);
1339 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1340 TmpInst.addOperand(Inst.getOperand(0));
1341 Inst = TmpInst;
1342 return true;
1343 }
1344 case X86::CMP32i32: {
1345 if (!Inst.getOperand(0).isImm() ||
1346 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1347 return false;
1348
1349 MCInst TmpInst;
1350 TmpInst.setOpcode(X86::CMP32ri8);
1351 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1352 TmpInst.addOperand(Inst.getOperand(0));
1353 Inst = TmpInst;
1354 return true;
1355 }
1356 case X86::CMP64i32: {
1357 if (!Inst.getOperand(0).isImm() ||
1358 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1359 return false;
1360
1361 MCInst TmpInst;
1362 TmpInst.setOpcode(X86::CMP64ri8);
1363 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1364 TmpInst.addOperand(Inst.getOperand(0));
1365 Inst = TmpInst;
1366 return true;
1367 }
Devang Patela951f772012-01-19 18:40:55 +00001368 case X86::ADD16i16: {
1369 if (!Inst.getOperand(0).isImm() ||
1370 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1371 return false;
1372
1373 MCInst TmpInst;
1374 TmpInst.setOpcode(X86::ADD16ri8);
1375 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1376 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1377 TmpInst.addOperand(Inst.getOperand(0));
1378 Inst = TmpInst;
1379 return true;
1380 }
1381 case X86::ADD32i32: {
1382 if (!Inst.getOperand(0).isImm() ||
1383 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1384 return false;
1385
1386 MCInst TmpInst;
1387 TmpInst.setOpcode(X86::ADD32ri8);
1388 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1389 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1390 TmpInst.addOperand(Inst.getOperand(0));
1391 Inst = TmpInst;
1392 return true;
1393 }
1394 case X86::ADD64i32: {
1395 if (!Inst.getOperand(0).isImm() ||
1396 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1397 return false;
1398
1399 MCInst TmpInst;
1400 TmpInst.setOpcode(X86::ADD64ri8);
1401 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1402 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1403 TmpInst.addOperand(Inst.getOperand(0));
1404 Inst = TmpInst;
1405 return true;
1406 }
1407 case X86::SUB16i16: {
1408 if (!Inst.getOperand(0).isImm() ||
1409 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1410 return false;
1411
1412 MCInst TmpInst;
1413 TmpInst.setOpcode(X86::SUB16ri8);
1414 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1415 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1416 TmpInst.addOperand(Inst.getOperand(0));
1417 Inst = TmpInst;
1418 return true;
1419 }
1420 case X86::SUB32i32: {
1421 if (!Inst.getOperand(0).isImm() ||
1422 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1423 return false;
1424
1425 MCInst TmpInst;
1426 TmpInst.setOpcode(X86::SUB32ri8);
1427 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1428 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1429 TmpInst.addOperand(Inst.getOperand(0));
1430 Inst = TmpInst;
1431 return true;
1432 }
1433 case X86::SUB64i32: {
1434 if (!Inst.getOperand(0).isImm() ||
1435 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1436 return false;
1437
1438 MCInst TmpInst;
1439 TmpInst.setOpcode(X86::SUB64ri8);
1440 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1441 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1442 TmpInst.addOperand(Inst.getOperand(0));
1443 Inst = TmpInst;
1444 return true;
1445 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001446 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001447}
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")) {
Devang Patel0db58bf2012-01-31 18:14:05 +00001647 getParser().setAssemblerDialect(1);
Devang Patelbe3e3102012-01-30 20:02:42 +00001648 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"