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.
47 kw_cfi_def_cfa_offset,
56 QuotedNamedGlobalValue,
70 unsigned StringOffset;
75 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
76 : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
78 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
79 unsigned StringOffset = 0)
80 : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
82 TokenKind kind() const { return Kind; }
84 bool isError() const { return Kind == Error; }
86 bool isRegister() const {
87 return Kind == NamedRegister || Kind == underscore ||
88 Kind == VirtualRegister;
91 bool isRegisterFlag() const {
92 return Kind == kw_implicit || Kind == kw_implicit_define ||
93 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
96 bool is(TokenKind K) const { return Kind == K; }
98 bool isNot(TokenKind K) const { return Kind != K; }
100 StringRef::iterator location() const { return Range.begin(); }
102 bool isStringValueQuoted() const {
103 return Kind == QuotedNamedGlobalValue || Kind == QuotedExternalSymbol;
106 /// Return the token's raw string value.
108 /// If the string value is quoted, this method returns that quoted string as
109 /// it is, without unescaping the string value.
110 StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
112 /// Return token's string value.
114 /// Expects the string value to be unquoted.
115 StringRef stringValue() const {
116 assert(!isStringValueQuoted() && "String value is quoted");
117 return Range.drop_front(StringOffset);
120 /// Unescapes the token's string value.
122 /// Expects the string value to be quoted.
123 void unescapeQuotedStringValue(std::string &Str) const;
125 const APSInt &integerValue() const { return IntVal; }
127 bool hasIntegerValue() const {
128 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
129 Kind == StackObject || Kind == FixedStackObject ||
130 Kind == GlobalValue || Kind == VirtualRegister ||
131 Kind == ConstantPoolItem || Kind == JumpTableIndex;
135 /// Consume a single machine instruction token in the given source and return
136 /// the remaining source string.
137 StringRef lexMIToken(
138 StringRef Source, MIToken &Token,
139 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
141 } // end namespace llvm