blob: c70d24db7817398434eedcbf5209aebe94e67a7e [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 Dunbar14c5bf82009-07-28 22:40:46 +000015#include "llvm/MC/MCValue.h"
16#include "llvm/Support/SourceMgr.h"
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +000017#include "llvm/Target/TargetRegistry.h"
18#include "llvm/Target/TargetAsmParser.h"
19using namespace llvm;
20
21namespace {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000022class X86Operand;
23
24class X86ATTAsmParser : public TargetAsmParser {
25 MCAsmParser &Parser;
26
27private:
28 bool MatchInstruction(const StringRef &Name,
Chris Lattnereb46b492009-07-29 06:29:53 +000029 SmallVectorImpl<X86Operand> &Operands,
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000030 MCInst &Inst);
31
32 MCAsmParser &getParser() const { return Parser; }
33
34 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
35
36 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
37
38 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
39
40 bool ParseRegister(X86Operand &Op);
41
42 bool ParseOperand(X86Operand &Op);
43
44 bool ParseMemOperand(X86Operand &Op);
Daniel Dunbar85f1b392009-07-29 00:02:19 +000045
46 /// @name Auto-generated Match Functions
47 /// {
48
49 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};
59
60/// X86Operand - Instances of this class represent a parsed X86 machine
61/// instruction.
62struct X86Operand {
63 enum {
64 Register,
65 Immediate,
66 Memory
67 } Kind;
68
69 union {
70 struct {
71 unsigned RegNo;
72 } Reg;
73
74 struct {
75 MCValue Val;
76 } Imm;
77
78 struct {
79 unsigned SegReg;
80 MCValue Disp;
81 unsigned BaseReg;
82 unsigned IndexReg;
83 unsigned Scale;
84 } Mem;
Daniel Dunbar78929e52009-07-20 20:01:54 +000085 };
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +000086
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000087 unsigned getReg() const {
88 assert(Kind == Register && "Invalid access!");
89 return Reg.RegNo;
90 }
Daniel Dunbard80432a2009-07-28 20:47:52 +000091
Daniel Dunbar14c5bf82009-07-28 22:40:46 +000092 static X86Operand CreateReg(unsigned RegNo) {
93 X86Operand Res;
94 Res.Kind = Register;
95 Res.Reg.RegNo = RegNo;
96 return Res;
97 }
98 static X86Operand CreateImm(MCValue Val) {
99 X86Operand Res;
100 Res.Kind = Immediate;
101 Res.Imm.Val = Val;
102 return Res;
103 }
104 static X86Operand CreateMem(unsigned SegReg, MCValue Disp, unsigned BaseReg,
105 unsigned IndexReg, unsigned Scale) {
106 // If there is no index register, we should never have a scale, and we
107 // should always have a scale (in {1,2,4,8}) if we do.
108 assert(((Scale == 0 && !IndexReg) ||
109 (IndexReg && (Scale == 1 || Scale == 2 ||
110 Scale == 4 || Scale == 8))) &&
111 "Invalid scale!");
112 X86Operand Res;
113 Res.Kind = Memory;
114 Res.Mem.SegReg = SegReg;
115 Res.Mem.Disp = Disp;
116 Res.Mem.BaseReg = BaseReg;
117 Res.Mem.IndexReg = IndexReg;
118 Res.Mem.Scale = Scale;
119 return Res;
120 }
121};
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000122
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000123}
Daniel Dunbard80432a2009-07-28 20:47:52 +0000124
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000125//
126
127bool X86ATTAsmParser::ParseRegister(X86Operand &Op) {
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000128 AsmToken Tok = getLexer().getTok();
129 assert(Tok.is(AsmToken::Register) && "Invalid token kind!");
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000130
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000131 // FIXME: Validate register for the current architecture; we have to do
132 // validation later, so maybe there is no need for this here.
133 unsigned RegNo;
134 assert(Tok.getString().startswith("%") && "Invalid register name!");
135 if (MatchRegisterName(Tok.getString().substr(1), RegNo))
136 return Error(Tok.getLoc(), "invalid register name");
137
138 Op = X86Operand::CreateReg(RegNo);
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000139 getLexer().Lex(); // Eat register token.
140
141 return false;
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000142}
143
Daniel Dunbar78929e52009-07-20 20:01:54 +0000144bool X86ATTAsmParser::ParseOperand(X86Operand &Op) {
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000145 switch (getLexer().getKind()) {
146 default:
147 return ParseMemOperand(Op);
148 case AsmToken::Register:
149 // FIXME: if a segment register, this could either be just the seg reg, or
150 // the start of a memory operand.
151 return ParseRegister(Op);
152 case AsmToken::Dollar: {
153 // $42 -> immediate.
154 getLexer().Lex();
155 MCValue Val;
156 if (getParser().ParseRelocatableExpression(Val))
157 return true;
158 Op = X86Operand::CreateImm(Val);
159 return false;
160 }
161 case AsmToken::Star: {
162 getLexer().Lex(); // Eat the star.
163
164 if (getLexer().is(AsmToken::Register)) {
165 if (ParseRegister(Op))
166 return true;
167 } else if (ParseMemOperand(Op))
168 return true;
169
170 // FIXME: Note the '*' in the operand for use by the matcher.
171 return false;
172 }
173 }
Daniel Dunbar78929e52009-07-20 20:01:54 +0000174}
175
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000176/// ParseMemOperand: segment: disp(basereg, indexreg, scale)
177bool X86ATTAsmParser::ParseMemOperand(X86Operand &Op) {
178 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
179 unsigned SegReg = 0;
180
181 // We have to disambiguate a parenthesized expression "(4+5)" from the start
182 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
183 // only way to do this without lookahead is to eat the ( and see what is after
184 // it.
185 MCValue Disp = MCValue::get(0, 0, 0);
186 if (getLexer().isNot(AsmToken::LParen)) {
187 if (getParser().ParseRelocatableExpression(Disp)) return true;
188
189 // After parsing the base expression we could either have a parenthesized
190 // memory address or not. If not, return now. If so, eat the (.
191 if (getLexer().isNot(AsmToken::LParen)) {
192 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
193 return false;
194 }
195
196 // Eat the '('.
197 getLexer().Lex();
198 } else {
199 // Okay, we have a '('. We don't know if this is an expression or not, but
200 // so we have to eat the ( to see beyond it.
201 getLexer().Lex(); // Eat the '('.
202
203 if (getLexer().is(AsmToken::Register) || getLexer().is(AsmToken::Comma)) {
204 // Nothing to do here, fall into the code below with the '(' part of the
205 // memory operand consumed.
206 } else {
207 // It must be an parenthesized expression, parse it now.
208 if (getParser().ParseParenRelocatableExpression(Disp))
209 return true;
210
211 // After parsing the base expression we could either have a parenthesized
212 // memory address or not. If not, return now. If so, eat the (.
213 if (getLexer().isNot(AsmToken::LParen)) {
214 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
215 return false;
216 }
217
218 // Eat the '('.
219 getLexer().Lex();
220 }
221 }
222
223 // If we reached here, then we just ate the ( of the memory operand. Process
224 // the rest of the memory operand.
225 unsigned BaseReg = 0, IndexReg = 0, Scale = 0;
226
227 if (getLexer().is(AsmToken::Register)) {
228 if (ParseRegister(Op))
229 return true;
230 BaseReg = Op.getReg();
231 }
232
233 if (getLexer().is(AsmToken::Comma)) {
234 getLexer().Lex(); // Eat the comma.
235
236 // Following the comma we should have either an index register, or a scale
237 // value. We don't support the later form, but we want to parse it
238 // correctly.
239 //
240 // Not that even though it would be completely consistent to support syntax
241 // like "1(%eax,,1)", the assembler doesn't.
242 if (getLexer().is(AsmToken::Register)) {
243 if (ParseRegister(Op))
244 return true;
245 IndexReg = Op.getReg();
246 Scale = 1; // If not specified, the scale defaults to 1.
247
248 if (getLexer().isNot(AsmToken::RParen)) {
249 // Parse the scale amount:
250 // ::= ',' [scale-expression]
251 if (getLexer().isNot(AsmToken::Comma))
252 return true;
253 getLexer().Lex(); // Eat the comma.
254
255 if (getLexer().isNot(AsmToken::RParen)) {
256 SMLoc Loc = getLexer().getTok().getLoc();
257
258 int64_t ScaleVal;
259 if (getParser().ParseAbsoluteExpression(ScaleVal))
260 return true;
261
262 // Validate the scale amount.
263 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
264 return Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
265 Scale = (unsigned)ScaleVal;
266 }
267 }
268 } else if (getLexer().isNot(AsmToken::RParen)) {
269 // Otherwise we have the unsupported form of a scale amount without an
270 // index.
271 SMLoc Loc = getLexer().getTok().getLoc();
272
273 int64_t Value;
274 if (getParser().ParseAbsoluteExpression(Value))
275 return true;
276
277 return Error(Loc, "cannot have scale factor without index register");
278 }
279 }
280
281 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
282 if (getLexer().isNot(AsmToken::RParen))
283 return Error(getLexer().getTok().getLoc(),
284 "unexpected token in memory operand");
285 getLexer().Lex(); // Eat the ')'.
286
287 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale);
288 return false;
289}
290
291bool
292X86ATTAsmParser::MatchInstruction(const StringRef &Name,
Chris Lattnereb46b492009-07-29 06:29:53 +0000293 SmallVectorImpl<X86Operand> &Operands,
Daniel Dunbar78929e52009-07-20 20:01:54 +0000294 MCInst &Inst) {
295 return false;
296}
297
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000298bool X86ATTAsmParser::ParseInstruction(const StringRef &Name, MCInst &Inst) {
Chris Lattnereb46b492009-07-29 06:29:53 +0000299 SmallVector<X86Operand, 3> Operands;
Daniel Dunbar14c5bf82009-07-28 22:40:46 +0000300
301 if (getLexer().isNot(AsmToken::EndOfStatement)) {
302 // Read the first operand.
303 Operands.push_back(X86Operand());
304 if (ParseOperand(Operands.back()))
305 return true;
306
307 while (getLexer().is(AsmToken::Comma)) {
308 getLexer().Lex(); // Eat the comma.
309
310 // Parse and remember the operand.
311 Operands.push_back(X86Operand());
312 if (ParseOperand(Operands.back()))
313 return true;
314 }
315 }
316
Daniel Dunbar78929e52009-07-20 20:01:54 +0000317 return MatchInstruction(Name, Operands, Inst);
Daniel Dunbar4b0f4ef2009-07-20 18:55:04 +0000318}
319
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000320// Force static initialization.
321extern "C" void LLVMInitializeX86AsmParser() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000322 RegisterAsmParser<X86ATTAsmParser> X(TheX86_32Target);
323 RegisterAsmParser<X86ATTAsmParser> Y(TheX86_64Target);
Daniel Dunbarc7df3cb2009-07-17 20:42:00 +0000324}
Daniel Dunbar85f1b392009-07-29 00:02:19 +0000325
326#include "X86GenAsmMatcher.inc"