blob: 75395288ba5fd49ed5e0a95fd56fbddc87c2f7e4 [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 {
Chris Lattner61cd2c32010-01-15 19:06:59 +000077 enum KindTy {
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
Chris Lattner61cd2c32010-01-15 19:06:59 +0000109 X86Operand(KindTy K, SMLoc Start = SMLoc(), SMLoc End = SMLoc())
110 : Kind(K), StartLoc(Start), EndLoc(End) {}
111
112 /// getStartLoc - Get the location of the first token of this operand.
113 SMLoc getStartLoc() const { return StartLoc; }
114 /// getEndLoc - Get the location of the last token of this operand.
115 SMLoc getEndLoc() const { return EndLoc; }
116
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000117 StringRef getToken() const {
118 assert(Kind == Token && "Invalid access!");
119 return StringRef(Tok.Data, Tok.Length);
120 }
121
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000122 unsigned getReg() const {
123 assert(Kind == Register && "Invalid access!");
124 return Reg.RegNo;
125 }
Daniel Dunbard80432a2009-07-28 20:47:52 +0000126
Daniel Dunbar6e966212009-08-31 08:08:38 +0000127 const MCExpr *getImm() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000128 assert(Kind == Immediate && "Invalid access!");
129 return Imm.Val;
130 }
131
Daniel Dunbar6e966212009-08-31 08:08:38 +0000132 const MCExpr *getMemDisp() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000133 assert(Kind == Memory && "Invalid access!");
134 return Mem.Disp;
135 }
136 unsigned getMemSegReg() const {
137 assert(Kind == Memory && "Invalid access!");
138 return Mem.SegReg;
139 }
140 unsigned getMemBaseReg() const {
141 assert(Kind == Memory && "Invalid access!");
142 return Mem.BaseReg;
143 }
144 unsigned getMemIndexReg() const {
145 assert(Kind == Memory && "Invalid access!");
146 return Mem.IndexReg;
147 }
148 unsigned getMemScale() const {
149 assert(Kind == Memory && "Invalid access!");
150 return Mem.Scale;
151 }
152
Daniel Dunbar378bee92009-08-08 07:50:56 +0000153 bool isToken() const {return Kind == Token; }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000154
155 bool isImm() const { return Kind == Immediate; }
156
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000157 bool isImmSExt8() const {
158 // Accept immediates which fit in 8 bits when sign extended, and
159 // non-absolute immediates.
160 if (!isImm())
161 return false;
162
Daniel Dunbar6e966212009-08-31 08:08:38 +0000163 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
164 int64_t Value = CE->getValue();
165 return Value == (int64_t) (int8_t) Value;
166 }
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000167
Daniel Dunbar6e966212009-08-31 08:08:38 +0000168 return true;
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000169 }
170
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000171 bool isMem() const { return Kind == Memory; }
172
173 bool isReg() const { return Kind == Register; }
174
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000175 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000176 assert(N == 1 && "Invalid number of operands!");
177 Inst.addOperand(MCOperand::CreateReg(getReg()));
178 }
179
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000180 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000181 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000182 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000183 }
184
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000185 void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000186 // FIXME: Support user customization of the render method.
187 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000188 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000189 }
190
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000191 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000192 assert((N == 4 || N == 5) && "Invalid number of operands!");
193
194 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
195 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
196 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar6e966212009-08-31 08:08:38 +0000197 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000198
199 // FIXME: What a hack.
200 if (N == 5)
201 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
202 }
203
Chris Lattner284abb62010-01-15 19:28:38 +0000204 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
205 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000206 Res->Tok.Data = Str.data();
207 Res->Tok.Length = Str.size();
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000208 return Res;
209 }
210
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000211 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner61cd2c32010-01-15 19:06:59 +0000212 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000213 Res->Reg.RegNo = RegNo;
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000214 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000215 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000216
Chris Lattner284abb62010-01-15 19:28:38 +0000217 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
218 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000219 Res->Imm.Val = Val;
220 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000221 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000222
Chris Lattnere4d457c2010-01-15 18:44:13 +0000223 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
224 unsigned BaseReg, unsigned IndexReg,
225 unsigned Scale) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000226 // We should never just have a displacement, that would be an immediate.
227 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
228
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000229 // The scale should always be one of {1,2,4,8}.
230 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000231 "Invalid scale!");
Chris Lattner61cd2c32010-01-15 19:06:59 +0000232 X86Operand *Res = new X86Operand(Memory);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000233 Res->Mem.SegReg = SegReg;
234 Res->Mem.Disp = Disp;
235 Res->Mem.BaseReg = BaseReg;
236 Res->Mem.IndexReg = IndexReg;
237 Res->Mem.Scale = Scale;
238 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000239 }
240};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000241
Chris Lattnere54532b2009-07-29 06:33:53 +0000242} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000243
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000244
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000245bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
246 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner977d91a2010-01-15 18:27:19 +0000247 RegNo = 0;
Kevin Enderbye71842b2009-09-03 17:15:07 +0000248 const AsmToken &TokPercent = getLexer().getTok();
249 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000250 StartLoc = TokPercent.getLoc();
Kevin Enderbye71842b2009-09-03 17:15:07 +0000251 getLexer().Lex(); // Eat percent token.
252
Chris Lattnere54532b2009-07-29 06:33:53 +0000253 const AsmToken &Tok = getLexer().getTok();
Kevin Enderby01b83cf2009-09-16 17:18:29 +0000254 if (Tok.isNot(AsmToken::Identifier))
255 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000256
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000257 // FIXME: Validate register for the current architecture; we have to do
258 // validation later, so maybe there is no need for this here.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000259 RegNo = MatchRegisterName(Tok.getString());
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +0000260 if (RegNo == 0)
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000261 return Error(Tok.getLoc(), "invalid register name");
262
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000263 EndLoc = Tok.getLoc();
Kevin Enderbye71842b2009-09-03 17:15:07 +0000264 getLexer().Lex(); // Eat identifier token.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000265 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000266}
267
Chris Lattnere4d457c2010-01-15 18:44:13 +0000268X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000269 switch (getLexer().getKind()) {
270 default:
Chris Lattnere4d457c2010-01-15 18:44:13 +0000271 return ParseMemOperand();
Chris Lattner977d91a2010-01-15 18:27:19 +0000272 case AsmToken::Percent: {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000273 // FIXME: if a segment register, this could either be just the seg reg, or
274 // the start of a memory operand.
Chris Lattner977d91a2010-01-15 18:27:19 +0000275 unsigned RegNo;
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000276 SMLoc Start, End;
277 if (ParseRegister(RegNo, Start, End)) return 0;
278 return X86Operand::CreateReg(RegNo, Start, End);
Chris Lattner977d91a2010-01-15 18:27:19 +0000279 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000280 case AsmToken::Dollar: {
281 // $42 -> immediate.
282 getLexer().Lex();
Daniel Dunbar6e966212009-08-31 08:08:38 +0000283 const MCExpr *Val;
Chris Lattner284abb62010-01-15 19:28:38 +0000284 SMLoc Start, End;
285 if (getParser().ParseExpression(Val, Start, End))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000286 return 0;
Chris Lattner284abb62010-01-15 19:28:38 +0000287 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000288 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000289 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000290}
291
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000292/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
Chris Lattnere4d457c2010-01-15 18:44:13 +0000293X86Operand *X86ATTAsmParser::ParseMemOperand() {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000294 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
295 unsigned SegReg = 0;
296
297 // We have to disambiguate a parenthesized expression "(4+5)" from the start
298 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
299 // only way to do this without lookahead is to eat the ( and see what is after
300 // it.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000301 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000302 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner284abb62010-01-15 19:28:38 +0000303 SMLoc ExprStart, ExprEnd;
304 if (getParser().ParseExpression(Disp, ExprStart, ExprEnd)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000305
306 // After parsing the base expression we could either have a parenthesized
307 // memory address or not. If not, return now. If so, eat the (.
308 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000309 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000310 if (SegReg == 0)
Chris Lattner284abb62010-01-15 19:28:38 +0000311 return X86Operand::CreateImm(Disp, ExprStart, ExprEnd);
Chris Lattnere4d457c2010-01-15 18:44:13 +0000312 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000313 }
314
315 // Eat the '('.
316 getLexer().Lex();
317 } else {
318 // Okay, we have a '('. We don't know if this is an expression or not, but
319 // so we have to eat the ( to see beyond it.
Chris Lattner284abb62010-01-15 19:28:38 +0000320 SMLoc LParenLoc = getLexer().getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000321 getLexer().Lex(); // Eat the '('.
322
Kevin Enderbye71842b2009-09-03 17:15:07 +0000323 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000324 // Nothing to do here, fall into the code below with the '(' part of the
325 // memory operand consumed.
326 } else {
Chris Lattner284abb62010-01-15 19:28:38 +0000327 SMLoc ExprEnd;
328
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000329 // It must be an parenthesized expression, parse it now.
Chris Lattner284abb62010-01-15 19:28:38 +0000330 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000331 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000332
333 // After parsing the base expression we could either have a parenthesized
334 // memory address or not. If not, return now. If so, eat the (.
335 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000336 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000337 if (SegReg == 0)
Chris Lattner284abb62010-01-15 19:28:38 +0000338 return X86Operand::CreateImm(Disp, LParenLoc, ExprEnd);
Chris Lattnere4d457c2010-01-15 18:44:13 +0000339 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000340 }
341
342 // Eat the '('.
343 getLexer().Lex();
344 }
345 }
346
347 // If we reached here, then we just ate the ( of the memory operand. Process
348 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000349 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000350
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000351 if (getLexer().is(AsmToken::Percent)) {
352 SMLoc L;
353 if (ParseRegister(BaseReg, L, L)) return 0;
354 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000355
356 if (getLexer().is(AsmToken::Comma)) {
357 getLexer().Lex(); // Eat the comma.
358
359 // Following the comma we should have either an index register, or a scale
360 // value. We don't support the later form, but we want to parse it
361 // correctly.
362 //
363 // Not that even though it would be completely consistent to support syntax
364 // like "1(%eax,,1)", the assembler doesn't.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000365 if (getLexer().is(AsmToken::Percent)) {
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000366 SMLoc L;
367 if (ParseRegister(IndexReg, L, L)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000368
369 if (getLexer().isNot(AsmToken::RParen)) {
370 // Parse the scale amount:
371 // ::= ',' [scale-expression]
Chris Lattnere4d457c2010-01-15 18:44:13 +0000372 if (getLexer().isNot(AsmToken::Comma)) {
373 Error(getLexer().getTok().getLoc(),
374 "expected comma in scale expression");
375 return 0;
376 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000377 getLexer().Lex(); // Eat the comma.
378
379 if (getLexer().isNot(AsmToken::RParen)) {
380 SMLoc Loc = getLexer().getTok().getLoc();
381
382 int64_t ScaleVal;
383 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000384 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000385
386 // Validate the scale amount.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000387 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
388 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
389 return 0;
390 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000391 Scale = (unsigned)ScaleVal;
392 }
393 }
394 } else if (getLexer().isNot(AsmToken::RParen)) {
395 // Otherwise we have the unsupported form of a scale amount without an
396 // index.
397 SMLoc Loc = getLexer().getTok().getLoc();
398
399 int64_t Value;
400 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000401 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000402
Chris Lattnere4d457c2010-01-15 18:44:13 +0000403 Error(Loc, "cannot have scale factor without index register");
404 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000405 }
406 }
407
408 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000409 if (getLexer().isNot(AsmToken::RParen)) {
410 Error(getLexer().getTok().getLoc(), "unexpected token in memory operand");
411 return 0;
412 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000413 getLexer().Lex(); // Eat the ')'.
414
Chris Lattnere4d457c2010-01-15 18:44:13 +0000415 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000416}
417
Chris Lattner22f480d2010-01-14 22:21:20 +0000418bool X86ATTAsmParser::
419ParseInstruction(const StringRef &Name, SMLoc NameLoc,
420 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000421
Chris Lattner284abb62010-01-15 19:28:38 +0000422 Operands.push_back(X86Operand::CreateToken(Name, NameLoc));
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000423
424 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar76953672009-08-11 05:00:25 +0000425
426 // Parse '*' modifier.
427 if (getLexer().is(AsmToken::Star)) {
Chris Lattner284abb62010-01-15 19:28:38 +0000428 SMLoc Loc = getLexer().getTok().getLoc();
429 Operands.push_back(X86Operand::CreateToken("*", Loc));
Daniel Dunbar76953672009-08-11 05:00:25 +0000430 getLexer().Lex(); // Eat the star.
Daniel Dunbar76953672009-08-11 05:00:25 +0000431 }
432
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000433 // Read the first operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000434 if (X86Operand *Op = ParseOperand())
435 Operands.push_back(Op);
436 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000437 return true;
Chris Lattnere4d457c2010-01-15 18:44:13 +0000438
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000439 while (getLexer().is(AsmToken::Comma)) {
440 getLexer().Lex(); // Eat the comma.
441
442 // Parse and remember the operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000443 if (X86Operand *Op = ParseOperand())
444 Operands.push_back(Op);
445 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000446 return true;
447 }
448 }
449
Chris Lattner22f480d2010-01-14 22:21:20 +0000450 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000451}
452
Kevin Enderbyae90d092009-09-10 20:51:44 +0000453bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
454 StringRef IDVal = DirectiveID.getIdentifier();
455 if (IDVal == ".word")
456 return ParseDirectiveWord(2, DirectiveID.getLoc());
457 return true;
458}
459
460/// ParseDirectiveWord
461/// ::= .word [ expression (, expression)* ]
462bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
463 if (getLexer().isNot(AsmToken::EndOfStatement)) {
464 for (;;) {
465 const MCExpr *Value;
466 if (getParser().ParseExpression(Value))
467 return true;
468
469 getParser().getStreamer().EmitValue(Value, Size);
470
471 if (getLexer().is(AsmToken::EndOfStatement))
472 break;
473
474 // FIXME: Improve diagnostic.
475 if (getLexer().isNot(AsmToken::Comma))
476 return Error(L, "unexpected token in directive");
477 getLexer().Lex();
478 }
479 }
480
481 getLexer().Lex();
482 return false;
483}
484
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000485// Force static initialization.
486extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000487 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
488 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000489}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000490
491#include "X86GenAsmMatcher.inc"