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.
50 kw_cfi_def_cfa_register,
51 kw_cfi_def_cfa_offset,
60 QuotedNamedGlobalValue,
75 unsigned StringOffset;
80 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
81 : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
83 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
84 unsigned StringOffset = 0)
85 : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
87 TokenKind kind() const { return Kind; }
89 bool isError() const { return Kind == Error; }
91 bool isRegister() const {
92 return Kind == NamedRegister || Kind == underscore ||
93 Kind == VirtualRegister;
96 bool isRegisterFlag() const {
97 return Kind == kw_implicit || Kind == kw_implicit_define ||
98 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
101 bool is(TokenKind K) const { return Kind == K; }
103 bool isNot(TokenKind K) const { return Kind != K; }
105 StringRef::iterator location() const { return Range.begin(); }
107 bool isStringValueQuoted() const {
108 return Kind == QuotedNamedGlobalValue || Kind == QuotedExternalSymbol;
111 /// Return the token's raw string value.
113 /// If the string value is quoted, this method returns that quoted string as
114 /// it is, without unescaping the string value.
115 StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
117 /// Return token's string value.
119 /// Expects the string value to be unquoted.
120 StringRef stringValue() const {
121 assert(!isStringValueQuoted() && "String value is quoted");
122 return Range.drop_front(StringOffset);
125 /// Unescapes the token's string value.
127 /// Expects the string value to be quoted.
128 void unescapeQuotedStringValue(std::string &Str) const;
130 const APSInt &integerValue() const { return IntVal; }
132 bool hasIntegerValue() const {
133 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
134 Kind == StackObject || Kind == FixedStackObject ||
135 Kind == GlobalValue || Kind == VirtualRegister ||
136 Kind == ConstantPoolItem || Kind == JumpTableIndex ||
141 /// Consume a single machine instruction token in the given source and return
142 /// the remaining source string.
143 StringRef lexMIToken(
144 StringRef Source, MIToken &Token,
145 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
147 } // end namespace llvm