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