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