MIR Serialization: Serialize the 'early-clobber' register 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
44     // Keywords
45     kw_implicit,
46     kw_implicit_define,
47     kw_dead,
48     kw_killed,
49     kw_undef,
50     kw_early_clobber,
51     kw_debug_use,
52     kw_frame_setup,
53     kw_debug_location,
54     kw_cfi_offset,
55     kw_cfi_def_cfa_register,
56     kw_cfi_def_cfa_offset,
57     kw_cfi_def_cfa,
58     kw_blockaddress,
59     kw_target_index,
60     kw_half,
61     kw_float,
62     kw_double,
63     kw_x86_fp80,
64     kw_fp128,
65     kw_ppc_fp128,
66     kw_volatile,
67
68     // Identifier tokens
69     Identifier,
70     NamedRegister,
71     MachineBasicBlock,
72     StackObject,
73     FixedStackObject,
74     NamedGlobalValue,
75     GlobalValue,
76     ExternalSymbol,
77
78     // Other tokens
79     IntegerLiteral,
80     FloatingPointLiteral,
81     VirtualRegister,
82     ConstantPoolItem,
83     JumpTableIndex,
84     NamedIRBlock,
85     IRBlock,
86     NamedIRValue,
87   };
88
89 private:
90   TokenKind Kind;
91   unsigned StringOffset;
92   bool HasStringValue;
93   StringRef Range;
94   std::string StringValue;
95   APSInt IntVal;
96
97 public:
98   MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
99       : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
100         Range(Range) {}
101
102   MIToken(TokenKind Kind, StringRef Range, std::string StringValue,
103           unsigned StringOffset = 0)
104       : Kind(Kind), StringOffset(StringOffset), HasStringValue(true),
105         Range(Range), StringValue(std::move(StringValue)) {}
106
107   MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
108           unsigned StringOffset = 0)
109       : Kind(Kind), StringOffset(StringOffset), HasStringValue(false),
110         Range(Range), IntVal(IntVal) {}
111
112   TokenKind kind() const { return Kind; }
113
114   bool isError() const { return Kind == Error; }
115
116   bool isRegister() const {
117     return Kind == NamedRegister || Kind == underscore ||
118            Kind == VirtualRegister;
119   }
120
121   bool isRegisterFlag() const {
122     return Kind == kw_implicit || Kind == kw_implicit_define ||
123            Kind == kw_dead || Kind == kw_killed || Kind == kw_undef ||
124            Kind == kw_early_clobber || Kind == kw_debug_use;
125   }
126
127   bool isMemoryOperandFlag() const { return Kind == kw_volatile; }
128
129   bool is(TokenKind K) const { return Kind == K; }
130
131   bool isNot(TokenKind K) const { return Kind != K; }
132
133   StringRef::iterator location() const { return Range.begin(); }
134
135   /// Return the token's raw string value.
136   ///
137   /// If the string value is quoted, this method returns that quoted string as
138   /// it is, without unescaping the string value.
139   StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
140
141   /// Return the token's string value.
142   StringRef stringValue() const {
143     return HasStringValue ? StringRef(StringValue)
144                           : Range.drop_front(StringOffset);
145   }
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