Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 1 | //===- 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 Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 15 | #include "llvm/Support/SourceMgr.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame^] | 19 | bool AsmParser::Error(SMLoc L, const char *Msg) { |
| 20 | Lexer.PrintMessage(L, Msg); |
| 21 | return true; |
| 22 | } |
| 23 | |
| 24 | bool AsmParser::TokError(const char *Msg) { |
| 25 | Lexer.PrintMessage(Lexer.getLoc(), Msg); |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | |
Chris Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 30 | bool AsmParser::Run() { |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 31 | // 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 |
| 45 | bool AsmParser::ParseStatement() { |
| 46 | switch (Lexer.getKind()) { |
| 47 | default: |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame^] | 48 | return TokError("unexpected token at start of statement"); |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 49 | 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 Lattner | 27aa7d2 | 2009-06-21 20:16:42 +0000 | [diff] [blame] | 82 | return false; |
| 83 | } |