blob: 2c79eeb962b34a3c71a24fc69932e7ce2c93c0aa [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 Dunbar6b3888b2010-02-10 21:19:28 +000013#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000014#include "llvm/ADT/Twine.h"
Kevin Enderbyae90d092009-09-10 20:51:44 +000015#include "llvm/MC/MCStreamer.h"
Daniel Dunbar6e966212009-08-31 08:08:38 +000016#include "llvm/MC/MCExpr.h"
Daniel Dunbara54716c2009-07-31 02:32:59 +000017#include "llvm/MC/MCInst.h"
Chris Lattner291d6692010-01-22 01:44:57 +000018#include "llvm/MC/MCParser/MCAsmLexer.h"
19#include "llvm/MC/MCParser/MCAsmParser.h"
20#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000021#include "llvm/Support/SourceMgr.h"
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +000022#include "llvm/Target/TargetRegistry.h"
23#include "llvm/Target/TargetAsmParser.h"
24using namespace llvm;
25
26namespace {
Benjamin Kramer264834b2009-07-31 11:35:26 +000027struct X86Operand;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000028
29class X86ATTAsmParser : public TargetAsmParser {
30 MCAsmParser &Parser;
31
32private:
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000033 MCAsmParser &getParser() const { return Parser; }
34
35 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
36
37 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
38
39 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
40
Chris Lattnerc2fc91a2010-01-15 18:51:29 +000041 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000042
Chris Lattnere4d457c2010-01-15 18:44:13 +000043 X86Operand *ParseOperand();
44 X86Operand *ParseMemOperand();
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 Dunbar85f1b392009-07-29 00:02:19 +000054 /// }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000055
56public:
57 X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
58 : TargetAsmParser(T), Parser(_Parser) {}
59
Chris Lattnerf66e4eb2010-01-14 21:32:45 +000060 virtual bool ParseInstruction(const StringRef &Name, SMLoc NameLoc,
Chris Lattner22f480d2010-01-14 22:21:20 +000061 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyae90d092009-09-10 20:51:44 +000062
63 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000064};
Chris Lattnere54532b2009-07-29 06:33:53 +000065
66} // end anonymous namespace
67
Sean Callananef372de2010-01-23 00:40:33 +000068/// @name Auto-generated Match Functions
69/// {
70
Chris Lattner8b382002010-02-09 00:34:28 +000071static unsigned MatchRegisterName(StringRef Name);
Sean Callananef372de2010-01-23 00:40:33 +000072
73/// }
Chris Lattnere54532b2009-07-29 06:33:53 +000074
75namespace {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000076
77/// X86Operand - Instances of this class represent a parsed X86 machine
78/// instruction.
Chris Lattner0c119a72010-01-14 21:20:55 +000079struct X86Operand : public MCParsedAsmOperand {
Chris Lattner61cd2c32010-01-15 19:06:59 +000080 enum KindTy {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000081 Token,
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000082 Register,
83 Immediate,
84 Memory
85 } Kind;
86
Chris Lattnerc2fc91a2010-01-15 18:51:29 +000087 SMLoc StartLoc, EndLoc;
88
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000089 union {
90 struct {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000091 const char *Data;
92 unsigned Length;
93 } Tok;
94
95 struct {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000096 unsigned RegNo;
97 } Reg;
98
99 struct {
Daniel Dunbar6e966212009-08-31 08:08:38 +0000100 const MCExpr *Val;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000101 } Imm;
102
103 struct {
104 unsigned SegReg;
Daniel Dunbar6e966212009-08-31 08:08:38 +0000105 const MCExpr *Disp;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000106 unsigned BaseReg;
107 unsigned IndexReg;
108 unsigned Scale;
109 } Mem;
Daniel Dunbar78929e52009-07-20 20:01:54 +0000110 };
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000111
Chris Lattner80cc03a2010-01-15 19:33:43 +0000112 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner61cd2c32010-01-15 19:06:59 +0000113 : Kind(K), StartLoc(Start), EndLoc(End) {}
114
115 /// getStartLoc - Get the location of the first token of this operand.
116 SMLoc getStartLoc() const { return StartLoc; }
117 /// getEndLoc - Get the location of the last token of this operand.
118 SMLoc getEndLoc() const { return EndLoc; }
119
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000120 StringRef getToken() const {
121 assert(Kind == Token && "Invalid access!");
122 return StringRef(Tok.Data, Tok.Length);
123 }
124
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000125 unsigned getReg() const {
126 assert(Kind == Register && "Invalid access!");
127 return Reg.RegNo;
128 }
Daniel Dunbard80432a2009-07-28 20:47:52 +0000129
Daniel Dunbar6e966212009-08-31 08:08:38 +0000130 const MCExpr *getImm() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000131 assert(Kind == Immediate && "Invalid access!");
132 return Imm.Val;
133 }
134
Daniel Dunbar6e966212009-08-31 08:08:38 +0000135 const MCExpr *getMemDisp() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000136 assert(Kind == Memory && "Invalid access!");
137 return Mem.Disp;
138 }
139 unsigned getMemSegReg() const {
140 assert(Kind == Memory && "Invalid access!");
141 return Mem.SegReg;
142 }
143 unsigned getMemBaseReg() const {
144 assert(Kind == Memory && "Invalid access!");
145 return Mem.BaseReg;
146 }
147 unsigned getMemIndexReg() const {
148 assert(Kind == Memory && "Invalid access!");
149 return Mem.IndexReg;
150 }
151 unsigned getMemScale() const {
152 assert(Kind == Memory && "Invalid access!");
153 return Mem.Scale;
154 }
155
Daniel Dunbar378bee92009-08-08 07:50:56 +0000156 bool isToken() const {return Kind == Token; }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000157
158 bool isImm() const { return Kind == Immediate; }
159
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000160 bool isImmSExt8() const {
161 // Accept immediates which fit in 8 bits when sign extended, and
162 // non-absolute immediates.
163 if (!isImm())
164 return false;
165
Daniel Dunbar6e966212009-08-31 08:08:38 +0000166 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
167 int64_t Value = CE->getValue();
168 return Value == (int64_t) (int8_t) Value;
169 }
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000170
Daniel Dunbar6e966212009-08-31 08:08:38 +0000171 return true;
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000172 }
173
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000174 bool isMem() const { return Kind == Memory; }
175
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000176 bool isAbsMem() const {
177 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar08a1aae2010-02-02 21:44:16 +0000178 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000179 }
180
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000181 bool isNoSegMem() const {
182 return Kind == Memory && !getMemSegReg();
183 }
184
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000185 bool isReg() const { return Kind == Register; }
186
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000187 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000188 assert(N == 1 && "Invalid number of operands!");
189 Inst.addOperand(MCOperand::CreateReg(getReg()));
190 }
191
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000192 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000193 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000194 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000195 }
196
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000197 void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000198 // FIXME: Support user customization of the render method.
199 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000200 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000201 }
202
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000203 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000204 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000205 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
206 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
207 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar6e966212009-08-31 08:08:38 +0000208 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000209 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
210 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000211
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000212 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
213 assert((N == 1) && "Invalid number of operands!");
214 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
215 }
216
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000217 void addNoSegMemOperands(MCInst &Inst, unsigned N) const {
218 assert((N == 4) && "Invalid number of operands!");
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000219 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
220 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
221 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
222 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000223 }
224
Chris Lattner284abb62010-01-15 19:28:38 +0000225 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
226 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000227 Res->Tok.Data = Str.data();
228 Res->Tok.Length = Str.size();
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000229 return Res;
230 }
231
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000232 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner61cd2c32010-01-15 19:06:59 +0000233 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000234 Res->Reg.RegNo = RegNo;
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000235 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000236 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000237
Chris Lattner284abb62010-01-15 19:28:38 +0000238 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
239 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000240 Res->Imm.Val = Val;
241 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000242 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000243
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000244 /// Create an absolute memory operand.
245 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
246 SMLoc EndLoc) {
247 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
248 Res->Mem.SegReg = 0;
249 Res->Mem.Disp = Disp;
250 Res->Mem.BaseReg = 0;
251 Res->Mem.IndexReg = 0;
Daniel Dunbar08a1aae2010-02-02 21:44:16 +0000252 Res->Mem.Scale = 1;
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000253 return Res;
254 }
255
256 /// Create a generalized memory operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000257 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
258 unsigned BaseReg, unsigned IndexReg,
Chris Lattner80cc03a2010-01-15 19:33:43 +0000259 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000260 // We should never just have a displacement, that should be parsed as an
261 // absolute memory operand.
Daniel Dunbar24091712009-07-31 22:22:54 +0000262 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
263
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000264 // The scale should always be one of {1,2,4,8}.
265 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000266 "Invalid scale!");
Chris Lattner80cc03a2010-01-15 19:33:43 +0000267 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000268 Res->Mem.SegReg = SegReg;
269 Res->Mem.Disp = Disp;
270 Res->Mem.BaseReg = BaseReg;
271 Res->Mem.IndexReg = IndexReg;
272 Res->Mem.Scale = Scale;
273 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000274 }
275};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000276
Chris Lattnere54532b2009-07-29 06:33:53 +0000277} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000278
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000279
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000280bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
281 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner977d91a2010-01-15 18:27:19 +0000282 RegNo = 0;
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000283 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderbye71842b2009-09-03 17:15:07 +0000284 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000285 StartLoc = TokPercent.getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000286 Parser.Lex(); // Eat percent token.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000287
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000288 const AsmToken &Tok = Parser.getTok();
Kevin Enderby01b83cf2009-09-16 17:18:29 +0000289 if (Tok.isNot(AsmToken::Identifier))
290 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000291
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000292 // FIXME: Validate register for the current architecture; we have to do
293 // validation later, so maybe there is no need for this here.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000294 RegNo = MatchRegisterName(Tok.getString());
Chris Lattner8b382002010-02-09 00:34:28 +0000295
Chris Lattner57b278e2010-02-09 00:49:22 +0000296 // Parse %st(1) and "%st" as "%st(0)"
297 if (RegNo == 0 && Tok.getString() == "st") {
298 RegNo = X86::ST0;
299 EndLoc = Tok.getLoc();
300 Parser.Lex(); // Eat 'st'
301
302 // Check to see if we have '(4)' after %st.
303 if (getLexer().isNot(AsmToken::LParen))
304 return false;
305 // Lex the paren.
306 getParser().Lex();
307
308 const AsmToken &IntTok = Parser.getTok();
309 if (IntTok.isNot(AsmToken::Integer))
310 return Error(IntTok.getLoc(), "expected stack index");
311 switch (IntTok.getIntVal()) {
312 case 0: RegNo = X86::ST0; break;
313 case 1: RegNo = X86::ST1; break;
314 case 2: RegNo = X86::ST2; break;
315 case 3: RegNo = X86::ST3; break;
316 case 4: RegNo = X86::ST4; break;
317 case 5: RegNo = X86::ST5; break;
318 case 6: RegNo = X86::ST6; break;
319 case 7: RegNo = X86::ST7; break;
320 default: return Error(IntTok.getLoc(), "invalid stack index");
321 }
322
323 if (getParser().Lex().isNot(AsmToken::RParen))
324 return Error(Parser.getTok().getLoc(), "expected ')'");
325
326 EndLoc = Tok.getLoc();
327 Parser.Lex(); // Eat ')'
328 return false;
329 }
330
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +0000331 if (RegNo == 0)
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000332 return Error(Tok.getLoc(), "invalid register name");
333
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000334 EndLoc = Tok.getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000335 Parser.Lex(); // Eat identifier token.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000336 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000337}
338
Chris Lattnere4d457c2010-01-15 18:44:13 +0000339X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000340 switch (getLexer().getKind()) {
341 default:
Chris Lattnere4d457c2010-01-15 18:44:13 +0000342 return ParseMemOperand();
Chris Lattner977d91a2010-01-15 18:27:19 +0000343 case AsmToken::Percent: {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000344 // FIXME: if a segment register, this could either be just the seg reg, or
345 // the start of a memory operand.
Chris Lattner977d91a2010-01-15 18:27:19 +0000346 unsigned RegNo;
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000347 SMLoc Start, End;
348 if (ParseRegister(RegNo, Start, End)) return 0;
349 return X86Operand::CreateReg(RegNo, Start, End);
Chris Lattner977d91a2010-01-15 18:27:19 +0000350 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000351 case AsmToken::Dollar: {
352 // $42 -> immediate.
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000353 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanan34b4a462010-01-19 20:27:46 +0000354 Parser.Lex();
Daniel Dunbar6e966212009-08-31 08:08:38 +0000355 const MCExpr *Val;
Chris Lattner2ca686d2010-01-15 19:39:23 +0000356 if (getParser().ParseExpression(Val, End))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000357 return 0;
Chris Lattner284abb62010-01-15 19:28:38 +0000358 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000359 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000360 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000361}
362
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000363/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
Chris Lattnere4d457c2010-01-15 18:44:13 +0000364X86Operand *X86ATTAsmParser::ParseMemOperand() {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000365 SMLoc MemStart = Parser.getTok().getLoc();
Chris Lattner80cc03a2010-01-15 19:33:43 +0000366
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000367 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
368 unsigned SegReg = 0;
369
370 // We have to disambiguate a parenthesized expression "(4+5)" from the start
371 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattnerbded9a32010-01-24 01:07:33 +0000372 // only way to do this without lookahead is to eat the '(' and see what is
373 // after it.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000374 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000375 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner2ca686d2010-01-15 19:39:23 +0000376 SMLoc ExprEnd;
377 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000378
379 // After parsing the base expression we could either have a parenthesized
380 // memory address or not. If not, return now. If so, eat the (.
381 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000382 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000383 if (SegReg == 0)
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000384 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner80cc03a2010-01-15 19:33:43 +0000385 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000386 }
387
388 // Eat the '('.
Sean Callanan34b4a462010-01-19 20:27:46 +0000389 Parser.Lex();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000390 } else {
391 // Okay, we have a '('. We don't know if this is an expression or not, but
392 // so we have to eat the ( to see beyond it.
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000393 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000394 Parser.Lex(); // Eat the '('.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000395
Kevin Enderbye71842b2009-09-03 17:15:07 +0000396 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000397 // Nothing to do here, fall into the code below with the '(' part of the
398 // memory operand consumed.
399 } else {
Chris Lattner284abb62010-01-15 19:28:38 +0000400 SMLoc ExprEnd;
401
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000402 // It must be an parenthesized expression, parse it now.
Chris Lattner284abb62010-01-15 19:28:38 +0000403 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000404 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000405
406 // After parsing the base expression we could either have a parenthesized
407 // memory address or not. If not, return now. If so, eat the (.
408 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000409 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000410 if (SegReg == 0)
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000411 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner80cc03a2010-01-15 19:33:43 +0000412 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000413 }
414
415 // Eat the '('.
Sean Callanan34b4a462010-01-19 20:27:46 +0000416 Parser.Lex();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000417 }
418 }
419
420 // If we reached here, then we just ate the ( of the memory operand. Process
421 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000422 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000423
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000424 if (getLexer().is(AsmToken::Percent)) {
425 SMLoc L;
426 if (ParseRegister(BaseReg, L, L)) return 0;
427 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000428
429 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan34b4a462010-01-19 20:27:46 +0000430 Parser.Lex(); // Eat the comma.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000431
432 // Following the comma we should have either an index register, or a scale
433 // value. We don't support the later form, but we want to parse it
434 // correctly.
435 //
436 // Not that even though it would be completely consistent to support syntax
437 // like "1(%eax,,1)", the assembler doesn't.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000438 if (getLexer().is(AsmToken::Percent)) {
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000439 SMLoc L;
440 if (ParseRegister(IndexReg, L, L)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000441
442 if (getLexer().isNot(AsmToken::RParen)) {
443 // Parse the scale amount:
444 // ::= ',' [scale-expression]
Chris Lattnere4d457c2010-01-15 18:44:13 +0000445 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000446 Error(Parser.getTok().getLoc(),
Chris Lattnere4d457c2010-01-15 18:44:13 +0000447 "expected comma in scale expression");
448 return 0;
449 }
Sean Callanan34b4a462010-01-19 20:27:46 +0000450 Parser.Lex(); // Eat the comma.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000451
452 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000453 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000454
455 int64_t ScaleVal;
456 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000457 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000458
459 // Validate the scale amount.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000460 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
461 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
462 return 0;
463 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000464 Scale = (unsigned)ScaleVal;
465 }
466 }
467 } else if (getLexer().isNot(AsmToken::RParen)) {
468 // Otherwise we have the unsupported form of a scale amount without an
469 // index.
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000470 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000471
472 int64_t Value;
473 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000474 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000475
Chris Lattnere4d457c2010-01-15 18:44:13 +0000476 Error(Loc, "cannot have scale factor without index register");
477 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000478 }
479 }
480
481 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000482 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000483 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnere4d457c2010-01-15 18:44:13 +0000484 return 0;
485 }
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000486 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000487 Parser.Lex(); // Eat the ')'.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000488
Chris Lattner80cc03a2010-01-15 19:33:43 +0000489 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
490 MemStart, MemEnd);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000491}
492
Chris Lattner22f480d2010-01-14 22:21:20 +0000493bool X86ATTAsmParser::
494ParseInstruction(const StringRef &Name, SMLoc NameLoc,
495 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar6b3888b2010-02-10 21:19:28 +0000496 // FIXME: Hack to recognize "sal..." and "rep..." for now. We need a way to
497 // represent alternative syntaxes in the .td file, without requiring
498 // instruction duplication.
499 StringRef PatchedName = StringSwitch<StringRef>(Name)
500 .Case("sal", "shl")
501 .Case("salb", "shlb")
502 .Case("sall", "shll")
503 .Case("salq", "shlq")
504 .Case("salw", "shlw")
505 .Case("repe", "rep")
506 .Case("repz", "rep")
507 .Case("repnz", "repne")
508 .Default(Name);
509 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000510
511 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar76953672009-08-11 05:00:25 +0000512
513 // Parse '*' modifier.
514 if (getLexer().is(AsmToken::Star)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000515 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner284abb62010-01-15 19:28:38 +0000516 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanan34b4a462010-01-19 20:27:46 +0000517 Parser.Lex(); // Eat the star.
Daniel Dunbar76953672009-08-11 05:00:25 +0000518 }
519
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000520 // Read the first operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000521 if (X86Operand *Op = ParseOperand())
522 Operands.push_back(Op);
523 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000524 return true;
Chris Lattnere4d457c2010-01-15 18:44:13 +0000525
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000526 while (getLexer().is(AsmToken::Comma)) {
Sean Callanan34b4a462010-01-19 20:27:46 +0000527 Parser.Lex(); // Eat the comma.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000528
529 // Parse and remember the operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000530 if (X86Operand *Op = ParseOperand())
531 Operands.push_back(Op);
532 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000533 return true;
534 }
535 }
536
Chris Lattner22f480d2010-01-14 22:21:20 +0000537 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000538}
539
Kevin Enderbyae90d092009-09-10 20:51:44 +0000540bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
541 StringRef IDVal = DirectiveID.getIdentifier();
542 if (IDVal == ".word")
543 return ParseDirectiveWord(2, DirectiveID.getLoc());
544 return true;
545}
546
547/// ParseDirectiveWord
548/// ::= .word [ expression (, expression)* ]
549bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
550 if (getLexer().isNot(AsmToken::EndOfStatement)) {
551 for (;;) {
552 const MCExpr *Value;
553 if (getParser().ParseExpression(Value))
554 return true;
555
Chris Lattnera71dc602010-01-19 19:46:13 +0000556 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Kevin Enderbyae90d092009-09-10 20:51:44 +0000557
558 if (getLexer().is(AsmToken::EndOfStatement))
559 break;
560
561 // FIXME: Improve diagnostic.
562 if (getLexer().isNot(AsmToken::Comma))
563 return Error(L, "unexpected token in directive");
Sean Callanan34b4a462010-01-19 20:27:46 +0000564 Parser.Lex();
Kevin Enderbyae90d092009-09-10 20:51:44 +0000565 }
566 }
567
Sean Callanan34b4a462010-01-19 20:27:46 +0000568 Parser.Lex();
Kevin Enderbyae90d092009-09-10 20:51:44 +0000569 return false;
570}
571
Sean Callanan5bbbc372010-01-23 02:43:15 +0000572extern "C" void LLVMInitializeX86AsmLexer();
573
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000574// Force static initialization.
575extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000576 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
577 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanan5bbbc372010-01-23 02:43:15 +0000578 LLVMInitializeX86AsmLexer();
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000579}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000580
581#include "X86GenAsmMatcher.inc"