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