blob: da013505dabd7c369a5e35886a5366c58d6c2010 [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
Chris Lattner98986712010-01-14 22:21:20 +000010#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbar4cb1e132009-07-18 23:03:22 +000011#include "X86.h"
Daniel Dunbardbd692a2009-07-20 20:01:54 +000012#include "llvm/ADT/SmallVector.h"
Daniel Dunbar1b6c0602010-02-10 21:19:28 +000013#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000014#include "llvm/ADT/Twine.h"
Kevin Enderby9c656452009-09-10 20:51:44 +000015#include "llvm/MC/MCStreamer.h"
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +000016#include "llvm/MC/MCExpr.h"
Daniel Dunbara027d222009-07-31 02:32:59 +000017#include "llvm/MC/MCInst.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000021#include "llvm/Support/SourceMgr.h"
Daniel Dunbar092a9dd2009-07-17 20:42:00 +000022#include "llvm/Target/TargetRegistry.h"
23#include "llvm/Target/TargetAsmParser.h"
24using namespace llvm;
25
26namespace {
Benjamin Kramerc6b79ac2009-07-31 11:35:26 +000027struct X86Operand;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000028
29class X86ATTAsmParser : public TargetAsmParser {
30 MCAsmParser &Parser;
31
Daniel Dunbarf98bc632010-03-18 20:06:02 +000032protected:
33 unsigned Is64Bit : 1;
34
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000035private:
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000036 MCAsmParser &getParser() const { return Parser; }
37
38 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
39
40 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
41
42 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
43
Chris Lattner29ef9a22010-01-15 18:51:29 +000044 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000045
Chris Lattner309264d2010-01-15 18:44:13 +000046 X86Operand *ParseOperand();
Chris Lattnereef6d782010-04-17 18:56:34 +000047 X86Operand *ParseMemOperand(unsigned SegReg, SMLoc StartLoc);
Kevin Enderby9c656452009-09-10 20:51:44 +000048
49 bool ParseDirectiveWord(unsigned Size, SMLoc L);
50
Daniel Dunbarf98bc632010-03-18 20:06:02 +000051 void InstructionCleanup(MCInst &Inst);
52
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000053 /// @name Auto-generated Match Functions
54 /// {
55
Chris Lattner98986712010-01-14 22:21:20 +000056 bool MatchInstruction(const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Daniel Dunbar20927f22009-08-07 08:26:05 +000057 MCInst &Inst);
58
Daniel Dunbar0e2771f2009-07-29 00:02:19 +000059 /// }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000060
61public:
62 X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
63 : TargetAsmParser(T), Parser(_Parser) {}
64
Chris Lattnerf007e852010-01-14 21:32:45 +000065 virtual bool ParseInstruction(const StringRef &Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000066 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderby9c656452009-09-10 20:51:44 +000067
68 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000069};
Daniel Dunbarf98bc632010-03-18 20:06:02 +000070
71class X86_32ATTAsmParser : public X86ATTAsmParser {
72public:
73 X86_32ATTAsmParser(const Target &T, MCAsmParser &_Parser)
74 : X86ATTAsmParser(T, _Parser) {
75 Is64Bit = false;
76 }
77};
78
79class X86_64ATTAsmParser : public X86ATTAsmParser {
80public:
81 X86_64ATTAsmParser(const Target &T, MCAsmParser &_Parser)
82 : X86ATTAsmParser(T, _Parser) {
83 Is64Bit = true;
84 }
85};
86
Chris Lattner37dfdec2009-07-29 06:33:53 +000087} // end anonymous namespace
88
Sean Callanane9b466d2010-01-23 00:40:33 +000089/// @name Auto-generated Match Functions
90/// {
91
Chris Lattnerb8d6e982010-02-09 00:34:28 +000092static unsigned MatchRegisterName(StringRef Name);
Sean Callanane9b466d2010-01-23 00:40:33 +000093
94/// }
Chris Lattner37dfdec2009-07-29 06:33:53 +000095
96namespace {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +000097
98/// X86Operand - Instances of this class represent a parsed X86 machine
99/// instruction.
Chris Lattner45220a82010-01-14 21:20:55 +0000100struct X86Operand : public MCParsedAsmOperand {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000101 enum KindTy {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000102 Token,
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000103 Register,
104 Immediate,
105 Memory
106 } Kind;
107
Chris Lattner29ef9a22010-01-15 18:51:29 +0000108 SMLoc StartLoc, EndLoc;
109
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000110 union {
111 struct {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000112 const char *Data;
113 unsigned Length;
114 } Tok;
115
116 struct {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000117 unsigned RegNo;
118 } Reg;
119
120 struct {
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000121 const MCExpr *Val;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000122 } Imm;
123
124 struct {
125 unsigned SegReg;
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000126 const MCExpr *Disp;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000127 unsigned BaseReg;
128 unsigned IndexReg;
129 unsigned Scale;
130 } Mem;
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000131 };
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000132
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000133 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000134 : Kind(K), StartLoc(Start), EndLoc(End) {}
135
136 /// getStartLoc - Get the location of the first token of this operand.
137 SMLoc getStartLoc() const { return StartLoc; }
138 /// getEndLoc - Get the location of the last token of this operand.
139 SMLoc getEndLoc() const { return EndLoc; }
140
Daniel Dunbar20927f22009-08-07 08:26:05 +0000141 StringRef getToken() const {
142 assert(Kind == Token && "Invalid access!");
143 return StringRef(Tok.Data, Tok.Length);
144 }
145
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000146 unsigned getReg() const {
147 assert(Kind == Register && "Invalid access!");
148 return Reg.RegNo;
149 }
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000150
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000151 const MCExpr *getImm() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000152 assert(Kind == Immediate && "Invalid access!");
153 return Imm.Val;
154 }
155
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000156 const MCExpr *getMemDisp() const {
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000157 assert(Kind == Memory && "Invalid access!");
158 return Mem.Disp;
159 }
160 unsigned getMemSegReg() const {
161 assert(Kind == Memory && "Invalid access!");
162 return Mem.SegReg;
163 }
164 unsigned getMemBaseReg() const {
165 assert(Kind == Memory && "Invalid access!");
166 return Mem.BaseReg;
167 }
168 unsigned getMemIndexReg() const {
169 assert(Kind == Memory && "Invalid access!");
170 return Mem.IndexReg;
171 }
172 unsigned getMemScale() const {
173 assert(Kind == Memory && "Invalid access!");
174 return Mem.Scale;
175 }
176
Daniel Dunbara3741fa2009-08-08 07:50:56 +0000177 bool isToken() const {return Kind == Token; }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000178
179 bool isImm() const { return Kind == Immediate; }
180
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000181 bool isImmSExt8() const {
182 // Accept immediates which fit in 8 bits when sign extended, and
183 // non-absolute immediates.
184 if (!isImm())
185 return false;
186
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000187 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
188 int64_t Value = CE->getValue();
189 return Value == (int64_t) (int8_t) Value;
190 }
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000191
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000192 return true;
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000193 }
194
Daniel Dunbar20927f22009-08-07 08:26:05 +0000195 bool isMem() const { return Kind == Memory; }
196
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000197 bool isAbsMem() const {
198 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000199 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000200 }
201
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000202 bool isNoSegMem() const {
203 return Kind == Memory && !getMemSegReg();
204 }
205
Daniel Dunbar20927f22009-08-07 08:26:05 +0000206 bool isReg() const { return Kind == Register; }
207
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000208 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
209 // Add as immediates when possible.
210 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
211 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
212 else
213 Inst.addOperand(MCOperand::CreateExpr(Expr));
214 }
215
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000216 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000217 assert(N == 1 && "Invalid number of operands!");
218 Inst.addOperand(MCOperand::CreateReg(getReg()));
219 }
220
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000221 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar20927f22009-08-07 08:26:05 +0000222 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000223 addExpr(Inst, getImm());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000224 }
225
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000226 void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000227 // FIXME: Support user customization of the render method.
228 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000229 addExpr(Inst, getImm());
Daniel Dunbar5fe63382009-08-09 07:20:21 +0000230 }
231
Daniel Dunbar5c468e32009-08-10 21:00:45 +0000232 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000233 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbar20927f22009-08-07 08:26:05 +0000234 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
235 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
236 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000237 addExpr(Inst, getMemDisp());
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000238 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
239 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000240
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000241 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
242 assert((N == 1) && "Invalid number of operands!");
243 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
244 }
245
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000246 void addNoSegMemOperands(MCInst &Inst, unsigned N) const {
247 assert((N == 4) && "Invalid number of operands!");
Daniel Dunbarec2b1f12010-01-30 00:24:00 +0000248 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
249 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
250 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar9c60f532010-02-13 00:17:21 +0000251 addExpr(Inst, getMemDisp());
Daniel Dunbar20927f22009-08-07 08:26:05 +0000252 }
253
Chris Lattnerb4307b32010-01-15 19:28:38 +0000254 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
255 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000256 Res->Tok.Data = Str.data();
257 Res->Tok.Length = Str.size();
Daniel Dunbar20927f22009-08-07 08:26:05 +0000258 return Res;
259 }
260
Chris Lattner29ef9a22010-01-15 18:51:29 +0000261 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner1f19f0f2010-01-15 19:06:59 +0000262 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000263 Res->Reg.RegNo = RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000264 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000265 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000266
Chris Lattnerb4307b32010-01-15 19:28:38 +0000267 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
268 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000269 Res->Imm.Val = Val;
270 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000271 }
Daniel Dunbar20927f22009-08-07 08:26:05 +0000272
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000273 /// Create an absolute memory operand.
274 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
275 SMLoc EndLoc) {
276 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
277 Res->Mem.SegReg = 0;
278 Res->Mem.Disp = Disp;
279 Res->Mem.BaseReg = 0;
280 Res->Mem.IndexReg = 0;
Daniel Dunbar7b9147a2010-02-02 21:44:16 +0000281 Res->Mem.Scale = 1;
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000282 return Res;
283 }
284
285 /// Create a generalized memory operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000286 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
287 unsigned BaseReg, unsigned IndexReg,
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000288 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000289 // We should never just have a displacement, that should be parsed as an
290 // absolute memory operand.
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000291 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
292
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000293 // The scale should always be one of {1,2,4,8}.
294 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000295 "Invalid scale!");
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000296 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattner29ef9a22010-01-15 18:51:29 +0000297 Res->Mem.SegReg = SegReg;
298 Res->Mem.Disp = Disp;
299 Res->Mem.BaseReg = BaseReg;
300 Res->Mem.IndexReg = IndexReg;
301 Res->Mem.Scale = Scale;
302 return Res;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000303 }
304};
Daniel Dunbara3af3702009-07-20 18:55:04 +0000305
Chris Lattner37dfdec2009-07-29 06:33:53 +0000306} // end anonymous namespace.
Daniel Dunbara2edbab2009-07-28 20:47:52 +0000307
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000308
Chris Lattner29ef9a22010-01-15 18:51:29 +0000309bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
310 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner23075742010-01-15 18:27:19 +0000311 RegNo = 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000312 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000313 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattner29ef9a22010-01-15 18:51:29 +0000314 StartLoc = TokPercent.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000315 Parser.Lex(); // Eat percent token.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000316
Sean Callanan18b83232010-01-19 21:44:56 +0000317 const AsmToken &Tok = Parser.getTok();
Kevin Enderby0d6cd002009-09-16 17:18:29 +0000318 if (Tok.isNot(AsmToken::Identifier))
319 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000320
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000321 // FIXME: Validate register for the current architecture; we have to do
322 // validation later, so maybe there is no need for this here.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000323 RegNo = MatchRegisterName(Tok.getString());
Chris Lattnerb8d6e982010-02-09 00:34:28 +0000324
Chris Lattnere16b0fc2010-02-09 00:49:22 +0000325 // Parse %st(1) and "%st" as "%st(0)"
326 if (RegNo == 0 && Tok.getString() == "st") {
327 RegNo = X86::ST0;
328 EndLoc = Tok.getLoc();
329 Parser.Lex(); // Eat 'st'
330
331 // Check to see if we have '(4)' after %st.
332 if (getLexer().isNot(AsmToken::LParen))
333 return false;
334 // Lex the paren.
335 getParser().Lex();
336
337 const AsmToken &IntTok = Parser.getTok();
338 if (IntTok.isNot(AsmToken::Integer))
339 return Error(IntTok.getLoc(), "expected stack index");
340 switch (IntTok.getIntVal()) {
341 case 0: RegNo = X86::ST0; break;
342 case 1: RegNo = X86::ST1; break;
343 case 2: RegNo = X86::ST2; break;
344 case 3: RegNo = X86::ST3; break;
345 case 4: RegNo = X86::ST4; break;
346 case 5: RegNo = X86::ST5; break;
347 case 6: RegNo = X86::ST6; break;
348 case 7: RegNo = X86::ST7; break;
349 default: return Error(IntTok.getLoc(), "invalid stack index");
350 }
351
352 if (getParser().Lex().isNot(AsmToken::RParen))
353 return Error(Parser.getTok().getLoc(), "expected ')'");
354
355 EndLoc = Tok.getLoc();
356 Parser.Lex(); // Eat ')'
357 return false;
358 }
359
Daniel Dunbar245f0582009-08-08 21:22:41 +0000360 if (RegNo == 0)
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000361 return Error(Tok.getLoc(), "invalid register name");
362
Chris Lattner29ef9a22010-01-15 18:51:29 +0000363 EndLoc = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000364 Parser.Lex(); // Eat identifier token.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000365 return false;
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000366}
367
Chris Lattner309264d2010-01-15 18:44:13 +0000368X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000369 switch (getLexer().getKind()) {
370 default:
Chris Lattnereef6d782010-04-17 18:56:34 +0000371 // Parse a memory operand with no segment register.
372 return ParseMemOperand(0, Parser.getTok().getLoc());
Chris Lattner23075742010-01-15 18:27:19 +0000373 case AsmToken::Percent: {
Chris Lattnereef6d782010-04-17 18:56:34 +0000374 // Read the register.
Chris Lattner23075742010-01-15 18:27:19 +0000375 unsigned RegNo;
Chris Lattner29ef9a22010-01-15 18:51:29 +0000376 SMLoc Start, End;
377 if (ParseRegister(RegNo, Start, End)) return 0;
Chris Lattnereef6d782010-04-17 18:56:34 +0000378
379 // If this is a segment register followed by a ':', then this is the start
380 // of a memory reference, otherwise this is a normal register reference.
381 if (getLexer().isNot(AsmToken::Colon))
382 return X86Operand::CreateReg(RegNo, Start, End);
383
384
385 getParser().Lex(); // Eat the colon.
386 return ParseMemOperand(RegNo, Start);
Chris Lattner23075742010-01-15 18:27:19 +0000387 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000388 case AsmToken::Dollar: {
389 // $42 -> immediate.
Sean Callanan18b83232010-01-19 21:44:56 +0000390 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callananb9a25b72010-01-19 20:27:46 +0000391 Parser.Lex();
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000392 const MCExpr *Val;
Chris Lattner54482b42010-01-15 19:39:23 +0000393 if (getParser().ParseExpression(Val, End))
Chris Lattner309264d2010-01-15 18:44:13 +0000394 return 0;
Chris Lattnerb4307b32010-01-15 19:28:38 +0000395 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000396 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000397 }
Daniel Dunbardbd692a2009-07-20 20:01:54 +0000398}
399
Chris Lattnereef6d782010-04-17 18:56:34 +0000400/// ParseMemOperand: segment: disp(basereg, indexreg, scale). The '%ds:' prefix
401/// has already been parsed if present.
402X86Operand *X86ATTAsmParser::ParseMemOperand(unsigned SegReg, SMLoc MemStart) {
403
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000404 // We have to disambiguate a parenthesized expression "(4+5)" from the start
405 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattner75f265f2010-01-24 01:07:33 +0000406 // only way to do this without lookahead is to eat the '(' and see what is
407 // after it.
Daniel Dunbar8c2eebe2009-08-31 08:08:38 +0000408 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000409 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner54482b42010-01-15 19:39:23 +0000410 SMLoc ExprEnd;
411 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000412
413 // After parsing the base expression we could either have a parenthesized
414 // memory address or not. If not, return now. If so, eat the (.
415 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000416 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000417 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000418 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000419 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000420 }
421
422 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000423 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000424 } else {
425 // Okay, we have a '('. We don't know if this is an expression or not, but
426 // so we have to eat the ( to see beyond it.
Sean Callanan18b83232010-01-19 21:44:56 +0000427 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000428 Parser.Lex(); // Eat the '('.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000429
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000430 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000431 // Nothing to do here, fall into the code below with the '(' part of the
432 // memory operand consumed.
433 } else {
Chris Lattnerb4307b32010-01-15 19:28:38 +0000434 SMLoc ExprEnd;
435
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000436 // It must be an parenthesized expression, parse it now.
Chris Lattnerb4307b32010-01-15 19:28:38 +0000437 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattner309264d2010-01-15 18:44:13 +0000438 return 0;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000439
440 // After parsing the base expression we could either have a parenthesized
441 // memory address or not. If not, return now. If so, eat the (.
442 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarc09e4112009-07-31 22:22:54 +0000443 // Unless we have a segment register, treat this as an immediate.
Chris Lattner309264d2010-01-15 18:44:13 +0000444 if (SegReg == 0)
Daniel Dunbarb834f5d2010-01-30 01:02:48 +0000445 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000446 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000447 }
448
449 // Eat the '('.
Sean Callananb9a25b72010-01-19 20:27:46 +0000450 Parser.Lex();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000451 }
452 }
453
454 // If we reached here, then we just ate the ( of the memory operand. Process
455 // the rest of the memory operand.
Daniel Dunbar022e2a82009-07-31 20:53:16 +0000456 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000457
Chris Lattner29ef9a22010-01-15 18:51:29 +0000458 if (getLexer().is(AsmToken::Percent)) {
459 SMLoc L;
460 if (ParseRegister(BaseReg, L, L)) return 0;
461 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000462
463 if (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000464 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000465
466 // Following the comma we should have either an index register, or a scale
467 // value. We don't support the later form, but we want to parse it
468 // correctly.
469 //
470 // Not that even though it would be completely consistent to support syntax
471 // like "1(%eax,,1)", the assembler doesn't.
Kevin Enderby7b4608d2009-09-03 17:15:07 +0000472 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner29ef9a22010-01-15 18:51:29 +0000473 SMLoc L;
474 if (ParseRegister(IndexReg, L, L)) return 0;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000475
476 if (getLexer().isNot(AsmToken::RParen)) {
477 // Parse the scale amount:
478 // ::= ',' [scale-expression]
Chris Lattner309264d2010-01-15 18:44:13 +0000479 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000480 Error(Parser.getTok().getLoc(),
Chris Lattner309264d2010-01-15 18:44:13 +0000481 "expected comma in scale expression");
482 return 0;
483 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000484 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000485
486 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000487 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000488
489 int64_t ScaleVal;
490 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattner309264d2010-01-15 18:44:13 +0000491 return 0;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000492
493 // Validate the scale amount.
Chris Lattner309264d2010-01-15 18:44:13 +0000494 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
495 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
496 return 0;
497 }
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000498 Scale = (unsigned)ScaleVal;
499 }
500 }
501 } else if (getLexer().isNot(AsmToken::RParen)) {
502 // Otherwise we have the unsupported form of a scale amount without an
503 // index.
Sean Callanan18b83232010-01-19 21:44:56 +0000504 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000505
506 int64_t Value;
507 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattner309264d2010-01-15 18:44:13 +0000508 return 0;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000509
Chris Lattner309264d2010-01-15 18:44:13 +0000510 Error(Loc, "cannot have scale factor without index register");
511 return 0;
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000512 }
513 }
514
515 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattner309264d2010-01-15 18:44:13 +0000516 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000517 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattner309264d2010-01-15 18:44:13 +0000518 return 0;
519 }
Sean Callanan18b83232010-01-19 21:44:56 +0000520 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000521 Parser.Lex(); // Eat the ')'.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000522
Chris Lattner0a3c5a52010-01-15 19:33:43 +0000523 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
524 MemStart, MemEnd);
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000525}
526
Chris Lattner98986712010-01-14 22:21:20 +0000527bool X86ATTAsmParser::
528ParseInstruction(const StringRef &Name, SMLoc NameLoc,
529 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar1b6c0602010-02-10 21:19:28 +0000530 // FIXME: Hack to recognize "sal..." and "rep..." for now. We need a way to
531 // represent alternative syntaxes in the .td file, without requiring
532 // instruction duplication.
533 StringRef PatchedName = StringSwitch<StringRef>(Name)
534 .Case("sal", "shl")
535 .Case("salb", "shlb")
536 .Case("sall", "shll")
537 .Case("salq", "shlq")
538 .Case("salw", "shlw")
539 .Case("repe", "rep")
540 .Case("repz", "rep")
541 .Case("repnz", "repne")
542 .Default(Name);
543 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000544
545 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000546
547 // Parse '*' modifier.
548 if (getLexer().is(AsmToken::Star)) {
Sean Callanan18b83232010-01-19 21:44:56 +0000549 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattnerb4307b32010-01-15 19:28:38 +0000550 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callananb9a25b72010-01-19 20:27:46 +0000551 Parser.Lex(); // Eat the star.
Daniel Dunbar0db68f42009-08-11 05:00:25 +0000552 }
553
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000554 // Read the first operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000555 if (X86Operand *Op = ParseOperand())
556 Operands.push_back(Op);
557 else
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000558 return true;
Chris Lattner309264d2010-01-15 18:44:13 +0000559
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000560 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000561 Parser.Lex(); // Eat the comma.
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000562
563 // Parse and remember the operand.
Chris Lattner309264d2010-01-15 18:44:13 +0000564 if (X86Operand *Op = ParseOperand())
565 Operands.push_back(Op);
566 else
Daniel Dunbar16cdcb32009-07-28 22:40:46 +0000567 return true;
568 }
569 }
570
Daniel Dunbard5e77052010-03-13 00:47:29 +0000571 // FIXME: Hack to handle recognizing s{hr,ar,hl}? $1.
572 if ((Name.startswith("shr") || Name.startswith("sar") ||
573 Name.startswith("shl")) &&
574 Operands.size() == 3 &&
575 static_cast<X86Operand*>(Operands[1])->isImm() &&
576 isa<MCConstantExpr>(static_cast<X86Operand*>(Operands[1])->getImm()) &&
Daniel Dunbarf2de13f2010-03-20 22:36:38 +0000577 cast<MCConstantExpr>(static_cast<X86Operand*>(Operands[1])->getImm())->getValue() == 1) {
578 delete Operands[1];
Daniel Dunbard5e77052010-03-13 00:47:29 +0000579 Operands.erase(Operands.begin() + 1);
Daniel Dunbarf2de13f2010-03-20 22:36:38 +0000580 }
Daniel Dunbard5e77052010-03-13 00:47:29 +0000581
Chris Lattner98986712010-01-14 22:21:20 +0000582 return false;
Daniel Dunbara3af3702009-07-20 18:55:04 +0000583}
584
Kevin Enderby9c656452009-09-10 20:51:44 +0000585bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
586 StringRef IDVal = DirectiveID.getIdentifier();
587 if (IDVal == ".word")
588 return ParseDirectiveWord(2, DirectiveID.getLoc());
589 return true;
590}
591
592/// ParseDirectiveWord
593/// ::= .word [ expression (, expression)* ]
594bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
595 if (getLexer().isNot(AsmToken::EndOfStatement)) {
596 for (;;) {
597 const MCExpr *Value;
598 if (getParser().ParseExpression(Value))
599 return true;
600
Chris Lattneraaec2052010-01-19 19:46:13 +0000601 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Kevin Enderby9c656452009-09-10 20:51:44 +0000602
603 if (getLexer().is(AsmToken::EndOfStatement))
604 break;
605
606 // FIXME: Improve diagnostic.
607 if (getLexer().isNot(AsmToken::Comma))
608 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000609 Parser.Lex();
Kevin Enderby9c656452009-09-10 20:51:44 +0000610 }
611 }
612
Sean Callananb9a25b72010-01-19 20:27:46 +0000613 Parser.Lex();
Kevin Enderby9c656452009-09-10 20:51:44 +0000614 return false;
615}
616
Daniel Dunbarf98bc632010-03-18 20:06:02 +0000617// FIXME: Custom X86 cleanup function to implement a temporary hack to handle
618// matching INCL/DECL correctly for x86_64. This needs to be replaced by a
619// proper mechanism for supporting (ambiguous) feature dependent instructions.
620void X86ATTAsmParser::InstructionCleanup(MCInst &Inst) {
621 if (!Is64Bit) return;
622
623 switch (Inst.getOpcode()) {
624 case X86::DEC16r: Inst.setOpcode(X86::DEC64_16r); break;
625 case X86::DEC16m: Inst.setOpcode(X86::DEC64_16m); break;
626 case X86::DEC32r: Inst.setOpcode(X86::DEC64_32r); break;
627 case X86::DEC32m: Inst.setOpcode(X86::DEC64_32m); break;
628 case X86::INC16r: Inst.setOpcode(X86::INC64_16r); break;
629 case X86::INC16m: Inst.setOpcode(X86::INC64_16m); break;
630 case X86::INC32r: Inst.setOpcode(X86::INC64_32r); break;
631 case X86::INC32m: Inst.setOpcode(X86::INC64_32m); break;
632 }
633}
634
Sean Callanane88f5522010-01-23 02:43:15 +0000635extern "C" void LLVMInitializeX86AsmLexer();
636
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000637// Force static initialization.
638extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarf98bc632010-03-18 20:06:02 +0000639 RegisterAsmParser<X86_32ATTAsmParser> X(TheX86_32Target);
640 RegisterAsmParser<X86_64ATTAsmParser> Y(TheX86_64Target);
Sean Callanane88f5522010-01-23 02:43:15 +0000641 LLVMInitializeX86AsmLexer();
Daniel Dunbar092a9dd2009-07-17 20:42:00 +0000642}
Daniel Dunbar0e2771f2009-07-29 00:02:19 +0000643
644#include "X86GenAsmMatcher.inc"