blob: c3e292eb7e935462a46aae2a83517e8e6dc77fa1 [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 Dunbara54716c2009-07-31 02:32:59 +000015#include "llvm/MC/MCInst.h"
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000016#include "llvm/MC/MCValue.h"
17#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 {
88 MCValue Val;
89 } Imm;
90
91 struct {
92 unsigned SegReg;
93 MCValue Disp;
94 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 Dunbarb7ddef12009-07-31 20:53:16 +0000110 const MCValue &getImm() const {
111 assert(Kind == Immediate && "Invalid access!");
112 return Imm.Val;
113 }
114
115 const MCValue &getMemDisp() const {
116 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
146 if (!getImm().isAbsolute())
147 return true;
148
149 int64_t Value = getImm().getConstant();
150 return Value == (int64_t) (int8_t) Value;
151 }
152
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000153 bool isMem() const { return Kind == Memory; }
154
155 bool isReg() const { return Kind == Register; }
156
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000157 void addRegOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000158 assert(N == 1 && "Invalid number of operands!");
159 Inst.addOperand(MCOperand::CreateReg(getReg()));
160 }
161
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000162 void addImmOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000163 assert(N == 1 && "Invalid number of operands!");
164 Inst.addOperand(MCOperand::CreateMCValue(getImm()));
165 }
166
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000167 void addImmSExt8Operands(MCInst &Inst, unsigned N) const {
Daniel Dunbar06d5cb62009-08-09 07:20:21 +0000168 // FIXME: Support user customization of the render method.
169 assert(N == 1 && "Invalid number of operands!");
170 Inst.addOperand(MCOperand::CreateMCValue(getImm()));
171 }
172
Daniel Dunbarb3413d82009-08-10 21:00:45 +0000173 void addMemOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000174 assert((N == 4 || N == 5) && "Invalid number of operands!");
175
176 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
177 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
178 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
179 Inst.addOperand(MCOperand::CreateMCValue(getMemDisp()));
180
181 // FIXME: What a hack.
182 if (N == 5)
183 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
184 }
185
186 static X86Operand CreateToken(StringRef Str) {
187 X86Operand Res;
188 Res.Kind = Token;
189 Res.Tok.Data = Str.data();
190 Res.Tok.Length = Str.size();
191 return Res;
192 }
193
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000194 static X86Operand CreateReg(unsigned RegNo) {
195 X86Operand Res;
196 Res.Kind = Register;
197 Res.Reg.RegNo = RegNo;
198 return Res;
199 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000200
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000201 static X86Operand CreateImm(MCValue Val) {
202 X86Operand Res;
203 Res.Kind = Immediate;
204 Res.Imm.Val = Val;
205 return Res;
206 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000207
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000208 static X86Operand CreateMem(unsigned SegReg, MCValue Disp, unsigned BaseReg,
209 unsigned IndexReg, unsigned Scale) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000210 // We should never just have a displacement, that would be an immediate.
211 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
212
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000213 // The scale should always be one of {1,2,4,8}.
214 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000215 "Invalid scale!");
216 X86Operand Res;
217 Res.Kind = Memory;
218 Res.Mem.SegReg = SegReg;
219 Res.Mem.Disp = Disp;
220 Res.Mem.BaseReg = BaseReg;
221 Res.Mem.IndexReg = IndexReg;
222 Res.Mem.Scale = Scale;
223 return Res;
224 }
225};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000226
Chris Lattnere54532b2009-07-29 06:33:53 +0000227} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000228
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000229
230bool X86ATTAsmParser::ParseRegister(X86Operand &Op) {
Chris Lattnere54532b2009-07-29 06:33:53 +0000231 const AsmToken &Tok = getLexer().getTok();
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000232 assert(Tok.is(AsmToken::Register) && "Invalid token kind!");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000233
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000234 // FIXME: Validate register for the current architecture; we have to do
235 // validation later, so maybe there is no need for this here.
236 unsigned RegNo;
237 assert(Tok.getString().startswith("%") && "Invalid register name!");
Daniel Dunbarb0e6abe2009-08-08 21:22:41 +0000238
239 RegNo = MatchRegisterName(Tok.getString().substr(1));
240 if (RegNo == 0)
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000241 return Error(Tok.getLoc(), "invalid register name");
242
243 Op = X86Operand::CreateReg(RegNo);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000244 getLexer().Lex(); // Eat register token.
245
246 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000247}
248
Daniel Dunbar78929e52009-07-20 20:01:54 +0000249bool X86ATTAsmParser::ParseOperand(X86Operand &Op) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000250 switch (getLexer().getKind()) {
251 default:
252 return ParseMemOperand(Op);
253 case AsmToken::Register:
254 // FIXME: if a segment register, this could either be just the seg reg, or
255 // the start of a memory operand.
256 return ParseRegister(Op);
257 case AsmToken::Dollar: {
258 // $42 -> immediate.
259 getLexer().Lex();
260 MCValue Val;
261 if (getParser().ParseRelocatableExpression(Val))
262 return true;
263 Op = X86Operand::CreateImm(Val);
264 return false;
265 }
Chris Lattnere54532b2009-07-29 06:33:53 +0000266 case AsmToken::Star:
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000267 getLexer().Lex(); // Eat the star.
268
269 if (getLexer().is(AsmToken::Register)) {
270 if (ParseRegister(Op))
271 return true;
272 } else if (ParseMemOperand(Op))
273 return true;
274
275 // FIXME: Note the '*' in the operand for use by the matcher.
276 return false;
277 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000278}
279
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000280/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
281bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) {
282 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
283 unsigned SegReg = 0;
284
285 // We have to disambiguate a parenthesized expression "(4+5)" from the start
286 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
287 // only way to do this without lookahead is to eat the ( and see what is after
288 // it.
289 MCValue Disp = MCValue::get(0, 0, 0);
290 if (getLexer().isNot(AsmToken::LParen)) {
291 if (getParser().ParseRelocatableExpression(Disp)) return true;
292
293 // After parsing the base expression we could either have a parenthesized
294 // memory address or not. If not, return now. If so, eat the (.
295 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000296 // Unless we have a segment register, treat this as an immediate.
297 if (SegReg)
298 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
299 else
300 Op = X86Operand::CreateImm(Disp);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000301 return false;
302 }
303
304 // Eat the '('.
305 getLexer().Lex();
306 } else {
307 // Okay, we have a '('. We don't know if this is an expression or not, but
308 // so we have to eat the ( to see beyond it.
309 getLexer().Lex(); // Eat the '('.
310
311 if (getLexer().is(AsmToken::Register) || getLexer().is(AsmToken::Comma)) {
312 // Nothing to do here, fall into the code below with the '(' part of the
313 // memory operand consumed.
314 } else {
315 // It must be an parenthesized expression, parse it now.
316 if (getParser().ParseParenRelocatableExpression(Disp))
317 return true;
318
319 // After parsing the base expression we could either have a parenthesized
320 // memory address or not. If not, return now. If so, eat the (.
321 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000322 // Unless we have a segment register, treat this as an immediate.
323 if (SegReg)
324 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
325 else
326 Op = X86Operand::CreateImm(Disp);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000327 return false;
328 }
329
330 // Eat the '('.
331 getLexer().Lex();
332 }
333 }
334
335 // If we reached here, then we just ate the ( of the memory operand. Process
336 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000337 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000338
339 if (getLexer().is(AsmToken::Register)) {
340 if (ParseRegister(Op))
341 return true;
342 BaseReg = Op.getReg();
343 }
344
345 if (getLexer().is(AsmToken::Comma)) {
346 getLexer().Lex(); // Eat the comma.
347
348 // Following the comma we should have either an index register, or a scale
349 // value. We don't support the later form, but we want to parse it
350 // correctly.
351 //
352 // Not that even though it would be completely consistent to support syntax
353 // like "1(%eax,,1)", the assembler doesn't.
354 if (getLexer().is(AsmToken::Register)) {
355 if (ParseRegister(Op))
356 return true;
357 IndexReg = Op.getReg();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000358
359 if (getLexer().isNot(AsmToken::RParen)) {
360 // Parse the scale amount:
361 // ::= ',' [scale-expression]
362 if (getLexer().isNot(AsmToken::Comma))
363 return true;
364 getLexer().Lex(); // Eat the comma.
365
366 if (getLexer().isNot(AsmToken::RParen)) {
367 SMLoc Loc = getLexer().getTok().getLoc();
368
369 int64_t ScaleVal;
370 if (getParser().ParseAbsoluteExpression(ScaleVal))
371 return true;
372
373 // Validate the scale amount.
374 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
375 return Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
376 Scale = (unsigned)ScaleVal;
377 }
378 }
379 } else if (getLexer().isNot(AsmToken::RParen)) {
380 // Otherwise we have the unsupported form of a scale amount without an
381 // index.
382 SMLoc Loc = getLexer().getTok().getLoc();
383
384 int64_t Value;
385 if (getParser().ParseAbsoluteExpression(Value))
386 return true;
387
388 return Error(Loc, "cannot have scale factor without index register");
389 }
390 }
391
392 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
393 if (getLexer().isNot(AsmToken::RParen))
394 return Error(getLexer().getTok().getLoc(),
395 "unexpected token in memory operand");
396 getLexer().Lex(); // Eat the ')'.
397
398 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
399 return false;
400}
401
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000402bool X86ATTAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) {
Daniel Dunbar62beebc2009-08-07 20:33:39 +0000403 SmallVector<X86Operand, 8> Operands;
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000404
405 Operands.push_back(X86Operand::CreateToken(Name));
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000406
Daniel Dunbara54716c2009-07-31 02:32:59 +0000407 SMLoc Loc = getLexer().getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000408 if (getLexer().isNot(AsmToken::EndOfStatement)) {
409 // Read the first operand.
410 Operands.push_back(X86Operand());
411 if (ParseOperand(Operands.back()))
412 return true;
413
414 while (getLexer().is(AsmToken::Comma)) {
415 getLexer().Lex(); // Eat the comma.
416
417 // Parse and remember the operand.
418 Operands.push_back(X86Operand());
419 if (ParseOperand(Operands.back()))
420 return true;
421 }
422 }
423
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000424 if (!MatchInstruction(Operands, Inst))
Daniel Dunbara54716c2009-07-31 02:32:59 +0000425 return false;
426
427 // FIXME: We should give nicer diagnostics about the exact failure.
428
429 // FIXME: For now we just treat unrecognized instructions as "warnings".
430 Warning(Loc, "unrecognized instruction");
431
432 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000433}
434
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000435// Force static initialization.
436extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000437 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
438 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000439}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000440
441#include "X86GenAsmMatcher.inc"