blob: dcf4f8360f1f053fd62ac46452cefa2d9593d623 [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 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 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
Chris Lattnerc2fc91a2010-01-15 18:51:29 +000084 SMLoc StartLoc, EndLoc;
85
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000086 union {
87 struct {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000088 const char *Data;
89 unsigned Length;
90 } Tok;
91
92 struct {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000093 unsigned RegNo;
94 } Reg;
95
96 struct {
Daniel Dunbar6e966212009-08-31 08:08:38 +000097 const MCExpr *Val;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000098 } Imm;
99
100 struct {
101 unsigned SegReg;
Daniel Dunbar6e966212009-08-31 08:08:38 +0000102 const MCExpr *Disp;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000103 unsigned BaseReg;
104 unsigned IndexReg;
105 unsigned Scale;
106 } Mem;
Daniel Dunbar78929e52009-07-20 20:01:54 +0000107 };
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000108
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000109 StringRef getToken() const {
110 assert(Kind == Token && "Invalid access!");
111 return StringRef(Tok.Data, Tok.Length);
112 }
113
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000114 unsigned getReg() const {
115 assert(Kind == Register && "Invalid access!");
116 return Reg.RegNo;
117 }
Daniel Dunbard80432a2009-07-28 20:47:52 +0000118
Daniel Dunbar6e966212009-08-31 08:08:38 +0000119 const MCExpr *getImm() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000120 assert(Kind == Immediate && "Invalid access!");
121 return Imm.Val;
122 }
123
Daniel Dunbar6e966212009-08-31 08:08:38 +0000124 const MCExpr *getMemDisp() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000125 assert(Kind == Memory && "Invalid access!");
126 return Mem.Disp;
127 }
128 unsigned getMemSegReg() const {
129 assert(Kind == Memory && "Invalid access!");
130 return Mem.SegReg;
131 }
132 unsigned getMemBaseReg() const {
133 assert(Kind == Memory && "Invalid access!");
134 return Mem.BaseReg;
135 }
136 unsigned getMemIndexReg() const {
137 assert(Kind == Memory && "Invalid access!");
138 return Mem.IndexReg;
139 }
140 unsigned getMemScale() const {
141 assert(Kind == Memory && "Invalid access!");
142 return Mem.Scale;
143 }
144
Daniel Dunbar378bee92009-08-08 07:50:56 +0000145 bool isToken() const {return Kind == Token; }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000146
147 bool isImm() const { return Kind == Immediate; }
148
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000149 bool isImmSExt8() const {
150 // Accept immediates which fit in 8 bits when sign extended, and
151 // non-absolute immediates.
152 if (!isImm())
153 return false;
154
Daniel Dunbar6e966212009-08-31 08:08:38 +0000155 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
156 int64_t Value = CE->getValue();
157 return Value == (int64_t) (int8_t) Value;
158 }
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000159
Daniel Dunbar6e966212009-08-31 08:08:38 +0000160 return true;
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000161 }
162
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000163 bool isMem() const { return Kind == Memory; }
164
165 bool isReg() const { return Kind == Register; }
166
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000167 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000168 assert(N == 1 && "Invalid number of operands!");
169 Inst.addOperand(MCOperand::CreateReg(getReg()));
170 }
171
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000172 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000173 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000174 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000175 }
176
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000177 void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000178 // FIXME: Support user customization of the render method.
179 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000180 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000181 }
182
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000183 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000184 assert((N == 4 || N == 5) && "Invalid number of operands!");
185
186 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
187 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
188 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar6e966212009-08-31 08:08:38 +0000189 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000190
191 // FIXME: What a hack.
192 if (N == 5)
193 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
194 }
195
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000196 static X86Operand *CreateToken(StringRef Str) {
197 X86Operand *Res = new X86Operand();
198 Res->Kind = Token;
199 Res->Tok.Data = Str.data();
200 Res->Tok.Length = Str.size();
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000201 return Res;
202 }
203
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000204 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
205 X86Operand *Res = new X86Operand();
206 Res->Kind = Register;
207 Res->Reg.RegNo = RegNo;
208 Res->StartLoc = StartLoc;
209 Res->EndLoc = EndLoc;
210 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000211 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000212
Chris Lattnere4d457c2010-01-15 18:44:13 +0000213 static X86Operand *CreateImm(const MCExpr *Val) {
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000214 X86Operand *Res = new X86Operand();
215 Res->Kind = Immediate;
216 Res->Imm.Val = Val;
217 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000218 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000219
Chris Lattnere4d457c2010-01-15 18:44:13 +0000220 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
221 unsigned BaseReg, unsigned IndexReg,
222 unsigned Scale) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000223 // We should never just have a displacement, that would be an immediate.
224 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
225
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000226 // The scale should always be one of {1,2,4,8}.
227 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000228 "Invalid scale!");
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000229 X86Operand *Res = new X86Operand();
230 Res->Kind = Memory;
231 Res->Mem.SegReg = SegReg;
232 Res->Mem.Disp = Disp;
233 Res->Mem.BaseReg = BaseReg;
234 Res->Mem.IndexReg = IndexReg;
235 Res->Mem.Scale = Scale;
236 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000237 }
238};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000239
Chris Lattnere54532b2009-07-29 06:33:53 +0000240} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000241
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000242
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000243bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
244 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner977d91a2010-01-15 18:27:19 +0000245 RegNo = 0;
Kevin Enderbye71842b2009-09-03 17:15:07 +0000246 const AsmToken &TokPercent = getLexer().getTok();
247 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000248 StartLoc = TokPercent.getLoc();
Kevin Enderbye71842b2009-09-03 17:15:07 +0000249 getLexer().Lex(); // Eat percent token.
250
Chris Lattnere54532b2009-07-29 06:33:53 +0000251 const AsmToken &Tok = getLexer().getTok();
Kevin Enderby01b83cf2009-09-16 17:18:29 +0000252 if (Tok.isNot(AsmToken::Identifier))
253 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000254
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000255 // FIXME: Validate register for the current architecture; we have to do
256 // validation later, so maybe there is no need for this here.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000257 RegNo = MatchRegisterName(Tok.getString());
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +0000258 if (RegNo == 0)
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000259 return Error(Tok.getLoc(), "invalid register name");
260
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000261 EndLoc = Tok.getLoc();
Kevin Enderbye71842b2009-09-03 17:15:07 +0000262 getLexer().Lex(); // Eat identifier token.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000263 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000264}
265
Chris Lattnere4d457c2010-01-15 18:44:13 +0000266X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000267 switch (getLexer().getKind()) {
268 default:
Chris Lattnere4d457c2010-01-15 18:44:13 +0000269 return ParseMemOperand();
Chris Lattner977d91a2010-01-15 18:27:19 +0000270 case AsmToken::Percent: {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000271 // FIXME: if a segment register, this could either be just the seg reg, or
272 // the start of a memory operand.
Chris Lattner977d91a2010-01-15 18:27:19 +0000273 unsigned RegNo;
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000274 SMLoc Start, End;
275 if (ParseRegister(RegNo, Start, End)) return 0;
276 return X86Operand::CreateReg(RegNo, Start, End);
Chris Lattner977d91a2010-01-15 18:27:19 +0000277 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000278 case AsmToken::Dollar: {
279 // $42 -> immediate.
280 getLexer().Lex();
Daniel Dunbar6e966212009-08-31 08:08:38 +0000281 const MCExpr *Val;
282 if (getParser().ParseExpression(Val))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000283 return 0;
284 return X86Operand::CreateImm(Val);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000285 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000286 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000287}
288
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000289/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
Chris Lattnere4d457c2010-01-15 18:44:13 +0000290X86Operand *X86ATTAsmParser::ParseMemOperand() {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000291 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
292 unsigned SegReg = 0;
293
294 // We have to disambiguate a parenthesized expression "(4+5)" from the start
295 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
296 // only way to do this without lookahead is to eat the ( and see what is after
297 // it.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000298 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000299 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattnere4d457c2010-01-15 18:44:13 +0000300 if (getParser().ParseExpression(Disp)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000301
302 // After parsing the base expression we could either have a parenthesized
303 // memory address or not. If not, return now. If so, eat the (.
304 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000305 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000306 if (SegReg == 0)
307 return X86Operand::CreateImm(Disp);
308 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000309 }
310
311 // Eat the '('.
312 getLexer().Lex();
313 } else {
314 // Okay, we have a '('. We don't know if this is an expression or not, but
315 // so we have to eat the ( to see beyond it.
316 getLexer().Lex(); // Eat the '('.
317
Kevin Enderbye71842b2009-09-03 17:15:07 +0000318 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000319 // Nothing to do here, fall into the code below with the '(' part of the
320 // memory operand consumed.
321 } else {
322 // It must be an parenthesized expression, parse it now.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000323 if (getParser().ParseParenExpression(Disp))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000324 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000325
326 // After parsing the base expression we could either have a parenthesized
327 // memory address or not. If not, return now. If so, eat the (.
328 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000329 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000330 if (SegReg == 0)
331 return X86Operand::CreateImm(Disp);
332 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000333 }
334
335 // Eat the '('.
336 getLexer().Lex();
337 }
338 }
339
340 // If we reached here, then we just ate the ( of the memory operand. Process
341 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000342 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000343
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000344 if (getLexer().is(AsmToken::Percent)) {
345 SMLoc L;
346 if (ParseRegister(BaseReg, L, L)) return 0;
347 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000348
349 if (getLexer().is(AsmToken::Comma)) {
350 getLexer().Lex(); // Eat the comma.
351
352 // Following the comma we should have either an index register, or a scale
353 // value. We don't support the later form, but we want to parse it
354 // correctly.
355 //
356 // Not that even though it would be completely consistent to support syntax
357 // like "1(%eax,,1)", the assembler doesn't.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000358 if (getLexer().is(AsmToken::Percent)) {
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000359 SMLoc L;
360 if (ParseRegister(IndexReg, L, L)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000361
362 if (getLexer().isNot(AsmToken::RParen)) {
363 // Parse the scale amount:
364 // ::= ',' [scale-expression]
Chris Lattnere4d457c2010-01-15 18:44:13 +0000365 if (getLexer().isNot(AsmToken::Comma)) {
366 Error(getLexer().getTok().getLoc(),
367 "expected comma in scale expression");
368 return 0;
369 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000370 getLexer().Lex(); // Eat the comma.
371
372 if (getLexer().isNot(AsmToken::RParen)) {
373 SMLoc Loc = getLexer().getTok().getLoc();
374
375 int64_t ScaleVal;
376 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000377 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000378
379 // Validate the scale amount.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000380 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
381 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
382 return 0;
383 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000384 Scale = (unsigned)ScaleVal;
385 }
386 }
387 } else if (getLexer().isNot(AsmToken::RParen)) {
388 // Otherwise we have the unsupported form of a scale amount without an
389 // index.
390 SMLoc Loc = getLexer().getTok().getLoc();
391
392 int64_t Value;
393 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000394 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000395
Chris Lattnere4d457c2010-01-15 18:44:13 +0000396 Error(Loc, "cannot have scale factor without index register");
397 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000398 }
399 }
400
401 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000402 if (getLexer().isNot(AsmToken::RParen)) {
403 Error(getLexer().getTok().getLoc(), "unexpected token in memory operand");
404 return 0;
405 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000406 getLexer().Lex(); // Eat the ')'.
407
Chris Lattnere4d457c2010-01-15 18:44:13 +0000408 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000409}
410
Chris Lattner22f480d2010-01-14 22:21:20 +0000411bool X86ATTAsmParser::
412ParseInstruction(const StringRef &Name, SMLoc NameLoc,
413 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000414
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000415 Operands.push_back(X86Operand::CreateToken(Name));
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000416
Daniel Dunbara54716c2009-07-31 02:32:59 +0000417 SMLoc Loc = getLexer().getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000418 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar76953672009-08-11 05:00:25 +0000419
420 // Parse '*' modifier.
421 if (getLexer().is(AsmToken::Star)) {
422 getLexer().Lex(); // Eat the star.
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000423 Operands.push_back(X86Operand::CreateToken("*"));
Daniel Dunbar76953672009-08-11 05:00:25 +0000424 }
425
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000426 // Read the first 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;
Chris Lattnere4d457c2010-01-15 18:44:13 +0000431
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000432 while (getLexer().is(AsmToken::Comma)) {
433 getLexer().Lex(); // Eat the comma.
434
435 // Parse and remember the operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000436 if (X86Operand *Op = ParseOperand())
437 Operands.push_back(Op);
438 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000439 return true;
440 }
441 }
442
Chris Lattner22f480d2010-01-14 22:21:20 +0000443 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000444}
445
Kevin Enderbyae90d092009-09-10 20:51:44 +0000446bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
447 StringRef IDVal = DirectiveID.getIdentifier();
448 if (IDVal == ".word")
449 return ParseDirectiveWord(2, DirectiveID.getLoc());
450 return true;
451}
452
453/// ParseDirectiveWord
454/// ::= .word [ expression (, expression)* ]
455bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
456 if (getLexer().isNot(AsmToken::EndOfStatement)) {
457 for (;;) {
458 const MCExpr *Value;
459 if (getParser().ParseExpression(Value))
460 return true;
461
462 getParser().getStreamer().EmitValue(Value, Size);
463
464 if (getLexer().is(AsmToken::EndOfStatement))
465 break;
466
467 // FIXME: Improve diagnostic.
468 if (getLexer().isNot(AsmToken::Comma))
469 return Error(L, "unexpected token in directive");
470 getLexer().Lex();
471 }
472 }
473
474 getLexer().Lex();
475 return false;
476}
477
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000478// Force static initialization.
479extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000480 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
481 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000482}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000483
484#include "X86GenAsmMatcher.inc"