blob: 19fbf85e4affb9b1af0ddd54d7c38d4d8272d5be [file] [log] [blame]
Daniel Dunbarc7df3cb2009-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 Lattner22f480d2010-01-14 22:21:20 +000010#include "llvm/Target/TargetAsmParser.h"
Daniel Dunbar0b0441e2009-07-18 23:03:22 +000011#include "X86.h"
Daniel Dunbar78929e52009-07-20 20:01:54 +000012#include "llvm/ADT/SmallVector.h"
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000013#include "llvm/ADT/Twine.h"
Kevin Enderbyae90d092009-09-10 20:51:44 +000014#include "llvm/MC/MCStreamer.h"
Daniel Dunbar6e966212009-08-31 08:08:38 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbara54716c2009-07-31 02:32:59 +000016#include "llvm/MC/MCInst.h"
Chris Lattner291d6692010-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"
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000020#include "llvm/Support/SourceMgr.h"
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +000021#include "llvm/Target/TargetRegistry.h"
22#include "llvm/Target/TargetAsmParser.h"
23using namespace llvm;
24
25namespace {
Benjamin Kramer264834b2009-07-31 11:35:26 +000026struct X86Operand;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000027
28class X86ATTAsmParser : public TargetAsmParser {
29 MCAsmParser &Parser;
30
31private:
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000032 MCAsmParser &getParser() const { return Parser; }
33
34 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
35
36 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
37
38 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
39
Chris Lattnerc2fc91a2010-01-15 18:51:29 +000040 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000041
Chris Lattnere4d457c2010-01-15 18:44:13 +000042 X86Operand *ParseOperand();
43 X86Operand *ParseMemOperand();
Kevin Enderbyae90d092009-09-10 20:51:44 +000044
45 bool ParseDirectiveWord(unsigned Size, SMLoc L);
46
Daniel Dunbar85f1b392009-07-29 00:02:19 +000047 /// @name Auto-generated Match Functions
48 /// {
49
Chris Lattner22f480d2010-01-14 22:21:20 +000050 bool MatchInstruction(const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000051 MCInst &Inst);
52
Daniel Dunbar85f1b392009-07-29 00:02:19 +000053 /// }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000054
55public:
56 X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
57 : TargetAsmParser(T), Parser(_Parser) {}
58
Chris Lattnerf66e4eb2010-01-14 21:32:45 +000059 virtual bool ParseInstruction(const StringRef &Name, SMLoc NameLoc,
Chris Lattner22f480d2010-01-14 22:21:20 +000060 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyae90d092009-09-10 20:51:44 +000061
62 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000063};
Chris Lattnere54532b2009-07-29 06:33:53 +000064
65} // end anonymous namespace
66
Sean Callananef372de2010-01-23 00:40:33 +000067/// @name Auto-generated Match Functions
68/// {
69
70static unsigned MatchRegisterName(const StringRef &Name);
71
72/// }
Chris Lattnere54532b2009-07-29 06:33:53 +000073
74namespace {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000075
76/// X86Operand - Instances of this class represent a parsed X86 machine
77/// instruction.
Chris Lattner0c119a72010-01-14 21:20:55 +000078struct X86Operand : public MCParsedAsmOperand {
Chris Lattner61cd2c32010-01-15 19:06:59 +000079 enum KindTy {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000080 Token,
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000081 Register,
82 Immediate,
83 Memory
84 } Kind;
85
Chris Lattnerc2fc91a2010-01-15 18:51:29 +000086 SMLoc StartLoc, EndLoc;
87
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000088 union {
89 struct {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000090 const char *Data;
91 unsigned Length;
92 } Tok;
93
94 struct {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000095 unsigned RegNo;
96 } Reg;
97
98 struct {
Daniel Dunbar6e966212009-08-31 08:08:38 +000099 const MCExpr *Val;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000100 } Imm;
101
102 struct {
103 unsigned SegReg;
Daniel Dunbar6e966212009-08-31 08:08:38 +0000104 const MCExpr *Disp;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000105 unsigned BaseReg;
106 unsigned IndexReg;
107 unsigned Scale;
108 } Mem;
Daniel Dunbar78929e52009-07-20 20:01:54 +0000109 };
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000110
Chris Lattner80cc03a2010-01-15 19:33:43 +0000111 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner61cd2c32010-01-15 19:06:59 +0000112 : Kind(K), StartLoc(Start), EndLoc(End) {}
113
114 /// getStartLoc - Get the location of the first token of this operand.
115 SMLoc getStartLoc() const { return StartLoc; }
116 /// getEndLoc - Get the location of the last token of this operand.
117 SMLoc getEndLoc() const { return EndLoc; }
118
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000119 StringRef getToken() const {
120 assert(Kind == Token && "Invalid access!");
121 return StringRef(Tok.Data, Tok.Length);
122 }
123
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000124 unsigned getReg() const {
125 assert(Kind == Register && "Invalid access!");
126 return Reg.RegNo;
127 }
Daniel Dunbard80432a2009-07-28 20:47:52 +0000128
Daniel Dunbar6e966212009-08-31 08:08:38 +0000129 const MCExpr *getImm() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000130 assert(Kind == Immediate && "Invalid access!");
131 return Imm.Val;
132 }
133
Daniel Dunbar6e966212009-08-31 08:08:38 +0000134 const MCExpr *getMemDisp() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000135 assert(Kind == Memory && "Invalid access!");
136 return Mem.Disp;
137 }
138 unsigned getMemSegReg() const {
139 assert(Kind == Memory && "Invalid access!");
140 return Mem.SegReg;
141 }
142 unsigned getMemBaseReg() const {
143 assert(Kind == Memory && "Invalid access!");
144 return Mem.BaseReg;
145 }
146 unsigned getMemIndexReg() const {
147 assert(Kind == Memory && "Invalid access!");
148 return Mem.IndexReg;
149 }
150 unsigned getMemScale() const {
151 assert(Kind == Memory && "Invalid access!");
152 return Mem.Scale;
153 }
154
Daniel Dunbar378bee92009-08-08 07:50:56 +0000155 bool isToken() const {return Kind == Token; }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000156
157 bool isImm() const { return Kind == Immediate; }
158
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000159 bool isImmSExt8() const {
160 // Accept immediates which fit in 8 bits when sign extended, and
161 // non-absolute immediates.
162 if (!isImm())
163 return false;
164
Daniel Dunbar6e966212009-08-31 08:08:38 +0000165 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
166 int64_t Value = CE->getValue();
167 return Value == (int64_t) (int8_t) Value;
168 }
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000169
Daniel Dunbar6e966212009-08-31 08:08:38 +0000170 return true;
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000171 }
172
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000173 bool isMem() const { return Kind == Memory; }
174
175 bool isReg() const { return Kind == Register; }
176
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000177 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000178 assert(N == 1 && "Invalid number of operands!");
179 Inst.addOperand(MCOperand::CreateReg(getReg()));
180 }
181
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000182 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000183 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000184 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000185 }
186
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000187 void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000188 // FIXME: Support user customization of the render method.
189 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000190 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000191 }
192
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000193 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000194 assert((N == 4 || N == 5) && "Invalid number of operands!");
195
196 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
197 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
198 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar6e966212009-08-31 08:08:38 +0000199 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000200
201 // FIXME: What a hack.
202 if (N == 5)
203 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
204 }
205
Chris Lattner284abb62010-01-15 19:28:38 +0000206 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
207 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000208 Res->Tok.Data = Str.data();
209 Res->Tok.Length = Str.size();
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000210 return Res;
211 }
212
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000213 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner61cd2c32010-01-15 19:06:59 +0000214 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000215 Res->Reg.RegNo = RegNo;
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000216 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000217 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000218
Chris Lattner284abb62010-01-15 19:28:38 +0000219 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
220 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000221 Res->Imm.Val = Val;
222 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000223 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000224
Chris Lattnere4d457c2010-01-15 18:44:13 +0000225 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
226 unsigned BaseReg, unsigned IndexReg,
Chris Lattner80cc03a2010-01-15 19:33:43 +0000227 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000228 // We should never just have a displacement, that would be an immediate.
229 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
230
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000231 // The scale should always be one of {1,2,4,8}.
232 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000233 "Invalid scale!");
Chris Lattner80cc03a2010-01-15 19:33:43 +0000234 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000235 Res->Mem.SegReg = SegReg;
236 Res->Mem.Disp = Disp;
237 Res->Mem.BaseReg = BaseReg;
238 Res->Mem.IndexReg = IndexReg;
239 Res->Mem.Scale = Scale;
240 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000241 }
242};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000243
Chris Lattnere54532b2009-07-29 06:33:53 +0000244} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000245
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000246
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000247bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
248 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner977d91a2010-01-15 18:27:19 +0000249 RegNo = 0;
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000250 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderbye71842b2009-09-03 17:15:07 +0000251 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000252 StartLoc = TokPercent.getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000253 Parser.Lex(); // Eat percent token.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000254
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000255 const AsmToken &Tok = Parser.getTok();
Kevin Enderby01b83cf2009-09-16 17:18:29 +0000256 if (Tok.isNot(AsmToken::Identifier))
257 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000258
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000259 // FIXME: Validate register for the current architecture; we have to do
260 // validation later, so maybe there is no need for this here.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000261 RegNo = MatchRegisterName(Tok.getString());
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +0000262 if (RegNo == 0)
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000263 return Error(Tok.getLoc(), "invalid register name");
264
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000265 EndLoc = Tok.getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000266 Parser.Lex(); // Eat identifier token.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000267 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000268}
269
Chris Lattnere4d457c2010-01-15 18:44:13 +0000270X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000271 switch (getLexer().getKind()) {
272 default:
Chris Lattnere4d457c2010-01-15 18:44:13 +0000273 return ParseMemOperand();
Chris Lattner977d91a2010-01-15 18:27:19 +0000274 case AsmToken::Percent: {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000275 // FIXME: if a segment register, this could either be just the seg reg, or
276 // the start of a memory operand.
Chris Lattner977d91a2010-01-15 18:27:19 +0000277 unsigned RegNo;
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000278 SMLoc Start, End;
279 if (ParseRegister(RegNo, Start, End)) return 0;
280 return X86Operand::CreateReg(RegNo, Start, End);
Chris Lattner977d91a2010-01-15 18:27:19 +0000281 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000282 case AsmToken::Dollar: {
283 // $42 -> immediate.
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000284 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanan34b4a462010-01-19 20:27:46 +0000285 Parser.Lex();
Daniel Dunbar6e966212009-08-31 08:08:38 +0000286 const MCExpr *Val;
Chris Lattner2ca686d2010-01-15 19:39:23 +0000287 if (getParser().ParseExpression(Val, End))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000288 return 0;
Chris Lattner284abb62010-01-15 19:28:38 +0000289 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000290 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000291 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000292}
293
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000294/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
Chris Lattnere4d457c2010-01-15 18:44:13 +0000295X86Operand *X86ATTAsmParser::ParseMemOperand() {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000296 SMLoc MemStart = Parser.getTok().getLoc();
Chris Lattner80cc03a2010-01-15 19:33:43 +0000297
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000298 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
299 unsigned SegReg = 0;
300
301 // We have to disambiguate a parenthesized expression "(4+5)" from the start
302 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattnerbded9a32010-01-24 01:07:33 +0000303 // only way to do this without lookahead is to eat the '(' and see what is
304 // after it.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000305 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000306 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner2ca686d2010-01-15 19:39:23 +0000307 SMLoc ExprEnd;
308 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000309
310 // After parsing the base expression we could either have a parenthesized
311 // memory address or not. If not, return now. If so, eat the (.
312 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000313 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000314 if (SegReg == 0)
Chris Lattner2ca686d2010-01-15 19:39:23 +0000315 return X86Operand::CreateImm(Disp, MemStart, ExprEnd);
Chris Lattner80cc03a2010-01-15 19:33:43 +0000316 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000317 }
318
319 // Eat the '('.
Sean Callanan34b4a462010-01-19 20:27:46 +0000320 Parser.Lex();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000321 } else {
322 // Okay, we have a '('. We don't know if this is an expression or not, but
323 // so we have to eat the ( to see beyond it.
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000324 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000325 Parser.Lex(); // Eat the '('.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000326
Kevin Enderbye71842b2009-09-03 17:15:07 +0000327 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000328 // Nothing to do here, fall into the code below with the '(' part of the
329 // memory operand consumed.
330 } else {
Chris Lattner284abb62010-01-15 19:28:38 +0000331 SMLoc ExprEnd;
332
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000333 // It must be an parenthesized expression, parse it now.
Chris Lattner284abb62010-01-15 19:28:38 +0000334 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000335 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000336
337 // After parsing the base expression we could either have a parenthesized
338 // memory address or not. If not, return now. If so, eat the (.
339 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000340 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000341 if (SegReg == 0)
Chris Lattner284abb62010-01-15 19:28:38 +0000342 return X86Operand::CreateImm(Disp, LParenLoc, ExprEnd);
Chris Lattner80cc03a2010-01-15 19:33:43 +0000343 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000344 }
345
346 // Eat the '('.
Sean Callanan34b4a462010-01-19 20:27:46 +0000347 Parser.Lex();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000348 }
349 }
350
351 // If we reached here, then we just ate the ( of the memory operand. Process
352 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000353 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000354
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000355 if (getLexer().is(AsmToken::Percent)) {
356 SMLoc L;
357 if (ParseRegister(BaseReg, L, L)) return 0;
358 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000359
360 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan34b4a462010-01-19 20:27:46 +0000361 Parser.Lex(); // Eat the comma.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000362
363 // Following the comma we should have either an index register, or a scale
364 // value. We don't support the later form, but we want to parse it
365 // correctly.
366 //
367 // Not that even though it would be completely consistent to support syntax
368 // like "1(%eax,,1)", the assembler doesn't.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000369 if (getLexer().is(AsmToken::Percent)) {
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000370 SMLoc L;
371 if (ParseRegister(IndexReg, L, L)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000372
373 if (getLexer().isNot(AsmToken::RParen)) {
374 // Parse the scale amount:
375 // ::= ',' [scale-expression]
Chris Lattnere4d457c2010-01-15 18:44:13 +0000376 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000377 Error(Parser.getTok().getLoc(),
Chris Lattnere4d457c2010-01-15 18:44:13 +0000378 "expected comma in scale expression");
379 return 0;
380 }
Sean Callanan34b4a462010-01-19 20:27:46 +0000381 Parser.Lex(); // Eat the comma.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000382
383 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000384 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000385
386 int64_t ScaleVal;
387 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000388 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000389
390 // Validate the scale amount.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000391 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
392 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
393 return 0;
394 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000395 Scale = (unsigned)ScaleVal;
396 }
397 }
398 } else if (getLexer().isNot(AsmToken::RParen)) {
399 // Otherwise we have the unsupported form of a scale amount without an
400 // index.
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000401 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000402
403 int64_t Value;
404 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000405 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000406
Chris Lattnere4d457c2010-01-15 18:44:13 +0000407 Error(Loc, "cannot have scale factor without index register");
408 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000409 }
410 }
411
412 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000413 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000414 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnere4d457c2010-01-15 18:44:13 +0000415 return 0;
416 }
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000417 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000418 Parser.Lex(); // Eat the ')'.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000419
Chris Lattner80cc03a2010-01-15 19:33:43 +0000420 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
421 MemStart, MemEnd);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000422}
423
Chris Lattner22f480d2010-01-14 22:21:20 +0000424bool X86ATTAsmParser::
425ParseInstruction(const StringRef &Name, SMLoc NameLoc,
426 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000427
Chris Lattner284abb62010-01-15 19:28:38 +0000428 Operands.push_back(X86Operand::CreateToken(Name, NameLoc));
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000429
430 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar76953672009-08-11 05:00:25 +0000431
432 // Parse '*' modifier.
433 if (getLexer().is(AsmToken::Star)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000434 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner284abb62010-01-15 19:28:38 +0000435 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanan34b4a462010-01-19 20:27:46 +0000436 Parser.Lex(); // Eat the star.
Daniel Dunbar76953672009-08-11 05:00:25 +0000437 }
438
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000439 // Read the first operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000440 if (X86Operand *Op = ParseOperand())
441 Operands.push_back(Op);
442 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000443 return true;
Chris Lattnere4d457c2010-01-15 18:44:13 +0000444
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000445 while (getLexer().is(AsmToken::Comma)) {
Sean Callanan34b4a462010-01-19 20:27:46 +0000446 Parser.Lex(); // Eat the comma.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000447
448 // Parse and remember the operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000449 if (X86Operand *Op = ParseOperand())
450 Operands.push_back(Op);
451 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000452 return true;
453 }
454 }
455
Chris Lattner22f480d2010-01-14 22:21:20 +0000456 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000457}
458
Kevin Enderbyae90d092009-09-10 20:51:44 +0000459bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
460 StringRef IDVal = DirectiveID.getIdentifier();
461 if (IDVal == ".word")
462 return ParseDirectiveWord(2, DirectiveID.getLoc());
463 return true;
464}
465
466/// ParseDirectiveWord
467/// ::= .word [ expression (, expression)* ]
468bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
469 if (getLexer().isNot(AsmToken::EndOfStatement)) {
470 for (;;) {
471 const MCExpr *Value;
472 if (getParser().ParseExpression(Value))
473 return true;
474
Chris Lattnera71dc602010-01-19 19:46:13 +0000475 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Kevin Enderbyae90d092009-09-10 20:51:44 +0000476
477 if (getLexer().is(AsmToken::EndOfStatement))
478 break;
479
480 // FIXME: Improve diagnostic.
481 if (getLexer().isNot(AsmToken::Comma))
482 return Error(L, "unexpected token in directive");
Sean Callanan34b4a462010-01-19 20:27:46 +0000483 Parser.Lex();
Kevin Enderbyae90d092009-09-10 20:51:44 +0000484 }
485 }
486
Sean Callanan34b4a462010-01-19 20:27:46 +0000487 Parser.Lex();
Kevin Enderbyae90d092009-09-10 20:51:44 +0000488 return false;
489}
490
Sean Callanan5bbbc372010-01-23 02:43:15 +0000491extern "C" void LLVMInitializeX86AsmLexer();
492
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000493// Force static initialization.
494extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000495 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
496 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanan5bbbc372010-01-23 02:43:15 +0000497 LLVMInitializeX86AsmLexer();
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000498}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000499
500#include "X86GenAsmMatcher.inc"