MIR Serialization: Serialize the 'debug-use' register operand flag.
[oota-llvm.git] / lib / CodeGen / MIRParser / MILexer.h
1 //===- MILexer.h - Lexer for machine instructions -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the function that lexes the machine instruction source
11 // string.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
16 #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
17
18 #include "llvm/ADT/APSInt.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include <functional>
22
23 namespace llvm {
24
25 class Twine;
26
27 /// A token produced by the machine instruction lexer.
28 struct MIToken {
29   enum TokenKind {
30     // Markers
31     Eof,
32     Error,
33
34     // Tokens with no info.
35     comma,
36     equal,
37     underscore,
38     colon,
39     coloncolon,
40     exclaim,
41     lparen,
42     rparen,
43
44     // Keywords
45     kw_implicit,
46     kw_implicit_define,
47     kw_dead,
48     kw_killed,
49     kw_undef,
50     kw_debug_use,
51     kw_frame_setup,
52     kw_debug_location,
53     kw_cfi_offset,
54     kw_cfi_def_cfa_register,
55     kw_cfi_def_cfa_offset,
56     kw_cfi_def_cfa,
57     kw_blockaddress,
58     kw_target_index,
59     kw_half,
60     kw_float,
61     kw_double,
62     kw_x86_fp80,
63     kw_fp128,
64     kw_ppc_fp128,
65     kw_volatile,
66
67     // Identifier tokens
68     Identifier,
69     NamedRegister,
70     MachineBasicBlock,
71     StackObject,
72     FixedStackObject,
73     NamedGlobalValue,
74     GlobalValue,
75     ExternalSymbol,
76
77     // Other tokens
78     IntegerLiteral,
79     FloatingPointLiteral,
80     VirtualRegister,
81     ConstantPoolItem,
82     JumpTableIndex,
83     NamedIRBlock,
84     IRBlock,
85     NamedIRValue,
86   };
87
88 private:
89   TokenKind Kind;
90   unsigned StringOffset;
91   bool HasStringValue;
92   StringRef Range;
93   std::string StringValue;
94   APSInt IntVal;
95
96 public:
97   MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
98       : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
99         Range(Range) {}
100
101   MIToken(TokenKind Kind, StringRef Range, std::string StringValue,
102           unsigned StringOffset = 0)
103       : Kind(Kind), StringOffset(StringOffset), HasStringValue(true),
104         Range(Range), StringValue(std::move(StringValue)) {}
105
106   MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
107           unsigned StringOffset = 0)
108       : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
109         Range(Range), IntVal(IntVal) {}
110
111   TokenKind kind() const { return Kind; }
112
113   bool isError() const { return Kind == Error; }
114
115   bool isRegister() const {
116     return Kind == NamedRegister || Kind == underscore ||
117            Kind == VirtualRegister;
118   }
119
120   bool isRegisterFlag() const {
121     return Kind == kw_implicit || Kind == kw_implicit_define ||
122            Kind == kw_dead || Kind == kw_killed || Kind == kw_undef ||
123            Kind == kw_debug_use;
124   }
125
126   bool isMemoryOperandFlag() const { return Kind == kw_volatile; }
127
128   bool is(TokenKind K) const { return Kind == K; }
129
130   bool isNot(TokenKind K) const { return Kind != K; }
131
132   StringRef::iterator location() const { return Range.begin(); }
133
134   /// Return the token's raw string value.
135   ///
136   /// If the string value is quoted, this method returns that quoted string as
137   /// it is, without unescaping the string value.
138   StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
139
140   /// Return the token's string value.
141   StringRef stringValue() const {
142     return HasStringValue ? StringRef(StringValue)
143                           : Range.drop_front(StringOffset);
144   }
145
146   const APSInt &integerValue() const { return IntVal; }
147
148   bool hasIntegerValue() const {
149     return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
150            Kind == StackObject || Kind == FixedStackObject ||
151            Kind == GlobalValue || Kind == VirtualRegister ||
152            Kind == ConstantPoolItem || Kind == JumpTableIndex ||
153            Kind == IRBlock;
154   }
155 };
156
157 /// Consume a single machine instruction token in the given source and return
158 /// the remaining source string.
159 StringRef lexMIToken(
160     StringRef Source, MIToken &Token,
161     function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
162
163 } // end namespace llvm
164
165 #endif