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