blob: e319f92f05bc448022bbb7c3f72d2ada13bad243 [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"
Daniel Dunbard80432a2009-07-28 20:47:52 +000014#include "llvm/MC/MCAsmLexer.h"
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +000015#include "llvm/MC/MCAsmParser.h"
Kevin Enderbyae90d092009-09-10 20:51:44 +000016#include "llvm/MC/MCStreamer.h"
Daniel Dunbar6e966212009-08-31 08:08:38 +000017#include "llvm/MC/MCExpr.h"
Daniel Dunbara54716c2009-07-31 02:32:59 +000018#include "llvm/MC/MCInst.h"
Chris Lattner0c119a72010-01-14 21:20:55 +000019#include "llvm/MC/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 Lattner977d91a2010-01-15 18:27:19 +000040 bool ParseRegister(unsigned &RegNo);
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 Dunbarb0e6abe2009-08-08 21:22:41 +000053 /// MatchRegisterName - Match the given string to a register name, or 0 if
54 /// there is no match.
55 unsigned MatchRegisterName(const StringRef &Name);
Daniel Dunbar85f1b392009-07-29 00:02:19 +000056
57 /// }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000058
59public:
60 X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
61 : TargetAsmParser(T), Parser(_Parser) {}
62
Chris Lattnerf66e4eb2010-01-14 21:32:45 +000063 virtual bool ParseInstruction(const StringRef &Name, SMLoc NameLoc,
Chris Lattner22f480d2010-01-14 22:21:20 +000064 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyae90d092009-09-10 20:51:44 +000065
66 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000067};
Chris Lattnere54532b2009-07-29 06:33:53 +000068
69} // end anonymous namespace
70
71
72namespace {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000073
74/// X86Operand - Instances of this class represent a parsed X86 machine
75/// instruction.
Chris Lattner0c119a72010-01-14 21:20:55 +000076struct X86Operand : public MCParsedAsmOperand {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000077 enum {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000078 Token,
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000079 Register,
80 Immediate,
81 Memory
82 } Kind;
83
84 union {
85 struct {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000086 const char *Data;
87 unsigned Length;
88 } Tok;
89
90 struct {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000091 unsigned RegNo;
92 } Reg;
93
94 struct {
Daniel Dunbar6e966212009-08-31 08:08:38 +000095 const MCExpr *Val;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000096 } Imm;
97
98 struct {
99 unsigned SegReg;
Daniel Dunbar6e966212009-08-31 08:08:38 +0000100 const MCExpr *Disp;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000101 unsigned BaseReg;
102 unsigned IndexReg;
103 unsigned Scale;
104 } Mem;
Daniel Dunbar78929e52009-07-20 20:01:54 +0000105 };
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000106
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000107 StringRef getToken() const {
108 assert(Kind == Token && "Invalid access!");
109 return StringRef(Tok.Data, Tok.Length);
110 }
111
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000112 unsigned getReg() const {
113 assert(Kind == Register && "Invalid access!");
114 return Reg.RegNo;
115 }
Daniel Dunbard80432a2009-07-28 20:47:52 +0000116
Daniel Dunbar6e966212009-08-31 08:08:38 +0000117 const MCExpr *getImm() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000118 assert(Kind == Immediate && "Invalid access!");
119 return Imm.Val;
120 }
121
Daniel Dunbar6e966212009-08-31 08:08:38 +0000122 const MCExpr *getMemDisp() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000123 assert(Kind == Memory && "Invalid access!");
124 return Mem.Disp;
125 }
126 unsigned getMemSegReg() const {
127 assert(Kind == Memory && "Invalid access!");
128 return Mem.SegReg;
129 }
130 unsigned getMemBaseReg() const {
131 assert(Kind == Memory && "Invalid access!");
132 return Mem.BaseReg;
133 }
134 unsigned getMemIndexReg() const {
135 assert(Kind == Memory && "Invalid access!");
136 return Mem.IndexReg;
137 }
138 unsigned getMemScale() const {
139 assert(Kind == Memory && "Invalid access!");
140 return Mem.Scale;
141 }
142
Daniel Dunbar378bee92009-08-08 07:50:56 +0000143 bool isToken() const {return Kind == Token; }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000144
145 bool isImm() const { return Kind == Immediate; }
146
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000147 bool isImmSExt8() const {
148 // Accept immediates which fit in 8 bits when sign extended, and
149 // non-absolute immediates.
150 if (!isImm())
151 return false;
152
Daniel Dunbar6e966212009-08-31 08:08:38 +0000153 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
154 int64_t Value = CE->getValue();
155 return Value == (int64_t) (int8_t) Value;
156 }
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000157
Daniel Dunbar6e966212009-08-31 08:08:38 +0000158 return true;
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000159 }
160
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000161 bool isMem() const { return Kind == Memory; }
162
163 bool isReg() const { return Kind == Register; }
164
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000165 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000166 assert(N == 1 && "Invalid number of operands!");
167 Inst.addOperand(MCOperand::CreateReg(getReg()));
168 }
169
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000170 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000171 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000172 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000173 }
174
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000175 void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000176 // FIXME: Support user customization of the render method.
177 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000178 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000179 }
180
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000181 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000182 assert((N == 4 || N == 5) && "Invalid number of operands!");
183
184 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
185 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
186 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar6e966212009-08-31 08:08:38 +0000187 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000188
189 // FIXME: What a hack.
190 if (N == 5)
191 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
192 }
193
194 static X86Operand CreateToken(StringRef Str) {
195 X86Operand Res;
196 Res.Kind = Token;
197 Res.Tok.Data = Str.data();
198 Res.Tok.Length = Str.size();
199 return Res;
200 }
201
Chris Lattnere4d457c2010-01-15 18:44:13 +0000202 static X86Operand *CreateReg(unsigned RegNo) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000203 X86Operand Res;
204 Res.Kind = Register;
205 Res.Reg.RegNo = RegNo;
Chris Lattnere4d457c2010-01-15 18:44:13 +0000206 return new X86Operand(Res);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000207 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000208
Chris Lattnere4d457c2010-01-15 18:44:13 +0000209 static X86Operand *CreateImm(const MCExpr *Val) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000210 X86Operand Res;
211 Res.Kind = Immediate;
212 Res.Imm.Val = Val;
Chris Lattnere4d457c2010-01-15 18:44:13 +0000213 return new X86Operand(Res);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000214 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000215
Chris Lattnere4d457c2010-01-15 18:44:13 +0000216 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
217 unsigned BaseReg, unsigned IndexReg,
218 unsigned Scale) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000219 // We should never just have a displacement, that would be an immediate.
220 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
221
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000222 // The scale should always be one of {1,2,4,8}.
223 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000224 "Invalid scale!");
225 X86Operand Res;
226 Res.Kind = Memory;
227 Res.Mem.SegReg = SegReg;
228 Res.Mem.Disp = Disp;
229 Res.Mem.BaseReg = BaseReg;
230 Res.Mem.IndexReg = IndexReg;
231 Res.Mem.Scale = Scale;
Chris Lattnere4d457c2010-01-15 18:44:13 +0000232 return new X86Operand(Res);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000233 }
234};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000235
Chris Lattnere54532b2009-07-29 06:33:53 +0000236} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000237
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000238
Chris Lattner977d91a2010-01-15 18:27:19 +0000239bool X86ATTAsmParser::ParseRegister(unsigned &RegNo) {
240 RegNo = 0;
Kevin Enderbye71842b2009-09-03 17:15:07 +0000241 const AsmToken &TokPercent = getLexer().getTok();
Duncan Sandse0a6add2009-09-06 16:27:34 +0000242 (void)TokPercent; // Avoid warning when assertions are disabled.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000243 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
244 getLexer().Lex(); // Eat percent token.
245
Chris Lattnere54532b2009-07-29 06:33:53 +0000246 const AsmToken &Tok = getLexer().getTok();
Kevin Enderby01b83cf2009-09-16 17:18:29 +0000247 if (Tok.isNot(AsmToken::Identifier))
248 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000249
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000250 // FIXME: Validate register for the current architecture; we have to do
251 // validation later, so maybe there is no need for this here.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000252 RegNo = MatchRegisterName(Tok.getString());
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +0000253 if (RegNo == 0)
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000254 return Error(Tok.getLoc(), "invalid register name");
255
Kevin Enderbye71842b2009-09-03 17:15:07 +0000256 getLexer().Lex(); // Eat identifier token.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000257
258 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000259}
260
Chris Lattnere4d457c2010-01-15 18:44:13 +0000261X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000262 switch (getLexer().getKind()) {
263 default:
Chris Lattnere4d457c2010-01-15 18:44:13 +0000264 return ParseMemOperand();
Chris Lattner977d91a2010-01-15 18:27:19 +0000265 case AsmToken::Percent: {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000266 // FIXME: if a segment register, this could either be just the seg reg, or
267 // the start of a memory operand.
Chris Lattner977d91a2010-01-15 18:27:19 +0000268 unsigned RegNo;
Chris Lattnere4d457c2010-01-15 18:44:13 +0000269 if (ParseRegister(RegNo)) return 0;
270 return X86Operand::CreateReg(RegNo);
Chris Lattner977d91a2010-01-15 18:27:19 +0000271 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000272 case AsmToken::Dollar: {
273 // $42 -> immediate.
274 getLexer().Lex();
Daniel Dunbar6e966212009-08-31 08:08:38 +0000275 const MCExpr *Val;
276 if (getParser().ParseExpression(Val))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000277 return 0;
278 return X86Operand::CreateImm(Val);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000279 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000280 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000281}
282
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000283/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
Chris Lattnere4d457c2010-01-15 18:44:13 +0000284X86Operand *X86ATTAsmParser::ParseMemOperand() {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000285 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
286 unsigned SegReg = 0;
287
288 // We have to disambiguate a parenthesized expression "(4+5)" from the start
289 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
290 // only way to do this without lookahead is to eat the ( and see what is after
291 // it.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000292 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000293 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere4d457c2010-01-15 18:44:13 +0000294 if (getParser().ParseExpression(Disp)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000295
296 // After parsing the base expression we could either have a parenthesized
297 // memory address or not. If not, return now. If so, eat the (.
298 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000299 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000300 if (SegReg == 0)
301 return X86Operand::CreateImm(Disp);
302 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000303 }
304
305 // Eat the '('.
306 getLexer().Lex();
307 } else {
308 // Okay, we have a '('. We don't know if this is an expression or not, but
309 // so we have to eat the ( to see beyond it.
310 getLexer().Lex(); // Eat the '('.
311
Kevin Enderbye71842b2009-09-03 17:15:07 +0000312 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000313 // Nothing to do here, fall into the code below with the '(' part of the
314 // memory operand consumed.
315 } else {
316 // It must be an parenthesized expression, parse it now.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000317 if (getParser().ParseParenExpression(Disp))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000318 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000319
320 // After parsing the base expression we could either have a parenthesized
321 // memory address or not. If not, return now. If so, eat the (.
322 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000323 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000324 if (SegReg == 0)
325 return X86Operand::CreateImm(Disp);
326 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000327 }
328
329 // Eat the '('.
330 getLexer().Lex();
331 }
332 }
333
334 // If we reached here, then we just ate the ( of the memory operand. Process
335 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000336 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000337
Chris Lattner977d91a2010-01-15 18:27:19 +0000338 if (getLexer().is(AsmToken::Percent))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000339 if (ParseRegister(BaseReg)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000340
341 if (getLexer().is(AsmToken::Comma)) {
342 getLexer().Lex(); // Eat the comma.
343
344 // Following the comma we should have either an index register, or a scale
345 // value. We don't support the later form, but we want to parse it
346 // correctly.
347 //
348 // Not that even though it would be completely consistent to support syntax
349 // like "1(%eax,,1)", the assembler doesn't.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000350 if (getLexer().is(AsmToken::Percent)) {
Chris Lattnere4d457c2010-01-15 18:44:13 +0000351 if (ParseRegister(IndexReg)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000352
353 if (getLexer().isNot(AsmToken::RParen)) {
354 // Parse the scale amount:
355 // ::= ',' [scale-expression]
Chris Lattnere4d457c2010-01-15 18:44:13 +0000356 if (getLexer().isNot(AsmToken::Comma)) {
357 Error(getLexer().getTok().getLoc(),
358 "expected comma in scale expression");
359 return 0;
360 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000361 getLexer().Lex(); // Eat the comma.
362
363 if (getLexer().isNot(AsmToken::RParen)) {
364 SMLoc Loc = getLexer().getTok().getLoc();
365
366 int64_t ScaleVal;
367 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000368 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000369
370 // Validate the scale amount.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000371 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
372 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
373 return 0;
374 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000375 Scale = (unsigned)ScaleVal;
376 }
377 }
378 } else if (getLexer().isNot(AsmToken::RParen)) {
379 // Otherwise we have the unsupported form of a scale amount without an
380 // index.
381 SMLoc Loc = getLexer().getTok().getLoc();
382
383 int64_t Value;
384 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000385 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000386
Chris Lattnere4d457c2010-01-15 18:44:13 +0000387 Error(Loc, "cannot have scale factor without index register");
388 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000389 }
390 }
391
392 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000393 if (getLexer().isNot(AsmToken::RParen)) {
394 Error(getLexer().getTok().getLoc(), "unexpected token in memory operand");
395 return 0;
396 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000397 getLexer().Lex(); // Eat the ')'.
398
Chris Lattnere4d457c2010-01-15 18:44:13 +0000399 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000400}
401
Chris Lattner22f480d2010-01-14 22:21:20 +0000402bool X86ATTAsmParser::
403ParseInstruction(const StringRef &Name, SMLoc NameLoc,
404 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000405
Chris Lattner22f480d2010-01-14 22:21:20 +0000406 Operands.push_back(new X86Operand(X86Operand::CreateToken(Name)));
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000407
Daniel Dunbara54716c2009-07-31 02:32:59 +0000408 SMLoc Loc = getLexer().getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000409 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar76953672009-08-11 05:00:25 +0000410
411 // Parse '*' modifier.
412 if (getLexer().is(AsmToken::Star)) {
413 getLexer().Lex(); // Eat the star.
Chris Lattner22f480d2010-01-14 22:21:20 +0000414 Operands.push_back(new X86Operand(X86Operand::CreateToken("*")));
Daniel Dunbar76953672009-08-11 05:00:25 +0000415 }
416
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000417 // Read the first operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000418 if (X86Operand *Op = ParseOperand())
419 Operands.push_back(Op);
420 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000421 return true;
Chris Lattnere4d457c2010-01-15 18:44:13 +0000422
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000423 while (getLexer().is(AsmToken::Comma)) {
424 getLexer().Lex(); // Eat the comma.
425
426 // Parse and remember the operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000427 if (X86Operand *Op = ParseOperand())
428 Operands.push_back(Op);
429 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000430 return true;
431 }
432 }
433
Chris Lattner22f480d2010-01-14 22:21:20 +0000434 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000435}
436
Kevin Enderbyae90d092009-09-10 20:51:44 +0000437bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
438 StringRef IDVal = DirectiveID.getIdentifier();
439 if (IDVal == ".word")
440 return ParseDirectiveWord(2, DirectiveID.getLoc());
441 return true;
442}
443
444/// ParseDirectiveWord
445/// ::= .word [ expression (, expression)* ]
446bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
447 if (getLexer().isNot(AsmToken::EndOfStatement)) {
448 for (;;) {
449 const MCExpr *Value;
450 if (getParser().ParseExpression(Value))
451 return true;
452
453 getParser().getStreamer().EmitValue(Value, Size);
454
455 if (getLexer().is(AsmToken::EndOfStatement))
456 break;
457
458 // FIXME: Improve diagnostic.
459 if (getLexer().isNot(AsmToken::Comma))
460 return Error(L, "unexpected token in directive");
461 getLexer().Lex();
462 }
463 }
464
465 getLexer().Lex();
466 return false;
467}
468
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000469// Force static initialization.
470extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000471 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
472 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000473}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000474
475#include "X86GenAsmMatcher.inc"