blob: d102b371d4aebfd82b18480727d11de31c663101 [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
Daniel Dunbar0b0441e2009-07-18 23:03:22 +000010#include "X86.h"
Daniel Dunbar78929e52009-07-20 20:01:54 +000011#include "llvm/ADT/SmallVector.h"
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000012#include "llvm/ADT/Twine.h"
Daniel Dunbard80432a2009-07-28 20:47:52 +000013#include "llvm/MC/MCAsmLexer.h"
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +000014#include "llvm/MC/MCAsmParser.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"
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000017#include "llvm/Support/SourceMgr.h"
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +000018#include "llvm/Target/TargetRegistry.h"
19#include "llvm/Target/TargetAsmParser.h"
20using namespace llvm;
21
22namespace {
Benjamin Kramer264834b2009-07-31 11:35:26 +000023struct X86Operand;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000024
25class X86ATTAsmParser : public TargetAsmParser {
26 MCAsmParser &Parser;
27
28private:
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000029 MCAsmParser &getParser() const { return Parser; }
30
31 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
32
33 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
34
35 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
36
37 bool ParseRegister(X86Operand &Op);
38
39 bool ParseOperand(X86Operand &Op);
40
41 bool ParseMemOperand(X86Operand &Op);
Daniel Dunbar85f1b392009-07-29 00:02:19 +000042
43 /// @name Auto-generated Match Functions
44 /// {
45
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000046 bool MatchInstruction(SmallVectorImpl<X86Operand> &Operands,
47 MCInst &Inst);
48
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +000049 /// MatchRegisterName - Match the given string to a register name, or 0 if
50 /// there is no match.
51 unsigned MatchRegisterName(const StringRef &Name);
Daniel Dunbar85f1b392009-07-29 00:02:19 +000052
53 /// }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000054
55public:
56 X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
57 : TargetAsmParser(T), Parser(_Parser) {}
58
59 virtual bool ParseInstruction(const StringRef &Name, MCInst &Inst);
60};
Chris Lattnere54532b2009-07-29 06:33:53 +000061
62} // end anonymous namespace
63
64
65namespace {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000066
67/// X86Operand - Instances of this class represent a parsed X86 machine
68/// instruction.
69struct X86Operand {
70 enum {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000071 Token,
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000072 Register,
73 Immediate,
74 Memory
75 } Kind;
76
77 union {
78 struct {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000079 const char *Data;
80 unsigned Length;
81 } Tok;
82
83 struct {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000084 unsigned RegNo;
85 } Reg;
86
87 struct {
Daniel Dunbar6e966212009-08-31 08:08:38 +000088 const MCExpr *Val;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000089 } Imm;
90
91 struct {
92 unsigned SegReg;
Daniel Dunbar6e966212009-08-31 08:08:38 +000093 const MCExpr *Disp;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000094 unsigned BaseReg;
95 unsigned IndexReg;
96 unsigned Scale;
97 } Mem;
Daniel Dunbar78929e52009-07-20 20:01:54 +000098 };
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +000099
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000100 StringRef getToken() const {
101 assert(Kind == Token && "Invalid access!");
102 return StringRef(Tok.Data, Tok.Length);
103 }
104
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000105 unsigned getReg() const {
106 assert(Kind == Register && "Invalid access!");
107 return Reg.RegNo;
108 }
Daniel Dunbard80432a2009-07-28 20:47:52 +0000109
Daniel Dunbar6e966212009-08-31 08:08:38 +0000110 const MCExpr *getImm() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000111 assert(Kind == Immediate && "Invalid access!");
112 return Imm.Val;
113 }
114
Daniel Dunbar6e966212009-08-31 08:08:38 +0000115 const MCExpr *getMemDisp() const {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000116 assert(Kind == Memory && "Invalid access!");
117 return Mem.Disp;
118 }
119 unsigned getMemSegReg() const {
120 assert(Kind == Memory && "Invalid access!");
121 return Mem.SegReg;
122 }
123 unsigned getMemBaseReg() const {
124 assert(Kind == Memory && "Invalid access!");
125 return Mem.BaseReg;
126 }
127 unsigned getMemIndexReg() const {
128 assert(Kind == Memory && "Invalid access!");
129 return Mem.IndexReg;
130 }
131 unsigned getMemScale() const {
132 assert(Kind == Memory && "Invalid access!");
133 return Mem.Scale;
134 }
135
Daniel Dunbar378bee92009-08-08 07:50:56 +0000136 bool isToken() const {return Kind == Token; }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000137
138 bool isImm() const { return Kind == Immediate; }
139
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000140 bool isImmSExt8() const {
141 // Accept immediates which fit in 8 bits when sign extended, and
142 // non-absolute immediates.
143 if (!isImm())
144 return false;
145
Daniel Dunbar6e966212009-08-31 08:08:38 +0000146 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getImm())) {
147 int64_t Value = CE->getValue();
148 return Value == (int64_t) (int8_t) Value;
149 }
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000150
Daniel Dunbar6e966212009-08-31 08:08:38 +0000151 return true;
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000152 }
153
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000154 bool isMem() const { return Kind == Memory; }
155
156 bool isReg() const { return Kind == Register; }
157
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000158 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000159 assert(N == 1 && "Invalid number of operands!");
160 Inst.addOperand(MCOperand::CreateReg(getReg()));
161 }
162
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000163 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000164 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000165 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000166 }
167
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000168 void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000169 // FIXME: Support user customization of the render method.
170 assert(N == 1 && "Invalid number of operands!");
Daniel Dunbar6e966212009-08-31 08:08:38 +0000171 Inst.addOperand(MCOperand::CreateExpr(getImm()));
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000172 }
173
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000174 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000175 assert((N == 4 || N == 5) && "Invalid number of operands!");
176
177 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
178 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
179 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
Daniel Dunbar6e966212009-08-31 08:08:38 +0000180 Inst.addOperand(MCOperand::CreateExpr(getMemDisp()));
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000181
182 // FIXME: What a hack.
183 if (N == 5)
184 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
185 }
186
187 static X86Operand CreateToken(StringRef Str) {
188 X86Operand Res;
189 Res.Kind = Token;
190 Res.Tok.Data = Str.data();
191 Res.Tok.Length = Str.size();
192 return Res;
193 }
194
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000195 static X86Operand CreateReg(unsigned RegNo) {
196 X86Operand Res;
197 Res.Kind = Register;
198 Res.Reg.RegNo = RegNo;
199 return Res;
200 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000201
Daniel Dunbar6e966212009-08-31 08:08:38 +0000202 static X86Operand CreateImm(const MCExpr *Val) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000203 X86Operand Res;
204 Res.Kind = Immediate;
205 Res.Imm.Val = Val;
206 return Res;
207 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000208
Daniel Dunbar6e966212009-08-31 08:08:38 +0000209 static X86Operand CreateMem(unsigned SegReg, const MCExpr *Disp,
210 unsigned BaseReg, unsigned IndexReg,
211 unsigned Scale) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000212 // We should never just have a displacement, that would be an immediate.
213 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
214
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000215 // The scale should always be one of {1,2,4,8}.
216 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000217 "Invalid scale!");
218 X86Operand Res;
219 Res.Kind = Memory;
220 Res.Mem.SegReg = SegReg;
221 Res.Mem.Disp = Disp;
222 Res.Mem.BaseReg = BaseReg;
223 Res.Mem.IndexReg = IndexReg;
224 Res.Mem.Scale = Scale;
225 return Res;
226 }
227};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000228
Chris Lattnere54532b2009-07-29 06:33:53 +0000229} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000230
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000231
232bool X86ATTAsmParser::ParseRegister(X86Operand &Op) {
Kevin Enderbye71842b2009-09-03 17:15:07 +0000233 const AsmToken &TokPercent = getLexer().getTok();
234 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
235 getLexer().Lex(); // Eat percent token.
236
Chris Lattnere54532b2009-07-29 06:33:53 +0000237 const AsmToken &Tok = getLexer().getTok();
Kevin Enderbye71842b2009-09-03 17:15:07 +0000238 assert(TokPercent.is(AsmToken::Identifier) && "Invalid token kind!");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000239
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000240 // FIXME: Validate register for the current architecture; we have to do
241 // validation later, so maybe there is no need for this here.
242 unsigned RegNo;
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +0000243
Kevin Enderbye71842b2009-09-03 17:15:07 +0000244 RegNo = MatchRegisterName(Tok.getString());
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +0000245 if (RegNo == 0)
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000246 return Error(Tok.getLoc(), "invalid register name");
247
248 Op = X86Operand::CreateReg(RegNo);
Kevin Enderbye71842b2009-09-03 17:15:07 +0000249 getLexer().Lex(); // Eat identifier token.
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000250
251 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000252}
253
Daniel Dunbar78929e52009-07-20 20:01:54 +0000254bool X86ATTAsmParser::ParseOperand(X86Operand &Op) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000255 switch (getLexer().getKind()) {
256 default:
257 return ParseMemOperand(Op);
Kevin Enderbye71842b2009-09-03 17:15:07 +0000258 case AsmToken::Percent:
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000259 // FIXME: if a segment register, this could either be just the seg reg, or
260 // the start of a memory operand.
261 return ParseRegister(Op);
262 case AsmToken::Dollar: {
263 // $42 -> immediate.
264 getLexer().Lex();
Daniel Dunbar6e966212009-08-31 08:08:38 +0000265 const MCExpr *Val;
266 if (getParser().ParseExpression(Val))
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000267 return true;
268 Op = X86Operand::CreateImm(Val);
269 return false;
270 }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000271 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000272}
273
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000274/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
275bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) {
276 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
277 unsigned SegReg = 0;
278
279 // We have to disambiguate a parenthesized expression "(4+5)" from the start
280 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
281 // only way to do this without lookahead is to eat the ( and see what is after
282 // it.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000283 const MCExpr *Disp = MCConstantExpr::Create(0, getParser().getContext());
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000284 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar6e966212009-08-31 08:08:38 +0000285 if (getParser().ParseExpression(Disp)) return true;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000286
287 // After parsing the base expression we could either have a parenthesized
288 // memory address or not. If not, return now. If so, eat the (.
289 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000290 // Unless we have a segment register, treat this as an immediate.
291 if (SegReg)
292 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
293 else
294 Op = X86Operand::CreateImm(Disp);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000295 return false;
296 }
297
298 // Eat the '('.
299 getLexer().Lex();
300 } else {
301 // Okay, we have a '('. We don't know if this is an expression or not, but
302 // so we have to eat the ( to see beyond it.
303 getLexer().Lex(); // Eat the '('.
304
Kevin Enderbye71842b2009-09-03 17:15:07 +0000305 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000306 // Nothing to do here, fall into the code below with the '(' part of the
307 // memory operand consumed.
308 } else {
309 // It must be an parenthesized expression, parse it now.
Daniel Dunbar6e966212009-08-31 08:08:38 +0000310 if (getParser().ParseParenExpression(Disp))
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000311 return true;
312
313 // After parsing the base expression we could either have a parenthesized
314 // memory address or not. If not, return now. If so, eat the (.
315 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000316 // Unless we have a segment register, treat this as an immediate.
317 if (SegReg)
318 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
319 else
320 Op = X86Operand::CreateImm(Disp);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000321 return false;
322 }
323
324 // Eat the '('.
325 getLexer().Lex();
326 }
327 }
328
329 // If we reached here, then we just ate the ( of the memory operand. Process
330 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000331 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000332
Kevin Enderbye71842b2009-09-03 17:15:07 +0000333 if (getLexer().is(AsmToken::Percent)) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000334 if (ParseRegister(Op))
335 return true;
336 BaseReg = Op.getReg();
337 }
338
339 if (getLexer().is(AsmToken::Comma)) {
340 getLexer().Lex(); // Eat the comma.
341
342 // Following the comma we should have either an index register, or a scale
343 // value. We don't support the later form, but we want to parse it
344 // correctly.
345 //
346 // Not that even though it would be completely consistent to support syntax
347 // like "1(%eax,,1)", the assembler doesn't.
Kevin Enderbye71842b2009-09-03 17:15:07 +0000348 if (getLexer().is(AsmToken::Percent)) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000349 if (ParseRegister(Op))
350 return true;
351 IndexReg = Op.getReg();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000352
353 if (getLexer().isNot(AsmToken::RParen)) {
354 // Parse the scale amount:
355 // ::= ',' [scale-expression]
356 if (getLexer().isNot(AsmToken::Comma))
357 return true;
358 getLexer().Lex(); // Eat the comma.
359
360 if (getLexer().isNot(AsmToken::RParen)) {
361 SMLoc Loc = getLexer().getTok().getLoc();
362
363 int64_t ScaleVal;
364 if (getParser().ParseAbsoluteExpression(ScaleVal))
365 return true;
366
367 // Validate the scale amount.
368 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
369 return Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
370 Scale = (unsigned)ScaleVal;
371 }
372 }
373 } else if (getLexer().isNot(AsmToken::RParen)) {
374 // Otherwise we have the unsupported form of a scale amount without an
375 // index.
376 SMLoc Loc = getLexer().getTok().getLoc();
377
378 int64_t Value;
379 if (getParser().ParseAbsoluteExpression(Value))
380 return true;
381
382 return Error(Loc, "cannot have scale factor without index register");
383 }
384 }
385
386 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
387 if (getLexer().isNot(AsmToken::RParen))
388 return Error(getLexer().getTok().getLoc(),
389 "unexpected token in memory operand");
390 getLexer().Lex(); // Eat the ')'.
391
392 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
393 return false;
394}
395
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000396bool X86ATTAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) {
Daniel Dunbar62beebc2009-08-07 20:33:39 +0000397 SmallVector<X86Operand, 8> Operands;
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000398
399 Operands.push_back(X86Operand::CreateToken(Name));
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000400
Daniel Dunbara54716c2009-07-31 02:32:59 +0000401 SMLoc Loc = getLexer().getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000402 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Daniel Dunbar76953672009-08-11 05:00:25 +0000403
404 // Parse '*' modifier.
405 if (getLexer().is(AsmToken::Star)) {
406 getLexer().Lex(); // Eat the star.
407 Operands.push_back(X86Operand::CreateToken("*"));
408 }
409
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000410 // Read the first operand.
411 Operands.push_back(X86Operand());
412 if (ParseOperand(Operands.back()))
413 return true;
414
415 while (getLexer().is(AsmToken::Comma)) {
416 getLexer().Lex(); // Eat the comma.
417
418 // Parse and remember the operand.
419 Operands.push_back(X86Operand());
420 if (ParseOperand(Operands.back()))
421 return true;
422 }
423 }
424
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000425 if (!MatchInstruction(Operands, Inst))
Daniel Dunbara54716c2009-07-31 02:32:59 +0000426 return false;
427
428 // FIXME: We should give nicer diagnostics about the exact failure.
429
Daniel Dunbar575db962009-08-14 03:48:55 +0000430 Error(Loc, "unrecognized instruction");
431 return true;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000432}
433
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000434// Force static initialization.
435extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000436 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
437 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000438}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000439
440#include "X86GenAsmMatcher.inc"