blob: f2fee362611ef1d0ba80fcae9897b4720bc9694a [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;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +000037
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)
Evan Cheng94b95502011-07-26 00:24:13 +000097 : MCTargetAsmParser(), STI(sti), Parser(parser) {
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);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000108};
Chris Lattner37dfdec2009-07-29 06:33:53 +0000109} // end anonymous namespace
110
Sean Callanane9b466d2010-01-23 00:40:33 +0000111/// @name Auto-generated Match Functions
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000112/// {
Sean Callanane9b466d2010-01-23 00:40:33 +0000113
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000114static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +0000115
116/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +0000117
Devang Patelb8ba13f2012-01-18 22:42:29 +0000118static bool isImmSExti16i8Value(uint64_t Value) {
119 return (( Value <= 0x000000000000007FULL)||
120 (0x000000000000FF80ULL <= Value && Value <= 0x000000000000FFFFULL)||
121 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
122}
123
124static bool isImmSExti32i8Value(uint64_t Value) {
125 return (( Value <= 0x000000000000007FULL)||
126 (0x00000000FFFFFF80ULL <= Value && Value <= 0x00000000FFFFFFFFULL)||
127 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
128}
129
130static bool isImmZExtu32u8Value(uint64_t Value) {
131 return (Value <= 0x00000000000000FFULL);
132}
133
134static bool isImmSExti64i8Value(uint64_t Value) {
135 return (( Value <= 0x000000000000007FULL)||
136 (0xFFFFFFFFFFFFFF80ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
137}
138
139static bool isImmSExti64i32Value(uint64_t Value) {
140 return (( Value <= 0x000000007FFFFFFFULL)||
141 (0xFFFFFFFF80000000ULL <= Value && Value <= 0xFFFFFFFFFFFFFFFFULL));
142}
Chris Lattner37dfdec2009-07-29 06:33:53 +0000143namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000144
145/// X86Operand - Instances of this class represent a parsed X86 machine
146/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000147struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000148 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000149 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000150 Register,
151 Immediate,
152 Memory
153 } Kind;
154
Chris Lattner29ef9a22010-01-15 18:51:29 +0000155 SMLoc StartLoc, EndLoc;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000156
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000157 union {
158 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000159 const char *Data;
160 unsigned Length;
161 } Tok;
162
163 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000164 unsigned RegNo;
165 } Reg;
166
167 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000168 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000169 } Imm;
170
171 struct {
172 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000173 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000174 unsigned BaseReg;
175 unsigned IndexReg;
176 unsigned Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000177 unsigned Size;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000178 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000179 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000180
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000181 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000182 : Kind(K), StartLoc(Start), EndLoc(End) {}
Daniel Dunbarc918d602010-05-04 16:12:42 +0000183
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000184 /// getStartLoc - Get the location of the first token of this operand.
185 SMLoc getStartLoc() const { return StartLoc; }
186 /// getEndLoc - Get the location of the last token of this operand.
187 SMLoc getEndLoc() const { return EndLoc; }
Chris Lattnerd8b7aa22011-10-16 04:47:35 +0000188
189 SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000190
Jim Grosbachb7f689b2011-07-13 15:34:57 +0000191 virtual void print(raw_ostream &OS) const {}
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000192
Daniel Dunbar20927f22009-08-07 08:26:05 +0000193 StringRef getToken() const {
194 assert(Kind == Token && "Invalid access!");
195 return StringRef(Tok.Data, Tok.Length);
196 }
Daniel Dunbarc918d602010-05-04 16:12:42 +0000197 void setTokenValue(StringRef Value) {
198 assert(Kind == Token && "Invalid access!");
199 Tok.Data = Value.data();
200 Tok.Length = Value.size();
201 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000202
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000203 unsigned getReg() const {
204 assert(Kind == Register && "Invalid access!");
205 return Reg.RegNo;
206 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000207
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000208 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000209 assert(Kind == Immediate && "Invalid access!");
210 return Imm.Val;
211 }
212
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000213 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000214 assert(Kind == Memory && "Invalid access!");
215 return Mem.Disp;
216 }
217 unsigned getMemSegReg() const {
218 assert(Kind == Memory && "Invalid access!");
219 return Mem.SegReg;
220 }
221 unsigned getMemBaseReg() const {
222 assert(Kind == Memory && "Invalid access!");
223 return Mem.BaseReg;
224 }
225 unsigned getMemIndexReg() const {
226 assert(Kind == Memory && "Invalid access!");
227 return Mem.IndexReg;
228 }
229 unsigned getMemScale() const {
230 assert(Kind == Memory && "Invalid access!");
231 return Mem.Scale;
232 }
233
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000234 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000235
236 bool isImm() const { return Kind == Immediate; }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000237
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000238 bool isImmSExti16i8() const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000239 if (!isImm())
240 return false;
241
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000242 // If this isn't a constant expr, just assume it fits and let relaxation
243 // handle it.
244 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
245 if (!CE)
246 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000247
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000248 // Otherwise, check the value is in a range that makes sense for this
249 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000250 return isImmSExti16i8Value(CE->getValue());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000251 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000252 bool isImmSExti32i8() const {
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000253 if (!isImm())
254 return false;
255
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000256 // If this isn't a constant expr, just assume it fits and let relaxation
257 // handle it.
258 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
259 if (!CE)
260 return true;
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000261
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000262 // Otherwise, check the value is in a range that makes sense for this
263 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000264 return isImmSExti32i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000265 }
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000266 bool isImmZExtu32u8() const {
267 if (!isImm())
268 return false;
269
270 // If this isn't a constant expr, just assume it fits and let relaxation
271 // handle it.
272 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
273 if (!CE)
274 return true;
275
276 // Otherwise, check the value is in a range that makes sense for this
277 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000278 return isImmZExtu32u8Value(CE->getValue());
Kevin Enderbyc37d4bb2011-07-27 23:01:50 +0000279 }
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000280 bool isImmSExti64i8() const {
281 if (!isImm())
282 return false;
283
284 // If this isn't a constant expr, just assume it fits and let relaxation
285 // handle it.
286 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
287 if (!CE)
288 return true;
289
290 // Otherwise, check the value is in a range that makes sense for this
291 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000292 return isImmSExti64i8Value(CE->getValue());
Daniel Dunbar62e4c672010-05-22 21:02:33 +0000293 }
294 bool isImmSExti64i32() const {
295 if (!isImm())
296 return false;
297
298 // If this isn't a constant expr, just assume it fits and let relaxation
299 // handle it.
300 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm());
301 if (!CE)
302 return true;
303
304 // Otherwise, check the value is in a range that makes sense for this
305 // extension.
Devang Patelb8ba13f2012-01-18 22:42:29 +0000306 return isImmSExti64i32Value(CE->getValue());
Daniel Dunbar1fe591d2010-05-20 20:20:39 +0000307 }
308
Daniel Dunbar20927f22009-08-07 08:26:05 +0000309 bool isMem() const { return Kind == Memory; }
Devang Patelc59d9df2012-01-12 01:51:42 +0000310 bool isMem8() const {
311 return Kind == Memory && (!Mem.Size || Mem.Size == 8);
312 }
313 bool isMem16() const {
314 return Kind == Memory && (!Mem.Size || Mem.Size == 16);
315 }
316 bool isMem32() const {
317 return Kind == Memory && (!Mem.Size || Mem.Size == 32);
318 }
319 bool isMem64() const {
320 return Kind == Memory && (!Mem.Size || Mem.Size == 64);
321 }
322 bool isMem80() const {
323 return Kind == Memory && (!Mem.Size || Mem.Size == 80);
324 }
325 bool isMem128() const {
326 return Kind == Memory && (!Mem.Size || Mem.Size == 128);
327 }
328 bool isMem256() const {
329 return Kind == Memory && (!Mem.Size || Mem.Size == 256);
330 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000331
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000332 bool isAbsMem() const {
333 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000334 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000335 }
336
Daniel Dunbar20927f22009-08-07 08:26:05 +0000337 bool isReg() const { return Kind == Register; }
338
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000339 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
340 // Add as immediates when possible.
341 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
342 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
343 else
344 Inst.addOperand(MCOperand::CreateExpr(Expr));
345 }
346
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000347 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000348 assert(N == 1 && "Invalid number of operands!");
349 Inst.addOperand(MCOperand::CreateReg(getReg()));
350 }
351
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000352 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000353 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000354 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000355 }
356
Devang Patelc59d9df2012-01-12 01:51:42 +0000357 void addMem8Operands(MCInst &Inst, unsigned N) const {
358 addMemOperands(Inst, N);
359 }
360 void addMem16Operands(MCInst &Inst, unsigned N) const {
361 addMemOperands(Inst, N);
362 }
363 void addMem32Operands(MCInst &Inst, unsigned N) const {
364 addMemOperands(Inst, N);
365 }
366 void addMem64Operands(MCInst &Inst, unsigned N) const {
367 addMemOperands(Inst, N);
368 }
369 void addMem80Operands(MCInst &Inst, unsigned N) const {
370 addMemOperands(Inst, N);
371 }
372 void addMem128Operands(MCInst &Inst, unsigned N) const {
373 addMemOperands(Inst, N);
374 }
375 void addMem256Operands(MCInst &Inst, unsigned N) const {
376 addMemOperands(Inst, N);
377 }
378
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000379 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000380 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000381 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
382 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
383 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000384 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000385 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
386 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000387
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000388 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
389 assert((N == 1) && "Invalid number of operands!");
390 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
391 }
392
Chris Lattnerb4307b32010-01-15 19:28:38 +0000393 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +0000394 SMLoc EndLoc = SMLoc::getFromPointer(Loc.getPointer() + Str.size() - 1);
395 X86Operand *Res = new X86Operand(Token, Loc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000396 Res->Tok.Data = Str.data();
397 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000398 return Res;
399 }
400
Chris Lattner29ef9a22010-01-15 18:51:29 +0000401 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000402 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000403 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000404 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000405 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000406
Chris Lattnerb4307b32010-01-15 19:28:38 +0000407 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
408 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000409 Res->Imm.Val = Val;
410 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000411 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000412
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000413 /// Create an absolute memory operand.
414 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
Devang Patelc59d9df2012-01-12 01:51:42 +0000415 SMLoc EndLoc, unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000416 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
417 Res->Mem.SegReg = 0;
418 Res->Mem.Disp = Disp;
419 Res->Mem.BaseReg = 0;
420 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000421 Res->Mem.Scale = 1;
Devang Patelc59d9df2012-01-12 01:51:42 +0000422 Res->Mem.Size = Size;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000423 return Res;
424 }
425
426 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000427 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
428 unsigned BaseReg, unsigned IndexReg,
Devang Patelc59d9df2012-01-12 01:51:42 +0000429 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc,
430 unsigned Size = 0) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000431 // We should never just have a displacement, that should be parsed as an
432 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000433 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
434
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000435 // The scale should always be one of {1,2,4,8}.
436 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000437 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000438 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000439 Res->Mem.SegReg = SegReg;
440 Res->Mem.Disp = Disp;
441 Res->Mem.BaseReg = BaseReg;
442 Res->Mem.IndexReg = IndexReg;
443 Res->Mem.Scale = Scale;
Devang Patelc59d9df2012-01-12 01:51:42 +0000444 Res->Mem.Size = Size;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000445 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000446 }
447};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000448
Chris Lattner37dfdec2009-07-29 06:33:53 +0000449} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000450
Devang Pateldd929fc2012-01-12 18:03:40 +0000451bool X86AsmParser::isSrcOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000452 unsigned basereg = is64BitMode() ? X86::RSI : X86::ESI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000453
454 return (Op.isMem() &&
455 (Op.Mem.SegReg == 0 || Op.Mem.SegReg == X86::DS) &&
456 isa<MCConstantExpr>(Op.Mem.Disp) &&
457 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
458 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0);
459}
460
Devang Pateldd929fc2012-01-12 18:03:40 +0000461bool X86AsmParser::isDstOp(X86Operand &Op) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000462 unsigned basereg = is64BitMode() ? X86::RDI : X86::EDI;
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +0000463
464 return Op.isMem() && Op.Mem.SegReg == X86::ES &&
465 isa<MCConstantExpr>(Op.Mem.Disp) &&
466 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
467 Op.Mem.BaseReg == basereg && Op.Mem.IndexReg == 0;
468}
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000469
Devang Pateldd929fc2012-01-12 18:03:40 +0000470bool X86AsmParser::ParseRegister(unsigned &RegNo,
471 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000472 RegNo = 0;
Devang Patel1aea4302012-01-20 22:32:05 +0000473 bool IntelSyntax = getParser().getAssemblerDialect();
474 if (!IntelSyntax) {
475 const AsmToken &TokPercent = Parser.getTok();
Devang Pateld37ad242012-01-17 18:00:18 +0000476 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
477 StartLoc = TokPercent.getLoc();
478 Parser.Lex(); // Eat percent token.
479 }
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000480
Sean Callanan18b83232010-01-19 21:44:56 +0000481 const AsmToken &Tok = Parser.getTok();
Devang Patel1aea4302012-01-20 22:32:05 +0000482 if (Tok.isNot(AsmToken::Identifier)) {
483 if (IntelSyntax) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000484 return Error(StartLoc, "invalid register name",
485 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000486 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000487
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000488 RegNo = MatchRegisterName(Tok.getString());
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000489
Chris Lattner33d60d52010-09-22 04:11:10 +0000490 // If the match failed, try the register name as lowercase.
491 if (RegNo == 0)
Benjamin Kramer59085362011-11-06 20:37:06 +0000492 RegNo = MatchRegisterName(Tok.getString().lower());
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000493
Evan Cheng5de728c2011-07-27 23:22:03 +0000494 if (!is64BitMode()) {
495 // FIXME: This should be done using Requires<In32BitMode> and
496 // Requires<In64BitMode> so "eiz" usage in 64-bit instructions can be also
497 // checked.
498 // FIXME: Check AH, CH, DH, BH cannot be used in an instruction requiring a
499 // REX prefix.
500 if (RegNo == X86::RIZ ||
501 X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
502 X86II::isX86_64NonExtLowByteReg(RegNo) ||
503 X86II::isX86_64ExtendedReg(RegNo))
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000504 return Error(StartLoc, "register %"
505 + Tok.getString() + " is only available in 64-bit mode",
506 SMRange(StartLoc, Tok.getEndLoc()));
Evan Cheng5de728c2011-07-27 23:22:03 +0000507 }
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000508
Chris Lattner33d60d52010-09-22 04:11:10 +0000509 // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
510 if (RegNo == 0 && (Tok.getString() == "st" || Tok.getString() == "ST")) {
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000511 RegNo = X86::ST0;
512 EndLoc = Tok.getLoc();
513 Parser.Lex(); // Eat 'st'
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000514
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000515 // Check to see if we have '(4)' after %st.
516 if (getLexer().isNot(AsmToken::LParen))
517 return false;
518 // Lex the paren.
519 getParser().Lex();
520
521 const AsmToken &IntTok = Parser.getTok();
522 if (IntTok.isNot(AsmToken::Integer))
523 return Error(IntTok.getLoc(), "expected stack index");
524 switch (IntTok.getIntVal()) {
525 case 0: RegNo = X86::ST0; break;
526 case 1: RegNo = X86::ST1; break;
527 case 2: RegNo = X86::ST2; break;
528 case 3: RegNo = X86::ST3; break;
529 case 4: RegNo = X86::ST4; break;
530 case 5: RegNo = X86::ST5; break;
531 case 6: RegNo = X86::ST6; break;
532 case 7: RegNo = X86::ST7; break;
533 default: return Error(IntTok.getLoc(), "invalid stack index");
534 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000535
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000536 if (getParser().Lex().isNot(AsmToken::RParen))
537 return Error(Parser.getTok().getLoc(), "expected ')'");
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000538
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000539 EndLoc = Tok.getLoc();
540 Parser.Lex(); // Eat ')'
541 return false;
542 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000543
Chris Lattner645b2092010-06-24 07:29:18 +0000544 // If this is "db[0-7]", match it as an alias
545 // for dr[0-7].
546 if (RegNo == 0 && Tok.getString().size() == 3 &&
547 Tok.getString().startswith("db")) {
548 switch (Tok.getString()[2]) {
549 case '0': RegNo = X86::DR0; break;
550 case '1': RegNo = X86::DR1; break;
551 case '2': RegNo = X86::DR2; break;
552 case '3': RegNo = X86::DR3; break;
553 case '4': RegNo = X86::DR4; break;
554 case '5': RegNo = X86::DR5; break;
555 case '6': RegNo = X86::DR6; break;
556 case '7': RegNo = X86::DR7; break;
557 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000558
Chris Lattner645b2092010-06-24 07:29:18 +0000559 if (RegNo != 0) {
560 EndLoc = Tok.getLoc();
561 Parser.Lex(); // Eat it.
562 return false;
563 }
564 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000565
Devang Patel1aea4302012-01-20 22:32:05 +0000566 if (RegNo == 0) {
567 if (IntelSyntax) return true;
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000568 return Error(StartLoc, "invalid register name",
569 SMRange(StartLoc, Tok.getEndLoc()));
Devang Patel1aea4302012-01-20 22:32:05 +0000570 }
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000571
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000572 EndLoc = Tok.getEndLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000573 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000574 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000575}
576
Devang Pateldd929fc2012-01-12 18:03:40 +0000577X86Operand *X86AsmParser::ParseOperand() {
Devang Patel0a338862012-01-12 01:36:43 +0000578 if (getParser().getAssemblerDialect())
579 return ParseIntelOperand();
580 return ParseATTOperand();
581}
582
Devang Pateld37ad242012-01-17 18:00:18 +0000583/// getIntelMemOperandSize - Return intel memory operand size.
584static unsigned getIntelMemOperandSize(StringRef OpStr) {
585 unsigned Size = 0;
Devang Patel0a338862012-01-12 01:36:43 +0000586 if (OpStr == "BYTE") Size = 8;
587 if (OpStr == "WORD") Size = 16;
588 if (OpStr == "DWORD") Size = 32;
589 if (OpStr == "QWORD") Size = 64;
590 if (OpStr == "XWORD") Size = 80;
591 if (OpStr == "XMMWORD") Size = 128;
592 if (OpStr == "YMMWORD") Size = 256;
Devang Pateld37ad242012-01-17 18:00:18 +0000593 return Size;
Devang Patel0a338862012-01-12 01:36:43 +0000594}
595
Devang Patel7c64fe62012-01-23 18:31:58 +0000596X86Operand *X86AsmParser::ParseIntelBracExpression(unsigned SegReg,
597 unsigned Size) {
598 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Devang Patel0a338862012-01-12 01:36:43 +0000599 SMLoc Start = Parser.getTok().getLoc(), End;
600
Devang Pateld37ad242012-01-17 18:00:18 +0000601 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
602 // Parse [ BaseReg + Scale*IndexReg + Disp ] or [ symbol ]
603
604 // Eat '['
605 if (getLexer().isNot(AsmToken::LBrac))
606 return ErrorOperand(Start, "Expected '[' token!");
607 Parser.Lex();
608
609 if (getLexer().is(AsmToken::Identifier)) {
610 // Parse BaseReg
Devang Patel1aea4302012-01-20 22:32:05 +0000611 if (ParseRegister(BaseReg, Start, End)) {
Devang Pateld37ad242012-01-17 18:00:18 +0000612 // Handle '[' 'symbol' ']'
613 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
614 if (getParser().ParseExpression(Disp, End)) return 0;
615 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000616 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000617 Parser.Lex();
618 return X86Operand::CreateMem(Disp, Start, End, Size);
619 }
620 } else if (getLexer().is(AsmToken::Integer)) {
621 // Handle '[' number ']'
622 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
623 if (getParser().ParseExpression(Disp, End)) return 0;
624 if (getLexer().isNot(AsmToken::RBrac))
Devang Patelbc51e502012-01-17 19:09:22 +0000625 return ErrorOperand(Start, "Expected ']' token!");
Devang Pateld37ad242012-01-17 18:00:18 +0000626 Parser.Lex();
627 return X86Operand::CreateMem(Disp, Start, End, Size);
628 }
629
630 if (getLexer().is(AsmToken::Plus) || getLexer().is(AsmToken::Minus)) {
631 bool isPlus = getLexer().is(AsmToken::Plus);
632 Parser.Lex();
633 SMLoc PlusLoc = Parser.getTok().getLoc();
634 if (getLexer().is(AsmToken::Integer)) {
635 int64_t Val = Parser.getTok().getIntVal();
636 Parser.Lex();
637 if (getLexer().is(AsmToken::Star)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000638 Parser.Lex();
639 SMLoc IdxRegLoc = Parser.getTok().getLoc();
Devang Patel1aea4302012-01-20 22:32:05 +0000640 if (ParseRegister(IndexReg, IdxRegLoc, End))
641 return ErrorOperand(IdxRegLoc, "Expected register");
Devang Patelbc51e502012-01-17 19:09:22 +0000642 Scale = Val;
Devang Pateld37ad242012-01-17 18:00:18 +0000643 } else if (getLexer().is(AsmToken::RBrac)) {
Devang Patelbc51e502012-01-17 19:09:22 +0000644 const MCExpr *ValExpr = MCConstantExpr::Create(Val, getContext());
Devang Patele60540f2012-01-19 18:15:51 +0000645 Disp = isPlus ? ValExpr : MCConstantExpr::Create(0-Val, getContext());
Devang Pateld37ad242012-01-17 18:00:18 +0000646 } else
Devang Patelbc51e502012-01-17 19:09:22 +0000647 return ErrorOperand(PlusLoc, "unexpected token after +");
Devang Patel1aea4302012-01-20 22:32:05 +0000648 } else if (getLexer().is(AsmToken::Identifier))
649 ParseRegister(IndexReg, Start, End);
Devang Pateld37ad242012-01-17 18:00:18 +0000650 }
651
652 if (getLexer().isNot(AsmToken::RBrac))
653 if (getParser().ParseExpression(Disp, End)) return 0;
654
655 End = Parser.getTok().getLoc();
656 if (getLexer().isNot(AsmToken::RBrac))
657 return ErrorOperand(End, "expected ']' token!");
658 Parser.Lex();
659 End = Parser.getTok().getLoc();
Devang Patelfdd3b302012-01-20 21:21:01 +0000660
661 // handle [-42]
662 if (!BaseReg && !IndexReg)
663 return X86Operand::CreateMem(Disp, Start, End, Size);
664
Devang Pateld37ad242012-01-17 18:00:18 +0000665 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
Devang Patelbc51e502012-01-17 19:09:22 +0000666 Start, End, Size);
Devang Pateld37ad242012-01-17 18:00:18 +0000667}
668
669/// ParseIntelMemOperand - Parse intel style memory operand.
670X86Operand *X86AsmParser::ParseIntelMemOperand() {
671 const AsmToken &Tok = Parser.getTok();
672 SMLoc Start = Parser.getTok().getLoc(), End;
Devang Patel7c64fe62012-01-23 18:31:58 +0000673 unsigned SegReg = 0;
Devang Pateld37ad242012-01-17 18:00:18 +0000674
675 unsigned Size = getIntelMemOperandSize(Tok.getString());
676 if (Size) {
677 Parser.Lex();
678 assert (Tok.getString() == "PTR" && "Unexpected token!");
679 Parser.Lex();
680 }
681
682 if (getLexer().is(AsmToken::LBrac))
Devang Patel7c64fe62012-01-23 18:31:58 +0000683 return ParseIntelBracExpression(SegReg, Size);
684
685 if (!ParseRegister(SegReg, Start, End)) {
686 // Handel SegReg : [ ... ]
687 if (getLexer().isNot(AsmToken::Colon))
688 return ErrorOperand(Start, "Expected ':' token!");
689 Parser.Lex(); // Eat :
690 if (getLexer().isNot(AsmToken::LBrac))
691 return ErrorOperand(Start, "Expected '[' token!");
692 return ParseIntelBracExpression(SegReg, Size);
693 }
Devang Pateld37ad242012-01-17 18:00:18 +0000694
695 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
696 if (getParser().ParseExpression(Disp, End)) return 0;
697 return X86Operand::CreateMem(Disp, Start, End, Size);
698}
699
700X86Operand *X86AsmParser::ParseIntelOperand() {
Devang Pateld37ad242012-01-17 18:00:18 +0000701 SMLoc Start = Parser.getTok().getLoc(), End;
702
703 // immediate.
704 if (getLexer().is(AsmToken::Integer) || getLexer().is(AsmToken::Real) ||
705 getLexer().is(AsmToken::Minus)) {
706 const MCExpr *Val;
707 if (!getParser().ParseExpression(Val, End)) {
708 End = Parser.getTok().getLoc();
709 return X86Operand::CreateImm(Val, Start, End);
710 }
711 }
712
Devang Patel0a338862012-01-12 01:36:43 +0000713 // register
Devang Patel1aea4302012-01-20 22:32:05 +0000714 unsigned RegNo = 0;
715 if (!ParseRegister(RegNo, Start, End)) {
Devang Patel0a338862012-01-12 01:36:43 +0000716 End = Parser.getTok().getLoc();
717 return X86Operand::CreateReg(RegNo, Start, End);
718 }
719
720 // mem operand
Devang Pateld37ad242012-01-17 18:00:18 +0000721 return ParseIntelMemOperand();
Devang Patel0a338862012-01-12 01:36:43 +0000722}
723
Devang Pateldd929fc2012-01-12 18:03:40 +0000724X86Operand *X86AsmParser::ParseATTOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000725 switch (getLexer().getKind()) {
726 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000727 // Parse a memory operand with no segment register.
728 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000729 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000730 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000731 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000732 SMLoc Start, End;
733 if (ParseRegister(RegNo, Start, End)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000734 if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000735 Error(Start, "%eiz and %riz can only be used as index registers",
736 SMRange(Start, End));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000737 return 0;
738 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000739
Chris Lattnereef6d782010-04-17 18:56:34 +0000740 // If this is a segment register followed by a ':', then this is the start
741 // of a memory reference, otherwise this is a normal register reference.
742 if (getLexer().isNot(AsmToken::Colon))
743 return X86Operand::CreateReg(RegNo, Start, End);
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000744
745
Chris Lattnereef6d782010-04-17 18:56:34 +0000746 getParser().Lex(); // Eat the colon.
747 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000748 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000749 case AsmToken::Dollar: {
750 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000751 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000752 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000753 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000754 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000755 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000756 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000757 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000758 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000759}
760
Chris Lattnereef6d782010-04-17 18:56:34 +0000761/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
762/// has already been parsed if present.
Devang Pateldd929fc2012-01-12 18:03:40 +0000763X86Operand *X86AsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000764
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000765 // We have to disambiguate a parenthesized expression "(4+5)" from the start
766 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000767 // only way to do this without lookahead is to eat the '(' and see what is
768 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000769 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000770 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000771 SMLoc ExprEnd;
772 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000773
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000774 // After parsing the base expression we could either have a parenthesized
775 // memory address or not. If not, return now. If so, eat the (.
776 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000777 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000778 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000779 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000780 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000781 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000782
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000783 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000784 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000785 } else {
786 // Okay, we have a '('. We don't know if this is an expression or not, but
787 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000788 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000789 Parser.Lex(); // Eat the '('.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000790
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000791 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000792 // Nothing to do here, fall into the code below with the '(' part of the
793 // memory operand consumed.
794 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000795 SMLoc ExprEnd;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000796
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000797 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000798 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000799 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000800
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000801 // After parsing the base expression we could either have a parenthesized
802 // memory address or not. If not, return now. If so, eat the (.
803 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000804 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000805 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000806 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000807 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000808 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000809
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000810 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000811 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000812 }
813 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000814
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000815 // If we reached here, then we just ate the ( of the memory operand. Process
816 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000817 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000818
Chris Lattner29ef9a22010-01-15 18:51:29 +0000819 if (getLexer().is(AsmToken::Percent)) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000820 SMLoc StartLoc, EndLoc;
821 if (ParseRegister(BaseReg, StartLoc, EndLoc)) return 0;
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000822 if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
Benjamin Kramer5efabcf2011-10-16 12:10:27 +0000823 Error(StartLoc, "eiz and riz can only be used as index registers",
824 SMRange(StartLoc, EndLoc));
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000825 return 0;
826 }
Chris Lattner29ef9a22010-01-15 18:51:29 +0000827 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000828
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000829 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000830 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000831
832 // Following the comma we should have either an index register, or a scale
833 // value. We don't support the later form, but we want to parse it
834 // correctly.
835 //
836 // Not that even though it would be completely consistent to support syntax
Bruno Cardoso Lopes3c8e1be2010-07-24 00:06:39 +0000837 // like "1(%eax,,1)", the assembler doesn't. Use "eiz" or "riz" for this.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000838 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000839 SMLoc L;
840 if (ParseRegister(IndexReg, L, L)) return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000841
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000842 if (getLexer().isNot(AsmToken::RParen)) {
843 // Parse the scale amount:
844 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000845 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000846 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000847 "expected comma in scale expression");
848 return 0;
849 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000850 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000851
852 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000853 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000854
855 int64_t ScaleVal;
856 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000857 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000858
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000859 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000860 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
861 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
862 return 0;
863 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000864 Scale = (unsigned)ScaleVal;
865 }
866 }
867 } else if (getLexer().isNot(AsmToken::RParen)) {
Daniel Dunbaree910252010-08-24 19:13:38 +0000868 // A scale amount without an index is ignored.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000869 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000870 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000871
872 int64_t Value;
873 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000874 return 0;
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000875
Daniel Dunbaree910252010-08-24 19:13:38 +0000876 if (Value != 1)
877 Warning(Loc, "scale factor without index register is ignored");
878 Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000879 }
880 }
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000881
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000882 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000883 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000884 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000885 return 0;
886 }
Sean Callanan18b83232010-01-19 21:44:56 +0000887 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000888 Parser.Lex(); // Eat the ')'.
Bruno Cardoso Lopesf64a7d42010-07-23 22:15:26 +0000889
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000890 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
891 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000892}
893
Devang Pateldd929fc2012-01-12 18:03:40 +0000894bool X86AsmParser::
Benjamin Kramer38e59892010-07-14 22:38:02 +0000895ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000896 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattner693173f2010-10-30 19:23:13 +0000897 StringRef PatchedName = Name;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000898
Chris Lattnerd8f71792010-11-28 20:23:50 +0000899 // FIXME: Hack to recognize setneb as setne.
900 if (PatchedName.startswith("set") && PatchedName.endswith("b") &&
901 PatchedName != "setb" && PatchedName != "setnb")
902 PatchedName = PatchedName.substr(0, Name.size()-1);
903
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000904 // FIXME: Hack to recognize cmp<comparison code>{ss,sd,ps,pd}.
905 const MCExpr *ExtraImmOp = 0;
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000906 if ((PatchedName.startswith("cmp") || PatchedName.startswith("vcmp")) &&
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000907 (PatchedName.endswith("ss") || PatchedName.endswith("sd") ||
908 PatchedName.endswith("ps") || PatchedName.endswith("pd"))) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000909 bool IsVCMP = PatchedName.startswith("vcmp");
910 unsigned SSECCIdx = IsVCMP ? 4 : 3;
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000911 unsigned SSEComparisonCode = StringSwitch<unsigned>(
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000912 PatchedName.slice(SSECCIdx, PatchedName.size() - 2))
Bruno Cardoso Lopescc69e132010-07-07 22:24:03 +0000913 .Case("eq", 0)
914 .Case("lt", 1)
915 .Case("le", 2)
916 .Case("unord", 3)
917 .Case("neq", 4)
918 .Case("nlt", 5)
919 .Case("nle", 6)
920 .Case("ord", 7)
921 .Case("eq_uq", 8)
922 .Case("nge", 9)
923 .Case("ngt", 0x0A)
924 .Case("false", 0x0B)
925 .Case("neq_oq", 0x0C)
926 .Case("ge", 0x0D)
927 .Case("gt", 0x0E)
928 .Case("true", 0x0F)
929 .Case("eq_os", 0x10)
930 .Case("lt_oq", 0x11)
931 .Case("le_oq", 0x12)
932 .Case("unord_s", 0x13)
933 .Case("neq_us", 0x14)
934 .Case("nlt_uq", 0x15)
935 .Case("nle_uq", 0x16)
936 .Case("ord_s", 0x17)
937 .Case("eq_us", 0x18)
938 .Case("nge_uq", 0x19)
939 .Case("ngt_uq", 0x1A)
940 .Case("false_os", 0x1B)
941 .Case("neq_os", 0x1C)
942 .Case("ge_oq", 0x1D)
943 .Case("gt_oq", 0x1E)
944 .Case("true_us", 0x1F)
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000945 .Default(~0U);
946 if (SSEComparisonCode != ~0U) {
947 ExtraImmOp = MCConstantExpr::Create(SSEComparisonCode,
948 getParser().getContext());
949 if (PatchedName.endswith("ss")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000950 PatchedName = IsVCMP ? "vcmpss" : "cmpss";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000951 } else if (PatchedName.endswith("sd")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000952 PatchedName = IsVCMP ? "vcmpsd" : "cmpsd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000953 } else if (PatchedName.endswith("ps")) {
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000954 PatchedName = IsVCMP ? "vcmpps" : "cmpps";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000955 } else {
956 assert(PatchedName.endswith("pd") && "Unexpected mnemonic!");
Bruno Cardoso Lopes428256b2010-06-23 21:10:57 +0000957 PatchedName = IsVCMP ? "vcmppd" : "cmppd";
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000958 }
959 }
960 }
Bruno Cardoso Lopesf528d2b2010-07-23 18:41:12 +0000961
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000962 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000963
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000964 if (ExtraImmOp)
965 Operands.push_back(X86Operand::CreateImm(ExtraImmOp, NameLoc, NameLoc));
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000966
967
Chris Lattner2544f422010-09-08 05:17:37 +0000968 // Determine whether this is an instruction prefix.
969 bool isPrefix =
Chris Lattner693173f2010-10-30 19:23:13 +0000970 Name == "lock" || Name == "rep" ||
971 Name == "repe" || Name == "repz" ||
Rafael Espindolabeb68982010-11-23 11:23:24 +0000972 Name == "repne" || Name == "repnz" ||
Rafael Espindolabfd2d262010-11-27 20:29:45 +0000973 Name == "rex64" || Name == "data16";
Michael J. Spencerc0c8df32010-10-09 11:00:50 +0000974
975
Chris Lattner2544f422010-09-08 05:17:37 +0000976 // This does the actual operand parsing. Don't parse any more if we have a
977 // prefix juxtaposed with an operation like "lock incl 4(%rax)", because we
978 // just want to parse the "lock" as the first instruction and the "incl" as
979 // the next one.
980 if (getLexer().isNot(AsmToken::EndOfStatement) && !isPrefix) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000981
982 // Parse '*' modifier.
983 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000984 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000985 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000986 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000987 }
988
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000989 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000990 if (X86Operand *Op = ParseOperand())
991 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +0000992 else {
993 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000994 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +0000995 }
Daniel Dunbar39e2dd72010-05-25 19:49:32 +0000996
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000997 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000998 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000999
1000 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +00001001 if (X86Operand *Op = ParseOperand())
1002 Operands.push_back(Op);
Chris Lattnercbf8a982010-09-11 16:18:25 +00001003 else {
1004 Parser.EatToEndOfStatement();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001005 return true;
Chris Lattnercbf8a982010-09-11 16:18:25 +00001006 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001007 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001008
Chris Lattnercbf8a982010-09-11 16:18:25 +00001009 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001010 SMLoc Loc = getLexer().getLoc();
Chris Lattnercbf8a982010-09-11 16:18:25 +00001011 Parser.EatToEndOfStatement();
Chris Lattnerc146c4d2010-11-18 02:53:02 +00001012 return Error(Loc, "unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001013 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001014 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001015
Chris Lattner2544f422010-09-08 05:17:37 +00001016 if (getLexer().is(AsmToken::EndOfStatement))
1017 Parser.Lex(); // Consume the EndOfStatement
Kevin Enderby76331752010-12-08 23:57:59 +00001018 else if (isPrefix && getLexer().is(AsmToken::Slash))
1019 Parser.Lex(); // Consume the prefix separator Slash
Daniel Dunbar16cdcb32009-07-28 22:40:46 +00001020
Chris Lattner98c870f2010-11-06 19:25:43 +00001021 // This is a terrible hack to handle "out[bwl]? %al, (%dx)" ->
1022 // "outb %al, %dx". Out doesn't take a memory form, but this is a widely
1023 // documented form in various unofficial manuals, so a lot of code uses it.
1024 if ((Name == "outb" || Name == "outw" || Name == "outl" || Name == "out") &&
1025 Operands.size() == 3) {
1026 X86Operand &Op = *(X86Operand*)Operands.back();
1027 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1028 isa<MCConstantExpr>(Op.Mem.Disp) &&
1029 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1030 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1031 SMLoc Loc = Op.getEndLoc();
1032 Operands.back() = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1033 delete &Op;
1034 }
1035 }
Joerg Sonnenberger00743c22011-02-22 20:40:09 +00001036 // Same hack for "in[bwl]? (%dx), %al" -> "inb %dx, %al".
1037 if ((Name == "inb" || Name == "inw" || Name == "inl" || Name == "in") &&
1038 Operands.size() == 3) {
1039 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1040 if (Op.isMem() && Op.Mem.SegReg == 0 &&
1041 isa<MCConstantExpr>(Op.Mem.Disp) &&
1042 cast<MCConstantExpr>(Op.Mem.Disp)->getValue() == 0 &&
1043 Op.Mem.BaseReg == MatchRegisterName("dx") && Op.Mem.IndexReg == 0) {
1044 SMLoc Loc = Op.getEndLoc();
1045 Operands.begin()[1] = X86Operand::CreateReg(Op.Mem.BaseReg, Loc, Loc);
1046 delete &Op;
1047 }
1048 }
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001049 // Transform "ins[bwl] %dx, %es:(%edi)" into "ins[bwl]"
1050 if (Name.startswith("ins") && Operands.size() == 3 &&
1051 (Name == "insb" || Name == "insw" || Name == "insl")) {
1052 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1053 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1054 if (Op.isReg() && Op.getReg() == X86::DX && isDstOp(Op2)) {
1055 Operands.pop_back();
1056 Operands.pop_back();
1057 delete &Op;
1058 delete &Op2;
1059 }
1060 }
1061
1062 // Transform "outs[bwl] %ds:(%esi), %dx" into "out[bwl]"
1063 if (Name.startswith("outs") && Operands.size() == 3 &&
1064 (Name == "outsb" || Name == "outsw" || Name == "outsl")) {
1065 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1066 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1067 if (isSrcOp(Op) && Op2.isReg() && Op2.getReg() == X86::DX) {
1068 Operands.pop_back();
1069 Operands.pop_back();
1070 delete &Op;
1071 delete &Op2;
1072 }
1073 }
1074
1075 // Transform "movs[bwl] %ds:(%esi), %es:(%edi)" into "movs[bwl]"
1076 if (Name.startswith("movs") && Operands.size() == 3 &&
1077 (Name == "movsb" || Name == "movsw" || Name == "movsl" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001078 (is64BitMode() && Name == "movsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001079 X86Operand &Op = *(X86Operand*)Operands.begin()[1];
1080 X86Operand &Op2 = *(X86Operand*)Operands.begin()[2];
1081 if (isSrcOp(Op) && isDstOp(Op2)) {
1082 Operands.pop_back();
1083 Operands.pop_back();
1084 delete &Op;
1085 delete &Op2;
1086 }
1087 }
1088 // Transform "lods[bwl] %ds:(%esi),{%al,%ax,%eax,%rax}" into "lods[bwl]"
1089 if (Name.startswith("lods") && Operands.size() == 3 &&
1090 (Name == "lods" || Name == "lodsb" || Name == "lodsw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001091 Name == "lodsl" || (is64BitMode() && Name == "lodsq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001092 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1093 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1094 if (isSrcOp(*Op1) && Op2->isReg()) {
1095 const char *ins;
1096 unsigned reg = Op2->getReg();
1097 bool isLods = Name == "lods";
1098 if (reg == X86::AL && (isLods || Name == "lodsb"))
1099 ins = "lodsb";
1100 else if (reg == X86::AX && (isLods || Name == "lodsw"))
1101 ins = "lodsw";
1102 else if (reg == X86::EAX && (isLods || Name == "lodsl"))
1103 ins = "lodsl";
1104 else if (reg == X86::RAX && (isLods || Name == "lodsq"))
1105 ins = "lodsq";
1106 else
1107 ins = NULL;
1108 if (ins != NULL) {
1109 Operands.pop_back();
1110 Operands.pop_back();
1111 delete Op1;
1112 delete Op2;
1113 if (Name != ins)
1114 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1115 }
1116 }
1117 }
1118 // Transform "stos[bwl] {%al,%ax,%eax,%rax},%es:(%edi)" into "stos[bwl]"
1119 if (Name.startswith("stos") && Operands.size() == 3 &&
1120 (Name == "stos" || Name == "stosb" || Name == "stosw" ||
Evan Cheng59ee62d2011-07-11 03:57:24 +00001121 Name == "stosl" || (is64BitMode() && Name == "stosq"))) {
Joerg Sonnenberger96622aa2011-03-18 11:59:40 +00001122 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1123 X86Operand *Op2 = static_cast<X86Operand*>(Operands[2]);
1124 if (isDstOp(*Op2) && Op1->isReg()) {
1125 const char *ins;
1126 unsigned reg = Op1->getReg();
1127 bool isStos = Name == "stos";
1128 if (reg == X86::AL && (isStos || Name == "stosb"))
1129 ins = "stosb";
1130 else if (reg == X86::AX && (isStos || Name == "stosw"))
1131 ins = "stosw";
1132 else if (reg == X86::EAX && (isStos || Name == "stosl"))
1133 ins = "stosl";
1134 else if (reg == X86::RAX && (isStos || Name == "stosq"))
1135 ins = "stosq";
1136 else
1137 ins = NULL;
1138 if (ins != NULL) {
1139 Operands.pop_back();
1140 Operands.pop_back();
1141 delete Op1;
1142 delete Op2;
1143 if (Name != ins)
1144 static_cast<X86Operand*>(Operands[0])->setTokenValue(ins);
1145 }
1146 }
1147 }
1148
Chris Lattnere9e16a32010-09-15 04:33:27 +00001149 // FIXME: Hack to handle recognize s{hr,ar,hl} $1, <op>. Canonicalize to
Chris Lattneree211d02010-09-11 16:32:12 +00001150 // "shift <op>".
Daniel Dunbard5e77052010-03-13 00:47:29 +00001151 if ((Name.startswith("shr") || Name.startswith("sar") ||
Chris Lattner8c24b0c2010-11-06 21:23:40 +00001152 Name.startswith("shl") || Name.startswith("sal") ||
1153 Name.startswith("rcl") || Name.startswith("rcr") ||
1154 Name.startswith("rol") || Name.startswith("ror")) &&
Chris Lattner47ab90b2010-09-06 18:32:06 +00001155 Operands.size() == 3) {
1156 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1157 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1158 cast<MCConstantExpr>(Op1->getImm())->getValue() == 1) {
1159 delete Operands[1];
1160 Operands.erase(Operands.begin() + 1);
1161 }
Daniel Dunbarf2de13f2010-03-20 22:36:38 +00001162 }
Chris Lattner15f89512011-04-09 19:41:05 +00001163
1164 // Transforms "int $3" into "int3" as a size optimization. We can't write an
1165 // instalias with an immediate operand yet.
1166 if (Name == "int" && Operands.size() == 2) {
1167 X86Operand *Op1 = static_cast<X86Operand*>(Operands[1]);
1168 if (Op1->isImm() && isa<MCConstantExpr>(Op1->getImm()) &&
1169 cast<MCConstantExpr>(Op1->getImm())->getValue() == 3) {
1170 delete Operands[1];
1171 Operands.erase(Operands.begin() + 1);
1172 static_cast<X86Operand*>(Operands[0])->setTokenValue("int3");
1173 }
1174 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001175
Chris Lattner98986712010-01-14 22:21:20 +00001176 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +00001177}
1178
Devang Pateldd929fc2012-01-12 18:03:40 +00001179bool X86AsmParser::
Devang Patelb8ba13f2012-01-18 22:42:29 +00001180processInstruction(MCInst &Inst,
1181 const SmallVectorImpl<MCParsedAsmOperand*> &Ops) {
1182 switch (Inst.getOpcode()) {
1183 default: return false;
1184 case X86::AND16i16: {
1185 if (!Inst.getOperand(0).isImm() ||
1186 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1187 return false;
1188
1189 MCInst TmpInst;
1190 TmpInst.setOpcode(X86::AND16ri8);
1191 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1192 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1193 TmpInst.addOperand(Inst.getOperand(0));
1194 Inst = TmpInst;
1195 return true;
1196 }
1197 case X86::AND32i32: {
1198 if (!Inst.getOperand(0).isImm() ||
1199 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1200 return false;
1201
1202 MCInst TmpInst;
1203 TmpInst.setOpcode(X86::AND32ri8);
1204 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1205 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1206 TmpInst.addOperand(Inst.getOperand(0));
1207 Inst = TmpInst;
1208 return true;
1209 }
1210 case X86::AND64i32: {
1211 if (!Inst.getOperand(0).isImm() ||
1212 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1213 return false;
1214
1215 MCInst TmpInst;
1216 TmpInst.setOpcode(X86::AND64ri8);
1217 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1218 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1219 TmpInst.addOperand(Inst.getOperand(0));
1220 Inst = TmpInst;
1221 return true;
1222 }
Devang Patelac0f0482012-01-19 17:53:25 +00001223 case X86::XOR16i16: {
1224 if (!Inst.getOperand(0).isImm() ||
1225 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1226 return false;
1227
1228 MCInst TmpInst;
1229 TmpInst.setOpcode(X86::XOR16ri8);
1230 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1231 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1232 TmpInst.addOperand(Inst.getOperand(0));
1233 Inst = TmpInst;
1234 return true;
1235 }
1236 case X86::XOR32i32: {
1237 if (!Inst.getOperand(0).isImm() ||
1238 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1239 return false;
1240
1241 MCInst TmpInst;
1242 TmpInst.setOpcode(X86::XOR32ri8);
1243 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1244 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1245 TmpInst.addOperand(Inst.getOperand(0));
1246 Inst = TmpInst;
1247 return true;
1248 }
1249 case X86::XOR64i32: {
1250 if (!Inst.getOperand(0).isImm() ||
1251 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1252 return false;
1253
1254 MCInst TmpInst;
1255 TmpInst.setOpcode(X86::XOR64ri8);
1256 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1257 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1258 TmpInst.addOperand(Inst.getOperand(0));
1259 Inst = TmpInst;
1260 return true;
1261 }
1262 case X86::OR16i16: {
1263 if (!Inst.getOperand(0).isImm() ||
1264 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1265 return false;
1266
1267 MCInst TmpInst;
1268 TmpInst.setOpcode(X86::OR16ri8);
1269 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1270 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1271 TmpInst.addOperand(Inst.getOperand(0));
1272 Inst = TmpInst;
1273 return true;
1274 }
1275 case X86::OR32i32: {
1276 if (!Inst.getOperand(0).isImm() ||
1277 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1278 return false;
1279
1280 MCInst TmpInst;
1281 TmpInst.setOpcode(X86::OR32ri8);
1282 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1283 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1284 TmpInst.addOperand(Inst.getOperand(0));
1285 Inst = TmpInst;
1286 return true;
1287 }
1288 case X86::OR64i32: {
1289 if (!Inst.getOperand(0).isImm() ||
1290 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1291 return false;
1292
1293 MCInst TmpInst;
1294 TmpInst.setOpcode(X86::OR64ri8);
1295 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1296 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1297 TmpInst.addOperand(Inst.getOperand(0));
1298 Inst = TmpInst;
1299 return true;
1300 }
1301 case X86::CMP16i16: {
1302 if (!Inst.getOperand(0).isImm() ||
1303 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1304 return false;
1305
1306 MCInst TmpInst;
1307 TmpInst.setOpcode(X86::CMP16ri8);
1308 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1309 TmpInst.addOperand(Inst.getOperand(0));
1310 Inst = TmpInst;
1311 return true;
1312 }
1313 case X86::CMP32i32: {
1314 if (!Inst.getOperand(0).isImm() ||
1315 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1316 return false;
1317
1318 MCInst TmpInst;
1319 TmpInst.setOpcode(X86::CMP32ri8);
1320 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1321 TmpInst.addOperand(Inst.getOperand(0));
1322 Inst = TmpInst;
1323 return true;
1324 }
1325 case X86::CMP64i32: {
1326 if (!Inst.getOperand(0).isImm() ||
1327 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1328 return false;
1329
1330 MCInst TmpInst;
1331 TmpInst.setOpcode(X86::CMP64ri8);
1332 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1333 TmpInst.addOperand(Inst.getOperand(0));
1334 Inst = TmpInst;
1335 return true;
1336 }
Devang Patela951f772012-01-19 18:40:55 +00001337 case X86::ADD16i16: {
1338 if (!Inst.getOperand(0).isImm() ||
1339 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1340 return false;
1341
1342 MCInst TmpInst;
1343 TmpInst.setOpcode(X86::ADD16ri8);
1344 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1345 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1346 TmpInst.addOperand(Inst.getOperand(0));
1347 Inst = TmpInst;
1348 return true;
1349 }
1350 case X86::ADD32i32: {
1351 if (!Inst.getOperand(0).isImm() ||
1352 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1353 return false;
1354
1355 MCInst TmpInst;
1356 TmpInst.setOpcode(X86::ADD32ri8);
1357 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1358 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1359 TmpInst.addOperand(Inst.getOperand(0));
1360 Inst = TmpInst;
1361 return true;
1362 }
1363 case X86::ADD64i32: {
1364 if (!Inst.getOperand(0).isImm() ||
1365 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1366 return false;
1367
1368 MCInst TmpInst;
1369 TmpInst.setOpcode(X86::ADD64ri8);
1370 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1371 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1372 TmpInst.addOperand(Inst.getOperand(0));
1373 Inst = TmpInst;
1374 return true;
1375 }
1376 case X86::SUB16i16: {
1377 if (!Inst.getOperand(0).isImm() ||
1378 !isImmSExti16i8Value(Inst.getOperand(0).getImm()))
1379 return false;
1380
1381 MCInst TmpInst;
1382 TmpInst.setOpcode(X86::SUB16ri8);
1383 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1384 TmpInst.addOperand(MCOperand::CreateReg(X86::AX));
1385 TmpInst.addOperand(Inst.getOperand(0));
1386 Inst = TmpInst;
1387 return true;
1388 }
1389 case X86::SUB32i32: {
1390 if (!Inst.getOperand(0).isImm() ||
1391 !isImmSExti32i8Value(Inst.getOperand(0).getImm()))
1392 return false;
1393
1394 MCInst TmpInst;
1395 TmpInst.setOpcode(X86::SUB32ri8);
1396 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1397 TmpInst.addOperand(MCOperand::CreateReg(X86::EAX));
1398 TmpInst.addOperand(Inst.getOperand(0));
1399 Inst = TmpInst;
1400 return true;
1401 }
1402 case X86::SUB64i32: {
1403 if (!Inst.getOperand(0).isImm() ||
1404 !isImmSExti64i8Value(Inst.getOperand(0).getImm()))
1405 return false;
1406
1407 MCInst TmpInst;
1408 TmpInst.setOpcode(X86::SUB64ri8);
1409 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1410 TmpInst.addOperand(MCOperand::CreateReg(X86::RAX));
1411 TmpInst.addOperand(Inst.getOperand(0));
1412 Inst = TmpInst;
1413 return true;
1414 }
Devang Patelb8ba13f2012-01-18 22:42:29 +00001415 }
1416 return false;
1417}
1418
1419bool X86AsmParser::
Chris Lattner7036f8b2010-09-29 01:42:58 +00001420MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +00001421 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattner7036f8b2010-09-29 01:42:58 +00001422 MCStreamer &Out) {
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001423 assert(!Operands.empty() && "Unexpect empty operand list!");
Chris Lattner7c51a312010-09-29 01:50:45 +00001424 X86Operand *Op = static_cast<X86Operand*>(Operands[0]);
1425 assert(Op->isToken() && "Leading operand should always be a mnemonic!");
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001426
Chris Lattner7c51a312010-09-29 01:50:45 +00001427 // First, handle aliases that expand to multiple instructions.
1428 // FIXME: This should be replaced with a real .td file alias mechanism.
Chris Lattner90fd7972010-11-06 19:57:21 +00001429 // Also, MatchInstructionImpl should do actually *do* the EmitInstruction
1430 // call.
Andrew Trick0966ec02010-10-22 03:58:29 +00001431 if (Op->getToken() == "fstsw" || Op->getToken() == "fstcw" ||
Chris Lattner8b260a72010-10-30 18:07:17 +00001432 Op->getToken() == "fstsww" || Op->getToken() == "fstcww" ||
Chris Lattner905f2e02010-09-30 17:11:29 +00001433 Op->getToken() == "finit" || Op->getToken() == "fsave" ||
Kevin Enderby5a378072010-10-27 02:53:04 +00001434 Op->getToken() == "fstenv" || Op->getToken() == "fclex") {
Chris Lattner7c51a312010-09-29 01:50:45 +00001435 MCInst Inst;
1436 Inst.setOpcode(X86::WAIT);
1437 Out.EmitInstruction(Inst);
1438
Chris Lattner0bb83a82010-09-30 16:39:29 +00001439 const char *Repl =
1440 StringSwitch<const char*>(Op->getToken())
Chris Lattner8b260a72010-10-30 18:07:17 +00001441 .Case("finit", "fninit")
1442 .Case("fsave", "fnsave")
1443 .Case("fstcw", "fnstcw")
1444 .Case("fstcww", "fnstcw")
Chris Lattner905f2e02010-09-30 17:11:29 +00001445 .Case("fstenv", "fnstenv")
Chris Lattner8b260a72010-10-30 18:07:17 +00001446 .Case("fstsw", "fnstsw")
1447 .Case("fstsww", "fnstsw")
1448 .Case("fclex", "fnclex")
Chris Lattner0bb83a82010-09-30 16:39:29 +00001449 .Default(0);
1450 assert(Repl && "Unknown wait-prefixed instruction");
Benjamin Kramerb0f96fa2010-10-01 12:25:27 +00001451 delete Operands[0];
Chris Lattner0bb83a82010-09-30 16:39:29 +00001452 Operands[0] = X86Operand::CreateToken(Repl, IDLoc);
Chris Lattner7c51a312010-09-29 01:50:45 +00001453 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001454
Chris Lattnera008e8a2010-09-06 21:54:15 +00001455 bool WasOriginallyInvalidOperand = false;
Chris Lattnerce4a3352010-09-06 22:11:18 +00001456 unsigned OrigErrorInfo;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001457 MCInst Inst;
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001458
Daniel Dunbarc918d602010-05-04 16:12:42 +00001459 // First, try a direct match.
Devang Patel0a338862012-01-12 01:36:43 +00001460 switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo,
1461 getParser().getAssemblerDialect())) {
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001462 default: break;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001463 case Match_Success:
Devang Patelb8ba13f2012-01-18 22:42:29 +00001464 // Some instructions need post-processing to, for example, tweak which
1465 // encoding is selected. Loop on it while changes happen so the
1466 // individual transformations can chain off each other.
1467 while (processInstruction(Inst, Operands))
1468 ;
1469
Chris Lattner7036f8b2010-09-29 01:42:58 +00001470 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001471 return false;
Chris Lattnerec6789f2010-09-06 20:08:02 +00001472 case Match_MissingFeature:
1473 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1474 return true;
Daniel Dunbarb4129152011-02-04 17:12:23 +00001475 case Match_ConversionFail:
1476 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001477 case Match_InvalidOperand:
1478 WasOriginallyInvalidOperand = true;
1479 break;
1480 case Match_MnemonicFail:
Chris Lattnerec6789f2010-09-06 20:08:02 +00001481 break;
1482 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001483
Daniel Dunbarc918d602010-05-04 16:12:42 +00001484 // FIXME: Ideally, we would only attempt suffix matches for things which are
1485 // valid prefixes, and we could just infer the right unambiguous
1486 // type. However, that requires substantially more matcher support than the
1487 // following hack.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001488
Daniel Dunbarc918d602010-05-04 16:12:42 +00001489 // Change the operand to point to a temporary token.
Daniel Dunbarc918d602010-05-04 16:12:42 +00001490 StringRef Base = Op->getToken();
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001491 SmallString<16> Tmp;
1492 Tmp += Base;
1493 Tmp += ' ';
1494 Op->setTokenValue(Tmp.str());
Daniel Dunbarc918d602010-05-04 16:12:42 +00001495
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001496 // If this instruction starts with an 'f', then it is a floating point stack
1497 // instruction. These come in up to three forms for 32-bit, 64-bit, and
1498 // 80-bit floating point, which use the suffixes s,l,t respectively.
1499 //
1500 // Otherwise, we assume that this may be an integer instruction, which comes
1501 // in 8/16/32/64-bit forms using the b,w,l,q suffixes respectively.
1502 const char *Suffixes = Base[0] != 'f' ? "bwlq" : "slt\0";
1503
Daniel Dunbarc918d602010-05-04 16:12:42 +00001504 // Check for the various suffix matches.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001505 Tmp[Base.size()] = Suffixes[0];
1506 unsigned ErrorInfoIgnore;
Jim Grosbach19cb7f42011-08-15 23:03:29 +00001507 unsigned Match1, Match2, Match3, Match4;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001508
1509 Match1 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1510 Tmp[Base.size()] = Suffixes[1];
1511 Match2 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1512 Tmp[Base.size()] = Suffixes[2];
1513 Match3 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
1514 Tmp[Base.size()] = Suffixes[3];
1515 Match4 = MatchInstructionImpl(Operands, Inst, ErrorInfoIgnore);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001516
1517 // Restore the old token.
1518 Op->setTokenValue(Base);
1519
1520 // If exactly one matched, then we treat that as a successful match (and the
1521 // instruction will already have been filled in correctly, since the failing
1522 // matches won't have modified it).
Chris Lattnerec6789f2010-09-06 20:08:02 +00001523 unsigned NumSuccessfulMatches =
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001524 (Match1 == Match_Success) + (Match2 == Match_Success) +
1525 (Match3 == Match_Success) + (Match4 == Match_Success);
Chris Lattner7036f8b2010-09-29 01:42:58 +00001526 if (NumSuccessfulMatches == 1) {
1527 Out.EmitInstruction(Inst);
Daniel Dunbarc918d602010-05-04 16:12:42 +00001528 return false;
Chris Lattner7036f8b2010-09-29 01:42:58 +00001529 }
Daniel Dunbarc918d602010-05-04 16:12:42 +00001530
Chris Lattnerec6789f2010-09-06 20:08:02 +00001531 // Otherwise, the match failed, try to produce a decent error message.
Daniel Dunbarf1e29d42010-08-12 00:55:38 +00001532
Daniel Dunbar09062b12010-08-12 00:55:42 +00001533 // If we had multiple suffix matches, then identify this as an ambiguous
1534 // match.
Chris Lattnerec6789f2010-09-06 20:08:02 +00001535 if (NumSuccessfulMatches > 1) {
Daniel Dunbar09062b12010-08-12 00:55:42 +00001536 char MatchChars[4];
1537 unsigned NumMatches = 0;
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001538 if (Match1 == Match_Success) MatchChars[NumMatches++] = Suffixes[0];
1539 if (Match2 == Match_Success) MatchChars[NumMatches++] = Suffixes[1];
1540 if (Match3 == Match_Success) MatchChars[NumMatches++] = Suffixes[2];
1541 if (Match4 == Match_Success) MatchChars[NumMatches++] = Suffixes[3];
Daniel Dunbar09062b12010-08-12 00:55:42 +00001542
1543 SmallString<126> Msg;
1544 raw_svector_ostream OS(Msg);
1545 OS << "ambiguous instructions require an explicit suffix (could be ";
1546 for (unsigned i = 0; i != NumMatches; ++i) {
1547 if (i != 0)
1548 OS << ", ";
1549 if (i + 1 == NumMatches)
1550 OS << "or ";
1551 OS << "'" << Base << MatchChars[i] << "'";
1552 }
1553 OS << ")";
1554 Error(IDLoc, OS.str());
Chris Lattnerec6789f2010-09-06 20:08:02 +00001555 return true;
Daniel Dunbar09062b12010-08-12 00:55:42 +00001556 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001557
Chris Lattnera008e8a2010-09-06 21:54:15 +00001558 // Okay, we know that none of the variants matched successfully.
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001559
Chris Lattnera008e8a2010-09-06 21:54:15 +00001560 // If all of the instructions reported an invalid mnemonic, then the original
1561 // mnemonic was invalid.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001562 if ((Match1 == Match_MnemonicFail) && (Match2 == Match_MnemonicFail) &&
1563 (Match3 == Match_MnemonicFail) && (Match4 == Match_MnemonicFail)) {
Chris Lattnerce4a3352010-09-06 22:11:18 +00001564 if (!WasOriginallyInvalidOperand) {
Benjamin Kramerf82edaf2011-10-16 11:28:29 +00001565 return Error(IDLoc, "invalid instruction mnemonic '" + Base + "'",
1566 Op->getLocRange());
Chris Lattnerce4a3352010-09-06 22:11:18 +00001567 }
1568
1569 // Recover location info for the operand if we know which was the problem.
Chris Lattnerce4a3352010-09-06 22:11:18 +00001570 if (OrigErrorInfo != ~0U) {
Chris Lattnerf8840122010-09-15 03:50:11 +00001571 if (OrigErrorInfo >= Operands.size())
1572 return Error(IDLoc, "too few operands for instruction");
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001573
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001574 X86Operand *Operand = (X86Operand*)Operands[OrigErrorInfo];
1575 if (Operand->getStartLoc().isValid()) {
1576 SMRange OperandRange = Operand->getLocRange();
1577 return Error(Operand->getStartLoc(), "invalid operand for instruction",
1578 OperandRange);
1579 }
Chris Lattnerce4a3352010-09-06 22:11:18 +00001580 }
1581
Chris Lattnerd8b7aa22011-10-16 04:47:35 +00001582 return Error(IDLoc, "invalid operand for instruction");
Chris Lattnera008e8a2010-09-06 21:54:15 +00001583 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001584
Chris Lattnerec6789f2010-09-06 20:08:02 +00001585 // If one instruction matched with a missing feature, report this as a
1586 // missing feature.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001587 if ((Match1 == Match_MissingFeature) + (Match2 == Match_MissingFeature) +
1588 (Match3 == Match_MissingFeature) + (Match4 == Match_MissingFeature) == 1){
Chris Lattnerec6789f2010-09-06 20:08:02 +00001589 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1590 return true;
1591 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001592
Chris Lattnera008e8a2010-09-06 21:54:15 +00001593 // If one instruction matched with an invalid operand, report this as an
1594 // operand failure.
Chris Lattnerfb7000f2010-11-06 18:28:02 +00001595 if ((Match1 == Match_InvalidOperand) + (Match2 == Match_InvalidOperand) +
1596 (Match3 == Match_InvalidOperand) + (Match4 == Match_InvalidOperand) == 1){
Chris Lattnera008e8a2010-09-06 21:54:15 +00001597 Error(IDLoc, "invalid operand for instruction");
1598 return true;
1599 }
Michael J. Spencerc0c8df32010-10-09 11:00:50 +00001600
Chris Lattnerec6789f2010-09-06 20:08:02 +00001601 // If all of these were an outright failure, report it in a useless way.
Chris Lattnera008e8a2010-09-06 21:54:15 +00001602 Error(IDLoc, "unknown use of instruction mnemonic without a size suffix");
Daniel Dunbarc918d602010-05-04 16:12:42 +00001603 return true;
1604}
1605
1606
Devang Pateldd929fc2012-01-12 18:03:40 +00001607bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
Chris Lattner537ca842010-10-30 17:38:55 +00001608 StringRef IDVal = DirectiveID.getIdentifier();
1609 if (IDVal == ".word")
1610 return ParseDirectiveWord(2, DirectiveID.getLoc());
Evan Chengbd27f5a2011-07-27 00:38:12 +00001611 else if (IDVal.startswith(".code"))
1612 return ParseDirectiveCode(IDVal, DirectiveID.getLoc());
Chris Lattner537ca842010-10-30 17:38:55 +00001613 return true;
1614}
1615
1616/// ParseDirectiveWord
1617/// ::= .word [ expression (, expression)* ]
Devang Pateldd929fc2012-01-12 18:03:40 +00001618bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
Chris Lattner537ca842010-10-30 17:38:55 +00001619 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1620 for (;;) {
1621 const MCExpr *Value;
1622 if (getParser().ParseExpression(Value))
1623 return true;
1624
1625 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
1626
1627 if (getLexer().is(AsmToken::EndOfStatement))
1628 break;
1629
1630 // FIXME: Improve diagnostic.
1631 if (getLexer().isNot(AsmToken::Comma))
1632 return Error(L, "unexpected token in directive");
1633 Parser.Lex();
1634 }
1635 }
1636
1637 Parser.Lex();
1638 return false;
1639}
1640
Evan Chengbd27f5a2011-07-27 00:38:12 +00001641/// ParseDirectiveCode
1642/// ::= .code32 | .code64
Devang Pateldd929fc2012-01-12 18:03:40 +00001643bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
Evan Chengbd27f5a2011-07-27 00:38:12 +00001644 if (IDVal == ".code32") {
1645 Parser.Lex();
1646 if (is64BitMode()) {
1647 SwitchMode();
1648 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1649 }
1650 } else if (IDVal == ".code64") {
1651 Parser.Lex();
1652 if (!is64BitMode()) {
1653 SwitchMode();
1654 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
1655 }
1656 } else {
1657 return Error(L, "unexpected directive " + IDVal);
1658 }
Chris Lattner537ca842010-10-30 17:38:55 +00001659
Evan Chengbd27f5a2011-07-27 00:38:12 +00001660 return false;
1661}
Chris Lattner537ca842010-10-30 17:38:55 +00001662
1663
Sean Callanane88f5522010-01-23 02:43:15 +00001664extern "C" void LLVMInitializeX86AsmLexer();
1665
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001666// Force static initialization.
1667extern "C" void LLVMInitializeX86AsmParser() {
Devang Pateldd929fc2012-01-12 18:03:40 +00001668 RegisterMCAsmParser<X86AsmParser> X(TheX86_32Target);
1669 RegisterMCAsmParser<X86AsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +00001670 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +00001671}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001672
Chris Lattner0692ee62010-09-06 19:11:01 +00001673#define GET_REGISTER_MATCHER
1674#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar0e2771f2009-07-29 00:02:19 +00001675#include "X86GenAsmMatcher.inc"