LLVM 24.0.0git
AsmLexer.h
Go to the documentation of this file.
1//===- AsmLexer.h - Lexer for Assembly Files --------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This class declares the lexer for assembly files.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_MC_MCPARSER_ASMLEXER_H
14#define LLVM_MC_MCPARSER_ASMLEXER_H
15
16#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/MC/MCAsmMacro.h"
22#include <cassert>
23#include <cstddef>
24#include <string>
25
26namespace llvm {
27
28class MCAsmInfo;
29
30/// A callback class which is notified of each comment in an assembly file as
31/// it is lexed.
33public:
34 virtual ~AsmCommentConsumer() = default;
35
36 /// Callback function for when a comment is lexed. Loc is the start of the
37 /// comment text (excluding the comment-start marker). CommentText is the text
38 /// of the comment, excluding the comment start and end markers, and the
39 /// newline for single-line comments.
40 virtual void HandleComment(SMLoc Loc, StringRef CommentText) = 0;
41};
42
43class AsmLexer {
44 /// The current token, stored in the base class for faster access.
46
47 const char *CurPtr = nullptr;
48 /// NULL-terminated buffer. NULL terminator must reside at `CurBuf.end()`.
49 StringRef CurBuf;
50
51 /// The location and description of the current error
52 SMLoc ErrLoc;
53 std::string Err;
54
55 const MCAsmInfo &MAI;
56
57 bool IsAtStartOfLine = true;
58 bool JustConsumedEOL = true;
59 bool IsPeeking = false;
60 bool EndStatementAtEOF = true;
61
62 const char *TokStart = nullptr;
63 bool SkipSpace = true;
64 bool AllowAtInIdentifier = false;
65 bool AllowHashInIdentifier = false;
66 bool IsAtStartOfStatement = true;
67 bool LexMasmHexFloats = false;
68 bool LexMasmIntegers = false;
69 bool LexMasmStrings = false;
70 bool LexMotorolaIntegers = false;
71 bool UseMasmDefaultRadix = false;
72 unsigned DefaultRadix = 10;
73 bool LexHLASMIntegers = false;
74 bool LexHLASMStrings = false;
75 AsmCommentConsumer *CommentConsumer = nullptr;
76
77 LLVM_ABI AsmToken LexToken();
78
79 void SetError(SMLoc errLoc, const std::string &err) {
80 ErrLoc = errLoc;
81 Err = err;
82 }
83
84public:
85 LLVM_ABI AsmLexer(const MCAsmInfo &MAI);
86 AsmLexer(const AsmLexer &) = delete;
87 AsmLexer &operator=(const AsmLexer &) = delete;
88
89 /// Consume the next token from the input stream and return it.
90 ///
91 /// The lexer will continuously return the end-of-file token once the end of
92 /// the main input file has been reached.
93 const AsmToken &Lex() {
94 assert(!CurTok.empty());
95 // Mark if we parsing out a EndOfStatement.
96 JustConsumedEOL = CurTok.front().getKind() == AsmToken::EndOfStatement;
97 CurTok.erase(CurTok.begin());
98 // LexToken may generate multiple tokens via UnLex but will always return
99 // the first one. Place returned value at head of CurTok vector.
100 if (CurTok.empty()) {
101 AsmToken T = LexToken();
102 CurTok.insert(CurTok.begin(), T);
103 }
104 return CurTok.front();
105 }
106
107 void UnLex(AsmToken const &Token) {
108 CurTok.insert(CurTok.begin(), Token);
109 }
110
111 bool justConsumedEOL() { return JustConsumedEOL; }
112
114
115 /// Get the current source location.
116 SMLoc getLoc() const { return SMLoc::getFromPointer(TokStart); }
117
118 /// Get the current (last) lexed token.
119 const AsmToken &getTok() const { return CurTok[0]; }
120
121 /// Look ahead at the next token to be lexed.
122 const AsmToken peekTok(bool ShouldSkipSpace = true) {
123 AsmToken Tok;
124
126 size_t ReadCount = peekTokens(Buf, ShouldSkipSpace);
127
128 assert(ReadCount == 1);
129 (void)ReadCount;
130
131 return Tok;
132 }
133
134 /// Look ahead an arbitrary number of tokens.
136 bool ShouldSkipSpace = true);
137
138 /// Get the current error location
139 SMLoc getErrLoc() { return ErrLoc; }
140
141 /// Get the current error string
142 const std::string &getErr() { return Err; }
143
144 /// Get the kind of current token.
146
147 /// Check if the current token has kind \p K.
148 bool is(AsmToken::TokenKind K) const { return getTok().is(K); }
149
150 /// Check if the current token has kind \p K.
151 bool isNot(AsmToken::TokenKind K) const { return getTok().isNot(K); }
152
153 /// Set whether spaces should be ignored by the lexer
154 void setSkipSpace(bool val) { SkipSpace = val; }
155
156 bool getAllowAtInIdentifier() { return AllowAtInIdentifier; }
157 void setAllowAtInIdentifier(bool v) { AllowAtInIdentifier = v; }
158
159 void setAllowHashInIdentifier(bool V) { AllowHashInIdentifier = V; }
160
161 void setCommentConsumer(AsmCommentConsumer *CommentConsumer) {
162 this->CommentConsumer = CommentConsumer;
163 }
164
165 /// Set whether to lex masm-style binary (e.g., 0b1101) and radix-specified
166 /// literals (e.g., 0ABCh [hex], 576t [decimal], 77o [octal], 1101y [binary]).
167 void setLexMasmIntegers(bool V) { LexMasmIntegers = V; }
168
169 /// Set whether to use masm-style default-radix integer literals. If disabled,
170 /// assume decimal unless prefixed (e.g., 0x2c [hex], 077 [octal]).
171 void useMasmDefaultRadix(bool V) { UseMasmDefaultRadix = V; }
172
173 unsigned getMasmDefaultRadix() const { return DefaultRadix; }
174 void setMasmDefaultRadix(unsigned Radix) { DefaultRadix = Radix; }
175
176 /// Set whether to lex masm-style hex float literals, such as 3f800000r.
177 void setLexMasmHexFloats(bool V) { LexMasmHexFloats = V; }
178
179 /// Set whether to lex masm-style string literals, such as 'Can''t find file'
180 /// and "This ""value"" not found".
181 void setLexMasmStrings(bool V) { LexMasmStrings = V; }
182
183 /// Set whether to lex Motorola-style integer literals, such as $deadbeef or
184 /// %01010110.
185 void setLexMotorolaIntegers(bool V) { LexMotorolaIntegers = V; }
186
187 /// Set whether to lex HLASM-flavour integers. For now this is only [0-9]*
188 void setLexHLASMIntegers(bool V) { LexHLASMIntegers = V; }
189
190 /// Set whether to "lex" HLASM-flavour character and string literals. For now,
191 /// setting this option to true, will disable lexing for character and string
192 /// literals.
193 void setLexHLASMStrings(bool V) { LexHLASMStrings = V; }
194
195 /// Set buffer to be lexed.
196 /// `Buf` must be NULL-terminated. NULL terminator must reside at `Buf.end()`.
197 /// `ptr` if provided must be in range [`Buf.begin()`, `buf.end()`] or NULL.
198 /// Specifies where lexing of buffer should begin.
199 /// `EndStatementAtEOF` specifies whether `AsmToken::EndOfStatement` should be
200 /// returned upon reaching end of buffer.
201 LLVM_ABI void setBuffer(StringRef Buf, const char *ptr = nullptr,
202 bool EndStatementAtEOF = true);
203
204 const MCAsmInfo &getMAI() const { return MAI; }
205
206 static bool isIdentifierChar(char C, bool AllowAt, bool AllowHash) {
207 return isAlnum(C) || C == '_' || C == '$' || C == '.' || C == '?' ||
208 (AllowAt && C == '@') || (AllowHash && C == '#');
209 }
210
211private:
212 bool isAtStartOfComment(const char *Ptr);
213 bool isAtStatementSeparator(const char *Ptr);
214 [[nodiscard]] int getNextChar();
215 int peekNextChar();
216 AsmToken ReturnError(const char *Loc, const std::string &Msg);
217
218 AsmToken LexIdentifier();
219 AsmToken LexSlash();
220 AsmToken LexLineComment();
221 AsmToken LexDigit();
222 AsmToken LexSingleQuote();
223 AsmToken LexQuote();
224 AsmToken LexFloatLiteral();
225 AsmToken LexHexFloatLiteral(bool NoIntDigits);
226
227 StringRef LexUntilEndOfLine();
228};
229
230} // end namespace llvm
231
232#endif // LLVM_MC_MCPARSER_ASMLEXER_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:215
#define T
const char * Msg
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
A callback class which is notified of each comment in an assembly file as it is lexed.
Definition AsmLexer.h:32
virtual ~AsmCommentConsumer()=default
virtual void HandleComment(SMLoc Loc, StringRef CommentText)=0
Callback function for when a comment is lexed.
void setLexHLASMStrings(bool V)
Set whether to "lex" HLASM-flavour character and string literals.
Definition AsmLexer.h:193
static bool isIdentifierChar(char C, bool AllowAt, bool AllowHash)
Definition AsmLexer.h:206
void setLexMasmIntegers(bool V)
Set whether to lex masm-style binary (e.g., 0b1101) and radix-specified literals (e....
Definition AsmLexer.h:167
SMLoc getLoc() const
Get the current source location.
Definition AsmLexer.h:116
void setLexMasmStrings(bool V)
Set whether to lex masm-style string literals, such as 'Can''t find file' and "This ""value"" not fou...
Definition AsmLexer.h:181
LLVM_ABI AsmLexer(const MCAsmInfo &MAI)
Definition AsmLexer.cpp:110
const AsmToken peekTok(bool ShouldSkipSpace=true)
Look ahead at the next token to be lexed.
Definition AsmLexer.h:122
bool getAllowAtInIdentifier()
Definition AsmLexer.h:156
void UnLex(AsmToken const &Token)
Definition AsmLexer.h:107
void setMasmDefaultRadix(unsigned Radix)
Definition AsmLexer.h:174
AsmToken::TokenKind getKind() const
Get the kind of current token.
Definition AsmLexer.h:145
void setLexMasmHexFloats(bool V)
Set whether to lex masm-style hex float literals, such as 3f800000r.
Definition AsmLexer.h:177
const MCAsmInfo & getMAI() const
Definition AsmLexer.h:204
const AsmToken & getTok() const
Get the current (last) lexed token.
Definition AsmLexer.h:119
bool is(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition AsmLexer.h:148
void setLexMotorolaIntegers(bool V)
Set whether to lex Motorola-style integer literals, such as $deadbeef or %01010110.
Definition AsmLexer.h:185
SMLoc getErrLoc()
Get the current error location.
Definition AsmLexer.h:139
bool justConsumedEOL()
Definition AsmLexer.h:111
AsmLexer(const AsmLexer &)=delete
const std::string & getErr()
Get the current error string.
Definition AsmLexer.h:142
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition AsmLexer.h:93
void setSkipSpace(bool val)
Set whether spaces should be ignored by the lexer.
Definition AsmLexer.h:154
void setAllowAtInIdentifier(bool v)
Definition AsmLexer.h:157
LLVM_ABI StringRef LexUntilEndOfStatement()
Definition AsmLexer.cpp:742
AsmLexer & operator=(const AsmLexer &)=delete
LLVM_ABI void setBuffer(StringRef Buf, const char *ptr=nullptr, bool EndStatementAtEOF=true)
Set buffer to be lexed.
Definition AsmLexer.cpp:120
unsigned getMasmDefaultRadix() const
Definition AsmLexer.h:173
void useMasmDefaultRadix(bool V)
Set whether to use masm-style default-radix integer literals.
Definition AsmLexer.h:171
void setLexHLASMIntegers(bool V)
Set whether to lex HLASM-flavour integers. For now this is only [0-9]*.
Definition AsmLexer.h:188
bool isNot(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition AsmLexer.h:151
LLVM_ABI size_t peekTokens(MutableArrayRef< AsmToken > Buf, bool ShouldSkipSpace=true)
Look ahead an arbitrary number of tokens.
Definition AsmLexer.cpp:762
void setCommentConsumer(AsmCommentConsumer *CommentConsumer)
Definition AsmLexer.h:161
void setAllowHashInIdentifier(bool V)
Definition AsmLexer.h:159
Target independent representation for an assembler token.
Definition MCAsmMacro.h:22
bool isNot(TokenKind K) const
Definition MCAsmMacro.h:76
bool is(TokenKind K) const
Definition MCAsmMacro.h:75
TokenKind getKind() const
Definition MCAsmMacro.h:74
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:67
Represent a mutable reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:294
Represents a location in source code.
Definition SMLoc.h:22
static SMLoc getFromPointer(const char *Ptr)
Definition SMLoc.h:35
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This is an optimization pass for GlobalISel generic memory operations.
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...