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