LLVM  9.0.0svn
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
122 
123  // Named metadata keywords
130 
131  // Identifier tokens
143 
144  // Other tokens
155  QuotedIRValue, // `<constant value>`
158  };
159 
160 private:
161  TokenKind Kind = Error;
162  StringRef Range;
163  StringRef StringValue;
164  std::string StringValueStorage;
165  APSInt IntVal;
166 
167 public:
168  MIToken() = default;
169 
170  MIToken &reset(TokenKind Kind, StringRef Range);
171 
173  MIToken &setOwnedStringValue(std::string StrVal);
174  MIToken &setIntegerValue(APSInt IntVal);
175 
176  TokenKind kind() const { return Kind; }
177 
178  bool isError() const { return Kind == Error; }
179 
180  bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
181 
182  bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
183 
184  bool isRegister() const {
185  return Kind == NamedRegister || Kind == underscore ||
186  Kind == NamedVirtualRegister || Kind == VirtualRegister;
187  }
188 
189  bool isRegisterFlag() const {
190  return Kind == kw_implicit || Kind == kw_implicit_define ||
191  Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
192  Kind == kw_undef || Kind == kw_internal ||
193  Kind == kw_early_clobber || Kind == kw_debug_use ||
194  Kind == kw_renamable;
195  }
196 
197  bool isMemoryOperandFlag() const {
198  return Kind == kw_volatile || Kind == kw_non_temporal ||
199  Kind == kw_dereferenceable || Kind == kw_invariant ||
200  Kind == StringConstant;
201  }
202 
203  bool is(TokenKind K) const { return Kind == K; }
204 
205  bool isNot(TokenKind K) const { return Kind != K; }
206 
207  StringRef::iterator location() const { return Range.begin(); }
208 
209  StringRef range() const { return Range; }
210 
211  /// Return the token's string value.
212  StringRef stringValue() const { return StringValue; }
213 
214  const APSInt &integerValue() const { return IntVal; }
215 
216  bool hasIntegerValue() const {
217  return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
218  Kind == MachineBasicBlockLabel || Kind == StackObject ||
219  Kind == FixedStackObject || Kind == GlobalValue ||
220  Kind == VirtualRegister || Kind == ConstantPoolItem ||
221  Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
222  }
223 };
224 
225 /// Consume a single machine instruction token in the given source and return
226 /// the remaining source string.
228  StringRef Source, MIToken &Token,
229  function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
230 
231 } // end namespace llvm
232 
233 #endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
StringRef stringValue() const
Return the token&#39;s string value.
Definition: MILexer.h:212
bool is(TokenKind K) const
Definition: MILexer.h:203
bool isError() const
Definition: MILexer.h:178
This class represents lattice values for constants.
Definition: AllocatorList.h:23
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:116
bool isMemoryOperandFlag() const
Definition: MILexer.h:197
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:205
bool isNewlineOrEOF() const
Definition: MILexer.h:180
const APSInt & integerValue() const
Definition: MILexer.h:214
MIToken & setOwnedStringValue(std::string StrVal)
Definition: MILexer.cpp:78
bool isErrorOrEOF() const
Definition: MILexer.h:182
MIToken()=default
bool hasIntegerValue() const
Definition: MILexer.h:216
TokenKind kind() const
Definition: MILexer.h:176
MIToken & setStringValue(StringRef StrVal)
Definition: MILexer.cpp:73
StringRef range() const
Definition: MILexer.h:209
StringRef::iterator location() const
Definition: MILexer.h:207
iterator begin() const
Definition: StringRef.h:101
bool isRegisterFlag() const
Definition: MILexer.h:189
bool isRegister() const
Definition: MILexer.h:184
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:48
MIToken & setIntegerValue(APSInt IntVal)
Definition: MILexer.cpp:84