blob: 841b42743f3cacfe27e304e4f1d00559857ef23b [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 Dunbar85f1b392009-07-29 00:02:19 +000049 bool MatchRegisterName(const StringRef &Name, unsigned &RegNo);
50
51 /// }
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000052
53public:
54 X86ATTAsmParser(const Target &T, MCAsmParser &_Parser)
55 : TargetAsmParser(T), Parser(_Parser) {}
56
57 virtual bool ParseInstruction(const StringRef &Name, MCInst &Inst);
58};
Chris Lattnere54532b2009-07-29 06:33:53 +000059
60} // end anonymous namespace
61
62
63namespace {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000064
65/// X86Operand - Instances of this class represent a parsed X86 machine
66/// instruction.
67struct X86Operand {
68 enum {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000069 Token,
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000070 Register,
71 Immediate,
72 Memory
73 } Kind;
74
75 union {
76 struct {
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000077 const char *Data;
78 unsigned Length;
79 } Tok;
80
81 struct {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000082 unsigned RegNo;
83 } Reg;
84
85 struct {
86 MCValue Val;
87 } Imm;
88
89 struct {
90 unsigned SegReg;
91 MCValue Disp;
92 unsigned BaseReg;
93 unsigned IndexReg;
94 unsigned Scale;
95 } Mem;
Daniel Dunbar78929e52009-07-20 20:01:54 +000096 };
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +000097
Daniel Dunbarfe6759e2009-08-07 08:26:05 +000098 StringRef getToken() const {
99 assert(Kind == Token && "Invalid access!");
100 return StringRef(Tok.Data, Tok.Length);
101 }
102
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000103 unsigned getReg() const {
104 assert(Kind == Register && "Invalid access!");
105 return Reg.RegNo;
106 }
Daniel Dunbard80432a2009-07-28 20:47:52 +0000107
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000108 const MCValue &getImm() const {
109 assert(Kind == Immediate && "Invalid access!");
110 return Imm.Val;
111 }
112
113 const MCValue &getMemDisp() const {
114 assert(Kind == Memory && "Invalid access!");
115 return Mem.Disp;
116 }
117 unsigned getMemSegReg() const {
118 assert(Kind == Memory && "Invalid access!");
119 return Mem.SegReg;
120 }
121 unsigned getMemBaseReg() const {
122 assert(Kind == Memory && "Invalid access!");
123 return Mem.BaseReg;
124 }
125 unsigned getMemIndexReg() const {
126 assert(Kind == Memory && "Invalid access!");
127 return Mem.IndexReg;
128 }
129 unsigned getMemScale() const {
130 assert(Kind == Memory && "Invalid access!");
131 return Mem.Scale;
132 }
133
Daniel Dunbar378bee92009-08-08 07:50:56 +0000134 bool isToken() const {return Kind == Token; }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000135
136 bool isImm() const { return Kind == Immediate; }
137
138 bool isMem() const { return Kind == Memory; }
139
140 bool isReg() const { return Kind == Register; }
141
142 void addRegOperands(MCInst &Inst, unsigned N) {
143 assert(N == 1 && "Invalid number of operands!");
144 Inst.addOperand(MCOperand::CreateReg(getReg()));
145 }
146
147 void addImmOperands(MCInst &Inst, unsigned N) {
148 assert(N == 1 && "Invalid number of operands!");
149 Inst.addOperand(MCOperand::CreateMCValue(getImm()));
150 }
151
152 void addMemOperands(MCInst &Inst, unsigned N) {
153 assert((N == 4 || N == 5) && "Invalid number of operands!");
154
155 Inst.addOperand(MCOperand::CreateReg(getMemBaseReg()));
156 Inst.addOperand(MCOperand::CreateImm(getMemScale()));
157 Inst.addOperand(MCOperand::CreateReg(getMemIndexReg()));
158 Inst.addOperand(MCOperand::CreateMCValue(getMemDisp()));
159
160 // FIXME: What a hack.
161 if (N == 5)
162 Inst.addOperand(MCOperand::CreateReg(getMemSegReg()));
163 }
164
165 static X86Operand CreateToken(StringRef Str) {
166 X86Operand Res;
167 Res.Kind = Token;
168 Res.Tok.Data = Str.data();
169 Res.Tok.Length = Str.size();
170 return Res;
171 }
172
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000173 static X86Operand CreateReg(unsigned RegNo) {
174 X86Operand Res;
175 Res.Kind = Register;
176 Res.Reg.RegNo = RegNo;
177 return Res;
178 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000179
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000180 static X86Operand CreateImm(MCValue Val) {
181 X86Operand Res;
182 Res.Kind = Immediate;
183 Res.Imm.Val = Val;
184 return Res;
185 }
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000186
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000187 static X86Operand CreateMem(unsigned SegReg, MCValue Disp, unsigned BaseReg,
188 unsigned IndexReg, unsigned Scale) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000189 // We should never just have a displacement, that would be an immediate.
190 assert((SegReg || BaseReg || IndexReg) && "Invalid memory operand!");
191
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000192 // The scale should always be one of {1,2,4,8}.
193 assert(((Scale == 1 || Scale == 2 || Scale == 4 || Scale == 8)) &&
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000194 "Invalid scale!");
195 X86Operand Res;
196 Res.Kind = Memory;
197 Res.Mem.SegReg = SegReg;
198 Res.Mem.Disp = Disp;
199 Res.Mem.BaseReg = BaseReg;
200 Res.Mem.IndexReg = IndexReg;
201 Res.Mem.Scale = Scale;
202 return Res;
203 }
204};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000205
Chris Lattnere54532b2009-07-29 06:33:53 +0000206} // end anonymous namespace.
Daniel Dunbard80432a2009-07-28 20:47:52 +0000207
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000208
209bool X86ATTAsmParser::ParseRegister(X86Operand &Op) {
Chris Lattnere54532b2009-07-29 06:33:53 +0000210 const AsmToken &Tok = getLexer().getTok();
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000211 assert(Tok.is(AsmToken::Register) && "Invalid token kind!");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000212
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000213 // FIXME: Validate register for the current architecture; we have to do
214 // validation later, so maybe there is no need for this here.
215 unsigned RegNo;
216 assert(Tok.getString().startswith("%") && "Invalid register name!");
217 if (MatchRegisterName(Tok.getString().substr(1), RegNo))
218 return Error(Tok.getLoc(), "invalid register name");
219
220 Op = X86Operand::CreateReg(RegNo);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000221 getLexer().Lex(); // Eat register token.
222
223 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000224}
225
Daniel Dunbar78929e52009-07-20 20:01:54 +0000226bool X86ATTAsmParser::ParseOperand(X86Operand &Op) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000227 switch (getLexer().getKind()) {
228 default:
229 return ParseMemOperand(Op);
230 case AsmToken::Register:
231 // FIXME: if a segment register, this could either be just the seg reg, or
232 // the start of a memory operand.
233 return ParseRegister(Op);
234 case AsmToken::Dollar: {
235 // $42 -> immediate.
236 getLexer().Lex();
237 MCValue Val;
238 if (getParser().ParseRelocatableExpression(Val))
239 return true;
240 Op = X86Operand::CreateImm(Val);
241 return false;
242 }
Chris Lattnere54532b2009-07-29 06:33:53 +0000243 case AsmToken::Star:
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000244 getLexer().Lex(); // Eat the star.
245
246 if (getLexer().is(AsmToken::Register)) {
247 if (ParseRegister(Op))
248 return true;
249 } else if (ParseMemOperand(Op))
250 return true;
251
252 // FIXME: Note the '*' in the operand for use by the matcher.
253 return false;
254 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000255}
256
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000257/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
258bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) {
259 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
260 unsigned SegReg = 0;
261
262 // We have to disambiguate a parenthesized expression "(4+5)" from the start
263 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
264 // only way to do this without lookahead is to eat the ( and see what is after
265 // it.
266 MCValue Disp = MCValue::get(0, 0, 0);
267 if (getLexer().isNot(AsmToken::LParen)) {
268 if (getParser().ParseRelocatableExpression(Disp)) return true;
269
270 // After parsing the base expression we could either have a parenthesized
271 // memory address or not. If not, return now. If so, eat the (.
272 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000273 // Unless we have a segment register, treat this as an immediate.
274 if (SegReg)
275 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
276 else
277 Op = X86Operand::CreateImm(Disp);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000278 return false;
279 }
280
281 // Eat the '('.
282 getLexer().Lex();
283 } else {
284 // Okay, we have a '('. We don't know if this is an expression or not, but
285 // so we have to eat the ( to see beyond it.
286 getLexer().Lex(); // Eat the '('.
287
288 if (getLexer().is(AsmToken::Register) || getLexer().is(AsmToken::Comma)) {
289 // Nothing to do here, fall into the code below with the '(' part of the
290 // memory operand consumed.
291 } else {
292 // It must be an parenthesized expression, parse it now.
293 if (getParser().ParseParenRelocatableExpression(Disp))
294 return true;
295
296 // After parsing the base expression we could either have a parenthesized
297 // memory address or not. If not, return now. If so, eat the (.
298 if (getLexer().isNot(AsmToken::LParen)) {
Daniel Dunbar24091712009-07-31 22:22:54 +0000299 // Unless we have a segment register, treat this as an immediate.
300 if (SegReg)
301 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 1);
302 else
303 Op = X86Operand::CreateImm(Disp);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000304 return false;
305 }
306
307 // Eat the '('.
308 getLexer().Lex();
309 }
310 }
311
312 // If we reached here, then we just ate the ( of the memory operand. Process
313 // the rest of the memory operand.
Daniel Dunbarb7ddef12009-07-31 20:53:16 +0000314 unsigned BaseReg = 0, IndexReg = 0, Scale = 1;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000315
316 if (getLexer().is(AsmToken::Register)) {
317 if (ParseRegister(Op))
318 return true;
319 BaseReg = Op.getReg();
320 }
321
322 if (getLexer().is(AsmToken::Comma)) {
323 getLexer().Lex(); // Eat the comma.
324
325 // Following the comma we should have either an index register, or a scale
326 // value. We don't support the later form, but we want to parse it
327 // correctly.
328 //
329 // Not that even though it would be completely consistent to support syntax
330 // like "1(%eax,,1)", the assembler doesn't.
331 if (getLexer().is(AsmToken::Register)) {
332 if (ParseRegister(Op))
333 return true;
334 IndexReg = Op.getReg();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000335
336 if (getLexer().isNot(AsmToken::RParen)) {
337 // Parse the scale amount:
338 // ::= ',' [scale-expression]
339 if (getLexer().isNot(AsmToken::Comma))
340 return true;
341 getLexer().Lex(); // Eat the comma.
342
343 if (getLexer().isNot(AsmToken::RParen)) {
344 SMLoc Loc = getLexer().getTok().getLoc();
345
346 int64_t ScaleVal;
347 if (getParser().ParseAbsoluteExpression(ScaleVal))
348 return true;
349
350 // Validate the scale amount.
351 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
352 return Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
353 Scale = (unsigned)ScaleVal;
354 }
355 }
356 } else if (getLexer().isNot(AsmToken::RParen)) {
357 // Otherwise we have the unsupported form of a scale amount without an
358 // index.
359 SMLoc Loc = getLexer().getTok().getLoc();
360
361 int64_t Value;
362 if (getParser().ParseAbsoluteExpression(Value))
363 return true;
364
365 return Error(Loc, "cannot have scale factor without index register");
366 }
367 }
368
369 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
370 if (getLexer().isNot(AsmToken::RParen))
371 return Error(getLexer().getTok().getLoc(),
372 "unexpected token in memory operand");
373 getLexer().Lex(); // Eat the ')'.
374
375 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
376 return false;
377}
378
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000379bool X86ATTAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) {
Daniel Dunbar62beebc2009-08-07 20:33:39 +0000380 SmallVector<X86Operand, 8> Operands;
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000381
382 Operands.push_back(X86Operand::CreateToken(Name));
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000383
Daniel Dunbara54716c2009-07-31 02:32:59 +0000384 SMLoc Loc = getLexer().getTok().getLoc();
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000385 if (getLexer().isNot(AsmToken::EndOfStatement)) {
386 // Read the first operand.
387 Operands.push_back(X86Operand());
388 if (ParseOperand(Operands.back()))
389 return true;
390
391 while (getLexer().is(AsmToken::Comma)) {
392 getLexer().Lex(); // Eat the comma.
393
394 // Parse and remember the operand.
395 Operands.push_back(X86Operand());
396 if (ParseOperand(Operands.back()))
397 return true;
398 }
399 }
400
Daniel Dunbarfe6759e2009-08-07 08:26:05 +0000401 if (!MatchInstruction(Operands, Inst))
Daniel Dunbara54716c2009-07-31 02:32:59 +0000402 return false;
403
404 // FIXME: We should give nicer diagnostics about the exact failure.
405
406 // FIXME: For now we just treat unrecognized instructions as "warnings".
407 Warning(Loc, "unrecognized instruction");
408
409 return false;
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000410}
411
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000412// Force static initialization.
413extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000414 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
415 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000416}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000417
418#include "X86GenAsmMatcher.inc"