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 | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
Daniel Dunbar | dbd692a | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAsmLexer.h" |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 19 | #include "llvm/Support/DataTypes.h" |
| 20 | #include <string> |
| 21 | #include <cassert> |
| 22 | |
| 23 | namespace llvm { |
| 24 | class MemoryBuffer; |
| 25 | class SourceMgr; |
| 26 | class SMLoc; |
| 27 | |
| 28 | namespace asmtok { |
| 29 | enum TokKind { |
| 30 | // Markers |
| 31 | Eof, Error, |
| 32 | |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 33 | // String values. |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 34 | Identifier, |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 35 | Register, |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 36 | String, |
| 37 | |
| 38 | // Integer values. |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 39 | IntVal, |
| 40 | |
Chris Lattner | 10a907d | 2009-06-21 19:56:35 +0000 | [diff] [blame] | 41 | // No-value. |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 42 | EndOfStatement, |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 43 | Colon, |
Chris Lattner | 74ec1a3 | 2009-06-22 06:32:03 +0000 | [diff] [blame] | 44 | Plus, Minus, Tilde, |
Chris Lattner | 4651bca | 2009-06-21 19:21:25 +0000 | [diff] [blame] | 45 | Slash, // '/' |
| 46 | LParen, RParen, |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 47 | Star, Comma, Dollar, Equal, EqualEqual, |
Chris Lattner | 8dfbe6c | 2009-06-23 05:57:07 +0000 | [diff] [blame] | 48 | |
Daniel Dunbar | 475839e | 2009-06-29 20:37:27 +0000 | [diff] [blame] | 49 | Pipe, PipePipe, Caret, |
| 50 | Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, |
| 51 | Less, LessEqual, LessLess, LessGreater, |
| 52 | Greater, GreaterEqual, GreaterGreater |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 53 | }; |
| 54 | } |
| 55 | |
Daniel Dunbar | cb358b6 | 2009-07-28 03:00:54 +0000 | [diff] [blame^] | 56 | /// AsmToken - Target independent representation for an assembler token. |
| 57 | struct AsmToken { |
| 58 | asmtok::TokKind Kind; |
| 59 | |
| 60 | /// A reference to the entire token contents; this is always a pointer into |
| 61 | /// a memory buffer owned by the source manager. |
| 62 | StringRef Str; |
| 63 | |
| 64 | int64_t IntVal; |
| 65 | |
| 66 | public: |
| 67 | AsmToken() {} |
| 68 | AsmToken(asmtok::TokKind _Kind, const StringRef &_Str, int64_t _IntVal = 0) |
| 69 | : Kind(_Kind), Str(_Str), IntVal(_IntVal) {} |
| 70 | |
| 71 | asmtok::TokKind getKind() const { return Kind; } |
| 72 | bool is(asmtok::TokKind K) const { return Kind == K; } |
| 73 | bool isNot(asmtok::TokKind K) const { return Kind != K; } |
| 74 | |
| 75 | SMLoc getLoc() const; |
| 76 | |
| 77 | StringRef getString() const { return Str; } |
| 78 | |
| 79 | int64_t getIntVal() const { |
| 80 | assert(Kind == asmtok::IntVal && "This token isn't an integer"); |
| 81 | return IntVal; |
| 82 | } |
| 83 | }; |
| 84 | |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 85 | /// AsmLexer - Lexer class for assembly files. |
Daniel Dunbar | dbd692a | 2009-07-20 20:01:54 +0000 | [diff] [blame] | 86 | class AsmLexer : public MCAsmLexer { |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 87 | SourceMgr &SrcMgr; |
| 88 | |
| 89 | const char *CurPtr; |
| 90 | const MemoryBuffer *CurBuf; |
| 91 | |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 92 | const char *TokStart; |
Daniel Dunbar | cb358b6 | 2009-07-28 03:00:54 +0000 | [diff] [blame^] | 93 | |
| 94 | /// The current token. |
| 95 | AsmToken CurTok; |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 96 | |
Daniel Dunbar | cb358b6 | 2009-07-28 03:00:54 +0000 | [diff] [blame^] | 97 | /// This is the current buffer index we're lexing from as managed by the |
| 98 | /// SourceMgr object. |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 99 | int CurBuffer; |
| 100 | |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 101 | void operator=(const AsmLexer&); // DO NOT IMPLEMENT |
| 102 | AsmLexer(const AsmLexer&); // DO NOT IMPLEMENT |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 103 | public: |
| 104 | AsmLexer(SourceMgr &SrcMgr); |
Chris Lattner | faf32c1 | 2009-06-24 00:33:19 +0000 | [diff] [blame] | 105 | ~AsmLexer(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 106 | |
| 107 | asmtok::TokKind Lex() { |
Daniel Dunbar | cb358b6 | 2009-07-28 03:00:54 +0000 | [diff] [blame^] | 108 | return CurTok = LexToken(), getKind(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Daniel Dunbar | cb358b6 | 2009-07-28 03:00:54 +0000 | [diff] [blame^] | 111 | asmtok::TokKind getKind() const { return CurTok.getKind(); } |
| 112 | bool is(asmtok::TokKind K) const { return CurTok.is(K); } |
| 113 | bool isNot(asmtok::TokKind K) const { return CurTok.isNot(K); } |
Daniel Dunbar | 9a7e2cc | 2009-07-27 21:49:56 +0000 | [diff] [blame] | 114 | |
| 115 | /// getCurStrVal - Get the string for the current token, this includes all |
| 116 | /// characters (for example, the quotes on strings) in the token. |
| 117 | /// |
| 118 | /// The returned StringRef points into the source manager's memory buffer, and |
| 119 | /// is safe to store across calls to Lex(). |
| 120 | StringRef getCurStrVal() const { |
Daniel Dunbar | cb358b6 | 2009-07-28 03:00:54 +0000 | [diff] [blame^] | 121 | return CurTok.getString(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 122 | } |
| 123 | int64_t getCurIntVal() const { |
Daniel Dunbar | cb358b6 | 2009-07-28 03:00:54 +0000 | [diff] [blame^] | 124 | return CurTok.getIntVal(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | SMLoc getLoc() const; |
| 128 | |
Chris Lattner | 8e25e2d | 2009-07-16 06:14:39 +0000 | [diff] [blame] | 129 | /// EnterIncludeFile - Enter the specified file. This returns true on failure. |
| 130 | bool EnterIncludeFile(const std::string &Filename); |
| 131 | |
Daniel Dunbar | 3fb7683 | 2009-06-30 00:49:23 +0000 | [diff] [blame] | 132 | void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const; |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 133 | |
| 134 | private: |
| 135 | int getNextChar(); |
Daniel Dunbar | cb358b6 | 2009-07-28 03:00:54 +0000 | [diff] [blame^] | 136 | AsmToken ReturnError(const char *Loc, const std::string &Msg); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 137 | |
| 138 | /// LexToken - Read the next token and return its code. |
Daniel Dunbar | cb358b6 | 2009-07-28 03:00:54 +0000 | [diff] [blame^] | 139 | AsmToken LexToken(); |
| 140 | AsmToken LexIdentifier(); |
| 141 | AsmToken LexPercent(); |
| 142 | AsmToken LexSlash(); |
| 143 | AsmToken LexLineComment(); |
| 144 | AsmToken LexDigit(); |
| 145 | AsmToken LexQuote(); |
Chris Lattner | a59e877 | 2009-06-21 07:19:10 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | } // end namespace llvm |
| 149 | |
| 150 | #endif |