|
13 | 13 | #ifndef LLVM_MC_MCPARSER_ASMLEXER_H
|
14 | 14 | #define LLVM_MC_MCPARSER_ASMLEXER_H
|
15 | 15 |
|
| 16 | +#include "llvm/ADT/ArrayRef.h" |
| 17 | +#include "llvm/ADT/SmallVector.h" |
16 | 18 | #include "llvm/ADT/StringRef.h"
|
17 |
| -#include "llvm/MC/MCParser/MCAsmLexer.h" |
| 19 | +#include "llvm/MC/MCAsmMacro.h" |
| 20 | +#include <cassert> |
| 21 | +#include <cstddef> |
18 | 22 | #include <string>
|
| 23 | +#include <utility> |
19 | 24 |
|
20 | 25 | namespace llvm {
|
21 | 26 |
|
22 | 27 | class MCAsmInfo;
|
23 | 28 |
|
| 29 | +/// A callback class which is notified of each comment in an assembly file as |
| 30 | +/// it is lexed. |
| 31 | +class AsmCommentConsumer { |
| 32 | +public: |
| 33 | + virtual ~AsmCommentConsumer() = default; |
| 34 | + |
| 35 | + /// Callback function for when a comment is lexed. Loc is the start of the |
| 36 | + /// comment text (excluding the comment-start marker). CommentText is the text |
| 37 | + /// of the comment, excluding the comment start and end markers, and the |
| 38 | + /// newline for single-line comments. |
| 39 | + virtual void HandleComment(SMLoc Loc, StringRef CommentText) = 0; |
| 40 | +}; |
| 41 | + |
| 42 | +/// Generic assembler lexer interface, for use by target specific assembly |
| 43 | +/// lexers. |
| 44 | +class MCAsmLexer { |
| 45 | + /// The current token, stored in the base class for faster access. |
| 46 | + SmallVector<AsmToken, 1> CurTok; |
| 47 | + |
| 48 | + /// The location and description of the current error |
| 49 | + SMLoc ErrLoc; |
| 50 | + std::string Err; |
| 51 | + |
| 52 | +protected: // Can only create subclasses. |
| 53 | + const char *TokStart = nullptr; |
| 54 | + bool SkipSpace = true; |
| 55 | + bool AllowAtInIdentifier = false; |
| 56 | + bool AllowHashInIdentifier = false; |
| 57 | + bool IsAtStartOfStatement = true; |
| 58 | + bool LexMasmHexFloats = false; |
| 59 | + bool LexMasmIntegers = false; |
| 60 | + bool LexMasmStrings = false; |
| 61 | + bool LexMotorolaIntegers = false; |
| 62 | + bool UseMasmDefaultRadix = false; |
| 63 | + unsigned DefaultRadix = 10; |
| 64 | + bool LexHLASMIntegers = false; |
| 65 | + bool LexHLASMStrings = false; |
| 66 | + AsmCommentConsumer *CommentConsumer = nullptr; |
| 67 | + |
| 68 | + MCAsmLexer(); |
| 69 | + |
| 70 | + virtual AsmToken LexToken() = 0; |
| 71 | + |
| 72 | + void SetError(SMLoc errLoc, const std::string &err) { |
| 73 | + ErrLoc = errLoc; |
| 74 | + Err = err; |
| 75 | + } |
| 76 | + |
| 77 | +public: |
| 78 | + MCAsmLexer(const MCAsmLexer &) = delete; |
| 79 | + MCAsmLexer &operator=(const MCAsmLexer &) = delete; |
| 80 | + virtual ~MCAsmLexer(); |
| 81 | + |
| 82 | + /// Consume the next token from the input stream and return it. |
| 83 | + /// |
| 84 | + /// The lexer will continuously return the end-of-file token once the end of |
| 85 | + /// the main input file has been reached. |
| 86 | + const AsmToken &Lex() { |
| 87 | + assert(!CurTok.empty()); |
| 88 | + // Mark if we parsing out a EndOfStatement. |
| 89 | + IsAtStartOfStatement = CurTok.front().getKind() == AsmToken::EndOfStatement; |
| 90 | + CurTok.erase(CurTok.begin()); |
| 91 | + // LexToken may generate multiple tokens via UnLex but will always return |
| 92 | + // the first one. Place returned value at head of CurTok vector. |
| 93 | + if (CurTok.empty()) { |
| 94 | + AsmToken T = LexToken(); |
| 95 | + CurTok.insert(CurTok.begin(), T); |
| 96 | + } |
| 97 | + return CurTok.front(); |
| 98 | + } |
| 99 | + |
| 100 | + void UnLex(AsmToken const &Token) { |
| 101 | + IsAtStartOfStatement = false; |
| 102 | + CurTok.insert(CurTok.begin(), Token); |
| 103 | + } |
| 104 | + |
| 105 | + bool isAtStartOfStatement() { return IsAtStartOfStatement; } |
| 106 | + |
| 107 | + virtual StringRef LexUntilEndOfStatement() = 0; |
| 108 | + |
| 109 | + /// Get the current source location. |
| 110 | + SMLoc getLoc() const; |
| 111 | + |
| 112 | + /// Get the current (last) lexed token. |
| 113 | + const AsmToken &getTok() const { return CurTok[0]; } |
| 114 | + |
| 115 | + /// Look ahead at the next token to be lexed. |
| 116 | + const AsmToken peekTok(bool ShouldSkipSpace = true) { |
| 117 | + AsmToken Tok; |
| 118 | + |
| 119 | + MutableArrayRef<AsmToken> Buf(Tok); |
| 120 | + size_t ReadCount = peekTokens(Buf, ShouldSkipSpace); |
| 121 | + |
| 122 | + assert(ReadCount == 1); |
| 123 | + (void)ReadCount; |
| 124 | + |
| 125 | + return Tok; |
| 126 | + } |
| 127 | + |
| 128 | + /// Look ahead an arbitrary number of tokens. |
| 129 | + virtual size_t peekTokens(MutableArrayRef<AsmToken> Buf, |
| 130 | + bool ShouldSkipSpace = true) = 0; |
| 131 | + |
| 132 | + /// Get the current error location |
| 133 | + SMLoc getErrLoc() { return ErrLoc; } |
| 134 | + |
| 135 | + /// Get the current error string |
| 136 | + const std::string &getErr() { return Err; } |
| 137 | + |
| 138 | + /// Get the kind of current token. |
| 139 | + AsmToken::TokenKind getKind() const { return getTok().getKind(); } |
| 140 | + |
| 141 | + /// Check if the current token has kind \p K. |
| 142 | + bool is(AsmToken::TokenKind K) const { return getTok().is(K); } |
| 143 | + |
| 144 | + /// Check if the current token has kind \p K. |
| 145 | + bool isNot(AsmToken::TokenKind K) const { return getTok().isNot(K); } |
| 146 | + |
| 147 | + /// Set whether spaces should be ignored by the lexer |
| 148 | + void setSkipSpace(bool val) { SkipSpace = val; } |
| 149 | + |
| 150 | + bool getAllowAtInIdentifier() { return AllowAtInIdentifier; } |
| 151 | + void setAllowAtInIdentifier(bool v) { AllowAtInIdentifier = v; } |
| 152 | + |
| 153 | + void setAllowHashInIdentifier(bool V) { AllowHashInIdentifier = V; } |
| 154 | + |
| 155 | + void setCommentConsumer(AsmCommentConsumer *CommentConsumer) { |
| 156 | + this->CommentConsumer = CommentConsumer; |
| 157 | + } |
| 158 | + |
| 159 | + /// Set whether to lex masm-style binary (e.g., 0b1101) and radix-specified |
| 160 | + /// literals (e.g., 0ABCh [hex], 576t [decimal], 77o [octal], 1101y [binary]). |
| 161 | + void setLexMasmIntegers(bool V) { LexMasmIntegers = V; } |
| 162 | + |
| 163 | + /// Set whether to use masm-style default-radix integer literals. If disabled, |
| 164 | + /// assume decimal unless prefixed (e.g., 0x2c [hex], 077 [octal]). |
| 165 | + void useMasmDefaultRadix(bool V) { UseMasmDefaultRadix = V; } |
| 166 | + |
| 167 | + unsigned getMasmDefaultRadix() const { return DefaultRadix; } |
| 168 | + void setMasmDefaultRadix(unsigned Radix) { DefaultRadix = Radix; } |
| 169 | + |
| 170 | + /// Set whether to lex masm-style hex float literals, such as 3f800000r. |
| 171 | + void setLexMasmHexFloats(bool V) { LexMasmHexFloats = V; } |
| 172 | + |
| 173 | + /// Set whether to lex masm-style string literals, such as 'Can''t find file' |
| 174 | + /// and "This ""value"" not found". |
| 175 | + void setLexMasmStrings(bool V) { LexMasmStrings = V; } |
| 176 | + |
| 177 | + /// Set whether to lex Motorola-style integer literals, such as $deadbeef or |
| 178 | + /// %01010110. |
| 179 | + void setLexMotorolaIntegers(bool V) { LexMotorolaIntegers = V; } |
| 180 | + |
| 181 | + /// Set whether to lex HLASM-flavour integers. For now this is only [0-9]* |
| 182 | + void setLexHLASMIntegers(bool V) { LexHLASMIntegers = V; } |
| 183 | + |
| 184 | + /// Set whether to "lex" HLASM-flavour character and string literals. For now, |
| 185 | + /// setting this option to true, will disable lexing for character and string |
| 186 | + /// literals. |
| 187 | + void setLexHLASMStrings(bool V) { LexHLASMStrings = V; } |
| 188 | +}; |
| 189 | + |
24 | 190 | /// AsmLexer - Lexer class for assembly files.
|
25 |
| -class AsmLexer : public MCAsmLexer { |
| 191 | +class AsmLexer final : public MCAsmLexer { |
26 | 192 | const MCAsmInfo &MAI;
|
27 | 193 |
|
28 | 194 | const char *CurPtr = nullptr;
|
|
0 commit comments