LLVM  10.0.0
MILexer.h
Go to the documentation of this file.
1 //===- MILexer.h - Lexer for machine instructions ---------------*- 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 file declares the function that lexes the machine instruction source
10 // string.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
15 #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
16 
17 #include "llvm/ADT/APSInt.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringRef.h"
20 #include <string>
21 
22 namespace llvm {
23 
24 class Twine;
25 
26 /// A token produced by the machine instruction lexer.
27 struct MIToken {
28  enum TokenKind {
29  // Markers
30  Eof,
33 
34  // Tokens with no info.
40  dot,
50 
51  // Keywords
126 
127  // Named metadata keywords
134 
135  // Identifier tokens
147 
148  // Other tokens
160  QuotedIRValue, // `<constant value>`
163  };
164 
165 private:
166  TokenKind Kind = Error;
167  StringRef Range;
168  StringRef StringValue;
169  std::string StringValueStorage;
170  APSInt IntVal;
171 
172 public:
173  MIToken() = default;
174 
175  MIToken &reset(TokenKind Kind, StringRef Range);
176 
178  MIToken &setOwnedStringValue(std::string StrVal);
179  MIToken &setIntegerValue(APSInt IntVal);
180 
181  TokenKind kind() const { return Kind; }
182 
183  bool isError() const { return Kind == Error; }
184 
185  bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
186 
187  bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
188 
189  bool isRegister() const {
190  return Kind == NamedRegister || Kind == underscore ||
191  Kind == NamedVirtualRegister || Kind == VirtualRegister;
192  }
193 
194  bool isRegisterFlag() const {
195  return Kind == kw_implicit || Kind == kw_implicit_define ||
196  Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
197  Kind == kw_undef || Kind == kw_internal ||
198  Kind == kw_early_clobber || Kind == kw_debug_use ||
199  Kind == kw_renamable;
200  }
201 
202  bool isMemoryOperandFlag() const {
203  return Kind == kw_volatile || Kind == kw_non_temporal ||
204  Kind == kw_dereferenceable || Kind == kw_invariant ||
205  Kind == StringConstant;
206  }
207 
208  bool is(TokenKind K) const { return Kind == K; }
209 
210  bool isNot(TokenKind K) const { return Kind != K; }
211 
212  StringRef::iterator location() const { return Range.begin(); }
213 
214  StringRef range() const { return Range; }
215 
216  /// Return the token's string value.
217  StringRef stringValue() const { return StringValue; }
218 
219  const APSInt &integerValue() const { return IntVal; }
220 
221  bool hasIntegerValue() const {
222  return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
223  Kind == MachineBasicBlockLabel || Kind == StackObject ||
224  Kind == FixedStackObject || Kind == GlobalValue ||
225  Kind == VirtualRegister || Kind == ConstantPoolItem ||
226  Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
227  }
228 };
229 
230 /// Consume a single machine instruction token in the given source and return
231 /// the remaining source string.
233  StringRef Source, MIToken &Token,
234  function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
235 
236 } // end namespace llvm
237 
238 #endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
StringRef stringValue() const
Return the token&#39;s string value.
Definition: MILexer.h:217
bool is(TokenKind K) const
Definition: MILexer.h:208
bool isError() const
Definition: MILexer.h:183
This class represents lattice values for constants.
Definition: AllocatorList.h:23
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:99
bool isMemoryOperandFlag() const
Definition: MILexer.h:202
StringRef lexMIToken(StringRef Source, MIToken &Token, function_ref< void(StringRef::iterator, const Twine &)> ErrorCallback)
Consume a single machine instruction token in the given source and return the remaining source string...
MIToken & reset(TokenKind Kind, StringRef Range)
Definition: MILexer.cpp:67
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
bool isNot(TokenKind K) const
Definition: MILexer.h:210
bool isNewlineOrEOF() const
Definition: MILexer.h:185
const APSInt & integerValue() const
Definition: MILexer.h:219
MIToken & setOwnedStringValue(std::string StrVal)
Definition: MILexer.cpp:78
bool isErrorOrEOF() const
Definition: MILexer.h:187
MIToken()=default
bool hasIntegerValue() const
Definition: MILexer.h:221
TokenKind kind() const
Definition: MILexer.h:181
MIToken & setStringValue(StringRef StrVal)
Definition: MILexer.cpp:73
StringRef range() const
Definition: MILexer.h:214
StringRef::iterator location() const
Definition: MILexer.h:212
iterator begin() const
Definition: StringRef.h:122
bool isRegisterFlag() const
Definition: MILexer.h:194
bool isRegister() const
Definition: MILexer.h:189
A token produced by the machine instruction lexer.
Definition: MILexer.h:27
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
MIToken & setIntegerValue(APSInt IntVal)
Definition: MILexer.cpp:84