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