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