LLVM  7.0.0
MILexer.h
Go to the documentation of this file.
1 //===- MILexer.h - Lexer for machine instructions ---------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the function that lexes the machine instruction source
11 // string.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
16 #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
17 
18 #include "llvm/ADT/APSInt.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringRef.h"
21 #include <string>
22 
23 namespace llvm {
24 
25 class Twine;
26 
27 /// A token produced by the machine instruction lexer.
28 struct MIToken {
29  enum TokenKind {
30  // Markers
31  Eof,
34 
35  // Tokens with no info.
41  dot,
51 
52  // Keywords
116 
117  // Named metadata keywords
123 
124  // Identifier tokens
135 
136  // Other tokens
147  QuotedIRValue, // `<constant value>`
150  };
151 
152 private:
153  TokenKind Kind = Error;
154  StringRef Range;
155  StringRef StringValue;
156  std::string StringValueStorage;
157  APSInt IntVal;
158 
159 public:
160  MIToken() = default;
161 
162  MIToken &reset(TokenKind Kind, StringRef Range);
163 
165  MIToken &setOwnedStringValue(std::string StrVal);
166  MIToken &setIntegerValue(APSInt IntVal);
167 
168  TokenKind kind() const { return Kind; }
169 
170  bool isError() const { return Kind == Error; }
171 
172  bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
173 
174  bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
175 
176  bool isRegister() const {
177  return Kind == NamedRegister || Kind == underscore ||
178  Kind == NamedVirtualRegister || Kind == VirtualRegister;
179  }
180 
181  bool isRegisterFlag() const {
182  return Kind == kw_implicit || Kind == kw_implicit_define ||
183  Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
184  Kind == kw_undef || Kind == kw_internal ||
185  Kind == kw_early_clobber || Kind == kw_debug_use ||
186  Kind == kw_renamable;
187  }
188 
189  bool isMemoryOperandFlag() const {
190  return Kind == kw_volatile || Kind == kw_non_temporal ||
191  Kind == kw_dereferenceable || Kind == kw_invariant ||
192  Kind == StringConstant;
193  }
194 
195  bool is(TokenKind K) const { return Kind == K; }
196 
197  bool isNot(TokenKind K) const { return Kind != K; }
198 
199  StringRef::iterator location() const { return Range.begin(); }
200 
201  StringRef range() const { return Range; }
202 
203  /// Return the token's string value.
204  StringRef stringValue() const { return StringValue; }
205 
206  const APSInt &integerValue() const { return IntVal; }
207 
208  bool hasIntegerValue() const {
209  return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
210  Kind == MachineBasicBlockLabel || Kind == StackObject ||
211  Kind == FixedStackObject || Kind == GlobalValue ||
212  Kind == VirtualRegister || Kind == ConstantPoolItem ||
213  Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
214  }
215 };
216 
217 /// Consume a single machine instruction token in the given source and return
218 /// the remaining source string.
220  StringRef Source, MIToken &Token,
221  function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
222 
223 } // end namespace llvm
224 
225 #endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
StringRef stringValue() const
Return the token&#39;s string value.
Definition: MILexer.h:204
bool is(TokenKind K) const
Definition: MILexer.h:195
bool isError() const
Definition: MILexer.h:170
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:106
bool isMemoryOperandFlag() const
Definition: MILexer.h:189
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:68
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
bool isNot(TokenKind K) const
Definition: MILexer.h:197
bool isNewlineOrEOF() const
Definition: MILexer.h:172
const APSInt & integerValue() const
Definition: MILexer.h:206
MIToken & setOwnedStringValue(std::string StrVal)
Definition: MILexer.cpp:79
bool isErrorOrEOF() const
Definition: MILexer.h:174
MIToken()=default
bool hasIntegerValue() const
Definition: MILexer.h:208
TokenKind kind() const
Definition: MILexer.h:168
MIToken & setStringValue(StringRef StrVal)
Definition: MILexer.cpp:74
StringRef range() const
Definition: MILexer.h:201
StringRef::iterator location() const
Definition: MILexer.h:199
iterator begin() const
Definition: StringRef.h:106
bool isRegisterFlag() const
Definition: MILexer.h:181
bool isRegister() const
Definition: MILexer.h:176
A token produced by the machine instruction lexer.
Definition: MILexer.h:28
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:49
MIToken & setIntegerValue(APSInt IntVal)
Definition: MILexer.cpp:85