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