MIR Serialization: Serialize the memory operand's TBAA 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
91     // Identifier tokens
92     Identifier,
93     IntegerType,
94     NamedRegister,
95     MachineBasicBlockLabel,
96     MachineBasicBlock,
97     StackObject,
98     FixedStackObject,
99     NamedGlobalValue,
100     GlobalValue,
101     ExternalSymbol,
102
103     // Other tokens
104     IntegerLiteral,
105     FloatingPointLiteral,
106     VirtualRegister,
107     ConstantPoolItem,
108     JumpTableIndex,
109     NamedIRBlock,
110     IRBlock,
111     NamedIRValue,
112   };
113
114 private:
115   TokenKind Kind;
116   StringRef Range;
117   StringRef StringValue;
118   std::string StringValueStorage;
119   APSInt IntVal;
120
121 public:
122   MIToken() : Kind(Error) {}
123
124   MIToken &reset(TokenKind Kind, StringRef Range);
125
126   MIToken &setStringValue(StringRef StrVal);
127   MIToken &setOwnedStringValue(std::string StrVal);
128   MIToken &setIntegerValue(APSInt IntVal);
129
130   TokenKind kind() const { return Kind; }
131
132   bool isError() const { return Kind == Error; }
133
134   bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
135
136   bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
137
138   bool isRegister() const {
139     return Kind == NamedRegister || Kind == underscore ||
140            Kind == VirtualRegister;
141   }
142
143   bool isRegisterFlag() const {
144     return Kind == kw_implicit || Kind == kw_implicit_define ||
145            Kind == kw_dead || Kind == kw_killed || Kind == kw_undef ||
146            Kind == kw_internal || Kind == kw_early_clobber ||
147            Kind == kw_debug_use;
148   }
149
150   bool isMemoryOperandFlag() const {
151     return Kind == kw_volatile || Kind == kw_non_temporal ||
152            Kind == kw_invariant;
153   }
154
155   bool is(TokenKind K) const { return Kind == K; }
156
157   bool isNot(TokenKind K) const { return Kind != K; }
158
159   StringRef::iterator location() const { return Range.begin(); }
160
161   StringRef range() const { return Range; }
162
163   /// Return the token's string value.
164   StringRef stringValue() const { return StringValue; }
165
166   const APSInt &integerValue() const { return IntVal; }
167
168   bool hasIntegerValue() const {
169     return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
170            Kind == MachineBasicBlockLabel || Kind == StackObject ||
171            Kind == FixedStackObject || Kind == GlobalValue ||
172            Kind == VirtualRegister || Kind == ConstantPoolItem ||
173            Kind == JumpTableIndex || Kind == IRBlock;
174   }
175 };
176
177 /// Consume a single machine instruction token in the given source and return
178 /// the remaining source string.
179 StringRef lexMIToken(
180     StringRef Source, MIToken &Token,
181     function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
182
183 } // end namespace llvm
184
185 #endif