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