blob: acf497a820864d1b918559c848f8b010828175c4 [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"
Kevin Enderbyae90d092009-09-10 20:51:44 +000014#include "llvm/MC/MCStreamer.h"
Daniel Dunbar6e966212009-08-31 08:08:38 +000015#include "llvm/MC/MCExpr.h"
Daniel Dunbara54716c2009-07-31 02:32:59 +000016#include "llvm/MC/MCInst.h"
Chris Lattner291d6692010-01-22 01:44:57 +000017#include "llvm/MC/MCParser/MCAsmLexer.h"
18#include "llvm/MC/MCParser/MCAsmParser.h"
19#include "llvm/MC/MCParser/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 Dunbar85f1b392009-07-29 00:02:19 +000053 /// }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000054
55public:
56 X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
57 : TargetAsmParser(T), Parser(_Parser) {}
58
Chris Lattnerf66e4eb2010-01-14 21:32:45 +000059 virtual bool ParseInstruction(const StringRef &Name, SMLoc NameLoc,
Chris Lattner22f480d2010-01-14 22:21:20 +000060 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyae90d092009-09-10 20:51:44 +000061
62 virtual bool ParseDirective(AsmToken DirectiveID);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000063};
Chris Lattnere54532b2009-07-29 06:33:53 +000064
65} // end anonymous namespace
66
Sean Callananef372de2010-01-23 00:40:33 +000067/// @name Auto-generated Match Functions
68/// {
69
Chris Lattner8b382002010-02-09 00:34:28 +000070static unsigned MatchRegisterName(StringRef Name);
Sean Callananef372de2010-01-23 00:40:33 +000071
72/// }
Chris Lattnere54532b2009-07-29 06:33:53 +000073
74namespace {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000075
76/// X86Operand - Instances of this class represent a parsed X86 machine
77/// instruction.
Chris Lattner0c119a72010-01-14 21:20:55 +000078struct X86Operand : public MCParsedAsmOperand {
Chris Lattner61cd2c32010-01-15 19:06:59 +000079 enum KindTy {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000080 Token,
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000081 Register,
82 Immediate,
83 Memory
84 } Kind;
85
Chris Lattnerc2fc91a2010-01-15 18:51:29 +000086 SMLoc StartLoc, EndLoc;
87
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000088 union {
89 struct {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000090 const char *Data;
91 unsigned Length;
92 } Tok;
93
94 struct {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000095 unsigned RegNo;
96 } Reg;
97
98 struct {
Daniel Dunbar6e966212009-08-31 08:08:38 +000099 const MCExpr *Val;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000100 } Imm;
101
102 struct {
103 unsigned SegReg;
Daniel Dunbar6e966212009-08-31 08:08:38 +0000104 const MCExpr *Disp;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000105 unsigned BaseReg;
106 unsigned IndexReg;
107 unsigned Scale;
108 } Mem;
Daniel Dunbar78929e52009-07-20 20:01:54 +0000109 };
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000110
Chris Lattner80cc03a2010-01-15 19:33:43 +0000111 X86Operand(KindTy K, SMLoc Start, SMLoc End)
Chris Lattner61cd2c32010-01-15 19:06:59 +0000112 : Kind(K), StartLoc(Start), EndLoc(End) {}
113
114 /// getStartLoc - Get the location of the first token of this operand.
115 SMLoc getStartLoc() const { return StartLoc; }
116 /// getEndLoc - Get the location of the last token of this operand.
117 SMLoc getEndLoc() const { return EndLoc; }
118
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000119 StringRef getToken() const {
120 assert(Kind == Token && "Invalid access!");
121 return StringRef(Tok.Data, Tok.Length);
122 }
123
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000124 unsigned getReg() const {
125 assert(Kind == Register && "Invalid access!");
126 return Reg.RegNo;
127 }
Daniel Dunbard80432a2009-07-28 20:47:52 +0000128
Daniel Dunbar6e966212009-08-31 08:08:38 +0000129 const MCExpr *getImm() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000130 assert(Kind == Immediate && "Invalid access!");
131 return Imm.Val;
132 }
133
Daniel Dunbar6e966212009-08-31 08:08:38 +0000134 const MCExpr *getMemDisp() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000135 assert(Kind == Memory && "Invalid access!");
136 return Mem.Disp;
137 }
138 unsigned getMemSegReg() const {
139 assert(Kind == Memory && "Invalid access!");
140 return Mem.SegReg;
141 }
142 unsigned getMemBaseReg() const {
143 assert(Kind == Memory && "Invalid access!");
144 return Mem.BaseReg;
145 }
146 unsigned getMemIndexReg() const {
147 assert(Kind == Memory && "Invalid access!");
148 return Mem.IndexReg;
149 }
150 unsigned getMemScale() const {
151 assert(Kind == Memory && "Invalid access!");
152 return Mem.Scale;
153 }
154
Daniel Dunbar378bee92009-08-08 07:50:56 +0000155 bool isToken() const {return Kind == Token; }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000156
157 bool isImm() const { return Kind == Immediate; }
158
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000159 bool isImmSExt8() const {
160 // Accept immediates which fit in 8 bits when sign extended, and
161 // non-absolute immediates.
162 if (!isImm())
163 return false;
164
Daniel Dunbar6e966212009-08-31 08:08:38 +0000165 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
166 int64_t Value = CE->getValue();
167 return Value == (int64_t) (int8_t) Value;
168 }
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000169
Daniel Dunbar6e966212009-08-31 08:08:38 +0000170 return true;
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000171 }
172
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000173 bool isMem() const { return Kind == Memory; }
174
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000175 bool isAbsMem() const {
176 return Kind == Memory && !getMemSegReg() && !getMemBaseReg() &&
Daniel Dunbar08a1aae2010-02-02 21:44:16 +0000177 !getMemIndexReg() && getMemScale() == 1;
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000178 }
179
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000180 bool isNoSegMem() const {
181 return Kind == Memory && !getMemSegReg();
182 }
183
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000184 bool isReg() const { return Kind == Register; }
185
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000186 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000187 assert(N == 1 && "Invalid number of operands!");
188 Inst.addOperand(MCOperand::CreateReg(getReg()));
189 }
190
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000191 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000192 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000193 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000194 }
195
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000196 void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000197 // FIXME: Support user customization of the render method.
198 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000199 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000200 }
201
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000202 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000203 assert((N == 5) && "Invalid number of operands!");
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000204 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
205 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
206 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar6e966212009-08-31 08:08:38 +0000207 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000208 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
209 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000210
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000211 void addAbsMemOperands(MCInst &Inst, unsigned N) const {
212 assert((N == 1) && "Invalid number of operands!");
213 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
214 }
215
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000216 void addNoSegMemOperands(MCInst &Inst, unsigned N) const {
217 assert((N == 4) && "Invalid number of operands!");
Daniel Dunbarfc1b32a2010-01-30 00:24:00 +0000218 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
219 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
220 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
221 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000222 }
223
Chris Lattner284abb62010-01-15 19:28:38 +0000224 static X86Operand *CreateToken(StringRef Str, SMLoc Loc) {
225 X86Operand *Res = new X86Operand(Token, Loc, Loc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000226 Res->Tok.Data = Str.data();
227 Res->Tok.Length = Str.size();
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000228 return Res;
229 }
230
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000231 static X86Operand *CreateReg(unsigned RegNo, SMLoc StartLoc, SMLoc EndLoc) {
Chris Lattner61cd2c32010-01-15 19:06:59 +0000232 X86Operand *Res = new X86Operand(Register, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000233 Res->Reg.RegNo = RegNo;
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000234 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000235 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000236
Chris Lattner284abb62010-01-15 19:28:38 +0000237 static X86Operand *CreateImm(const MCExpr *Val, SMLoc StartLoc, SMLoc EndLoc){
238 X86Operand *Res = new X86Operand(Immediate, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000239 Res->Imm.Val = Val;
240 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000241 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000242
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000243 /// Create an absolute memory operand.
244 static X86Operand *CreateMem(const MCExpr *Disp, SMLoc StartLoc,
245 SMLoc EndLoc) {
246 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
247 Res->Mem.SegReg = 0;
248 Res->Mem.Disp = Disp;
249 Res->Mem.BaseReg = 0;
250 Res->Mem.IndexReg = 0;
Daniel Dunbar08a1aae2010-02-02 21:44:16 +0000251 Res->Mem.Scale = 1;
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000252 return Res;
253 }
254
255 /// Create a generalized memory operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000256 static X86Operand *CreateMem(unsigned SegReg, const MCExpr *Disp,
257 unsigned BaseReg, unsigned IndexReg,
Chris Lattner80cc03a2010-01-15 19:33:43 +0000258 unsigned Scale, SMLoc StartLoc, SMLoc EndLoc) {
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000259 // We should never just have a displacement, that should be parsed as an
260 // absolute memory operand.
Daniel Dunbar24091712009-07-31 22:22:54 +0000261 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
262
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000263 // The scale should always be one of {1,2,4,8}.
264 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000265 "Invalid scale!");
Chris Lattner80cc03a2010-01-15 19:33:43 +0000266 X86Operand *Res = new X86Operand(Memory, StartLoc, EndLoc);
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000267 Res->Mem.SegReg = SegReg;
268 Res->Mem.Disp = Disp;
269 Res->Mem.BaseReg = BaseReg;
270 Res->Mem.IndexReg = IndexReg;
271 Res->Mem.Scale = Scale;
272 return Res;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000273 }
274};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000275
Chris Lattnere54532b2009-07-29 06:33:53 +0000276} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000277
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000278
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000279bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
280 SMLoc &StartLoc, SMLoc &EndLoc) {
Chris Lattner977d91a2010-01-15 18:27:19 +0000281 RegNo = 0;
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000282 const AsmToken &TokPercent = Parser.getTok();
Kevin Enderbye71842b2009-09-03 17:15:07 +0000283 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000284 StartLoc = TokPercent.getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000285 Parser.Lex(); // Eat percent token.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000286
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000287 const AsmToken &Tok = Parser.getTok();
Kevin Enderby01b83cf2009-09-16 17:18:29 +0000288 if (Tok.isNot(AsmToken::Identifier))
289 return Error(Tok.getLoc(), "invalid register name");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000290
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000291 // FIXME: Validate register for the current architecture; we have to do
292 // validation later, so maybe there is no need for this here.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000293 RegNo = MatchRegisterName(Tok.getString());
Chris Lattner8b382002010-02-09 00:34:28 +0000294
Chris Lattner57b278e2010-02-09 00:49:22 +0000295 // Parse %st(1) and "%st" as "%st(0)"
296 if (RegNo == 0 && Tok.getString() == "st") {
297 RegNo = X86::ST0;
298 EndLoc = Tok.getLoc();
299 Parser.Lex(); // Eat 'st'
300
301 // Check to see if we have '(4)' after %st.
302 if (getLexer().isNot(AsmToken::LParen))
303 return false;
304 // Lex the paren.
305 getParser().Lex();
306
307 const AsmToken &IntTok = Parser.getTok();
308 if (IntTok.isNot(AsmToken::Integer))
309 return Error(IntTok.getLoc(), "expected stack index");
310 switch (IntTok.getIntVal()) {
311 case 0: RegNo = X86::ST0; break;
312 case 1: RegNo = X86::ST1; break;
313 case 2: RegNo = X86::ST2; break;
314 case 3: RegNo = X86::ST3; break;
315 case 4: RegNo = X86::ST4; break;
316 case 5: RegNo = X86::ST5; break;
317 case 6: RegNo = X86::ST6; break;
318 case 7: RegNo = X86::ST7; break;
319 default: return Error(IntTok.getLoc(), "invalid stack index");
320 }
321
322 if (getParser().Lex().isNot(AsmToken::RParen))
323 return Error(Parser.getTok().getLoc(), "expected ')'");
324
325 EndLoc = Tok.getLoc();
326 Parser.Lex(); // Eat ')'
327 return false;
328 }
329
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +0000330 if (RegNo == 0)
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000331 return Error(Tok.getLoc(), "invalid register name");
332
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000333 EndLoc = Tok.getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000334 Parser.Lex(); // Eat identifier token.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000335 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000336}
337
Chris Lattnere4d457c2010-01-15 18:44:13 +0000338X86Operand *X86ATTAsmParser::ParseOperand() {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000339 switch (getLexer().getKind()) {
340 default:
Chris Lattnere4d457c2010-01-15 18:44:13 +0000341 return ParseMemOperand();
Chris Lattner977d91a2010-01-15 18:27:19 +0000342 case AsmToken::Percent: {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000343 // FIXME: if a segment register, this could either be just the seg reg, or
344 // the start of a memory operand.
Chris Lattner977d91a2010-01-15 18:27:19 +0000345 unsigned RegNo;
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000346 SMLoc Start, End;
347 if (ParseRegister(RegNo, Start, End)) return 0;
348 return X86Operand::CreateReg(RegNo, Start, End);
Chris Lattner977d91a2010-01-15 18:27:19 +0000349 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000350 case AsmToken::Dollar: {
351 // $42 -> immediate.
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000352 SMLoc Start = Parser.getTok().getLoc(), End;
Sean Callanan34b4a462010-01-19 20:27:46 +0000353 Parser.Lex();
Daniel Dunbar6e966212009-08-31 08:08:38 +0000354 const MCExpr *Val;
Chris Lattner2ca686d2010-01-15 19:39:23 +0000355 if (getParser().ParseExpression(Val, End))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000356 return 0;
Chris Lattner284abb62010-01-15 19:28:38 +0000357 return X86Operand::CreateImm(Val, Start, End);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000358 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000359 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000360}
361
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000362/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
Chris Lattnere4d457c2010-01-15 18:44:13 +0000363X86Operand *X86ATTAsmParser::ParseMemOperand() {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000364 SMLoc MemStart = Parser.getTok().getLoc();
Chris Lattner80cc03a2010-01-15 19:33:43 +0000365
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000366 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
367 unsigned SegReg = 0;
368
369 // We have to disambiguate a parenthesized expression "(4+5)" from the start
370 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
Chris Lattnerbded9a32010-01-24 01:07:33 +0000371 // only way to do this without lookahead is to eat the '(' and see what is
372 // after it.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000373 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000374 if (getLexer().isNot(AsmToken::LParen)) {
Chris Lattner2ca686d2010-01-15 19:39:23 +0000375 SMLoc ExprEnd;
376 if (getParser().ParseExpression(Disp, ExprEnd)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000377
378 // After parsing the base expression we could either have a parenthesized
379 // memory address or not. If not, return now. If so, eat the (.
380 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000381 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000382 if (SegReg == 0)
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000383 return X86Operand::CreateMem(Disp, MemStart, ExprEnd);
Chris Lattner80cc03a2010-01-15 19:33:43 +0000384 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000385 }
386
387 // Eat the '('.
Sean Callanan34b4a462010-01-19 20:27:46 +0000388 Parser.Lex();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000389 } else {
390 // Okay, we have a '('. We don't know if this is an expression or not, but
391 // so we have to eat the ( to see beyond it.
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000392 SMLoc LParenLoc = Parser.getTok().getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000393 Parser.Lex(); // Eat the '('.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000394
Kevin Enderbye71842b2009-09-03 17:15:07 +0000395 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000396 // Nothing to do here, fall into the code below with the '(' part of the
397 // memory operand consumed.
398 } else {
Chris Lattner284abb62010-01-15 19:28:38 +0000399 SMLoc ExprEnd;
400
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000401 // It must be an parenthesized expression, parse it now.
Chris Lattner284abb62010-01-15 19:28:38 +0000402 if (getParser().ParseParenExpression(Disp, ExprEnd))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000403 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000404
405 // After parsing the base expression we could either have a parenthesized
406 // memory address or not. If not, return now. If so, eat the (.
407 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000408 // Unless we have a segment register, treat this as an immediate.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000409 if (SegReg == 0)
Daniel Dunbar4dcefd72010-01-30 01:02:48 +0000410 return X86Operand::CreateMem(Disp, LParenLoc, ExprEnd);
Chris Lattner80cc03a2010-01-15 19:33:43 +0000411 return X86Operand::CreateMem(SegReg, Disp, 0, 0, 1, MemStart, ExprEnd);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000412 }
413
414 // Eat the '('.
Sean Callanan34b4a462010-01-19 20:27:46 +0000415 Parser.Lex();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000416 }
417 }
418
419 // If we reached here, then we just ate the ( of the memory operand. Process
420 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000421 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000422
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000423 if (getLexer().is(AsmToken::Percent)) {
424 SMLoc L;
425 if (ParseRegister(BaseReg, L, L)) return 0;
426 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000427
428 if (getLexer().is(AsmToken::Comma)) {
Sean Callanan34b4a462010-01-19 20:27:46 +0000429 Parser.Lex(); // Eat the comma.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000430
431 // Following the comma we should have either an index register, or a scale
432 // value. We don't support the later form, but we want to parse it
433 // correctly.
434 //
435 // Not that even though it would be completely consistent to support syntax
436 // like "1(%eax,,1)", the assembler doesn't.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000437 if (getLexer().is(AsmToken::Percent)) {
Chris Lattnerc2fc91a2010-01-15 18:51:29 +0000438 SMLoc L;
439 if (ParseRegister(IndexReg, L, L)) return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000440
441 if (getLexer().isNot(AsmToken::RParen)) {
442 // Parse the scale amount:
443 // ::= ',' [scale-expression]
Chris Lattnere4d457c2010-01-15 18:44:13 +0000444 if (getLexer().isNot(AsmToken::Comma)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000445 Error(Parser.getTok().getLoc(),
Chris Lattnere4d457c2010-01-15 18:44:13 +0000446 "expected comma in scale expression");
447 return 0;
448 }
Sean Callanan34b4a462010-01-19 20:27:46 +0000449 Parser.Lex(); // Eat the comma.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000450
451 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000452 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000453
454 int64_t ScaleVal;
455 if (getParser().ParseAbsoluteExpression(ScaleVal))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000456 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000457
458 // Validate the scale amount.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000459 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8){
460 Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
461 return 0;
462 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000463 Scale = (unsigned)ScaleVal;
464 }
465 }
466 } else if (getLexer().isNot(AsmToken::RParen)) {
467 // Otherwise we have the unsupported form of a scale amount without an
468 // index.
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000469 SMLoc Loc = Parser.getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000470
471 int64_t Value;
472 if (getParser().ParseAbsoluteExpression(Value))
Chris Lattnere4d457c2010-01-15 18:44:13 +0000473 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000474
Chris Lattnere4d457c2010-01-15 18:44:13 +0000475 Error(Loc, "cannot have scale factor without index register");
476 return 0;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000477 }
478 }
479
480 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000481 if (getLexer().isNot(AsmToken::RParen)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000482 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
Chris Lattnere4d457c2010-01-15 18:44:13 +0000483 return 0;
484 }
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000485 SMLoc MemEnd = Parser.getTok().getLoc();
Sean Callanan34b4a462010-01-19 20:27:46 +0000486 Parser.Lex(); // Eat the ')'.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000487
Chris Lattner80cc03a2010-01-15 19:33:43 +0000488 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
489 MemStart, MemEnd);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000490}
491
Chris Lattner22f480d2010-01-14 22:21:20 +0000492bool X86ATTAsmParser::
493ParseInstruction(const StringRef &Name, SMLoc NameLoc,
494 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbarfed66ee2010-02-02 23:46:47 +0000495 // FIXME: Hack to recognize "sal..." for now. We need a way to represent
496 // alternative syntaxes in the .td file, without requiring instruction
497 // duplication.
498 if (Name.startswith("sal")) {
499 std::string Tmp = "shl" + Name.substr(3).str();
500 Operands.push_back(X86Operand::CreateToken(Tmp, NameLoc));
Kevin Enderby3aa67c02010-02-03 21:04:42 +0000501 } else {
502 // FIXME: This is a hack. We eventually want to add a general pattern
503 // mechanism to be used in the table gen file for these assembly names that
504 // use the same opcodes. Also we should only allow the "alternate names"
505 // for rep and repne with the instructions they can only appear with.
506 StringRef PatchedName = Name;
507 if (Name == "repe" || Name == "repz")
508 PatchedName = "rep";
509 else if (Name == "repnz")
510 PatchedName = "repne";
511 Operands.push_back(X86Operand::CreateToken(PatchedName, NameLoc));
512 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000513
514 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar76953672009-08-11 05:00:25 +0000515
516 // Parse '*' modifier.
517 if (getLexer().is(AsmToken::Star)) {
Sean Callanan3cc5fa02010-01-19 21:44:56 +0000518 SMLoc Loc = Parser.getTok().getLoc();
Chris Lattner284abb62010-01-15 19:28:38 +0000519 Operands.push_back(X86Operand::CreateToken("*", Loc));
Sean Callanan34b4a462010-01-19 20:27:46 +0000520 Parser.Lex(); // Eat the star.
Daniel Dunbar76953672009-08-11 05:00:25 +0000521 }
522
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000523 // Read the first operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000524 if (X86Operand *Op = ParseOperand())
525 Operands.push_back(Op);
526 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000527 return true;
Chris Lattnere4d457c2010-01-15 18:44:13 +0000528
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000529 while (getLexer().is(AsmToken::Comma)) {
Sean Callanan34b4a462010-01-19 20:27:46 +0000530 Parser.Lex(); // Eat the comma.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000531
532 // Parse and remember the operand.
Chris Lattnere4d457c2010-01-15 18:44:13 +0000533 if (X86Operand *Op = ParseOperand())
534 Operands.push_back(Op);
535 else
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000536 return true;
537 }
538 }
539
Chris Lattner22f480d2010-01-14 22:21:20 +0000540 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000541}
542
Kevin Enderbyae90d092009-09-10 20:51:44 +0000543bool X86ATTAsmParser::ParseDirective(AsmToken DirectiveID) {
544 StringRef IDVal = DirectiveID.getIdentifier();
545 if (IDVal == ".word")
546 return ParseDirectiveWord(2, DirectiveID.getLoc());
547 return true;
548}
549
550/// ParseDirectiveWord
551/// ::= .word [ expression (, expression)* ]
552bool X86ATTAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
553 if (getLexer().isNot(AsmToken::EndOfStatement)) {
554 for (;;) {
555 const MCExpr *Value;
556 if (getParser().ParseExpression(Value))
557 return true;
558
Chris Lattnera71dc602010-01-19 19:46:13 +0000559 getParser().getStreamer().EmitValue(Value, Size, 0 /*addrspace*/);
Kevin Enderbyae90d092009-09-10 20:51:44 +0000560
561 if (getLexer().is(AsmToken::EndOfStatement))
562 break;
563
564 // FIXME: Improve diagnostic.
565 if (getLexer().isNot(AsmToken::Comma))
566 return Error(L, "unexpected token in directive");
Sean Callanan34b4a462010-01-19 20:27:46 +0000567 Parser.Lex();
Kevin Enderbyae90d092009-09-10 20:51:44 +0000568 }
569 }
570
Sean Callanan34b4a462010-01-19 20:27:46 +0000571 Parser.Lex();
Kevin Enderbyae90d092009-09-10 20:51:44 +0000572 return false;
573}
574
Sean Callanan5bbbc372010-01-23 02:43:15 +0000575extern "C" void LLVMInitializeX86AsmLexer();
576
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000577// Force static initialization.
578extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000579 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
580 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Sean Callanan5bbbc372010-01-23 02:43:15 +0000581 LLVMInitializeX86AsmLexer();
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000582}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000583
584#include "X86GenAsmMatcher.inc"