blob: 1c55c3fc7b28a78889ea41766c718b281599ba6e [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 Dunbarb7ddef12009-07-31 20:53:16 +0000138 // The scale should always be one of {1,2,4,8}.
139 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000140 "Invalid scale!");
141 X86Operand Res;
142 Res.Kind = Memory;
143 Res.Mem.SegReg = SegReg;
144 Res.Mem.Disp = Disp;
145 Res.Mem.BaseReg = BaseReg;
146 Res.Mem.IndexReg = IndexReg;
147 Res.Mem.Scale = Scale;
148 return Res;
149 }
150};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000151
Chris Lattnere54532b2009-07-29 06:33:53 +0000152} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000153
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000154
155bool X86ATTAsmParser::ParseRegister(X86Operand &Op) {
Chris Lattnere54532b2009-07-29 06:33:53 +0000156 const AsmToken &Tok = getLexer().getTok();
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000157 assert(Tok.is(AsmToken::Register) && "Invalid token kind!");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000158
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000159 // FIXME: Validate register for the current architecture; we have to do
160 // validation later, so maybe there is no need for this here.
161 unsigned RegNo;
162 assert(Tok.getString().startswith("%") && "Invalid register name!");
163 if (MatchRegisterName(Tok.getString().substr(1), RegNo))
164 return Error(Tok.getLoc(), "invalid register name");
165
166 Op = X86Operand::CreateReg(RegNo);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000167 getLexer().Lex(); // Eat register token.
168
169 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000170}
171
Daniel Dunbar78929e52009-07-20 20:01:54 +0000172bool X86ATTAsmParser::ParseOperand(X86Operand &Op) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000173 switch (getLexer().getKind()) {
174 default:
175 return ParseMemOperand(Op);
176 case AsmToken::Register:
177 // FIXME: if a segment register, this could either be just the seg reg, or
178 // the start of a memory operand.
179 return ParseRegister(Op);
180 case AsmToken::Dollar: {
181 // $42 -> immediate.
182 getLexer().Lex();
183 MCValue Val;
184 if (getParser().ParseRelocatableExpression(Val))
185 return true;
186 Op = X86Operand::CreateImm(Val);
187 return false;
188 }
Chris Lattnere54532b2009-07-29 06:33:53 +0000189 case AsmToken::Star:
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000190 getLexer().Lex(); // Eat the star.
191
192 if (getLexer().is(AsmToken::Register)) {
193 if (ParseRegister(Op))
194 return true;
195 } else if (ParseMemOperand(Op))
196 return true;
197
198 // FIXME: Note the '*' in the operand for use by the matcher.
199 return false;
200 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000201}
202
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000203/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
204bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) {
205 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
206 unsigned SegReg = 0;
207
208 // We have to disambiguate a parenthesized expression "(4+5)" from the start
209 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
210 // only way to do this without lookahead is to eat the ( and see what is after
211 // it.
212 MCValue Disp = MCValue::get(0, 0, 0);
213 if (getLexer().isNot(AsmToken::LParen)) {
214 if (getParser().ParseRelocatableExpression(Disp)) return true;
215
216 // After parsing the base expression we could either have a parenthesized
217 // memory address or not. If not, return now. If so, eat the (.
218 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000219 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000220 return false;
221 }
222
223 // Eat the '('.
224 getLexer().Lex();
225 } else {
226 // Okay, we have a '('. We don't know if this is an expression or not, but
227 // so we have to eat the ( to see beyond it.
228 getLexer().Lex(); // Eat the '('.
229
230 if (getLexer().is(AsmToken::Register) || getLexer().is(AsmToken::Comma)) {
231 // Nothing to do here, fall into the code below with the '(' part of the
232 // memory operand consumed.
233 } else {
234 // It must be an parenthesized expression, parse it now.
235 if (getParser().ParseParenRelocatableExpression(Disp))
236 return true;
237
238 // After parsing the base expression we could either have a parenthesized
239 // memory address or not. If not, return now. If so, eat the (.
240 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000241 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000242 return false;
243 }
244
245 // Eat the '('.
246 getLexer().Lex();
247 }
248 }
249
250 // If we reached here, then we just ate the ( of the memory operand. Process
251 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000252 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000253
254 if (getLexer().is(AsmToken::Register)) {
255 if (ParseRegister(Op))
256 return true;
257 BaseReg = Op.getReg();
258 }
259
260 if (getLexer().is(AsmToken::Comma)) {
261 getLexer().Lex(); // Eat the comma.
262
263 // Following the comma we should have either an index register, or a scale
264 // value. We don't support the later form, but we want to parse it
265 // correctly.
266 //
267 // Not that even though it would be completely consistent to support syntax
268 // like "1(%eax,,1)", the assembler doesn't.
269 if (getLexer().is(AsmToken::Register)) {
270 if (ParseRegister(Op))
271 return true;
272 IndexReg = Op.getReg();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000273
274 if (getLexer().isNot(AsmToken::RParen)) {
275 // Parse the scale amount:
276 // ::= ',' [scale-expression]
277 if (getLexer().isNot(AsmToken::Comma))
278 return true;
279 getLexer().Lex(); // Eat the comma.
280
281 if (getLexer().isNot(AsmToken::RParen)) {
282 SMLoc Loc = getLexer().getTok().getLoc();
283
284 int64_t ScaleVal;
285 if (getParser().ParseAbsoluteExpression(ScaleVal))
286 return true;
287
288 // Validate the scale amount.
289 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
290 return Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
291 Scale = (unsigned)ScaleVal;
292 }
293 }
294 } else if (getLexer().isNot(AsmToken::RParen)) {
295 // Otherwise we have the unsupported form of a scale amount without an
296 // index.
297 SMLoc Loc = getLexer().getTok().getLoc();
298
299 int64_t Value;
300 if (getParser().ParseAbsoluteExpression(Value))
301 return true;
302
303 return Error(Loc, "cannot have scale factor without index register");
304 }
305 }
306
307 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
308 if (getLexer().isNot(AsmToken::RParen))
309 return Error(getLexer().getTok().getLoc(),
310 "unexpected token in memory operand");
311 getLexer().Lex(); // Eat the ')'.
312
313 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
314 return false;
315}
316
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000317bool X86ATTAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) {
Chris Lattnereb46b492009-07-29 06:29:53 +0000318 SmallVector<X86Operand, 3> Operands;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000319
Daniel Dunbara54716c2009-07-31 02:32:59 +0000320 SMLoc Loc = getLexer().getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000321 if (getLexer().isNot(AsmToken::EndOfStatement)) {
322 // Read the first operand.
323 Operands.push_back(X86Operand());
324 if (ParseOperand(Operands.back()))
325 return true;
326
327 while (getLexer().is(AsmToken::Comma)) {
328 getLexer().Lex(); // Eat the comma.
329
330 // Parse and remember the operand.
331 Operands.push_back(X86Operand());
332 if (ParseOperand(Operands.back()))
333 return true;
334 }
335 }
336
Daniel Dunbara54716c2009-07-31 02:32:59 +0000337 if (!MatchInstruction(Name, Operands, Inst))
338 return false;
339
340 // FIXME: We should give nicer diagnostics about the exact failure.
341
342 // FIXME: For now we just treat unrecognized instructions as "warnings".
343 Warning(Loc, "unrecognized instruction");
344
345 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000346}
347
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000348// Force static initialization.
349extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000350 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
351 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000352}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000353
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000354// FIXME: These should come from tblgen?
Daniel Dunbara54716c2009-07-31 02:32:59 +0000355
Daniel Dunbara54716c2009-07-31 02:32:59 +0000356static bool
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000357Match_X86_Op_REG(const X86Operand &Op, MCOperand *MCOps, unsigned NumOps) {
Daniel Dunbara54716c2009-07-31 02:32:59 +0000358 assert(NumOps == 1 && "Invalid number of ops!");
359
360 // FIXME: Match correct registers.
361 if (Op.Kind != X86Operand::Register)
362 return true;
363
364 MCOps[0].MakeReg(Op.getReg());
365 return false;
366}
367
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000368static bool
369Match_X86_Op_IMM(const X86Operand &Op, MCOperand *MCOps, unsigned NumOps) {
370 assert(NumOps == 1 && "Invalid number of ops!");
371
372 // FIXME: We need to check widths.
373 if (Op.Kind != X86Operand::Immediate)
374 return true;
375
376 MCOps[0].MakeMCValue(Op.getImm());
377 return false;
378}
379
380static bool Match_X86_Op_MEM(const X86Operand &Op,
381 MCOperand *MCOps,
382 unsigned NumMCOps) {
383 assert(NumMCOps == 5 && "Invalid number of ops!");
384
385 if (Op.Kind != X86Operand::Memory)
386 return true;
387
388 MCOps[0].MakeReg(Op.getMemBaseReg());
389 MCOps[1].MakeImm(Op.getMemScale());
390 MCOps[2].MakeReg(Op.getMemIndexReg());
391 MCOps[3].MakeMCValue(Op.getMemDisp());
392 MCOps[4].MakeReg(Op.getMemSegReg());
393
394 return false;
395}
396
397#define REG(name) \
398 static bool Match_X86_Op_##name(const X86Operand &Op, \
399 MCOperand *MCOps, \
400 unsigned NumMCOps) { \
401 return Match_X86_Op_REG(Op, MCOps, NumMCOps); \
402 }
403
404REG(GR64)
405REG(GR32)
406REG(GR16)
407REG(GR8)
408
409#define IMM(name) \
410 static bool Match_X86_Op_##name(const X86Operand &Op, \
411 MCOperand *MCOps, \
412 unsigned NumMCOps) { \
413 return Match_X86_Op_IMM(Op, MCOps, NumMCOps); \
414 }
415
416IMM(i16i8imm)
417IMM(i16imm)
418IMM(i32i8imm)
419IMM(i32imm)
420IMM(i64i32imm)
421IMM(i64i8imm)
422IMM(i64imm)
423IMM(i8imm)
424
425#define MEM(name) \
426 static bool Match_X86_Op_##name(const X86Operand &Op, \
427 MCOperand *MCOps, \
428 unsigned NumMCOps) { \
429 return Match_X86_Op_MEM(Op, MCOps, NumMCOps); \
430 }
431
432MEM(f128mem)
433MEM(f32mem)
434MEM(f64mem)
435MEM(f80mem)
436MEM(i128mem)
437MEM(i16mem)
438MEM(i32mem)
439MEM(i64mem)
440MEM(i8mem)
441MEM(lea32mem)
442MEM(lea64_32mem)
443MEM(lea64mem)
444MEM(sdmem)
445MEM(ssmem)
446
Daniel Dunbara54716c2009-07-31 02:32:59 +0000447#define DUMMY(name) \
448 static bool Match_X86_Op_##name(const X86Operand &Op, \
449 MCOperand *MCOps, \
450 unsigned NumMCOps) { \
451 return true; \
452 }
453
454DUMMY(FR32)
455DUMMY(FR64)
Daniel Dunbara54716c2009-07-31 02:32:59 +0000456DUMMY(GR32_NOREX)
Daniel Dunbara54716c2009-07-31 02:32:59 +0000457DUMMY(GR8_NOREX)
458DUMMY(RST)
459DUMMY(VR128)
460DUMMY(VR64)
461DUMMY(brtarget)
462DUMMY(brtarget8)
Daniel Dunbara54716c2009-07-31 02:32:59 +0000463DUMMY(i32imm_pcrel)
Daniel Dunbara54716c2009-07-31 02:32:59 +0000464DUMMY(i64i32imm_pcrel)
Daniel Dunbara54716c2009-07-31 02:32:59 +0000465DUMMY(i8mem_NOREX)
Daniel Dunbara54716c2009-07-31 02:32:59 +0000466
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000467#include "X86GenAsmMatcher.inc"