MIR Serialization: Serialize instruction's register ties.
[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   };
118
119 private:
120   TokenKind Kind;
121   StringRef Range;
122   StringRef StringValue;
123   std::string StringValueStorage;
124   APSInt IntVal;
125
126 public:
127   MIToken() : Kind(Error) {}
128
129   MIToken &reset(TokenKind Kind, StringRef Range);
130
131   MIToken &setStringValue(StringRef StrVal);
132   MIToken &setOwnedStringValue(std::string StrVal);
133   MIToken &setIntegerValue(APSInt IntVal);
134
135   TokenKind kind() const { return Kind; }
136
137   bool isError() const { return Kind == Error; }
138
139   bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
140
141   bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
142
143   bool isRegister() const {
144     return Kind == NamedRegister || Kind == underscore ||
145            Kind == VirtualRegister;
146   }
147
148   bool isRegisterFlag() const {
149     return Kind == kw_implicit || Kind == kw_implicit_define ||
150            Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
151            Kind == kw_undef || Kind == kw_internal ||
152            Kind == kw_early_clobber || Kind == kw_debug_use;
153   }
154
155   bool isMemoryOperandFlag() const {
156     return Kind == kw_volatile || Kind == kw_non_temporal ||
157            Kind == kw_invariant;
158   }
159
160   bool is(TokenKind K) const { return Kind == K; }
161
162   bool isNot(TokenKind K) const { return Kind != K; }
163
164   StringRef::iterator location() const { return Range.begin(); }
165
166   StringRef range() const { return Range; }
167
168   /// Return the token's string value.
169   StringRef stringValue() const { return StringValue; }
170
171   const APSInt &integerValue() const { return IntVal; }
172
173   bool hasIntegerValue() const {
174     return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
175            Kind == MachineBasicBlockLabel || Kind == StackObject ||
176            Kind == FixedStackObject || Kind == GlobalValue ||
177            Kind == VirtualRegister || Kind == ConstantPoolItem ||
178            Kind == JumpTableIndex || Kind == IRBlock;
179   }
180 };
181
182 /// Consume a single machine instruction token in the given source and return
183 /// the remaining source string.
184 StringRef lexMIToken(
185     StringRef Source, MIToken &Token,
186     function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
187
188 } // end namespace llvm
189
190 #endif