blob: 9c0b4abb076b057ed1c03d3098871403d9463fbc [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
19bool AsmParser::Run() {
Chris Lattnerb0789ed2009-06-21 20:54:55 +000020 // Prime the lexer.
21 Lexer.Lex();
22
23 while (Lexer.isNot(asmtok::Eof))
24 if (ParseStatement())
25 return true;
26
27 return false;
28}
29
30
31/// ParseStatement:
32/// ::= EndOfStatement
33/// ::= Label* Identifier Operands* EndOfStatement
34bool AsmParser::ParseStatement() {
35 switch (Lexer.getKind()) {
36 default:
37 Lexer.PrintError(Lexer.getLoc(), "unexpected token at start of statement");
38 return true;
39 case asmtok::EndOfStatement:
40 Lexer.Lex();
41 return false;
42 case asmtok::Identifier:
43 break;
44 // TODO: Recurse on local labels etc.
45 }
46
47 // If we have an identifier, handle it as the key symbol.
48 //SMLoc IDLoc = Lexer.getLoc();
49 std::string IDVal = Lexer.getCurStrVal();
50
51 // Consume the identifier, see what is after it.
52 if (Lexer.Lex() == asmtok::Colon) {
53 // identifier ':' -> Label.
54 Lexer.Lex();
55 return ParseStatement();
56 }
57
58 // Otherwise, we have a normal instruction or directive.
59 if (IDVal[0] == '.')
60 outs() << "Found directive: " << IDVal << "\n";
61 else
62 outs() << "Found instruction: " << IDVal << "\n";
63
64 // Skip to end of line for now.
65 while (Lexer.isNot(asmtok::EndOfStatement) &&
66 Lexer.isNot(asmtok::Eof))
67 Lexer.Lex();
68
69 // Eat EOL.
70 if (Lexer.is(asmtok::EndOfStatement))
71 Lexer.Lex();
Chris Lattner27aa7d22009-06-21 20:16:42 +000072 return false;
73}