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.
53 kw_cfi_def_cfa_register,
54 kw_cfi_def_cfa_offset,
89 unsigned StringOffset;
92 std::string StringValue;
96 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
97 : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
100 MIToken(TokenKind Kind, StringRef Range, std::string StringValue,
101 unsigned StringOffset = 0)
102 : Kind(Kind), StringOffset(StringOffset), HasStringValue(true),
103 Range(Range), StringValue(std::move(StringValue)) {}
105 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
106 unsigned StringOffset = 0)
107 : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
108 Range(Range), IntVal(IntVal) {}
110 TokenKind kind() const { return Kind; }
112 bool isError() const { return Kind == Error; }
114 bool isRegister() const {
115 return Kind == NamedRegister || Kind == underscore ||
116 Kind == VirtualRegister;
119 bool isRegisterFlag() const {
120 return Kind == kw_implicit || Kind == kw_implicit_define ||
121 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
124 bool isMemoryOperandFlag() const { return Kind == kw_volatile; }
126 bool is(TokenKind K) const { return Kind == K; }
128 bool isNot(TokenKind K) const { return Kind != K; }
130 StringRef::iterator location() const { return Range.begin(); }
132 /// Return the token's raw string value.
134 /// If the string value is quoted, this method returns that quoted string as
135 /// it is, without unescaping the string value.
136 StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
138 /// Return the token's string value.
139 StringRef stringValue() const {
140 return HasStringValue ? StringRef(StringValue)
141 : Range.drop_front(StringOffset);
144 const APSInt &integerValue() const { return IntVal; }
146 bool hasIntegerValue() const {
147 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
148 Kind == StackObject || Kind == FixedStackObject ||
149 Kind == GlobalValue || Kind == VirtualRegister ||
150 Kind == ConstantPoolItem || Kind == JumpTableIndex ||
155 /// Consume a single machine instruction token in the given source and return
156 /// the remaining source string.
157 StringRef lexMIToken(
158 StringRef Source, MIToken &Token,
159 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
161 } // end namespace llvm