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