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