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.
52 kw_cfi_def_cfa_register,
53 kw_cfi_def_cfa_offset,
71 QuotedNamedGlobalValue,
89 unsigned StringOffset;
94 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
95 : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
97 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
98 unsigned StringOffset = 0)
99 : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
101 TokenKind kind() const { return Kind; }
103 bool isError() const { return Kind == Error; }
105 bool isRegister() const {
106 return Kind == NamedRegister || Kind == underscore ||
107 Kind == VirtualRegister;
110 bool isRegisterFlag() const {
111 return Kind == kw_implicit || Kind == kw_implicit_define ||
112 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
115 bool is(TokenKind K) const { return Kind == K; }
117 bool isNot(TokenKind K) const { return Kind != K; }
119 StringRef::iterator location() const { return Range.begin(); }
121 bool isStringValueQuoted() const {
122 return Kind == QuotedNamedGlobalValue || Kind == QuotedExternalSymbol ||
123 Kind == QuotedNamedIRBlock;
126 /// Return the token's raw string value.
128 /// If the string value is quoted, this method returns that quoted string as
129 /// it is, without unescaping the string value.
130 StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
132 /// Return token's string value.
134 /// Expects the string value to be unquoted.
135 StringRef stringValue() const {
136 assert(!isStringValueQuoted() && "String value is quoted");
137 return Range.drop_front(StringOffset);
140 /// Unescapes the token's string value.
142 /// Expects the string value to be quoted.
143 void unescapeQuotedStringValue(std::string &Str) const;
145 const APSInt &integerValue() const { return IntVal; }
147 bool hasIntegerValue() const {
148 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
149 Kind == StackObject || Kind == FixedStackObject ||
150 Kind == GlobalValue || Kind == VirtualRegister ||
151 Kind == ConstantPoolItem || Kind == JumpTableIndex ||
156 /// Consume a single machine instruction token in the given source and return
157 /// the remaining source string.
158 StringRef lexMIToken(
159 StringRef Source, MIToken &Token,
160 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
162 } // end namespace llvm