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.
55 kw_cfi_def_cfa_register,
56 kw_cfi_def_cfa_offset,
92 unsigned StringOffset;
95 std::string StringValue;
99 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
100 : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
103 MIToken(TokenKind Kind, StringRef Range, std::string StringValue,
104 unsigned StringOffset = 0)
105 : Kind(Kind), StringOffset(StringOffset), HasStringValue(true),
106 Range(Range), StringValue(std::move(StringValue)) {}
108 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
109 unsigned StringOffset = 0)
110 : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
111 Range(Range), IntVal(IntVal) {}
113 TokenKind kind() const { return Kind; }
115 bool isError() const { return Kind == Error; }
117 bool isRegister() const {
118 return Kind == NamedRegister || Kind == underscore ||
119 Kind == VirtualRegister;
122 bool isRegisterFlag() const {
123 return Kind == kw_implicit || Kind == kw_implicit_define ||
124 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef ||
125 Kind == kw_early_clobber || Kind == kw_debug_use;
128 bool isMemoryOperandFlag() const { return Kind == kw_volatile; }
130 bool is(TokenKind K) const { return Kind == K; }
132 bool isNot(TokenKind K) const { return Kind != K; }
134 StringRef::iterator location() const { return Range.begin(); }
136 /// Return the token's raw string value.
138 /// If the string value is quoted, this method returns that quoted string as
139 /// it is, without unescaping the string value.
140 StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
142 /// Return the token's string value.
143 StringRef stringValue() const {
144 return HasStringValue ? StringRef(StringValue)
145 : Range.drop_front(StringOffset);
148 const APSInt &integerValue() const { return IntVal; }
150 bool hasIntegerValue() const {
151 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
152 Kind == StackObject || Kind == FixedStackObject ||
153 Kind == GlobalValue || Kind == VirtualRegister ||
154 Kind == ConstantPoolItem || Kind == JumpTableIndex ||
159 /// Consume a single machine instruction token in the given source and return
160 /// the remaining source string.
161 StringRef lexMIToken(
162 StringRef Source, MIToken &Token,
163 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
165 } // end namespace llvm