MIR Serialization: Initial serialization of machine constant pools.
[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
40     // Keywords
41     kw_implicit,
42     kw_implicit_define,
43     kw_dead,
44     kw_killed,
45     kw_undef,
46     kw_frame_setup,
47
48     // Identifier tokens
49     Identifier,
50     NamedRegister,
51     MachineBasicBlock,
52     StackObject,
53     FixedStackObject,
54     NamedGlobalValue,
55     QuotedNamedGlobalValue,
56     GlobalValue,
57
58     // Other tokens
59     IntegerLiteral,
60     VirtualRegister,
61     ConstantPoolItem,
62     JumpTableIndex
63   };
64
65 private:
66   TokenKind Kind;
67   unsigned StringOffset;
68   StringRef Range;
69   APSInt IntVal;
70
71 public:
72   MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0)
73       : Kind(Kind), StringOffset(StringOffset), Range(Range) {}
74
75   MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal,
76           unsigned StringOffset = 0)
77       : Kind(Kind), StringOffset(StringOffset), Range(Range), IntVal(IntVal) {}
78
79   TokenKind kind() const { return Kind; }
80
81   bool isError() const { return Kind == Error; }
82
83   bool isRegister() const {
84     return Kind == NamedRegister || Kind == underscore ||
85            Kind == VirtualRegister;
86   }
87
88   bool isRegisterFlag() const {
89     return Kind == kw_implicit || Kind == kw_implicit_define ||
90            Kind == kw_dead || Kind == kw_killed || Kind == kw_undef;
91   }
92
93   bool is(TokenKind K) const { return Kind == K; }
94
95   bool isNot(TokenKind K) const { return Kind != K; }
96
97   StringRef::iterator location() const { return Range.begin(); }
98
99   bool isStringValueQuoted() const { return Kind == QuotedNamedGlobalValue; }
100
101   /// Return the token's raw string value.
102   ///
103   /// If the string value is quoted, this method returns that quoted string as
104   /// it is, without unescaping the string value.
105   StringRef rawStringValue() const { return Range.drop_front(StringOffset); }
106
107   /// Return token's string value.
108   ///
109   /// Expects the string value to be unquoted.
110   StringRef stringValue() const {
111     assert(!isStringValueQuoted() && "String value is quoted");
112     return Range.drop_front(StringOffset);
113   }
114
115   /// Unescapes the token's string value.
116   ///
117   /// Expects the string value to be quoted.
118   void unescapeQuotedStringValue(std::string &Str) const;
119
120   const APSInt &integerValue() const { return IntVal; }
121
122   bool hasIntegerValue() const {
123     return Kind == IntegerLiteral || Kind == MachineBasicBlock ||
124            Kind == StackObject || Kind == FixedStackObject ||
125            Kind == GlobalValue || Kind == VirtualRegister ||
126            Kind == ConstantPoolItem || Kind == JumpTableIndex;
127   }
128 };
129
130 /// Consume a single machine instruction token in the given source and return
131 /// the remaining source string.
132 StringRef lexMIToken(
133     StringRef Source, MIToken &Token,
134     function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
135
136 } // end namespace llvm
137
138 #endif