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