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, |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 45 | Star, Comma, Dollar, Equal, EqualEqual, |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 46 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame^] | 47 | Pipe, PipePipe, Caret, |
| 48 | Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, |
| 49 | Less, LessEqual, LessLess, LessGreater, |
| 50 | Greater, GreaterEqual, GreaterGreater |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 51 | }; |
| 52 | } |
| 53 | |
| 54 | /// AsmLexer - Lexer class for assembly files. |
| 55 | class AsmLexer { |
| 56 | SourceMgr &SrcMgr; |
| 57 | |
| 58 | const char *CurPtr; |
| 59 | const MemoryBuffer *CurBuf; |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 60 | // A llvm::StringSet<>, which provides uniqued and null-terminated strings. |
| 61 | void *TheStringSet; |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 62 | |
| 63 | // Information about the current token. |
| 64 | const char *TokStart; |
| 65 | asmtok::TokKind CurKind; |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 66 | const char *CurStrVal; // This is valid for Identifier. |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 67 | int64_t CurIntVal; |
| 68 | |
| 69 | /// CurBuffer - This is the current buffer index we're lexing from as managed |
| 70 | /// by the SourceMgr object. |
| 71 | int CurBuffer; |
| 72 | |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 73 | void operator=(const AsmLexer&); // DO NOT IMPLEMENT |
| 74 | AsmLexer(const AsmLexer&); // DO NOT IMPLEMENT |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 75 | public: |
| 76 | AsmLexer(SourceMgr &SrcMgr); |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 77 | ~AsmLexer(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 78 | |
| 79 | asmtok::TokKind Lex() { |
| 80 | return CurKind = LexToken(); |
| 81 | } |
| 82 | |
| 83 | asmtok::TokKind getKind() const { return CurKind; } |
Chris Lattner | b0789ed | 2009-06-21 20:54:55 +0000 | [diff] [blame] | 84 | bool is(asmtok::TokKind K) const { return CurKind == K; } |
| 85 | bool isNot(asmtok::TokKind K) const { return CurKind != K; } |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 86 | |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 87 | const char *getCurStrVal() const { |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 88 | assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register || |
| 89 | CurKind == asmtok::String) && |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 90 | "This token doesn't have a string value"); |
| 91 | return CurStrVal; |
| 92 | } |
| 93 | int64_t getCurIntVal() const { |
| 94 | assert(CurKind == asmtok::IntVal && "This token isn't an integer"); |
| 95 | return CurIntVal; |
| 96 | } |
| 97 | |
| 98 | SMLoc getLoc() const; |
| 99 | |
Chris Lattner | 14ee48a | 2009-06-21 21:22:11 +0000 | [diff] [blame] | 100 | void PrintMessage(SMLoc Loc, const std::string &Msg) const; |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 101 | |
| 102 | private: |
| 103 | int getNextChar(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 104 | asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 105 | |
| 106 | /// LexToken - Read the next token and return its code. |
| 107 | asmtok::TokKind LexToken(); |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 108 | asmtok::TokKind LexIdentifier(); |
| 109 | asmtok::TokKind LexPercent(); |
| 110 | asmtok::TokKind LexSlash(); |
| 111 | asmtok::TokKind LexHash(); |
| 112 | asmtok::TokKind LexDigit(); |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 113 | asmtok::TokKind LexQuote(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } // end namespace llvm |
| 117 | |
| 118 | #endif |