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,
73 QuotedNamedGlobalValue,
93 unsigned StringOffset;
98 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
99 : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
101 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
102 unsigned StringOffset = 0)
103 : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
105 TokenKind kind() const { return Kind; }
107 bool isError() const { return Kind == Error; }
109 bool isRegister() const {
110 return Kind == NamedRegister || Kind == underscore ||
111 Kind == VirtualRegister;
114 bool isRegisterFlag() const {
115 return Kind == kw_implicit || Kind == kw_implicit_define ||
116 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
119 bool isMemoryOperandFlag() const { return Kind == kw_volatile; }
121 bool is(TokenKind K) const { return Kind == K; }
123 bool isNot(TokenKind K) const { return Kind != K; }
125 StringRef::iterator location() const { return Range.begin(); }
127 bool isStringValueQuoted() const {
128 return Kind == QuotedNamedGlobalValue || Kind == QuotedExternalSymbol ||
129 Kind == QuotedNamedIRBlock || Kind == QuotedNamedIRValue;
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 token's string value.
140 /// Expects the string value to be unquoted.
141 StringRef stringValue() const {
142 assert(!isStringValueQuoted() && "String value is quoted");
143 return Range.drop_front(StringOffset);
146 /// Unescapes the token's string value.
148 /// Expects the string value to be quoted.
149 void unescapeQuotedStringValue(std::string &Str) const;
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