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