blob: 51ff44e6b4f6ba5204b3fe361844c05c00816a02 [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
42 bool ParseOperand(X86Operand &Op);
43
44 bool ParseMemOperand(X86Operand &Op);
Kevin Enderbyae90d092009-09-10 20:51:44 +000045
46 bool ParseDirectiveWord(unsigned Size, SMLoc L);
47
Daniel Dunbar85f1b392009-07-29 00:02:19 +000048 /// @name Auto-generated Match Functions
49 /// {
50
Chris Lattner22f480d2010-01-14 22:21:20 +000051 bool MatchInstruction(const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000052 MCInst &Inst);
53
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +000054 /// MatchRegisterName - Match the given string to a register name, or 0 if
55 /// there is no match.
56 unsigned MatchRegisterName(const StringRef &Name);
Daniel Dunbar85f1b392009-07-29 00:02:19 +000057
58 /// }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000059
60public:
61 X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
62 : TargetAsmParser(T), Parser(_Parser) {}
63
Chris Lattnerf66e4eb2010-01-14 21:32:45 +000064 virtual bool ParseInstruction(const StringRef &Name, SMLoc NameLoc,
Chris Lattner22f480d2010-01-14 22:21:20 +000065 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyae90d092009-09-10 20:51:44 +000066
67 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000068};
Chris Lattnere54532b2009-07-29 06:33:53 +000069
70} // end anonymous namespace
71
72
73namespace {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000074
75/// X86Operand - Instances of this class represent a parsed X86 machine
76/// instruction.
Chris Lattner0c119a72010-01-14 21:20:55 +000077struct X86Operand : public MCParsedAsmOperand {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000078 enum {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000079 Token,
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000080 Register,
81 Immediate,
82 Memory
83 } Kind;
84
85 union {
86 struct {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000087 const char *Data;
88 unsigned Length;
89 } Tok;
90
91 struct {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000092 unsigned RegNo;
93 } Reg;
94
95 struct {
Daniel Dunbar6e966212009-08-31 08:08:38 +000096 const MCExpr *Val;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000097 } Imm;
98
99 struct {
100 unsigned SegReg;
Daniel Dunbar6e966212009-08-31 08:08:38 +0000101 const MCExpr *Disp;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000102 unsigned BaseReg;
103 unsigned IndexReg;
104 unsigned Scale;
105 } Mem;
Daniel Dunbar78929e52009-07-20 20:01:54 +0000106 };
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000107
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000108 StringRef getToken() const {
109 assert(Kind == Token && "Invalid access!");
110 return StringRef(Tok.Data, Tok.Length);
111 }
112
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000113 unsigned getReg() const {
114 assert(Kind == Register && "Invalid access!");
115 return Reg.RegNo;
116 }
Daniel Dunbard80432a2009-07-28 20:47:52 +0000117
Daniel Dunbar6e966212009-08-31 08:08:38 +0000118 const MCExpr *getImm() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000119 assert(Kind == Immediate && "Invalid access!");
120 return Imm.Val;
121 }
122
Daniel Dunbar6e966212009-08-31 08:08:38 +0000123 const MCExpr *getMemDisp() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000124 assert(Kind == Memory && "Invalid access!");
125 return Mem.Disp;
126 }
127 unsigned getMemSegReg() const {
128 assert(Kind == Memory && "Invalid access!");
129 return Mem.SegReg;
130 }
131 unsigned getMemBaseReg() const {
132 assert(Kind == Memory && "Invalid access!");
133 return Mem.BaseReg;
134 }
135 unsigned getMemIndexReg() const {
136 assert(Kind == Memory && "Invalid access!");
137 return Mem.IndexReg;
138 }
139 unsigned getMemScale() const {
140 assert(Kind == Memory && "Invalid access!");
141 return Mem.Scale;
142 }
143
Daniel Dunbar378bee92009-08-08 07:50:56 +0000144 bool isToken() const {return Kind == Token; }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000145
146 bool isImm() const { return Kind == Immediate; }
147
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000148 bool isImmSExt8() const {
149 // Accept immediates which fit in 8 bits when sign extended, and
150 // non-absolute immediates.
151 if (!isImm())
152 return false;
153
Daniel Dunbar6e966212009-08-31 08:08:38 +0000154 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
155 int64_t Value = CE->getValue();
156 return Value == (int64_t) (int8_t) Value;
157 }
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000158
Daniel Dunbar6e966212009-08-31 08:08:38 +0000159 return true;
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000160 }
161
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000162 bool isMem() const { return Kind == Memory; }
163
164 bool isReg() const { return Kind == Register; }
165
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000166 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000167 assert(N == 1 && "Invalid number of operands!");
168 Inst.addOperand(MCOperand::CreateReg(getReg()));
169 }
170
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000171 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000172 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000173 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000174 }
175
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000176 void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000177 // FIXME: Support user customization of the render method.
178 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000179 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000180 }
181
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000182 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000183 assert((N == 4 || N == 5) && "Invalid number of operands!");
184
185 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
186 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
187 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar6e966212009-08-31 08:08:38 +0000188 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000189
190 // FIXME: What a hack.
191 if (N == 5)
192 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
193 }
194
195 static X86Operand CreateToken(StringRef Str) {
196 X86Operand Res;
197 Res.Kind = Token;
198 Res.Tok.Data = Str.data();
199 Res.Tok.Length = Str.size();
200 return Res;
201 }
202
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000203 static X86Operand CreateReg(unsigned RegNo) {
204 X86Operand Res;
205 Res.Kind = Register;
206 Res.Reg.RegNo = RegNo;
207 return Res;
208 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000209
Daniel Dunbar6e966212009-08-31 08:08:38 +0000210 static X86Operand CreateImm(const MCExpr *Val) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000211 X86Operand Res;
212 Res.Kind = Immediate;
213 Res.Imm.Val = Val;
214 return Res;
215 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000216
Daniel Dunbar6e966212009-08-31 08:08:38 +0000217 static X86Operand CreateMem(unsigned SegReg, const MCExpr *Disp,
218 unsigned BaseReg, unsigned IndexReg,
219 unsigned Scale) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000220 // We should never just have a displacement, that would be an immediate.
221 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
222
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000223 // The scale should always be one of {1,2,4,8}.
224 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000225 "Invalid scale!");
226 X86Operand Res;
227 Res.Kind = Memory;
228 Res.Mem.SegReg = SegReg;
229 Res.Mem.Disp = Disp;
230 Res.Mem.BaseReg = BaseReg;
231 Res.Mem.IndexReg = IndexReg;
232 Res.Mem.Scale = Scale;
233 return Res;
234 }
235};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000236
Chris Lattnere54532b2009-07-29 06:33:53 +0000237} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000238
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000239
Chris Lattner977d91a2010-01-15 18:27:19 +0000240bool X86ATTAsmParser::ParseRegister(unsigned &RegNo) {
241 RegNo = 0;
Kevin Enderbye71842b2009-09-03 17:15:07 +0000242 const AsmToken &TokPercent = getLexer().getTok();
Duncan Sandse0a6add2009-09-06 16:27:34 +0000243 (void)TokPercent; // Avoid warning when assertions are disabled.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000244 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
245 getLexer().Lex(); // Eat percent token.
246
Chris Lattnere54532b2009-07-29 06:33:53 +0000247 const AsmToken &Tok = getLexer().getTok();
Kevin Enderby01b83cf2009-09-16 17:18:29 +0000248 if (Tok.isNot(AsmToken::Identifier))
249 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000250
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000251 // FIXME: Validate register for the current architecture; we have to do
252 // validation later, so maybe there is no need for this here.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000253 RegNo = MatchRegisterName(Tok.getString());
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +0000254 if (RegNo == 0)
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000255 return Error(Tok.getLoc(), "invalid register name");
256
Kevin Enderbye71842b2009-09-03 17:15:07 +0000257 getLexer().Lex(); // Eat identifier token.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000258
259 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000260}
261
Daniel Dunbar78929e52009-07-20 20:01:54 +0000262bool X86ATTAsmParser::ParseOperand(X86Operand &Op) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000263 switch (getLexer().getKind()) {
264 default:
265 return ParseMemOperand(Op);
Chris Lattner977d91a2010-01-15 18:27:19 +0000266 case AsmToken::Percent: {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000267 // FIXME: if a segment register, this could either be just the seg reg, or
268 // the start of a memory operand.
Chris Lattner977d91a2010-01-15 18:27:19 +0000269 unsigned RegNo;
270 if (ParseRegister(RegNo)) return true;
271 Op = X86Operand::CreateReg(RegNo);
272 return false;
273 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000274 case AsmToken::Dollar: {
275 // $42 -> immediate.
276 getLexer().Lex();
Daniel Dunbar6e966212009-08-31 08:08:38 +0000277 const MCExpr *Val;
278 if (getParser().ParseExpression(Val))
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000279 return true;
280 Op = X86Operand::CreateImm(Val);
281 return false;
282 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000283 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000284}
285
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000286/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
287bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) {
288 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
289 unsigned SegReg = 0;
290
291 // We have to disambiguate a parenthesized expression "(4+5)" from the start
292 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
293 // only way to do this without lookahead is to eat the ( and see what is after
294 // it.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000295 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000296 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar6e966212009-08-31 08:08:38 +0000297 if (getParser().ParseExpression(Disp)) return true;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000298
299 // After parsing the base expression we could either have a parenthesized
300 // memory address or not. If not, return now. If so, eat the (.
301 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000302 // Unless we have a segment register, treat this as an immediate.
303 if (SegReg)
304 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
305 else
306 Op = X86Operand::CreateImm(Disp);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000307 return false;
308 }
309
310 // Eat the '('.
311 getLexer().Lex();
312 } else {
313 // Okay, we have a '('. We don't know if this is an expression or not, but
314 // so we have to eat the ( to see beyond it.
315 getLexer().Lex(); // Eat the '('.
316
Kevin Enderbye71842b2009-09-03 17:15:07 +0000317 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000318 // Nothing to do here, fall into the code below with the '(' part of the
319 // memory operand consumed.
320 } else {
321 // It must be an parenthesized expression, parse it now.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000322 if (getParser().ParseParenExpression(Disp))
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000323 return true;
324
325 // After parsing the base expression we could either have a parenthesized
326 // memory address or not. If not, return now. If so, eat the (.
327 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000328 // Unless we have a segment register, treat this as an immediate.
329 if (SegReg)
330 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
331 else
332 Op = X86Operand::CreateImm(Disp);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000333 return false;
334 }
335
336 // Eat the '('.
337 getLexer().Lex();
338 }
339 }
340
341 // If we reached here, then we just ate the ( of the memory operand. Process
342 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000343 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000344
Chris Lattner977d91a2010-01-15 18:27:19 +0000345 if (getLexer().is(AsmToken::Percent))
346 if (ParseRegister(BaseReg)) return true;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000347
348 if (getLexer().is(AsmToken::Comma)) {
349 getLexer().Lex(); // Eat the comma.
350
351 // Following the comma we should have either an index register, or a scale
352 // value. We don't support the later form, but we want to parse it
353 // correctly.
354 //
355 // Not that even though it would be completely consistent to support syntax
356 // like "1(%eax,,1)", the assembler doesn't.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000357 if (getLexer().is(AsmToken::Percent)) {
Chris Lattner977d91a2010-01-15 18:27:19 +0000358 if (ParseRegister(IndexReg)) return true;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000359
360 if (getLexer().isNot(AsmToken::RParen)) {
361 // Parse the scale amount:
362 // ::= ',' [scale-expression]
363 if (getLexer().isNot(AsmToken::Comma))
364 return true;
365 getLexer().Lex(); // Eat the comma.
366
367 if (getLexer().isNot(AsmToken::RParen)) {
368 SMLoc Loc = getLexer().getTok().getLoc();
369
370 int64_t ScaleVal;
371 if (getParser().ParseAbsoluteExpression(ScaleVal))
372 return true;
373
374 // Validate the scale amount.
375 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
376 return Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
377 Scale = (unsigned)ScaleVal;
378 }
379 }
380 } else if (getLexer().isNot(AsmToken::RParen)) {
381 // Otherwise we have the unsupported form of a scale amount without an
382 // index.
383 SMLoc Loc = getLexer().getTok().getLoc();
384
385 int64_t Value;
386 if (getParser().ParseAbsoluteExpression(Value))
387 return true;
388
389 return Error(Loc, "cannot have scale factor without index register");
390 }
391 }
392
393 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
394 if (getLexer().isNot(AsmToken::RParen))
395 return Error(getLexer().getTok().getLoc(),
396 "unexpected token in memory operand");
397 getLexer().Lex(); // Eat the ')'.
398
399 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
400 return false;
401}
402
Chris Lattner22f480d2010-01-14 22:21:20 +0000403bool X86ATTAsmParser::
404ParseInstruction(const StringRef &Name, SMLoc NameLoc,
405 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000406
Chris Lattner22f480d2010-01-14 22:21:20 +0000407 Operands.push_back(new X86Operand(X86Operand::CreateToken(Name)));
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000408
Daniel Dunbara54716c2009-07-31 02:32:59 +0000409 SMLoc Loc = getLexer().getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000410 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar76953672009-08-11 05:00:25 +0000411
412 // Parse '*' modifier.
413 if (getLexer().is(AsmToken::Star)) {
414 getLexer().Lex(); // Eat the star.
Chris Lattner22f480d2010-01-14 22:21:20 +0000415 Operands.push_back(new X86Operand(X86Operand::CreateToken("*")));
Daniel Dunbar76953672009-08-11 05:00:25 +0000416 }
417
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000418 // Read the first operand.
Chris Lattner22f480d2010-01-14 22:21:20 +0000419 X86Operand Op;
420 if (ParseOperand(Op))
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000421 return true;
422
Chris Lattner22f480d2010-01-14 22:21:20 +0000423 Operands.push_back(new X86Operand(Op));
424
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000425 while (getLexer().is(AsmToken::Comma)) {
426 getLexer().Lex(); // Eat the comma.
427
428 // Parse and remember the operand.
Chris Lattner22f480d2010-01-14 22:21:20 +0000429 if (ParseOperand(Op))
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000430 return true;
Chris Lattner22f480d2010-01-14 22:21:20 +0000431 Operands.push_back(new X86Operand(Op));
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000432 }
433 }
434
Chris Lattner22f480d2010-01-14 22:21:20 +0000435 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000436}
437
Kevin Enderbyae90d092009-09-10 20:51:44 +0000438bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
439 StringRef IDVal = DirectiveID.getIdentifier();
440 if (IDVal == ".word")
441 return ParseDirectiveWord(2, DirectiveID.getLoc());
442 return true;
443}
444
445/// ParseDirectiveWord
446/// ::= .word [ expression (, expression)* ]
447bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
448 if (getLexer().isNot(AsmToken::EndOfStatement)) {
449 for (;;) {
450 const MCExpr *Value;
451 if (getParser().ParseExpression(Value))
452 return true;
453
454 getParser().getStreamer().EmitValue(Value, Size);
455
456 if (getLexer().is(AsmToken::EndOfStatement))
457 break;
458
459 // FIXME: Improve diagnostic.
460 if (getLexer().isNot(AsmToken::Comma))
461 return Error(L, "unexpected token in directive");
462 getLexer().Lex();
463 }
464 }
465
466 getLexer().Lex();
467 return false;
468}
469
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000470// Force static initialization.
471extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000472 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
473 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000474}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000475
476#include "X86GenAsmMatcher.inc"