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.
55 QuotedNamedGlobalValue,
69 unsigned StringOffset;
74 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
75 : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
77 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
78 unsigned StringOffset = 0)
79 : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
81 TokenKind kind() const { return Kind; }
83 bool isError() const { return Kind == Error; }
85 bool isRegister() const {
86 return Kind == NamedRegister || Kind == underscore ||
87 Kind == VirtualRegister;
90 bool isRegisterFlag() const {
91 return Kind == kw_implicit || Kind == kw_implicit_define ||
92 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
95 bool is(TokenKind K) const { return Kind == K; }
97 bool isNot(TokenKind K) const { return Kind != K; }
99 StringRef::iterator location() const { return Range.begin(); }
101 bool isStringValueQuoted() const {
102 return Kind == QuotedNamedGlobalValue || Kind == QuotedExternalSymbol;
105 /// Return the token's raw string value.
107 /// If the string value is quoted, this method returns that quoted string as
108 /// it is, without unescaping the string value.
109 StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
111 /// Return token's string value.
113 /// Expects the string value to be unquoted.
114 StringRef stringValue() const {
115 assert(!isStringValueQuoted() && "String value is quoted");
116 return Range.drop_front(StringOffset);
119 /// Unescapes the token's string value.
121 /// Expects the string value to be quoted.
122 void unescapeQuotedStringValue(std::string &Str) const;
124 const APSInt &integerValue() const { return IntVal; }
126 bool hasIntegerValue() const {
127 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
128 Kind == StackObject || Kind == FixedStackObject ||
129 Kind == GlobalValue || Kind == VirtualRegister ||
130 Kind == ConstantPoolItem || Kind == JumpTableIndex;
134 /// Consume a single machine instruction token in the given source and return
135 /// the remaining source string.
136 StringRef lexMIToken(
137 StringRef Source, MIToken &Token,
138 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
140 } // end namespace llvm