diff options
author | Chris Lattner <sabre@nondot.org> | 2009-06-21 20:54:55 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2009-06-21 20:54:55 +0000 |
commit | b0789ed5a45a90ee7ff398fcdc1383b49ee88918 (patch) | |
tree | 4a85b9feab5e6fd6683d486ce016495a7b4aabd4 /tools | |
parent | 27aa7d259b416a9d1bf837ed2c3c11463367b11c (diff) |
set up the top-level parsing loop.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73860 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools')
-rw-r--r-- | tools/llvm-mc/AsmLexer.h | 2 | ||||
-rw-r--r-- | tools/llvm-mc/AsmParser.cpp | 54 | ||||
-rw-r--r-- | tools/llvm-mc/AsmParser.h | 3 |
3 files changed, 59 insertions, 0 deletions
diff --git a/tools/llvm-mc/AsmLexer.h b/tools/llvm-mc/AsmLexer.h index c5d1722e49..c668e8639c 100644 --- a/tools/llvm-mc/AsmLexer.h +++ b/tools/llvm-mc/AsmLexer.h @@ -73,6 +73,8 @@ public: } asmtok::TokKind getKind() const { return CurKind; } + bool is(asmtok::TokKind K) const { return CurKind == K; } + bool isNot(asmtok::TokKind K) const { return CurKind != K; } const std::string &getCurStrVal() const { assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register || diff --git a/tools/llvm-mc/AsmParser.cpp b/tools/llvm-mc/AsmParser.cpp index abb0d35b08..9c0b4abb07 100644 --- a/tools/llvm-mc/AsmParser.cpp +++ b/tools/llvm-mc/AsmParser.cpp @@ -12,8 +12,62 @@ //===----------------------------------------------------------------------===// #include "AsmParser.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/raw_ostream.h" using namespace llvm; bool AsmParser::Run() { + // Prime the lexer. + Lexer.Lex(); + + while (Lexer.isNot(asmtok::Eof)) + if (ParseStatement()) + return true; + + return false; +} + + +/// ParseStatement: +/// ::= EndOfStatement +/// ::= Label* Identifier Operands* EndOfStatement +bool AsmParser::ParseStatement() { + switch (Lexer.getKind()) { + default: + Lexer.PrintError(Lexer.getLoc(), "unexpected token at start of statement"); + return true; + case asmtok::EndOfStatement: + Lexer.Lex(); + return false; + case asmtok::Identifier: + break; + // TODO: Recurse on local labels etc. + } + + // If we have an identifier, handle it as the key symbol. + //SMLoc IDLoc = Lexer.getLoc(); + std::string IDVal = Lexer.getCurStrVal(); + + // Consume the identifier, see what is after it. + if (Lexer.Lex() == asmtok::Colon) { + // identifier ':' -> Label. + Lexer.Lex(); + return ParseStatement(); + } + + // Otherwise, we have a normal instruction or directive. + if (IDVal[0] == '.') + outs() << "Found directive: " << IDVal << "\n"; + else + outs() << "Found instruction: " << IDVal << "\n"; + + // Skip to end of line for now. + while (Lexer.isNot(asmtok::EndOfStatement) && + Lexer.isNot(asmtok::Eof)) + Lexer.Lex(); + + // Eat EOL. + if (Lexer.is(asmtok::EndOfStatement)) + Lexer.Lex(); return false; } diff --git a/tools/llvm-mc/AsmParser.h b/tools/llvm-mc/AsmParser.h index adcd74c7a5..5e1f6c6562 100644 --- a/tools/llvm-mc/AsmParser.h +++ b/tools/llvm-mc/AsmParser.h @@ -27,6 +27,9 @@ public: bool Run(); +private: + bool ParseStatement(); + }; } // end namespace llvm |