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