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