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