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