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