MIR Serialization: Change syntax for the call entry pseudo source values.
[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_call_entry,
85     kw_liveout,
86     kw_address_taken,
87     kw_landing_pad,
88     kw_liveins,
89     kw_successors,
90
91     // Named metadata keywords
92     md_tbaa,
93     md_alias_scope,
94     md_noalias,
95     md_range,
96
97     // Identifier tokens
98     Identifier,
99     IntegerType,
100     NamedRegister,
101     MachineBasicBlockLabel,
102     MachineBasicBlock,
103     StackObject,
104     FixedStackObject,
105     NamedGlobalValue,
106     GlobalValue,
107     ExternalSymbol,
108
109     // Other tokens
110     IntegerLiteral,
111     FloatingPointLiteral,
112     VirtualRegister,
113     ConstantPoolItem,
114     JumpTableIndex,
115     NamedIRBlock,
116     IRBlock,
117     NamedIRValue,
118     IRValue
119   };
120
121 private:
122   TokenKind Kind;
123   StringRef Range;
124   StringRef StringValue;
125   std::string StringValueStorage;
126   APSInt IntVal;
127
128 public:
129   MIToken() : Kind(Error) {}
130
131   MIToken &reset(TokenKind Kind, StringRef Range);
132
133   MIToken &setStringValue(StringRef StrVal);
134   MIToken &setOwnedStringValue(std::string StrVal);
135   MIToken &setIntegerValue(APSInt IntVal);
136
137   TokenKind kind() const { return Kind; }
138
139   bool isError() const { return Kind == Error; }
140
141   bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
142
143   bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
144
145   bool isRegister() const {
146     return Kind == NamedRegister || Kind == underscore ||
147            Kind == VirtualRegister;
148   }
149
150   bool isRegisterFlag() const {
151     return Kind == kw_implicit || Kind == kw_implicit_define ||
152            Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
153            Kind == kw_undef || Kind == kw_internal ||
154            Kind == kw_early_clobber || Kind == kw_debug_use;
155   }
156
157   bool isMemoryOperandFlag() const {
158     return Kind == kw_volatile || Kind == kw_non_temporal ||
159            Kind == kw_invariant;
160   }
161
162   bool is(TokenKind K) const { return Kind == K; }
163
164   bool isNot(TokenKind K) const { return Kind != K; }
165
166   StringRef::iterator location() const { return Range.begin(); }
167
168   StringRef range() const { return Range; }
169
170   /// Return the token's string value.
171   StringRef stringValue() const { return StringValue; }
172
173   const APSInt &integerValue() const { return IntVal; }
174
175   bool hasIntegerValue() const {
176     return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
177            Kind == MachineBasicBlockLabel || Kind == StackObject ||
178            Kind == FixedStackObject || Kind == GlobalValue ||
179            Kind == VirtualRegister || Kind == ConstantPoolItem ||
180            Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
181   }
182 };
183
184 /// Consume a single machine instruction token in the given source and return
185 /// the remaining source string.
186 StringRef lexMIToken(
187     StringRef Source, MIToken &Token,
188     function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
189
190 } // end namespace llvm
191
192 #endif