MIR Serialization: Serialize the memory operand's range metadata node.
[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     Newline,
34
35     // Tokens with no info.
36     comma,
37     equal,
38     underscore,
39     colon,
40     coloncolon,
41     exclaim,
42     lparen,
43     rparen,
44     lbrace,
45     rbrace,
46     plus,
47     minus,
48
49     // Keywords
50     kw_implicit,
51     kw_implicit_define,
52     kw_dead,
53     kw_killed,
54     kw_undef,
55     kw_internal,
56     kw_early_clobber,
57     kw_debug_use,
58     kw_frame_setup,
59     kw_debug_location,
60     kw_cfi_same_value,
61     kw_cfi_offset,
62     kw_cfi_def_cfa_register,
63     kw_cfi_def_cfa_offset,
64     kw_cfi_def_cfa,
65     kw_blockaddress,
66     kw_target_index,
67     kw_half,
68     kw_float,
69     kw_double,
70     kw_x86_fp80,
71     kw_fp128,
72     kw_ppc_fp128,
73     kw_target_flags,
74     kw_volatile,
75     kw_non_temporal,
76     kw_invariant,
77     kw_align,
78     kw_stack,
79     kw_got,
80     kw_jump_table,
81     kw_constant_pool,
82     kw_liveout,
83     kw_address_taken,
84     kw_landing_pad,
85     kw_liveins,
86     kw_successors,
87
88     // Named metadata keywords
89     md_tbaa,
90     md_alias_scope,
91     md_noalias,
92     md_range,
93
94     // Identifier tokens
95     Identifier,
96     IntegerType,
97     NamedRegister,
98     MachineBasicBlockLabel,
99     MachineBasicBlock,
100     StackObject,
101     FixedStackObject,
102     NamedGlobalValue,
103     GlobalValue,
104     ExternalSymbol,
105
106     // Other tokens
107     IntegerLiteral,
108     FloatingPointLiteral,
109     VirtualRegister,
110     ConstantPoolItem,
111     JumpTableIndex,
112     NamedIRBlock,
113     IRBlock,
114     NamedIRValue,
115   };
116
117 private:
118   TokenKind Kind;
119   StringRef Range;
120   StringRef StringValue;
121   std::string StringValueStorage;
122   APSInt IntVal;
123
124 public:
125   MIToken() : Kind(Error) {}
126
127   MIToken &reset(TokenKind Kind, StringRef Range);
128
129   MIToken &setStringValue(StringRef StrVal);
130   MIToken &setOwnedStringValue(std::string StrVal);
131   MIToken &setIntegerValue(APSInt IntVal);
132
133   TokenKind kind() const { return Kind; }
134
135   bool isError() const { return Kind == Error; }
136
137   bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
138
139   bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
140
141   bool isRegister() const {
142     return Kind == NamedRegister || Kind == underscore ||
143            Kind == VirtualRegister;
144   }
145
146   bool isRegisterFlag() const {
147     return Kind == kw_implicit || Kind == kw_implicit_define ||
148            Kind == kw_dead || Kind == kw_killed || Kind == kw_undef ||
149            Kind == kw_internal || Kind == kw_early_clobber ||
150            Kind == kw_debug_use;
151   }
152
153   bool isMemoryOperandFlag() const {
154     return Kind == kw_volatile || Kind == kw_non_temporal ||
155            Kind == kw_invariant;
156   }
157
158   bool is(TokenKind K) const { return Kind == K; }
159
160   bool isNot(TokenKind K) const { return Kind != K; }
161
162   StringRef::iterator location() const { return Range.begin(); }
163
164   StringRef range() const { return Range; }
165
166   /// Return the token's string value.
167   StringRef stringValue() const { return StringValue; }
168
169   const APSInt &integerValue() const { return IntVal; }
170
171   bool hasIntegerValue() const {
172     return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
173            Kind == MachineBasicBlockLabel || Kind == StackObject ||
174            Kind == FixedStackObject || Kind == GlobalValue ||
175            Kind == VirtualRegister || Kind == ConstantPoolItem ||
176            Kind == JumpTableIndex || Kind == IRBlock;
177   }
178 };
179
180 /// Consume a single machine instruction token in the given source and return
181 /// the remaining source string.
182 StringRef lexMIToken(
183     StringRef Source, MIToken &Token,
184     function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
185
186 } // end namespace llvm
187
188 #endif