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