blob: 397c5fe9c7cebe523df4cd16433d6fd7926eec32 [file] [log] [blame]
Chris Lattner27aa7d22009-06-21 20:16:42 +00001//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
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//
10// This class implements the parser for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AsmParser.h"
Chris Lattnerb0789ed2009-06-21 20:54:55 +000015#include "llvm/Support/SourceMgr.h"
16#include "llvm/Support/raw_ostream.h"
Chris Lattner27aa7d22009-06-21 20:16:42 +000017using namespace llvm;
18
Chris Lattner14ee48a2009-06-21 21:22:11 +000019bool AsmParser::Error(SMLoc L, const char *Msg) {
20 Lexer.PrintMessage(L, Msg);
21 return true;
22}
23
24bool AsmParser::TokError(const char *Msg) {
25 Lexer.PrintMessage(Lexer.getLoc(), Msg);
26 return true;
27}
28
Chris Lattner27aa7d22009-06-21 20:16:42 +000029bool AsmParser::Run() {
Chris Lattnerb0789ed2009-06-21 20:54:55 +000030 // Prime the lexer.
31 Lexer.Lex();
32
33 while (Lexer.isNot(asmtok::Eof))
34 if (ParseStatement())
35 return true;
36
37 return false;
38}
39
Chris Lattner2cf5f142009-06-22 01:29:09 +000040/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
41void AsmParser::EatToEndOfStatement() {
42 while (Lexer.isNot(asmtok::EndOfStatement) &&
43 Lexer.isNot(asmtok::Eof))
44 Lexer.Lex();
45
46 // Eat EOL.
47 if (Lexer.is(asmtok::EndOfStatement))
48 Lexer.Lex();
49}
50
Chris Lattnerc4193832009-06-22 05:51:26 +000051
Chris Lattner2cf5f142009-06-22 01:29:09 +000052struct AsmParser::X86Operand {
53 enum {
54 Register,
Chris Lattnerc4193832009-06-22 05:51:26 +000055 Immediate,
56 Memory
Chris Lattner2cf5f142009-06-22 01:29:09 +000057 } Kind;
58
59 union {
60 struct {
61 unsigned RegNo;
62 } Reg;
63
64 struct {
65 // FIXME: Should be a general expression.
66 int64_t Val;
67 } Imm;
Chris Lattnerc4193832009-06-22 05:51:26 +000068
69 struct {
70 unsigned SegReg;
71 int64_t Disp; // FIXME: Should be a general expression.
72 unsigned BaseReg;
73 unsigned Scale;
74 unsigned ScaleReg;
75 } Mem;
Chris Lattner2cf5f142009-06-22 01:29:09 +000076 };
77
78 static X86Operand CreateReg(unsigned RegNo) {
79 X86Operand Res;
80 Res.Kind = Register;
81 Res.Reg.RegNo = RegNo;
82 return Res;
83 }
84 static X86Operand CreateImm(int64_t Val) {
85 X86Operand Res;
86 Res.Kind = Immediate;
87 Res.Imm.Val = Val;
88 return Res;
89 }
Chris Lattnerc4193832009-06-22 05:51:26 +000090 static X86Operand CreateMem(unsigned SegReg, int64_t Disp, unsigned BaseReg,
91 unsigned Scale, unsigned ScaleReg) {
92 X86Operand Res;
93 Res.Kind = Memory;
94 Res.Mem.SegReg = SegReg;
95 Res.Mem.Disp = Disp;
96 Res.Mem.BaseReg = BaseReg;
97 Res.Mem.Scale = Scale;
98 Res.Mem.ScaleReg = ScaleReg;
99 return Res;
100 }
Chris Lattner2cf5f142009-06-22 01:29:09 +0000101};
102
103bool AsmParser::ParseX86Operand(X86Operand &Op) {
104 switch (Lexer.getKind()) {
105 default:
Chris Lattnerc4193832009-06-22 05:51:26 +0000106 return ParseX86MemOperand(Op);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000107 case asmtok::Register:
108 // FIXME: Decode reg #.
Chris Lattnerc4193832009-06-22 05:51:26 +0000109 // FIXME: if a segment register, this could either be just the seg reg, or
110 // the start of a memory operand.
Chris Lattnerbe9c23f2009-06-22 06:02:13 +0000111 Op = X86Operand::CreateReg(123);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000112 Lexer.Lex(); // Eat register.
113 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000114 case asmtok::Dollar: {
Chris Lattner2cf5f142009-06-22 01:29:09 +0000115 // $42 -> immediate.
116 Lexer.Lex();
Chris Lattnerc4193832009-06-22 05:51:26 +0000117 int64_t Val;
118 if (ParseExpression(Val))
Chris Lattner2cf5f142009-06-22 01:29:09 +0000119 return TokError("expected integer constant");
Chris Lattnerc4193832009-06-22 05:51:26 +0000120 Op = X86Operand::CreateReg(Val);
Chris Lattner2cf5f142009-06-22 01:29:09 +0000121 return false;
Chris Lattnerbe9c23f2009-06-22 06:02:13 +0000122 case asmtok::Star:
123 Lexer.Lex(); // Eat the star.
124
125 if (Lexer.is(asmtok::Register)) {
126 Op = X86Operand::CreateReg(123);
127 Lexer.Lex(); // Eat register.
128 } else if (ParseX86MemOperand(Op))
129 return true;
130
131 // FIXME: Note that these are 'dereferenced' so that clients know the '*' is
132 // there.
133 return false;
Chris Lattnerc4193832009-06-22 05:51:26 +0000134 }
Chris Lattner2cf5f142009-06-22 01:29:09 +0000135 }
136}
137
Chris Lattnerc4193832009-06-22 05:51:26 +0000138/// ParseX86MemOperand: segment: disp(basereg, indexreg, scale)
139bool AsmParser::ParseX86MemOperand(X86Operand &Op) {
140 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
141 unsigned SegReg = 0;
142
143
144 // We have to disambiguate a parenthesized expression "(4+5)" from the start
145 // of a memory operand with a missing displacement "(%ebx)" or "(,%eax)". The
146 // only way to do this without lookahead is to eat the ( and see what is after
147 // it.
148 int64_t Disp = 0;
149 if (Lexer.isNot(asmtok::LParen)) {
150 if (ParseExpression(Disp)) return true;
151
152 // After parsing the base expression we could either have a parenthesized
153 // memory address or not. If not, return now. If so, eat the (.
154 if (Lexer.isNot(asmtok::LParen)) {
155 Op = X86Operand::CreateMem(SegReg, Disp, 0, 0, 0);
156 return false;
157 }
158
159 // Eat the '('.
160 Lexer.Lex();
161 } else {
162 // Okay, we have a '('. We don't know if this is an expression or not, but
163 // so we have to eat the ( to see beyond it.
164 Lexer.Lex(); // Eat the '('.
165
166 if (Lexer.is(asmtok::Register) || Lexer.is(asmtok::Comma)) {
167 // Nothing to do here, fall into the code below with the '(' part of the
168 // memory operand consumed.
169 } else {
170 // FIXME: Call ParseParenExpression with the leading ( consumed.
171 return TokError("FIXME: Paren expr not implemented yet!");
172 }
173 }
174
175 // If we reached here, then we just ate the ( of the memory operand. Process
176 // the rest of the memory operand.
177 unsigned BaseReg = 0, ScaleReg = 0, Scale = 0;
178
179 if (Lexer.is(asmtok::Register)) {
180 BaseReg = 123; // FIXME: decode reg #
181 Lexer.Lex(); // eat the register.
182 }
183
184 if (Lexer.is(asmtok::Comma)) {
185 Lexer.Lex(); // eat the comma.
186
187 if (Lexer.is(asmtok::Register)) {
188 ScaleReg = 123; // FIXME: decode reg #
189 Lexer.Lex(); // eat the register.
190 Scale = 1; // If not specified, the scale defaults to 1.
191 }
192
193 if (Lexer.is(asmtok::Comma)) {
194 Lexer.Lex(); // eat the comma.
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000195
Chris Lattnerc4193832009-06-22 05:51:26 +0000196 // If present, get and validate scale amount.
197 if (Lexer.is(asmtok::IntVal)) {
198 int64_t ScaleVal = Lexer.getCurIntVal();
199 if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 && ScaleVal != 8)
200 return TokError("scale factor in address must be 1, 2, 4 or 8");
201 Lexer.Lex(); // eat the scale.
202 Scale = (unsigned)ScaleVal;
203 }
204 }
205 }
206
207 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
208 if (Lexer.isNot(asmtok::RParen))
209 return TokError("unexpected token in memory operand");
210 Lexer.Lex(); // Eat the ')'.
211
212 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, Scale, ScaleReg);
213 return false;
214}
215
Chris Lattner74ec1a32009-06-22 06:32:03 +0000216/// ParseParenExpr - Parse a paren expression and return it.
217/// NOTE: This assumes the leading '(' has already been consumed.
218///
219/// parenexpr ::= expr)
220///
221bool AsmParser::ParseParenExpr(int64_t &Res) {
222 if (ParseExpression(Res)) return true;
223 if (Lexer.isNot(asmtok::RParen))
224 return TokError("expected ')' in parentheses expression");
225 Lexer.Lex();
226 return false;
227}
Chris Lattnerc4193832009-06-22 05:51:26 +0000228
Chris Lattner74ec1a32009-06-22 06:32:03 +0000229/// ParsePrimaryExpr - Parse a primary expression and return it.
230/// primaryexpr ::= (parenexpr
231/// primaryexpr ::= symbol
232/// primaryexpr ::= number
233/// primaryexpr ::= ~,+,- primaryexpr
234bool AsmParser::ParsePrimaryExpr(int64_t &Res) {
Chris Lattnerc4193832009-06-22 05:51:26 +0000235 switch (Lexer.getKind()) {
236 default:
237 return TokError("unknown token in expression");
238 case asmtok::Identifier:
239 // This is a label, this should be parsed as part of an expression, to
240 // handle things like LFOO+4
241 Res = 0; // FIXME.
242 Lexer.Lex(); // Eat identifier.
243 return false;
244 case asmtok::IntVal:
245 Res = Lexer.getCurIntVal();
246 Lexer.Lex(); // Eat identifier.
247 return false;
Chris Lattner74ec1a32009-06-22 06:32:03 +0000248 case asmtok::LParen:
249 Lexer.Lex(); // Eat the '('.
250 return ParseParenExpr(Res);
251 case asmtok::Tilde:
252 case asmtok::Plus:
253 case asmtok::Minus:
254 Lexer.Lex(); // Eat the operator.
255 return ParsePrimaryExpr(Res);
Chris Lattnerc4193832009-06-22 05:51:26 +0000256 }
257}
Chris Lattner74ec1a32009-06-22 06:32:03 +0000258
259/// ParseExpression - Parse an expression and return it.
260///
261/// expr ::= expr +,- expr -> lowest.
262/// expr ::= expr |,^,&,! expr -> middle.
263/// expr ::= expr *,/,%,<<,>> expr -> highest.
264/// expr ::= primaryexpr
265///
266bool AsmParser::ParseExpression(int64_t &Res) {
267 return ParsePrimaryExpr(Res);
268}
Chris Lattnerc4193832009-06-22 05:51:26 +0000269
270
271
272
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000273/// ParseStatement:
274/// ::= EndOfStatement
Chris Lattner2cf5f142009-06-22 01:29:09 +0000275/// ::= Label* Directive ...Operands... EndOfStatement
276/// ::= Label* Identifier OperandList* EndOfStatement
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000277bool AsmParser::ParseStatement() {
278 switch (Lexer.getKind()) {
279 default:
Chris Lattner14ee48a2009-06-21 21:22:11 +0000280 return TokError("unexpected token at start of statement");
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000281 case asmtok::EndOfStatement:
282 Lexer.Lex();
283 return false;
284 case asmtok::Identifier:
285 break;
286 // TODO: Recurse on local labels etc.
287 }
288
289 // If we have an identifier, handle it as the key symbol.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000290 SMLoc IDLoc = Lexer.getLoc();
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000291 std::string IDVal = Lexer.getCurStrVal();
292
293 // Consume the identifier, see what is after it.
294 if (Lexer.Lex() == asmtok::Colon) {
295 // identifier ':' -> Label.
296 Lexer.Lex();
297 return ParseStatement();
298 }
299
300 // Otherwise, we have a normal instruction or directive.
Chris Lattner2cf5f142009-06-22 01:29:09 +0000301 if (IDVal[0] == '.') {
302 Lexer.PrintMessage(IDLoc, "warning: ignoring directive for now");
303 EatToEndOfStatement();
304 return false;
305 }
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000306
Chris Lattner2cf5f142009-06-22 01:29:09 +0000307 // If it's an instruction, parse an operand list.
308 std::vector<X86Operand> Operands;
Chris Lattnerb0789ed2009-06-21 20:54:55 +0000309
Chris Lattner2cf5f142009-06-22 01:29:09 +0000310 // Read the first operand, if present. Note that we require a newline at the
311 // end of file, so we don't have to worry about Eof here.
312 if (Lexer.isNot(asmtok::EndOfStatement)) {
313 X86Operand Op;
314 if (ParseX86Operand(Op))
315 return true;
316 Operands.push_back(Op);
317 }
318
319 while (Lexer.is(asmtok::Comma)) {
320 Lexer.Lex(); // Eat the comma.
321
322 // Parse and remember the operand.
323 X86Operand Op;
324 if (ParseX86Operand(Op))
325 return true;
326 Operands.push_back(Op);
327 }
328
329 if (Lexer.isNot(asmtok::EndOfStatement))
330 return TokError("unexpected token in operand list");
331
332 // Eat the end of statement marker.
333 Lexer.Lex();
334
335 // Instruction is good, process it.
336 outs() << "Found instruction: " << IDVal << " with " << Operands.size()
337 << " operands.\n";
338
339 // Skip to end of line for now.
Chris Lattner27aa7d22009-06-21 20:16:42 +0000340 return false;
341}