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