1 //===- MILexer.h - Lexer for machine instructions ---------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file declares the function that lexes the machine instruction source
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
15 #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
17 #include "llvm/ADT/APSInt.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringRef.h"
26 /// A token produced by the machine instruction lexer.
34 // Tokens with no info.
81 kw_cfi_def_cfa_register,
82 kw_cfi_def_cfa_offset,
83 kw_cfi_adjust_cfa_offset,
87 kw_cfi_remember_state,
92 kw_cfi_aarch64_negate_ra_sign_state,
121 kw_post_instr_symbol,
124 // Named metadata keywords
135 NamedVirtualRegister,
136 MachineBasicBlockLabel,
147 FloatingPointLiteral,
156 QuotedIRValue, // `<constant value>`
162 TokenKind Kind = Error;
164 StringRef StringValue;
165 std::string StringValueStorage;
171 MIToken &reset(TokenKind Kind, StringRef Range);
173 MIToken &setStringValue(StringRef StrVal);
174 MIToken &setOwnedStringValue(std::string StrVal);
175 MIToken &setIntegerValue(APSInt IntVal);
177 TokenKind kind() const { return Kind; }
179 bool isError() const { return Kind == Error; }
181 bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
183 bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
185 bool isRegister() const {
186 return Kind == NamedRegister || Kind == underscore ||
187 Kind == NamedVirtualRegister || Kind == VirtualRegister;
190 bool isRegisterFlag() const {
191 return Kind == kw_implicit || Kind == kw_implicit_define ||
192 Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
193 Kind == kw_undef || Kind == kw_internal ||
194 Kind == kw_early_clobber || Kind == kw_debug_use ||
195 Kind == kw_renamable;
198 bool isMemoryOperandFlag() const {
199 return Kind == kw_volatile || Kind == kw_non_temporal ||
200 Kind == kw_dereferenceable || Kind == kw_invariant ||
201 Kind == StringConstant;
204 bool is(TokenKind K) const { return Kind == K; }
206 bool isNot(TokenKind K) const { return Kind != K; }
208 StringRef::iterator location() const { return Range.begin(); }
210 StringRef range() const { return Range; }
212 /// Return the token's string value.
213 StringRef stringValue() const { return StringValue; }
215 const APSInt &integerValue() const { return IntVal; }
217 bool hasIntegerValue() const {
218 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
219 Kind == MachineBasicBlockLabel || Kind == StackObject ||
220 Kind == FixedStackObject || Kind == GlobalValue ||
221 Kind == VirtualRegister || Kind == ConstantPoolItem ||
222 Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
226 /// Consume a single machine instruction token in the given source and return
227 /// the remaining source string.
228 StringRef lexMIToken(
229 StringRef Source, MIToken &Token,
230 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
232 } // end namespace llvm
234 #endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H