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