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 | |
Daniel Dunbar | dbd692a | 2009-07-20 20:01:54 +0000 | [diff] [blame^] | 17 | #include "llvm/MC/MCAsmLexer.h" |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 18 | #include "llvm/Support/DataTypes.h" |
| 19 | #include <string> |
| 20 | #include <cassert> |
| 21 | |
| 22 | namespace llvm { |
| 23 | class MemoryBuffer; |
| 24 | class SourceMgr; |
| 25 | class SMLoc; |
| 26 | |
| 27 | namespace asmtok { |
| 28 | enum TokKind { |
| 29 | // Markers |
| 30 | Eof, Error, |
| 31 | |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 32 | // String values. |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 33 | Identifier, |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 34 | Register, |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 35 | String, |
| 36 | |
| 37 | // Integer values. |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 38 | IntVal, |
| 39 | |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 40 | // No-value. |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 41 | EndOfStatement, |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 42 | Colon, |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 43 | Plus, Minus, Tilde, |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 44 | Slash, // '/' |
| 45 | LParen, RParen, |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 46 | Star, Comma, Dollar, Equal, EqualEqual, |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 47 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 48 | Pipe, PipePipe, Caret, |
| 49 | Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, |
| 50 | Less, LessEqual, LessLess, LessGreater, |
| 51 | Greater, GreaterEqual, GreaterGreater |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 52 | }; |
| 53 | } |
| 54 | |
| 55 | /// AsmLexer - Lexer class for assembly files. |
Daniel Dunbar | dbd692a | 2009-07-20 20:01:54 +0000 | [diff] [blame^] | 56 | class AsmLexer : public MCAsmLexer { |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 57 | SourceMgr &SrcMgr; |
| 58 | |
| 59 | const char *CurPtr; |
| 60 | const MemoryBuffer *CurBuf; |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 61 | // A llvm::StringSet<>, which provides uniqued and null-terminated strings. |
| 62 | void *TheStringSet; |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 63 | |
| 64 | // Information about the current token. |
| 65 | const char *TokStart; |
| 66 | asmtok::TokKind CurKind; |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 67 | const char *CurStrVal; // This is valid for Identifier. |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 68 | int64_t CurIntVal; |
| 69 | |
| 70 | /// CurBuffer - This is the current buffer index we're lexing from as managed |
| 71 | /// by the SourceMgr object. |
| 72 | int CurBuffer; |
| 73 | |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 74 | void operator=(const AsmLexer&); // DO NOT IMPLEMENT |
| 75 | AsmLexer(const AsmLexer&); // DO NOT IMPLEMENT |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 76 | public: |
| 77 | AsmLexer(SourceMgr &SrcMgr); |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 78 | ~AsmLexer(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 79 | |
| 80 | asmtok::TokKind Lex() { |
| 81 | return CurKind = LexToken(); |
| 82 | } |
| 83 | |
| 84 | asmtok::TokKind getKind() const { return CurKind; } |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 85 | bool is(asmtok::TokKind K) const { return CurKind == K; } |
| 86 | bool isNot(asmtok::TokKind K) const { return CurKind != K; } |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 87 | |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 88 | const char *getCurStrVal() const { |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 89 | assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register || |
| 90 | CurKind == asmtok::String) && |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 91 | "This token doesn't have a string value"); |
| 92 | return CurStrVal; |
| 93 | } |
| 94 | int64_t getCurIntVal() const { |
| 95 | assert(CurKind == asmtok::IntVal && "This token isn't an integer"); |
| 96 | return CurIntVal; |
| 97 | } |
| 98 | |
| 99 | SMLoc getLoc() const; |
| 100 | |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 101 | /// EnterIncludeFile - Enter the specified file. This returns true on failure. |
| 102 | bool EnterIncludeFile(const std::string &Filename); |
| 103 | |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 104 | void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const; |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 105 | |
| 106 | private: |
| 107 | int getNextChar(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 108 | asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 109 | |
| 110 | /// LexToken - Read the next token and return its code. |
| 111 | asmtok::TokKind LexToken(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 112 | asmtok::TokKind LexIdentifier(); |
| 113 | asmtok::TokKind LexPercent(); |
| 114 | asmtok::TokKind LexSlash(); |
Daniel Dunbar | 1ad7edc | 2009-06-29 22:00:57 +0000 | [diff] [blame] | 115 | asmtok::TokKind LexLineComment(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 116 | asmtok::TokKind LexDigit(); |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 117 | asmtok::TokKind LexQuote(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | } // end namespace llvm |
| 121 | |
| 122 | #endif |