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