blob: db52c118ea350a40763ffdfe2253655d09707325 [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:
29 bool MatchInstruction(const StringRef &Name,
Chris Lattnereb46b492009-07-29 06:29:53 +000030 SmallVectorImpl<X86Operand> &Operands,
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000031 MCInst &Inst);
32
33 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
41 bool ParseRegister(X86Operand &Op);
42
43 bool ParseOperand(X86Operand &Op);
44
45 bool ParseMemOperand(X86Operand &Op);
Daniel Dunbar85f1b392009-07-29 00:02:19 +000046
47 /// @name Auto-generated Match Functions
48 /// {
49
50 bool MatchRegisterName(const StringRef &Name, unsigned &RegNo);
51
52 /// }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000053
54public:
55 X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
56 : TargetAsmParser(T), Parser(_Parser) {}
57
58 virtual bool ParseInstruction(const StringRef &Name, MCInst &Inst);
59};
Chris Lattnere54532b2009-07-29 06:33:53 +000060
61} // end anonymous namespace
62
63
64namespace {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000065
66/// X86Operand - Instances of this class represent a parsed X86 machine
67/// instruction.
68struct X86Operand {
69 enum {
70 Register,
71 Immediate,
72 Memory
73 } Kind;
74
75 union {
76 struct {
77 unsigned RegNo;
78 } Reg;
79
80 struct {
81 MCValue Val;
82 } Imm;
83
84 struct {
85 unsigned SegReg;
86 MCValue Disp;
87 unsigned BaseReg;
88 unsigned IndexReg;
89 unsigned Scale;
90 } Mem;
Daniel Dunbar78929e52009-07-20 20:01:54 +000091 };
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +000092
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000093 unsigned getReg() const {
94 assert(Kind == Register && "Invalid access!");
95 return Reg.RegNo;
96 }
Daniel Dunbard80432a2009-07-28 20:47:52 +000097
Daniel Dunbarb7ddef12009-07-31 20:53:16 +000098 const MCValue &getImm() const {
99 assert(Kind == Immediate && "Invalid access!");
100 return Imm.Val;
101 }
102
103 const MCValue &getMemDisp() const {
104 assert(Kind == Memory && "Invalid access!");
105 return Mem.Disp;
106 }
107 unsigned getMemSegReg() const {
108 assert(Kind == Memory && "Invalid access!");
109 return Mem.SegReg;
110 }
111 unsigned getMemBaseReg() const {
112 assert(Kind == Memory && "Invalid access!");
113 return Mem.BaseReg;
114 }
115 unsigned getMemIndexReg() const {
116 assert(Kind == Memory && "Invalid access!");
117 return Mem.IndexReg;
118 }
119 unsigned getMemScale() const {
120 assert(Kind == Memory && "Invalid access!");
121 return Mem.Scale;
122 }
123
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000124 static X86Operand CreateReg(unsigned RegNo) {
125 X86Operand Res;
126 Res.Kind = Register;
127 Res.Reg.RegNo = RegNo;
128 return Res;
129 }
130 static X86Operand CreateImm(MCValue Val) {
131 X86Operand Res;
132 Res.Kind = Immediate;
133 Res.Imm.Val = Val;
134 return Res;
135 }
136 static X86Operand CreateMem(unsigned SegReg, MCValue Disp, unsigned BaseReg,
137 unsigned IndexReg, unsigned Scale) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000138 // We should never just have a displacement, that would be an immediate.
139 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
140
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000141 // The scale should always be one of {1,2,4,8}.
142 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000143 "Invalid scale!");
144 X86Operand Res;
145 Res.Kind = Memory;
146 Res.Mem.SegReg = SegReg;
147 Res.Mem.Disp = Disp;
148 Res.Mem.BaseReg = BaseReg;
149 Res.Mem.IndexReg = IndexReg;
150 Res.Mem.Scale = Scale;
151 return Res;
152 }
153};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000154
Chris Lattnere54532b2009-07-29 06:33:53 +0000155} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000156
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000157
158bool X86ATTAsmParser::ParseRegister(X86Operand &Op) {
Chris Lattnere54532b2009-07-29 06:33:53 +0000159 const AsmToken &Tok = getLexer().getTok();
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000160 assert(Tok.is(AsmToken::Register) && "Invalid token kind!");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000161
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000162 // FIXME: Validate register for the current architecture; we have to do
163 // validation later, so maybe there is no need for this here.
164 unsigned RegNo;
165 assert(Tok.getString().startswith("%") && "Invalid register name!");
166 if (MatchRegisterName(Tok.getString().substr(1), RegNo))
167 return Error(Tok.getLoc(), "invalid register name");
168
169 Op = X86Operand::CreateReg(RegNo);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000170 getLexer().Lex(); // Eat register token.
171
172 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000173}
174
Daniel Dunbar78929e52009-07-20 20:01:54 +0000175bool X86ATTAsmParser::ParseOperand(X86Operand &Op) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000176 switch (getLexer().getKind()) {
177 default:
178 return ParseMemOperand(Op);
179 case AsmToken::Register:
180 // FIXME: if a segment register, this could either be just the seg reg, or
181 // the start of a memory operand.
182 return ParseRegister(Op);
183 case AsmToken::Dollar: {
184 // $42 -> immediate.
185 getLexer().Lex();
186 MCValue Val;
187 if (getParser().ParseRelocatableExpression(Val))
188 return true;
189 Op = X86Operand::CreateImm(Val);
190 return false;
191 }
Chris Lattnere54532b2009-07-29 06:33:53 +0000192 case AsmToken::Star:
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000193 getLexer().Lex(); // Eat the star.
194
195 if (getLexer().is(AsmToken::Register)) {
196 if (ParseRegister(Op))
197 return true;
198 } else if (ParseMemOperand(Op))
199 return true;
200
201 // FIXME: Note the '*' in the operand for use by the matcher.
202 return false;
203 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000204}
205
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000206/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
207bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) {
208 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
209 unsigned SegReg = 0;
210
211 // We have to disambiguate a parenthesized expression "(4+5)" from the start
212 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
213 // only way to do this without lookahead is to eat the ( and see what is after
214 // it.
215 MCValue Disp = MCValue::get(0, 0, 0);
216 if (getLexer().isNot(AsmToken::LParen)) {
217 if (getParser().ParseRelocatableExpression(Disp)) return true;
218
219 // After parsing the base expression we could either have a parenthesized
220 // memory address or not. If not, return now. If so, eat the (.
221 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000222 // Unless we have a segment register, treat this as an immediate.
223 if (SegReg)
224 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
225 else
226 Op = X86Operand::CreateImm(Disp);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000227 return false;
228 }
229
230 // Eat the '('.
231 getLexer().Lex();
232 } else {
233 // Okay, we have a '('. We don't know if this is an expression or not, but
234 // so we have to eat the ( to see beyond it.
235 getLexer().Lex(); // Eat the '('.
236
237 if (getLexer().is(AsmToken::Register) || getLexer().is(AsmToken::Comma)) {
238 // Nothing to do here, fall into the code below with the '(' part of the
239 // memory operand consumed.
240 } else {
241 // It must be an parenthesized expression, parse it now.
242 if (getParser().ParseParenRelocatableExpression(Disp))
243 return true;
244
245 // After parsing the base expression we could either have a parenthesized
246 // memory address or not. If not, return now. If so, eat the (.
247 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000248 // Unless we have a segment register, treat this as an immediate.
249 if (SegReg)
250 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
251 else
252 Op = X86Operand::CreateImm(Disp);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000253 return false;
254 }
255
256 // Eat the '('.
257 getLexer().Lex();
258 }
259 }
260
261 // If we reached here, then we just ate the ( of the memory operand. Process
262 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000263 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000264
265 if (getLexer().is(AsmToken::Register)) {
266 if (ParseRegister(Op))
267 return true;
268 BaseReg = Op.getReg();
269 }
270
271 if (getLexer().is(AsmToken::Comma)) {
272 getLexer().Lex(); // Eat the comma.
273
274 // Following the comma we should have either an index register, or a scale
275 // value. We don't support the later form, but we want to parse it
276 // correctly.
277 //
278 // Not that even though it would be completely consistent to support syntax
279 // like "1(%eax,,1)", the assembler doesn't.
280 if (getLexer().is(AsmToken::Register)) {
281 if (ParseRegister(Op))
282 return true;
283 IndexReg = Op.getReg();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000284
285 if (getLexer().isNot(AsmToken::RParen)) {
286 // Parse the scale amount:
287 // ::= ',' [scale-expression]
288 if (getLexer().isNot(AsmToken::Comma))
289 return true;
290 getLexer().Lex(); // Eat the comma.
291
292 if (getLexer().isNot(AsmToken::RParen)) {
293 SMLoc Loc = getLexer().getTok().getLoc();
294
295 int64_t ScaleVal;
296 if (getParser().ParseAbsoluteExpression(ScaleVal))
297 return true;
298
299 // Validate the scale amount.
300 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
301 return Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
302 Scale = (unsigned)ScaleVal;
303 }
304 }
305 } else if (getLexer().isNot(AsmToken::RParen)) {
306 // Otherwise we have the unsupported form of a scale amount without an
307 // index.
308 SMLoc Loc = getLexer().getTok().getLoc();
309
310 int64_t Value;
311 if (getParser().ParseAbsoluteExpression(Value))
312 return true;
313
314 return Error(Loc, "cannot have scale factor without index register");
315 }
316 }
317
318 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
319 if (getLexer().isNot(AsmToken::RParen))
320 return Error(getLexer().getTok().getLoc(),
321 "unexpected token in memory operand");
322 getLexer().Lex(); // Eat the ')'.
323
324 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
325 return false;
326}
327
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000328bool X86ATTAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) {
Chris Lattnereb46b492009-07-29 06:29:53 +0000329 SmallVector<X86Operand, 3> Operands;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000330
Daniel Dunbara54716c2009-07-31 02:32:59 +0000331 SMLoc Loc = getLexer().getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000332 if (getLexer().isNot(AsmToken::EndOfStatement)) {
333 // Read the first operand.
334 Operands.push_back(X86Operand());
335 if (ParseOperand(Operands.back()))
336 return true;
337
338 while (getLexer().is(AsmToken::Comma)) {
339 getLexer().Lex(); // Eat the comma.
340
341 // Parse and remember the operand.
342 Operands.push_back(X86Operand());
343 if (ParseOperand(Operands.back()))
344 return true;
345 }
346 }
347
Daniel Dunbara54716c2009-07-31 02:32:59 +0000348 if (!MatchInstruction(Name, Operands, Inst))
349 return false;
350
351 // FIXME: We should give nicer diagnostics about the exact failure.
352
353 // FIXME: For now we just treat unrecognized instructions as "warnings".
354 Warning(Loc, "unrecognized instruction");
355
356 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000357}
358
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000359// Force static initialization.
360extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000361 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
362 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000363}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000364
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000365// FIXME: These should come from tblgen?
Daniel Dunbara54716c2009-07-31 02:32:59 +0000366
Daniel Dunbara54716c2009-07-31 02:32:59 +0000367static bool
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000368Match_X86_Op_REG(const X86Operand &Op, MCOperand *MCOps, unsigned NumOps) {
Daniel Dunbara54716c2009-07-31 02:32:59 +0000369 assert(NumOps == 1 && "Invalid number of ops!");
370
371 // FIXME: Match correct registers.
372 if (Op.Kind != X86Operand::Register)
373 return true;
374
375 MCOps[0].MakeReg(Op.getReg());
376 return false;
377}
378
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000379static bool
380Match_X86_Op_IMM(const X86Operand &Op, MCOperand *MCOps, unsigned NumOps) {
381 assert(NumOps == 1 && "Invalid number of ops!");
382
383 // FIXME: We need to check widths.
384 if (Op.Kind != X86Operand::Immediate)
385 return true;
386
387 MCOps[0].MakeMCValue(Op.getImm());
388 return false;
389}
390
Daniel Dunbar24091712009-07-31 22:22:54 +0000391static bool Match_X86_Op_LMEM(const X86Operand &Op,
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000392 MCOperand *MCOps,
393 unsigned NumMCOps) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000394 assert(NumMCOps == 4 && "Invalid number of ops!");
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000395
396 if (Op.Kind != X86Operand::Memory)
397 return true;
398
399 MCOps[0].MakeReg(Op.getMemBaseReg());
400 MCOps[1].MakeImm(Op.getMemScale());
401 MCOps[2].MakeReg(Op.getMemIndexReg());
402 MCOps[3].MakeMCValue(Op.getMemDisp());
Daniel Dunbar24091712009-07-31 22:22:54 +0000403
404 return false;
405}
406
407static bool Match_X86_Op_MEM(const X86Operand &Op,
408 MCOperand *MCOps,
409 unsigned NumMCOps) {
410 assert(NumMCOps == 5 && "Invalid number of ops!");
411
412 if (Match_X86_Op_LMEM(Op, MCOps, 4))
413 return true;
414
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000415 MCOps[4].MakeReg(Op.getMemSegReg());
416
417 return false;
418}
419
420#define REG(name) \
421 static bool Match_X86_Op_##name(const X86Operand &Op, \
422 MCOperand *MCOps, \
423 unsigned NumMCOps) { \
424 return Match_X86_Op_REG(Op, MCOps, NumMCOps); \
425 }
426
427REG(GR64)
428REG(GR32)
429REG(GR16)
430REG(GR8)
431
432#define IMM(name) \
433 static bool Match_X86_Op_##name(const X86Operand &Op, \
434 MCOperand *MCOps, \
435 unsigned NumMCOps) { \
436 return Match_X86_Op_IMM(Op, MCOps, NumMCOps); \
437 }
438
Daniel Dunbar24091712009-07-31 22:22:54 +0000439IMM(brtarget)
440IMM(brtarget8)
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000441IMM(i16i8imm)
442IMM(i16imm)
443IMM(i32i8imm)
444IMM(i32imm)
Daniel Dunbar24091712009-07-31 22:22:54 +0000445IMM(i32imm_pcrel)
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000446IMM(i64i32imm)
Daniel Dunbar24091712009-07-31 22:22:54 +0000447IMM(i64i32imm_pcrel)
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000448IMM(i64i8imm)
449IMM(i64imm)
450IMM(i8imm)
451
Daniel Dunbar24091712009-07-31 22:22:54 +0000452#define LMEM(name) \
453 static bool Match_X86_Op_##name(const X86Operand &Op, \
454 MCOperand *MCOps, \
455 unsigned NumMCOps) { \
456 return Match_X86_Op_LMEM(Op, MCOps, NumMCOps); \
457 }
458
459LMEM(lea32mem)
460LMEM(lea64_32mem)
461LMEM(lea64mem)
462
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000463#define MEM(name) \
464 static bool Match_X86_Op_##name(const X86Operand &Op, \
465 MCOperand *MCOps, \
466 unsigned NumMCOps) { \
467 return Match_X86_Op_MEM(Op, MCOps, NumMCOps); \
468 }
469
470MEM(f128mem)
471MEM(f32mem)
472MEM(f64mem)
473MEM(f80mem)
474MEM(i128mem)
475MEM(i16mem)
476MEM(i32mem)
477MEM(i64mem)
478MEM(i8mem)
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000479MEM(sdmem)
480MEM(ssmem)
481
Daniel Dunbara54716c2009-07-31 02:32:59 +0000482#define DUMMY(name) \
483 static bool Match_X86_Op_##name(const X86Operand &Op, \
484 MCOperand *MCOps, \
485 unsigned NumMCOps) { \
486 return true; \
487 }
488
489DUMMY(FR32)
490DUMMY(FR64)
Daniel Dunbara54716c2009-07-31 02:32:59 +0000491DUMMY(GR32_NOREX)
Daniel Dunbara54716c2009-07-31 02:32:59 +0000492DUMMY(GR8_NOREX)
493DUMMY(RST)
494DUMMY(VR128)
495DUMMY(VR64)
Daniel Dunbara54716c2009-07-31 02:32:59 +0000496DUMMY(i8mem_NOREX)
Daniel Dunbara54716c2009-07-31 02:32:59 +0000497
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000498#include "X86GenAsmMatcher.inc"