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,
95 unsigned StringOffset;
98 std::string StringValue;
102 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
103 : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
106 MIToken(TokenKind Kind, StringRef Range, std::string StringValue,
107 unsigned StringOffset = 0)
108 : Kind(Kind), StringOffset(StringOffset), HasStringValue(true),
109 Range(Range), StringValue(std::move(StringValue)) {}
111 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
112 unsigned StringOffset = 0)
113 : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
114 Range(Range), IntVal(IntVal) {}
116 TokenKind kind() const { return Kind; }
118 bool isError() const { return Kind == Error; }
120 bool isRegister() const {
121 return Kind == NamedRegister || Kind == underscore ||
122 Kind == VirtualRegister;
125 bool isRegisterFlag() const {
126 return Kind == kw_implicit || Kind == kw_implicit_define ||
127 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef ||
128 Kind == kw_early_clobber || Kind == kw_debug_use;
131 bool isMemoryOperandFlag() const { return Kind == kw_volatile; }
133 bool is(TokenKind K) const { return Kind == K; }
135 bool isNot(TokenKind K) const { return Kind != K; }
137 StringRef::iterator location() const { return Range.begin(); }
139 /// Return the token's raw string value.
141 /// If the string value is quoted, this method returns that quoted string as
142 /// it is, without unescaping the string value.
143 StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
145 /// Return the token's string value.
146 StringRef stringValue() const {
147 return HasStringValue ? StringRef(StringValue)
148 : Range.drop_front(StringOffset);
151 const APSInt &integerValue() const { return IntVal; }
153 bool hasIntegerValue() const {
154 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
155 Kind == StackObject || Kind == FixedStackObject ||
156 Kind == GlobalValue || Kind == VirtualRegister ||
157 Kind == ConstantPoolItem || Kind == JumpTableIndex ||
162 /// Consume a single machine instruction token in the given source and return
163 /// the remaining source string.
164 StringRef lexMIToken(
165 StringRef Source, MIToken &Token,
166 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
168 } // end namespace llvm