blob: 1f00f16d6933c9405aa4c1e62026a242abfe5450 [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 Dunbar14c5bf82009-07-28 22:40:46 +000098 static X86Operand CreateReg(unsigned RegNo) {
99 X86Operand Res;
100 Res.Kind = Register;
101 Res.Reg.RegNo = RegNo;
102 return Res;
103 }
104 static X86Operand CreateImm(MCValue Val) {
105 X86Operand Res;
106 Res.Kind = Immediate;
107 Res.Imm.Val = Val;
108 return Res;
109 }
110 static X86Operand CreateMem(unsigned SegReg, MCValue Disp, unsigned BaseReg,
111 unsigned IndexReg, unsigned Scale) {
112 // If there is no index register, we should never have a scale, and we
113 // should always have a scale (in {1,2,4,8}) if we do.
114 assert(((Scale == 0 && !IndexReg) ||
115 (IndexReg && (Scale == 1 || Scale == 2 ||
116 Scale == 4 || Scale == 8))) &&
117 "Invalid scale!");
118 X86Operand Res;
119 Res.Kind = Memory;
120 Res.Mem.SegReg = SegReg;
121 Res.Mem.Disp = Disp;
122 Res.Mem.BaseReg = BaseReg;
123 Res.Mem.IndexReg = IndexReg;
124 Res.Mem.Scale = Scale;
125 return Res;
126 }
127};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000128
Chris Lattnere54532b2009-07-29 06:33:53 +0000129} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000130
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000131
132bool X86ATTAsmParser::ParseRegister(X86Operand &Op) {
Chris Lattnere54532b2009-07-29 06:33:53 +0000133 const AsmToken &Tok = getLexer().getTok();
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000134 assert(Tok.is(AsmToken::Register) && "Invalid token kind!");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000135
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000136 // FIXME: Validate register for the current architecture; we have to do
137 // validation later, so maybe there is no need for this here.
138 unsigned RegNo;
139 assert(Tok.getString().startswith("%") && "Invalid register name!");
140 if (MatchRegisterName(Tok.getString().substr(1), RegNo))
141 return Error(Tok.getLoc(), "invalid register name");
142
143 Op = X86Operand::CreateReg(RegNo);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000144 getLexer().Lex(); // Eat register token.
145
146 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000147}
148
Daniel Dunbar78929e52009-07-20 20:01:54 +0000149bool X86ATTAsmParser::ParseOperand(X86Operand &Op) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000150 switch (getLexer().getKind()) {
151 default:
152 return ParseMemOperand(Op);
153 case AsmToken::Register:
154 // FIXME: if a segment register, this could either be just the seg reg, or
155 // the start of a memory operand.
156 return ParseRegister(Op);
157 case AsmToken::Dollar: {
158 // $42 -> immediate.
159 getLexer().Lex();
160 MCValue Val;
161 if (getParser().ParseRelocatableExpression(Val))
162 return true;
163 Op = X86Operand::CreateImm(Val);
164 return false;
165 }
Chris Lattnere54532b2009-07-29 06:33:53 +0000166 case AsmToken::Star:
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000167 getLexer().Lex(); // Eat the star.
168
169 if (getLexer().is(AsmToken::Register)) {
170 if (ParseRegister(Op))
171 return true;
172 } else if (ParseMemOperand(Op))
173 return true;
174
175 // FIXME: Note the '*' in the operand for use by the matcher.
176 return false;
177 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000178}
179
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000180/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
181bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) {
182 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
183 unsigned SegReg = 0;
184
185 // We have to disambiguate a parenthesized expression "(4+5)" from the start
186 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
187 // only way to do this without lookahead is to eat the ( and see what is after
188 // it.
189 MCValue Disp = MCValue::get(0, 0, 0);
190 if (getLexer().isNot(AsmToken::LParen)) {
191 if (getParser().ParseRelocatableExpression(Disp)) return true;
192
193 // After parsing the base expression we could either have a parenthesized
194 // memory address or not. If not, return now. If so, eat the (.
195 if (getLexer().isNot(AsmToken::LParen)) {
196 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
197 return false;
198 }
199
200 // Eat the '('.
201 getLexer().Lex();
202 } else {
203 // Okay, we have a '('. We don't know if this is an expression or not, but
204 // so we have to eat the ( to see beyond it.
205 getLexer().Lex(); // Eat the '('.
206
207 if (getLexer().is(AsmToken::Register) || getLexer().is(AsmToken::Comma)) {
208 // Nothing to do here, fall into the code below with the '(' part of the
209 // memory operand consumed.
210 } else {
211 // It must be an parenthesized expression, parse it now.
212 if (getParser().ParseParenRelocatableExpression(Disp))
213 return true;
214
215 // After parsing the base expression we could either have a parenthesized
216 // memory address or not. If not, return now. If so, eat the (.
217 if (getLexer().isNot(AsmToken::LParen)) {
218 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
219 return false;
220 }
221
222 // Eat the '('.
223 getLexer().Lex();
224 }
225 }
226
227 // If we reached here, then we just ate the ( of the memory operand. Process
228 // the rest of the memory operand.
229 unsigned BaseReg = 0, IndexReg = 0, Scale = 0;
230
231 if (getLexer().is(AsmToken::Register)) {
232 if (ParseRegister(Op))
233 return true;
234 BaseReg = Op.getReg();
235 }
236
237 if (getLexer().is(AsmToken::Comma)) {
238 getLexer().Lex(); // Eat the comma.
239
240 // Following the comma we should have either an index register, or a scale
241 // value. We don't support the later form, but we want to parse it
242 // correctly.
243 //
244 // Not that even though it would be completely consistent to support syntax
245 // like "1(%eax,,1)", the assembler doesn't.
246 if (getLexer().is(AsmToken::Register)) {
247 if (ParseRegister(Op))
248 return true;
249 IndexReg = Op.getReg();
250 Scale = 1; // If not specified, the scale defaults to 1.
251
252 if (getLexer().isNot(AsmToken::RParen)) {
253 // Parse the scale amount:
254 // ::= ',' [scale-expression]
255 if (getLexer().isNot(AsmToken::Comma))
256 return true;
257 getLexer().Lex(); // Eat the comma.
258
259 if (getLexer().isNot(AsmToken::RParen)) {
260 SMLoc Loc = getLexer().getTok().getLoc();
261
262 int64_t ScaleVal;
263 if (getParser().ParseAbsoluteExpression(ScaleVal))
264 return true;
265
266 // Validate the scale amount.
267 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
268 return Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
269 Scale = (unsigned)ScaleVal;
270 }
271 }
272 } else if (getLexer().isNot(AsmToken::RParen)) {
273 // Otherwise we have the unsupported form of a scale amount without an
274 // index.
275 SMLoc Loc = getLexer().getTok().getLoc();
276
277 int64_t Value;
278 if (getParser().ParseAbsoluteExpression(Value))
279 return true;
280
281 return Error(Loc, "cannot have scale factor without index register");
282 }
283 }
284
285 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
286 if (getLexer().isNot(AsmToken::RParen))
287 return Error(getLexer().getTok().getLoc(),
288 "unexpected token in memory operand");
289 getLexer().Lex(); // Eat the ')'.
290
291 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
292 return false;
293}
294
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000295bool X86ATTAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) {
Chris Lattnereb46b492009-07-29 06:29:53 +0000296 SmallVector<X86Operand, 3> Operands;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000297
Daniel Dunbara54716c2009-07-31 02:32:59 +0000298 SMLoc Loc = getLexer().getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000299 if (getLexer().isNot(AsmToken::EndOfStatement)) {
300 // Read the first operand.
301 Operands.push_back(X86Operand());
302 if (ParseOperand(Operands.back()))
303 return true;
304
305 while (getLexer().is(AsmToken::Comma)) {
306 getLexer().Lex(); // Eat the comma.
307
308 // Parse and remember the operand.
309 Operands.push_back(X86Operand());
310 if (ParseOperand(Operands.back()))
311 return true;
312 }
313 }
314
Daniel Dunbara54716c2009-07-31 02:32:59 +0000315 if (!MatchInstruction(Name, Operands, Inst))
316 return false;
317
318 // FIXME: We should give nicer diagnostics about the exact failure.
319
320 // FIXME: For now we just treat unrecognized instructions as "warnings".
321 Warning(Loc, "unrecognized instruction");
322
323 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000324}
325
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000326// Force static initialization.
327extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000328 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
329 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000330}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000331
Daniel Dunbara54716c2009-07-31 02:32:59 +0000332// FIXME: These should come from tblgen.
333
334// Match_X86_Op_GR8
335static bool
336Match_X86_Op_GR8(const X86Operand &Op, MCOperand *MCOps, unsigned NumOps) {
337 assert(NumOps == 1 && "Invalid number of ops!");
338
339 // FIXME: Match correct registers.
340 if (Op.Kind != X86Operand::Register)
341 return true;
342
343 MCOps[0].MakeReg(Op.getReg());
344 return false;
345}
346
347#define DUMMY(name) \
348 static bool Match_X86_Op_##name(const X86Operand &Op, \
349 MCOperand *MCOps, \
350 unsigned NumMCOps) { \
351 return true; \
352 }
353
354DUMMY(FR32)
355DUMMY(FR64)
356DUMMY(GR16)
357DUMMY(GR32)
358DUMMY(GR32_NOREX)
359DUMMY(GR64)
360DUMMY(GR8_NOREX)
361DUMMY(RST)
362DUMMY(VR128)
363DUMMY(VR64)
364DUMMY(brtarget)
365DUMMY(brtarget8)
366DUMMY(f128mem)
367DUMMY(f32mem)
368DUMMY(f64mem)
369DUMMY(f80mem)
370DUMMY(i128mem)
371DUMMY(i16i8imm)
372DUMMY(i16imm)
373DUMMY(i16mem)
374DUMMY(i32i8imm)
375DUMMY(i32imm_pcrel)
376DUMMY(i32imm)
377DUMMY(i32mem)
378DUMMY(i64i32imm_pcrel)
379DUMMY(i64i32imm)
380DUMMY(i64i8imm)
381DUMMY(i64imm)
382DUMMY(i64mem)
383DUMMY(i8imm)
384DUMMY(i8mem_NOREX)
385DUMMY(i8mem)
386DUMMY(lea32mem)
387DUMMY(lea64_32mem)
388DUMMY(lea64mem)
389DUMMY(sdmem)
390DUMMY(ssmem)
391
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000392#include "X86GenAsmMatcher.inc"