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