1 //===- MILexer.h - Lexer for machine instructions -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the function that lexes the machine instruction source
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
16 #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
18 #include "llvm/ADT/APSInt.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/STLExtras.h"
27 /// A token produced by the machine instruction lexer.
34 // Tokens with no info.
57 kw_cfi_def_cfa_register,
58 kw_cfi_def_cfa_offset,
97 unsigned StringOffset;
100 std::string StringValue;
104 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
105 : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
108 MIToken(TokenKind Kind, StringRef Range, std::string StringValue,
109 unsigned StringOffset = 0)
110 : Kind(Kind), StringOffset(StringOffset), HasStringValue(true),
111 Range(Range), StringValue(std::move(StringValue)) {}
113 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
114 unsigned StringOffset = 0)
115 : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
116 Range(Range), IntVal(IntVal) {}
118 TokenKind kind() const { return Kind; }
120 bool isError() const { return Kind == Error; }
122 bool isRegister() const {
123 return Kind == NamedRegister || Kind == underscore ||
124 Kind == VirtualRegister;
127 bool isRegisterFlag() const {
128 return Kind == kw_implicit || Kind == kw_implicit_define ||
129 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef ||
130 Kind == kw_early_clobber || Kind == kw_debug_use;
133 bool isMemoryOperandFlag() const {
134 return Kind == kw_volatile || Kind == kw_non_temporal ||
135 Kind == kw_invariant;
138 bool is(TokenKind K) const { return Kind == K; }
140 bool isNot(TokenKind K) const { return Kind != K; }
142 StringRef::iterator location() const { return Range.begin(); }
144 /// Return the token's raw string value.
146 /// If the string value is quoted, this method returns that quoted string as
147 /// it is, without unescaping the string value.
148 StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
150 /// Return the token's string value.
151 StringRef stringValue() const {
152 return HasStringValue ? StringRef(StringValue)
153 : Range.drop_front(StringOffset);
156 const APSInt &integerValue() const { return IntVal; }
158 bool hasIntegerValue() const {
159 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
160 Kind == StackObject || Kind == FixedStackObject ||
161 Kind == GlobalValue || Kind == VirtualRegister ||
162 Kind == ConstantPoolItem || Kind == JumpTableIndex ||
167 /// Consume a single machine instruction token in the given source and return
168 /// the remaining source string.
169 StringRef lexMIToken(
170 StringRef Source, MIToken &Token,
171 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
173 } // end namespace llvm