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