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