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,
65 QuotedNamedGlobalValue,
82 unsigned StringOffset;
87 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
88 : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
90 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
91 unsigned StringOffset = 0)
92 : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
94 TokenKind kind() const { return Kind; }
96 bool isError() const { return Kind == Error; }
98 bool isRegister() const {
99 return Kind == NamedRegister || Kind == underscore ||
100 Kind == VirtualRegister;
103 bool isRegisterFlag() const {
104 return Kind == kw_implicit || Kind == kw_implicit_define ||
105 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
108 bool is(TokenKind K) const { return Kind == K; }
110 bool isNot(TokenKind K) const { return Kind != K; }
112 StringRef::iterator location() const { return Range.begin(); }
114 bool isStringValueQuoted() const {
115 return Kind == QuotedNamedGlobalValue || Kind == QuotedExternalSymbol ||
116 Kind == QuotedNamedIRBlock;
119 /// Return the token's raw string value.
121 /// If the string value is quoted, this method returns that quoted string as
122 /// it is, without unescaping the string value.
123 StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
125 /// Return token's string value.
127 /// Expects the string value to be unquoted.
128 StringRef stringValue() const {
129 assert(!isStringValueQuoted() && "String value is quoted");
130 return Range.drop_front(StringOffset);
133 /// Unescapes the token's string value.
135 /// Expects the string value to be quoted.
136 void unescapeQuotedStringValue(std::string &Str) const;
138 const APSInt &integerValue() const { return IntVal; }
140 bool hasIntegerValue() const {
141 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
142 Kind == StackObject || Kind == FixedStackObject ||
143 Kind == GlobalValue || Kind == VirtualRegister ||
144 Kind == ConstantPoolItem || Kind == JumpTableIndex ||
149 /// Consume a single machine instruction token in the given source and return
150 /// the remaining source string.
151 StringRef lexMIToken(
152 StringRef Source, MIToken &Token,
153 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
155 } // end namespace llvm