blob: 65e498117f66c0726c3b204c8f16b13b1d97d668 [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
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000175 bool isAbsMem() const {
176 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
177 !getMemIndexReg() && !getMemScale();
178 }
179
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000180 bool isNoSegMem() const {
181 return Kind == Memory && !getMemSegReg();
182 }
183
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000184 bool isReg() const { return Kind == Register; }
185
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000186 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000187 assert(N == 1 && "Invalid number of operands!");
188 Inst.addOperand(MCOperand::CreateReg(getReg()));
189 }
190
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000191 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000192 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000193 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000194 }
195
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000196 void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000197 // FIXME: Support user customization of the render method.
198 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000199 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000200 }
201
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000202 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000203 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000204 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
205 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
206 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar6e966212009-08-31 08:08:38 +0000207 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000208 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
209 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000210
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000211 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
212 assert((N == 1) && "Invalid number of operands!");
213 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
214 }
215
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000216 void addNoSegMemOperands(MCInst &Inst, unsigned N) const {
217 assert((N == 4) && "Invalid number of operands!");
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000218 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
219 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
220 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
221 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000222 }
223
Chris Lattner284abb62010-01-15 19:28:38 +0000224 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
225 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000226 Res->Tok.Data = Str.data();
227 Res->Tok.Length = Str.size();
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000228 return Res;
229 }
230
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000231 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner61cd2c32010-01-15 19:06:59 +0000232 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000233 Res->Reg.RegNo = RegNo;
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000234 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000235 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000236
Chris Lattner284abb62010-01-15 19:28:38 +0000237 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
238 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000239 Res->Imm.Val = Val;
240 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000241 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000242
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000243 /// Create an absolute memory operand.
244 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
245 SMLoc EndLoc) {
246 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
247 Res->Mem.SegReg = 0;
248 Res->Mem.Disp = Disp;
249 Res->Mem.BaseReg = 0;
250 Res->Mem.IndexReg = 0;
251 Res->Mem.Scale = 0;
252 return Res;
253 }
254
255 /// Create a generalized memory operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000256 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
257 unsigned BaseReg, unsigned IndexReg,
Chris Lattner80cc03a2010-01-15 19:33:43 +0000258 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000259 // We should never just have a displacement, that should be parsed as an
260 // absolute memory operand.
Daniel Dunbar24091712009-07-31 22:22:54 +0000261 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
262
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000263 // The scale should always be one of {1,2,4,8}.
264 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000265 "Invalid scale!");
Chris Lattner80cc03a2010-01-15 19:33:43 +0000266 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000267 Res->Mem.SegReg = SegReg;
268 Res->Mem.Disp = Disp;
269 Res->Mem.BaseReg = BaseReg;
270 Res->Mem.IndexReg = IndexReg;
271 Res->Mem.Scale = Scale;
272 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000273 }
274};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000275
Chris Lattnere54532b2009-07-29 06:33:53 +0000276} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000277
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000278
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000279bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
280 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner977d91a2010-01-15 18:27:19 +0000281 RegNo = 0;
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000282 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderbye71842b2009-09-03 17:15:07 +0000283 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000284 StartLoc = TokPercent.getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000285 Parser.Lex(); // Eat percent token.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000286
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000287 const AsmToken &Tok = Parser.getTok();
Kevin Enderby01b83cf2009-09-16 17:18:29 +0000288 if (Tok.isNot(AsmToken::Identifier))
289 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000290
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000291 // FIXME: Validate register for the current architecture; we have to do
292 // validation later, so maybe there is no need for this here.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000293 RegNo = MatchRegisterName(Tok.getString());
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +0000294 if (RegNo == 0)
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000295 return Error(Tok.getLoc(), "invalid register name");
296
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000297 EndLoc = Tok.getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000298 Parser.Lex(); // Eat identifier token.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000299 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000300}
301
Chris Lattnere4d457c2010-01-15 18:44:13 +0000302X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000303 switch (getLexer().getKind()) {
304 default:
Chris Lattnere4d457c2010-01-15 18:44:13 +0000305 return ParseMemOperand();
Chris Lattner977d91a2010-01-15 18:27:19 +0000306 case AsmToken::Percent: {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000307 // FIXME: if a segment register, this could either be just the seg reg, or
308 // the start of a memory operand.
Chris Lattner977d91a2010-01-15 18:27:19 +0000309 unsigned RegNo;
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000310 SMLoc Start, End;
311 if (ParseRegister(RegNo, Start, End)) return 0;
312 return X86Operand::CreateReg(RegNo, Start, End);
Chris Lattner977d91a2010-01-15 18:27:19 +0000313 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000314 case AsmToken::Dollar: {
315 // $42 -> immediate.
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000316 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanan34b4a462010-01-19 20:27:46 +0000317 Parser.Lex();
Daniel Dunbar6e966212009-08-31 08:08:38 +0000318 const MCExpr *Val;
Chris Lattner2ca686d2010-01-15 19:39:23 +0000319 if (getParser().ParseExpression(Val, End))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000320 return 0;
Chris Lattner284abb62010-01-15 19:28:38 +0000321 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000322 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000323 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000324}
325
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000326/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
Chris Lattnere4d457c2010-01-15 18:44:13 +0000327X86Operand *X86ATTAsmParser::ParseMemOperand() {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000328 SMLoc MemStart = Parser.getTok().getLoc();
Chris Lattner80cc03a2010-01-15 19:33:43 +0000329
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000330 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
331 unsigned SegReg = 0;
332
333 // We have to disambiguate a parenthesized expression "(4+5)" from the start
334 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattnerbded9a32010-01-24 01:07:33 +0000335 // only way to do this without lookahead is to eat the '(' and see what is
336 // after it.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000337 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000338 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner2ca686d2010-01-15 19:39:23 +0000339 SMLoc ExprEnd;
340 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000341
342 // After parsing the base expression we could either have a parenthesized
343 // memory address or not. If not, return now. If so, eat the (.
344 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000345 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000346 if (SegReg == 0)
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000347 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner80cc03a2010-01-15 19:33:43 +0000348 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000349 }
350
351 // Eat the '('.
Sean Callanan34b4a462010-01-19 20:27:46 +0000352 Parser.Lex();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000353 } else {
354 // Okay, we have a '('. We don't know if this is an expression or not, but
355 // so we have to eat the ( to see beyond it.
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000356 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000357 Parser.Lex(); // Eat the '('.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000358
Kevin Enderbye71842b2009-09-03 17:15:07 +0000359 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000360 // Nothing to do here, fall into the code below with the '(' part of the
361 // memory operand consumed.
362 } else {
Chris Lattner284abb62010-01-15 19:28:38 +0000363 SMLoc ExprEnd;
364
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000365 // It must be an parenthesized expression, parse it now.
Chris Lattner284abb62010-01-15 19:28:38 +0000366 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000367 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000368
369 // After parsing the base expression we could either have a parenthesized
370 // memory address or not. If not, return now. If so, eat the (.
371 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000372 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000373 if (SegReg == 0)
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000374 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner80cc03a2010-01-15 19:33:43 +0000375 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000376 }
377
378 // Eat the '('.
Sean Callanan34b4a462010-01-19 20:27:46 +0000379 Parser.Lex();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000380 }
381 }
382
383 // If we reached here, then we just ate the ( of the memory operand. Process
384 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000385 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000386
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000387 if (getLexer().is(AsmToken::Percent)) {
388 SMLoc L;
389 if (ParseRegister(BaseReg, L, L)) return 0;
390 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000391
392 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan34b4a462010-01-19 20:27:46 +0000393 Parser.Lex(); // Eat the comma.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000394
395 // Following the comma we should have either an index register, or a scale
396 // value. We don't support the later form, but we want to parse it
397 // correctly.
398 //
399 // Not that even though it would be completely consistent to support syntax
400 // like "1(%eax,,1)", the assembler doesn't.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000401 if (getLexer().is(AsmToken::Percent)) {
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000402 SMLoc L;
403 if (ParseRegister(IndexReg, L, L)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000404
405 if (getLexer().isNot(AsmToken::RParen)) {
406 // Parse the scale amount:
407 // ::= ',' [scale-expression]
Chris Lattnere4d457c2010-01-15 18:44:13 +0000408 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000409 Error(Parser.getTok().getLoc(),
Chris Lattnere4d457c2010-01-15 18:44:13 +0000410 "expected comma in scale expression");
411 return 0;
412 }
Sean Callanan34b4a462010-01-19 20:27:46 +0000413 Parser.Lex(); // Eat the comma.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000414
415 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000416 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000417
418 int64_t ScaleVal;
419 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000420 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000421
422 // Validate the scale amount.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000423 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
424 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
425 return 0;
426 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000427 Scale = (unsigned)ScaleVal;
428 }
429 }
430 } else if (getLexer().isNot(AsmToken::RParen)) {
431 // Otherwise we have the unsupported form of a scale amount without an
432 // index.
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000433 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000434
435 int64_t Value;
436 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000437 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000438
Chris Lattnere4d457c2010-01-15 18:44:13 +0000439 Error(Loc, "cannot have scale factor without index register");
440 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000441 }
442 }
443
444 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000445 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000446 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnere4d457c2010-01-15 18:44:13 +0000447 return 0;
448 }
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000449 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000450 Parser.Lex(); // Eat the ')'.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000451
Chris Lattner80cc03a2010-01-15 19:33:43 +0000452 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
453 MemStart, MemEnd);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000454}
455
Chris Lattner22f480d2010-01-14 22:21:20 +0000456bool X86ATTAsmParser::
457ParseInstruction(const StringRef &Name, SMLoc NameLoc,
458 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000459
Chris Lattner284abb62010-01-15 19:28:38 +0000460 Operands.push_back(X86Operand::CreateToken(Name, NameLoc));
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000461
462 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar76953672009-08-11 05:00:25 +0000463
464 // Parse '*' modifier.
465 if (getLexer().is(AsmToken::Star)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000466 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner284abb62010-01-15 19:28:38 +0000467 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanan34b4a462010-01-19 20:27:46 +0000468 Parser.Lex(); // Eat the star.
Daniel Dunbar76953672009-08-11 05:00:25 +0000469 }
470
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000471 // Read the first operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000472 if (X86Operand *Op = ParseOperand())
473 Operands.push_back(Op);
474 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000475 return true;
Chris Lattnere4d457c2010-01-15 18:44:13 +0000476
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000477 while (getLexer().is(AsmToken::Comma)) {
Sean Callanan34b4a462010-01-19 20:27:46 +0000478 Parser.Lex(); // Eat the comma.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000479
480 // Parse and remember the operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000481 if (X86Operand *Op = ParseOperand())
482 Operands.push_back(Op);
483 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000484 return true;
485 }
486 }
487
Chris Lattner22f480d2010-01-14 22:21:20 +0000488 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000489}
490
Kevin Enderbyae90d092009-09-10 20:51:44 +0000491bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
492 StringRef IDVal = DirectiveID.getIdentifier();
493 if (IDVal == ".word")
494 return ParseDirectiveWord(2, DirectiveID.getLoc());
495 return true;
496}
497
498/// ParseDirectiveWord
499/// ::= .word [ expression (, expression)* ]
500bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
501 if (getLexer().isNot(AsmToken::EndOfStatement)) {
502 for (;;) {
503 const MCExpr *Value;
504 if (getParser().ParseExpression(Value))
505 return true;
506
Chris Lattnera71dc602010-01-19 19:46:13 +0000507 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Kevin Enderbyae90d092009-09-10 20:51:44 +0000508
509 if (getLexer().is(AsmToken::EndOfStatement))
510 break;
511
512 // FIXME: Improve diagnostic.
513 if (getLexer().isNot(AsmToken::Comma))
514 return Error(L, "unexpected token in directive");
Sean Callanan34b4a462010-01-19 20:27:46 +0000515 Parser.Lex();
Kevin Enderbyae90d092009-09-10 20:51:44 +0000516 }
517 }
518
Sean Callanan34b4a462010-01-19 20:27:46 +0000519 Parser.Lex();
Kevin Enderbyae90d092009-09-10 20:51:44 +0000520 return false;
521}
522
Sean Callanan5bbbc372010-01-23 02:43:15 +0000523extern "C" void LLVMInitializeX86AsmLexer();
524
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000525// Force static initialization.
526extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000527 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
528 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanan5bbbc372010-01-23 02:43:15 +0000529 LLVMInitializeX86AsmLexer();
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000530}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000531
532#include "X86GenAsmMatcher.inc"