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_offset,
59 QuotedNamedGlobalValue,
73 unsigned StringOffset;
78 MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
79 : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
81 MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
82 unsigned StringOffset = 0)
83 : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
85 TokenKind kind() const { return Kind; }
87 bool isError() const { return Kind == Error; }
89 bool isRegister() const {
90 return Kind == NamedRegister || Kind == underscore ||
91 Kind == VirtualRegister;
94 bool isRegisterFlag() const {
95 return Kind == kw_implicit || Kind == kw_implicit_define ||
96 Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
99 bool is(TokenKind K) const { return Kind == K; }
101 bool isNot(TokenKind K) const { return Kind != K; }
103 StringRef::iterator location() const { return Range.begin(); }
105 bool isStringValueQuoted() const {
106 return Kind == QuotedNamedGlobalValue || Kind == QuotedExternalSymbol;
109 /// Return the token's raw string value.
111 /// If the string value is quoted, this method returns that quoted string as
112 /// it is, without unescaping the string value.
113 StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
115 /// Return token's string value.
117 /// Expects the string value to be unquoted.
118 StringRef stringValue() const {
119 assert(!isStringValueQuoted() && "String value is quoted");
120 return Range.drop_front(StringOffset);
123 /// Unescapes the token's string value.
125 /// Expects the string value to be quoted.
126 void unescapeQuotedStringValue(std::string &Str) const;
128 const APSInt &integerValue() const { return IntVal; }
130 bool hasIntegerValue() const {
131 return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
132 Kind == StackObject || Kind == FixedStackObject ||
133 Kind == GlobalValue || Kind == VirtualRegister ||
134 Kind == ConstantPoolItem || Kind == JumpTableIndex;
138 /// Consume a single machine instruction token in the given source and return
139 /// the remaining source string.
140 StringRef lexMIToken(
141 StringRef Source, MIToken &Token,
142 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
144 } // end namespace llvm