1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements the parser for assembly files.
12 //===----------------------------------------------------------------------===//
14 #include "AsmParser.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/raw_ostream.h"
19 bool AsmParser::Error(SMLoc L, const char *Msg) {
20 Lexer.PrintMessage(L, Msg);
24 bool AsmParser::TokError(const char *Msg) {
25 Lexer.PrintMessage(Lexer.getLoc(), Msg);
29 bool AsmParser::Run() {
33 while (Lexer.isNot(asmtok::Eof))
40 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
41 void AsmParser::EatToEndOfStatement() {
42 while (Lexer.isNot(asmtok::EndOfStatement) &&
43 Lexer.isNot(asmtok::Eof))
47 if (Lexer.is(asmtok::EndOfStatement))
52 struct AsmParser::X86Operand {
65 // FIXME: Should be a general expression.
71 int64_t Disp; // FIXME: Should be a general expression.
78 static X86Operand CreateReg(unsigned RegNo) {
81 Res.Reg.RegNo = RegNo;
84 static X86Operand CreateImm(int64_t Val) {
90 static X86Operand CreateMem(unsigned SegReg, int64_t Disp, unsigned BaseReg,
91 unsigned Scale, unsigned ScaleReg) {
94 Res.Mem.SegReg = SegReg;
96 Res.Mem.BaseReg = BaseReg;
97 Res.Mem.Scale = Scale;
98 Res.Mem.ScaleReg = ScaleReg;
103 bool AsmParser::ParseX86Operand(X86Operand &Op) {
104 switch (Lexer.getKind()) {
106 return ParseX86MemOperand(Op);
107 case asmtok::Register:
108 // FIXME: Decode reg #.
109 // FIXME: if a segment register, this could either be just the seg reg, or
110 // the start of a memory operand.
111 Op = X86Operand::CreateReg(123);
112 Lexer.Lex(); // Eat register.
114 case asmtok::Dollar: {
118 if (ParseExpression(Val))
119 return TokError("expected integer constant");
120 Op = X86Operand::CreateReg(Val);
123 Lexer.Lex(); // Eat the star.
125 if (Lexer.is(asmtok::Register)) {
126 Op = X86Operand::CreateReg(123);
127 Lexer.Lex(); // Eat register.
128 } else if (ParseX86MemOperand(Op))
131 // FIXME: Note that these are 'dereferenced' so that clients know the '*' is
138 /// ParseX86MemOperand: segment: disp(basereg, indexreg, scale)
139 bool AsmParser::ParseX86MemOperand(X86Operand &Op) {
140 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
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
149 if (Lexer.isNot(asmtok::LParen)) {
150 if (ParseExpression(Disp)) return true;
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);
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 '('.
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.
170 // FIXME: Call ParseParenExpression with the leading ( consumed.
171 return TokError("FIXME: Paren expr not implemented yet!");
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;
179 if (Lexer.is(asmtok::Register)) {
180 BaseReg = 123; // FIXME: decode reg #
181 Lexer.Lex(); // eat the register.
184 if (Lexer.is(asmtok::Comma)) {
185 Lexer.Lex(); // eat the comma.
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.
193 if (Lexer.is(asmtok::Comma)) {
194 Lexer.Lex(); // eat the comma.
196 // 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;
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 ')'.
212 Op = X86Operand::CreateMem(SegReg, Disp, BaseReg, Scale, ScaleReg);
216 /// ParseParenExpr - Parse a paren expression and return it.
217 /// NOTE: This assumes the leading '(' has already been consumed.
219 /// parenexpr ::= expr)
221 bool AsmParser::ParseParenExpr(int64_t &Res) {
222 if (ParseExpression(Res)) return true;
223 if (Lexer.isNot(asmtok::RParen))
224 return TokError("expected ')' in parentheses expression");
229 /// ParsePrimaryExpr - Parse a primary expression and return it.
230 /// primaryexpr ::= (parenexpr
231 /// primaryexpr ::= symbol
232 /// primaryexpr ::= number
233 /// primaryexpr ::= ~,+,- primaryexpr
234 bool AsmParser::ParsePrimaryExpr(int64_t &Res) {
235 switch (Lexer.getKind()) {
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
242 Lexer.Lex(); // Eat identifier.
245 Res = Lexer.getCurIntVal();
246 Lexer.Lex(); // Eat identifier.
249 Lexer.Lex(); // Eat the '('.
250 return ParseParenExpr(Res);
254 Lexer.Lex(); // Eat the operator.
255 return ParsePrimaryExpr(Res);
259 /// ParseExpression - Parse an expression and return it.
261 /// expr ::= expr +,- expr -> lowest.
262 /// expr ::= expr |,^,&,! expr -> middle.
263 /// expr ::= expr *,/,%,<<,>> expr -> highest.
264 /// expr ::= primaryexpr
266 bool AsmParser::ParseExpression(int64_t &Res) {
267 return ParsePrimaryExpr(Res);
274 /// ::= EndOfStatement
275 /// ::= Label* Directive ...Operands... EndOfStatement
276 /// ::= Label* Identifier OperandList* EndOfStatement
277 bool AsmParser::ParseStatement() {
278 switch (Lexer.getKind()) {
280 return TokError("unexpected token at start of statement");
281 case asmtok::EndOfStatement:
284 case asmtok::Identifier:
286 // TODO: Recurse on local labels etc.
289 // If we have an identifier, handle it as the key symbol.
290 SMLoc IDLoc = Lexer.getLoc();
291 std::string IDVal = Lexer.getCurStrVal();
293 // Consume the identifier, see what is after it.
294 if (Lexer.Lex() == asmtok::Colon) {
295 // identifier ':' -> Label.
297 return ParseStatement();
300 // Otherwise, we have a normal instruction or directive.
301 if (IDVal[0] == '.') {
302 Lexer.PrintMessage(IDLoc, "warning: ignoring directive for now");
303 EatToEndOfStatement();
307 // If it's an instruction, parse an operand list.
308 std::vector<X86Operand> Operands;
310 // 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)) {
314 if (ParseX86Operand(Op))
316 Operands.push_back(Op);
319 while (Lexer.is(asmtok::Comma)) {
320 Lexer.Lex(); // Eat the comma.
322 // Parse and remember the operand.
324 if (ParseX86Operand(Op))
326 Operands.push_back(Op);
329 if (Lexer.isNot(asmtok::EndOfStatement))
330 return TokError("unexpected token in operand list");
332 // Eat the end of statement marker.
335 // Instruction is good, process it.
336 outs() << "Found instruction: " << IDVal << " with " << Operands.size()
339 // Skip to end of line for now.