MIR Serialization: Serialize the GOT 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_stack,
74     kw_got,
75     kw_constant_pool,
76     kw_liveout,
77
78     // Identifier tokens
79     Identifier,
80     IntegerType,
81     NamedRegister,
82     MachineBasicBlock,
83     StackObject,
84     FixedStackObject,
85     NamedGlobalValue,
86     GlobalValue,
87     ExternalSymbol,
88
89     // Other tokens
90     IntegerLiteral,
91     FloatingPointLiteral,
92     VirtualRegister,
93     ConstantPoolItem,
94     JumpTableIndex,
95     NamedIRBlock,
96     IRBlock,
97     NamedIRValue,
98   };
99
100 private:
101   TokenKind Kind;
102   StringRef Range;
103   StringRef StringValue;
104   std::string StringValueStorage;
105   APSInt IntVal;
106
107 public:
108   MIToken() : Kind(Error) {}
109
110   MIToken &reset(TokenKind Kind, StringRef Range);
111
112   MIToken &setStringValue(StringRef StrVal);
113   MIToken &setOwnedStringValue(std::string StrVal);
114   MIToken &setIntegerValue(APSInt IntVal);
115
116   TokenKind kind() const { return Kind; }
117
118   bool isError() const { return Kind == Error; }
119
120   bool isRegister() const {
121     return Kind == NamedRegister || Kind == underscore ||
122            Kind == VirtualRegister;
123   }
124
125   bool isRegisterFlag() const {
126     return Kind == kw_implicit || Kind == kw_implicit_define ||
127            Kind == kw_dead || Kind == kw_killed || Kind == kw_undef ||
128            Kind == kw_early_clobber || Kind == kw_debug_use;
129   }
130
131   bool isMemoryOperandFlag() const {
132     return Kind == kw_volatile || Kind == kw_non_temporal ||
133            Kind == kw_invariant;
134   }
135
136   bool is(TokenKind K) const { return Kind == K; }
137
138   bool isNot(TokenKind K) const { return Kind != K; }
139
140   StringRef::iterator location() const { return Range.begin(); }
141
142   StringRef range() const { return Range; }
143
144   /// Return the token's string value.
145   StringRef stringValue() const { return StringValue; }
146
147   const APSInt &integerValue() const { return IntVal; }
148
149   bool hasIntegerValue() const {
150     return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
151            Kind == StackObject || Kind == FixedStackObject ||
152            Kind == GlobalValue || Kind == VirtualRegister ||
153            Kind == ConstantPoolItem || Kind == JumpTableIndex ||
154            Kind == IRBlock;
155   }
156 };
157
158 /// Consume a single machine instruction token in the given source and return
159 /// the remaining source string.
160 StringRef lexMIToken(
161     StringRef Source, MIToken &Token,
162     function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
163
164 } // end namespace llvm
165
166 #endif