LLVM  16.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"
13 #include "llvm/ADT/SmallVector.h"
14 #include "llvm/MC/MCAsmMacro.h"
15 #include <cassert>
16 #include <cstddef>
17 #include <string>
18 
19 namespace llvm {
20 
21 /// A callback class which is notified of each comment in an assembly file as
22 /// it is lexed.
24 public:
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.
37 class MCAsmLexer {
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 
45 protected: // Can only create subclasses.
46  const char *TokStart = nullptr;
47  bool SkipSpace = true;
49  bool AllowHashInIdentifier = false;
50  bool IsAtStartOfStatement = true;
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 
70 public:
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.
82  IsAtStartOfStatement = CurTok.front().getKind() == AsmToken::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()) {
87  AsmToken T = LexToken();
88  CurTok.insert(CurTok.begin(), T);
89  }
90  return CurTok.front();
91  }
92 
93  void UnLex(AsmToken const &Token) {
94  IsAtStartOfStatement = false;
95  CurTok.insert(CurTok.begin(), Token);
96  }
97 
99 
100  virtual StringRef LexUntilEndOfStatement() = 0;
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 
114  MutableArrayRef<AsmToken> Buf(Tok);
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.
124  virtual size_t peekTokens(MutableArrayRef<AsmToken> Buf,
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.
138  AsmToken::TokenKind getKind() const { return getTok().getKind(); }
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]).
160  void setLexMasmIntegers(bool V) { LexMasmIntegers = V; }
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.
170  void setLexMasmHexFloats(bool V) { LexMasmHexFloats = V; }
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]*
181  void setLexHLASMIntegers(bool V) { LexHLASMIntegers = V; }
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.
186  void setLexHLASMStrings(bool V) { LexHLASMStrings = V; }
187 };
188 
189 } // end namespace llvm
190 
191 #endif // LLVM_MC_MCPARSER_MCASMLEXER_H
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::SmallVectorImpl::erase
iterator erase(const_iterator CI)
Definition: SmallVector.h:724
llvm::AsmToken::is
bool is(TokenKind K) const
Definition: MCAsmMacro.h:82
llvm::AsmCommentConsumer::HandleComment
virtual void HandleComment(SMLoc Loc, StringRef CommentText)=0
Callback function for when a comment is lexed.
llvm::MCAsmLexer
Generic assembler lexer interface, for use by target specific assembly lexers.
Definition: MCAsmLexer.h:37
llvm::MCAsmLexer::DefaultRadix
unsigned DefaultRadix
Definition: MCAsmLexer.h:56
llvm::MCAsmLexer::peekTok
const AsmToken peekTok(bool ShouldSkipSpace=true)
Look ahead at the next token to be lexed.
Definition: MCAsmLexer.h:111
llvm::AsmToken::EndOfStatement
@ EndOfStatement
Definition: MCAsmMacro.h:42
llvm::MCAsmLexer::setAllowHashInIdentifier
void setAllowHashInIdentifier(bool V)
Definition: MCAsmLexer.h:152
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1182
llvm::MCAsmLexer::SkipSpace
bool SkipSpace
Definition: MCAsmLexer.h:47
llvm::MCAsmLexer::getTok
const AsmToken & getTok() const
Get the current (last) lexed token.
Definition: MCAsmLexer.h:106
llvm::MCAsmLexer::LexMasmStrings
bool LexMasmStrings
Definition: MCAsmLexer.h:53
llvm::MCAsmLexer::LexToken
virtual AsmToken LexToken()=0
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::MCAsmLexer::Lex
const AsmToken & Lex()
Consume the next token from the input stream and return it.
Definition: MCAsmLexer.h:79
llvm::MCAsmLexer::is
bool is(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition: MCAsmLexer.h:141
llvm::AsmToken
Target independent representation for an assembler token.
Definition: MCAsmMacro.h:21
llvm::MCAsmLexer::IsAtStartOfStatement
bool IsAtStartOfStatement
Definition: MCAsmLexer.h:50
llvm::MCAsmLexer::~MCAsmLexer
virtual ~MCAsmLexer()
llvm::MCAsmLexer::isAtStartOfStatement
bool isAtStartOfStatement()
Definition: MCAsmLexer.h:98
llvm::MutableArrayRef
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:28
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
llvm::MCAsmLexer::setLexHLASMIntegers
void setLexHLASMIntegers(bool V)
Set whether to lex HLASM-flavour integers. For now this is only [0-9]*.
Definition: MCAsmLexer.h:181
llvm::MCAsmLexer::operator=
MCAsmLexer & operator=(const MCAsmLexer &)=delete
llvm::AsmToken::getKind
TokenKind getKind() const
Definition: MCAsmMacro.h:81
llvm::MCAsmLexer::MCAsmLexer
MCAsmLexer()
Definition: MCAsmLexer.cpp:16
llvm::MCAsmLexer::getErr
const std::string & getErr()
Get the current error string.
Definition: MCAsmLexer.h:133
llvm::MCAsmLexer::getErrLoc
SMLoc getErrLoc()
Get the current error location.
Definition: MCAsmLexer.h:128
val
The initial backend is deliberately restricted to z10 We should add support for later architectures at some point If an asm ties an i32 r result to an i64 the input will be treated as an leaving the upper bits uninitialised For i64 store i32 val
Definition: README.txt:15
llvm::MCAsmLexer::LexMotorolaIntegers
bool LexMotorolaIntegers
Definition: MCAsmLexer.h:54
llvm::MCAsmLexer::useMasmDefaultRadix
void useMasmDefaultRadix(bool V)
Set whether to use masm-style default-radix integer literals.
Definition: MCAsmLexer.h:164
llvm::MCAsmLexer::setCommentConsumer
void setCommentConsumer(AsmCommentConsumer *CommentConsumer)
Definition: MCAsmLexer.h:154
llvm::MCAsmLexer::getLoc
SMLoc getLoc() const
Get the current source location.
Definition: MCAsmLexer.cpp:22
llvm::MCAsmLexer::LexMasmIntegers
bool LexMasmIntegers
Definition: MCAsmLexer.h:52
llvm::MCAsmLexer::LexMasmHexFloats
bool LexMasmHexFloats
Definition: MCAsmLexer.h:51
llvm::MCAsmLexer::setLexMotorolaIntegers
void setLexMotorolaIntegers(bool V)
Set whether to lex Motorola-style integer literals, such as $deadbeef or %01010110.
Definition: MCAsmLexer.h:178
ArrayRef.h
llvm::MCAsmLexer::AllowHashInIdentifier
bool AllowHashInIdentifier
Definition: MCAsmLexer.h:49
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCAsmLexer::LexHLASMIntegers
bool LexHLASMIntegers
Definition: MCAsmLexer.h:57
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::MCAsmLexer::AllowAtInIdentifier
bool AllowAtInIdentifier
Definition: MCAsmLexer.h:48
llvm::MCAsmLexer::LexUntilEndOfStatement
virtual StringRef LexUntilEndOfStatement()=0
llvm::MCAsmLexer::getKind
AsmToken::TokenKind getKind() const
Get the kind of current token.
Definition: MCAsmLexer.h:138
MCAsmMacro.h
llvm::MCAsmLexer::setLexMasmHexFloats
void setLexMasmHexFloats(bool V)
Set whether to lex masm-style hex float literals, such as 3f800000r.
Definition: MCAsmLexer.h:170
llvm::MCAsmLexer::TokStart
const char * TokStart
Definition: MCAsmLexer.h:46
llvm::MCAsmLexer::CommentConsumer
AsmCommentConsumer * CommentConsumer
Definition: MCAsmLexer.h:59
llvm::AsmToken::isNot
bool isNot(TokenKind K) const
Definition: MCAsmMacro.h:83
llvm::MCAsmLexer::setMasmDefaultRadix
void setMasmDefaultRadix(unsigned Radix)
Definition: MCAsmLexer.h:167
llvm::MCAsmLexer::isNot
bool isNot(AsmToken::TokenKind K) const
Check if the current token has kind K.
Definition: MCAsmLexer.h:144
llvm::MCAsmLexer::setSkipSpace
void setSkipSpace(bool val)
Set whether spaces should be ignored by the lexer.
Definition: MCAsmLexer.h:147
llvm::AsmCommentConsumer::~AsmCommentConsumer
virtual ~AsmCommentConsumer()=default
llvm::MCAsmLexer::getMasmDefaultRadix
unsigned getMasmDefaultRadix() const
Definition: MCAsmLexer.h:166
llvm::AsmToken::TokenKind
TokenKind
Definition: MCAsmMacro.h:23
llvm::MCAsmLexer::setLexHLASMStrings
void setLexHLASMStrings(bool V)
Set whether to "lex" HLASM-flavour character and string literals.
Definition: MCAsmLexer.h:186
SmallVector.h
llvm::MCAsmLexer::peekTokens
virtual size_t peekTokens(MutableArrayRef< AsmToken > Buf, bool ShouldSkipSpace=true)=0
Look ahead an arbitrary number of tokens.
llvm::AsmCommentConsumer
A callback class which is notified of each comment in an assembly file as it is lexed.
Definition: MCAsmLexer.h:23
llvm::MCAsmLexer::LexHLASMStrings
bool LexHLASMStrings
Definition: MCAsmLexer.h:58
llvm::MCAsmLexer::setAllowAtInIdentifier
void setAllowAtInIdentifier(bool v)
Definition: MCAsmLexer.h:150
llvm::MCAsmLexer::UnLex
void UnLex(AsmToken const &Token)
Definition: MCAsmLexer.h:93
llvm::MCAsmLexer::SetError
void SetError(SMLoc errLoc, const std::string &err)
Definition: MCAsmLexer.h:65
llvm::MCAsmLexer::getAllowAtInIdentifier
bool getAllowAtInIdentifier()
Definition: MCAsmLexer.h:149
llvm::MCAsmLexer::UseMasmDefaultRadix
bool UseMasmDefaultRadix
Definition: MCAsmLexer.h:55
llvm::MCAsmLexer::setLexMasmIntegers
void setLexMasmIntegers(bool V)
Set whether to lex masm-style binary (e.g., 0b1101) and radix-specified literals (e....
Definition: MCAsmLexer.h:160
llvm::SmallVectorImpl::insert
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:792
llvm::MCAsmLexer::setLexMasmStrings
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