Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 1 | //===- AsmLexer.h - Lexer for Assembly Files --------------------*- C++ -*-===// |
| 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 declares the lexer for assembly files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef ASMLEXER_H |
| 15 | #define ASMLEXER_H |
| 16 | |
| 17 | #include "llvm/Support/DataTypes.h" |
| 18 | #include <string> |
| 19 | #include <cassert> |
| 20 | |
| 21 | namespace llvm { |
| 22 | class MemoryBuffer; |
| 23 | class SourceMgr; |
| 24 | class SMLoc; |
| 25 | |
| 26 | namespace asmtok { |
| 27 | enum TokKind { |
| 28 | // Markers |
| 29 | Eof, Error, |
| 30 | |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 31 | // String values. |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 32 | Identifier, |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 33 | Register, |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 34 | String, |
| 35 | |
| 36 | // Integer values. |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 37 | IntVal, |
| 38 | |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 39 | // No-value. |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 40 | EndOfStatement, |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 41 | Colon, |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 42 | Plus, Minus, Tilde, |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 43 | Slash, // '/' |
| 44 | LParen, RParen, |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 45 | Star, Comma, Dollar, |
| 46 | |
| 47 | Pipe, Caret, Amp, Exclaim, |
| 48 | Percent, LessLess, GreaterGreater |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 49 | }; |
| 50 | } |
| 51 | |
| 52 | /// AsmLexer - Lexer class for assembly files. |
| 53 | class AsmLexer { |
| 54 | SourceMgr &SrcMgr; |
| 55 | |
| 56 | const char *CurPtr; |
| 57 | const MemoryBuffer *CurBuf; |
| 58 | |
| 59 | // Information about the current token. |
| 60 | const char *TokStart; |
| 61 | asmtok::TokKind CurKind; |
| 62 | std::string CurStrVal; // This is valid for Identifier. |
| 63 | int64_t CurIntVal; |
| 64 | |
| 65 | /// CurBuffer - This is the current buffer index we're lexing from as managed |
| 66 | /// by the SourceMgr object. |
| 67 | int CurBuffer; |
| 68 | |
| 69 | public: |
| 70 | AsmLexer(SourceMgr &SrcMgr); |
| 71 | ~AsmLexer() {} |
| 72 | |
| 73 | asmtok::TokKind Lex() { |
| 74 | return CurKind = LexToken(); |
| 75 | } |
| 76 | |
| 77 | asmtok::TokKind getKind() const { return CurKind; } |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 78 | bool is(asmtok::TokKind K) const { return CurKind == K; } |
| 79 | bool isNot(asmtok::TokKind K) const { return CurKind != K; } |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 80 | |
| 81 | const std::string &getCurStrVal() const { |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 82 | assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register || |
| 83 | CurKind == asmtok::String) && |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 84 | "This token doesn't have a string value"); |
| 85 | return CurStrVal; |
| 86 | } |
| 87 | int64_t getCurIntVal() const { |
| 88 | assert(CurKind == asmtok::IntVal && "This token isn't an integer"); |
| 89 | return CurIntVal; |
| 90 | } |
| 91 | |
| 92 | SMLoc getLoc() const; |
| 93 | |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 94 | void PrintMessage(SMLoc Loc, const std::string &Msg) const; |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 95 | |
| 96 | private: |
| 97 | int getNextChar(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 98 | asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 99 | |
| 100 | /// LexToken - Read the next token and return its code. |
| 101 | asmtok::TokKind LexToken(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 102 | asmtok::TokKind LexIdentifier(); |
| 103 | asmtok::TokKind LexPercent(); |
| 104 | asmtok::TokKind LexSlash(); |
| 105 | asmtok::TokKind LexHash(); |
| 106 | asmtok::TokKind LexDigit(); |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 107 | asmtok::TokKind LexQuote(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | } // end namespace llvm |
| 111 | |
| 112 | #endif |