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