LLVM  5.0.0
MILexer.h
Go to the documentation of this file.
1 //===- MILexer.h - Lexer for machine instructions -------------------------===//
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 <functional>
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
97 
98  // Named metadata keywords
103 
104  // Identifier tokens
117 
118  // Other tokens
129  QuotedIRValue, // `<constant value>`
132  };
133 
134 private:
135  TokenKind Kind;
136  StringRef Range;
137  StringRef StringValue;
138  std::string StringValueStorage;
139  APSInt IntVal;
140 
141 public:
142  MIToken() : Kind(Error) {}
143 
144  MIToken &reset(TokenKind Kind, StringRef Range);
145 
147  MIToken &setOwnedStringValue(std::string StrVal);
148  MIToken &setIntegerValue(APSInt IntVal);
149 
150  TokenKind kind() const { return Kind; }
151 
152  bool isError() const { return Kind == Error; }
153 
154  bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
155 
156  bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
157 
158  bool isRegister() const {
159  return Kind == NamedRegister || Kind == underscore ||
160  Kind == VirtualRegister;
161  }
162 
163  bool isRegisterFlag() const {
164  return Kind == kw_implicit || Kind == kw_implicit_define ||
165  Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
166  Kind == kw_undef || Kind == kw_internal ||
167  Kind == kw_early_clobber || Kind == kw_debug_use;
168  }
169 
170  bool isMemoryOperandFlag() const {
171  return Kind == kw_volatile || Kind == kw_non_temporal ||
172  Kind == kw_dereferenceable || Kind == kw_invariant ||
173  Kind == StringConstant;
174  }
175 
176  bool is(TokenKind K) const { return Kind == K; }
177 
178  bool isNot(TokenKind K) const { return Kind != K; }
179 
180  StringRef::iterator location() const { return Range.begin(); }
181 
182  StringRef range() const { return Range; }
183 
184  /// Return the token's string value.
185  StringRef stringValue() const { return StringValue; }
186 
187  const APSInt &integerValue() const { return IntVal; }
188 
189  bool hasIntegerValue() const {
190  return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
191  Kind == MachineBasicBlockLabel || Kind == StackObject ||
192  Kind == FixedStackObject || Kind == GlobalValue ||
193  Kind == VirtualRegister || Kind == ConstantPoolItem ||
194  Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
195  }
196 };
197 
198 /// Consume a single machine instruction token in the given source and return
199 /// the remaining source string.
200 StringRef lexMIToken(
201  StringRef Source, MIToken &Token,
202  function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
203 
204 } // end namespace llvm
205 
206 #endif
StringRef range() const
Definition: MILexer.h:182
const char * iterator
Definition: StringRef.h:53
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:62
StringRef::iterator location() const
Definition: MILexer.h:180
bool isNot(TokenKind K) const
Definition: MILexer.h:178
bool isErrorOrEOF() const
Definition: MILexer.h:156
iterator begin() const
Definition: StringRef.h:106
bool isError() const
Definition: MILexer.h:152
bool isRegisterFlag() const
Definition: MILexer.h:163
bool hasIntegerValue() const
Definition: MILexer.h:189
MIToken & setOwnedStringValue(std::string StrVal)
Definition: MILexer.cpp:73
StringRef stringValue() const
Return the token's string value.
Definition: MILexer.h:185
TokenKind kind() const
Definition: MILexer.h:150
bool isNewlineOrEOF() const
Definition: MILexer.h:154
MIToken & setStringValue(StringRef StrVal)
Definition: MILexer.cpp:68
bool is(TokenKind K) const
Definition: MILexer.h:176
const APSInt & integerValue() const
Definition: MILexer.h:187
bool isRegister() const
Definition: MILexer.h:158
bool isMemoryOperandFlag() const
Definition: MILexer.h:170
A token produced by the machine instruction lexer.
Definition: MILexer.h:28
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
MIToken & setIntegerValue(APSInt IntVal)
Definition: MILexer.cpp:79