MIR Serialization: Serialize the jump table index operands.
[oota-llvm.git] / lib / CodeGen / MIRParser / MILexer.cpp
1 //===- MILexer.cpp - Machine instructions lexer implementation ----------===//
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 implements the lexing of machine instructions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MILexer.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/Twine.h"
17 #include <cctype>
18
19 using namespace llvm;
20
21 namespace {
22
23 /// This class provides a way to iterate and get characters from the source
24 /// string.
25 class Cursor {
26   const char *Ptr;
27   const char *End;
28
29 public:
30   Cursor(NoneType) : Ptr(nullptr), End(nullptr) {}
31
32   explicit Cursor(StringRef Str) {
33     Ptr = Str.data();
34     End = Ptr + Str.size();
35   }
36
37   bool isEOF() const { return Ptr == End; }
38
39   char peek(int I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
40
41   void advance(unsigned I = 1) { Ptr += I; }
42
43   StringRef remaining() const { return StringRef(Ptr, End - Ptr); }
44
45   StringRef upto(Cursor C) const {
46     assert(C.Ptr >= Ptr && C.Ptr <= End);
47     return StringRef(Ptr, C.Ptr - Ptr);
48   }
49
50   StringRef::iterator location() const { return Ptr; }
51
52   operator bool() const { return Ptr != nullptr; }
53 };
54
55 } // end anonymous namespace
56
57 /// Skip the leading whitespace characters and return the updated cursor.
58 static Cursor skipWhitespace(Cursor C) {
59   while (isspace(C.peek()))
60     C.advance();
61   return C;
62 }
63
64 static bool isIdentifierChar(char C) {
65   return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.';
66 }
67
68 static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
69   return StringSwitch<MIToken::TokenKind>(Identifier)
70       .Case("_", MIToken::underscore)
71       .Case("implicit", MIToken::kw_implicit)
72       .Case("implicit-def", MIToken::kw_implicit_define)
73       .Case("dead", MIToken::kw_dead)
74       .Case("killed", MIToken::kw_killed)
75       .Case("undef", MIToken::kw_undef)
76       .Default(MIToken::Identifier);
77 }
78
79 static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
80   if (!isalpha(C.peek()) && C.peek() != '_')
81     return None;
82   auto Range = C;
83   while (isIdentifierChar(C.peek()))
84     C.advance();
85   auto Identifier = Range.upto(C);
86   Token = MIToken(getIdentifierKind(Identifier), Identifier);
87   return C;
88 }
89
90 static Cursor maybeLexMachineBasicBlock(
91     Cursor C, MIToken &Token,
92     function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
93   if (!C.remaining().startswith("%bb."))
94     return None;
95   auto Range = C;
96   C.advance(4); // Skip '%bb.'
97   if (!isdigit(C.peek())) {
98     Token = MIToken(MIToken::Error, C.remaining());
99     ErrorCallback(C.location(), "expected a number after '%bb.'");
100     return C;
101   }
102   auto NumberRange = C;
103   while (isdigit(C.peek()))
104     C.advance();
105   StringRef Number = NumberRange.upto(C);
106   unsigned StringOffset = 4 + Number.size(); // Drop '%bb.<id>'
107   if (C.peek() == '.') {
108     C.advance(); // Skip '.'
109     ++StringOffset;
110     while (isIdentifierChar(C.peek()))
111       C.advance();
112   }
113   Token = MIToken(MIToken::MachineBasicBlock, Range.upto(C), APSInt(Number),
114                   StringOffset);
115   return C;
116 }
117
118 static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
119                             MIToken::TokenKind Kind) {
120   if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
121     return None;
122   auto Range = C;
123   C.advance(Rule.size());
124   auto NumberRange = C;
125   while (isdigit(C.peek()))
126     C.advance();
127   Token = MIToken(Kind, Range.upto(C), APSInt(NumberRange.upto(C)));
128   return C;
129 }
130
131 static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
132   return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
133 }
134
135 static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
136   auto Range = C;
137   C.advance(); // Skip '%'
138   auto NumberRange = C;
139   while (isdigit(C.peek()))
140     C.advance();
141   Token = MIToken(MIToken::VirtualRegister, Range.upto(C),
142                   APSInt(NumberRange.upto(C)));
143   return C;
144 }
145
146 static Cursor maybeLexRegister(Cursor C, MIToken &Token) {
147   if (C.peek() != '%')
148     return None;
149   if (isdigit(C.peek(1)))
150     return lexVirtualRegister(C, Token);
151   auto Range = C;
152   C.advance(); // Skip '%'
153   while (isIdentifierChar(C.peek()))
154     C.advance();
155   Token = MIToken(MIToken::NamedRegister, Range.upto(C),
156                   /*StringOffset=*/1); // Drop the '%'
157   return C;
158 }
159
160 static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token) {
161   if (C.peek() != '@')
162     return None;
163   auto Range = C;
164   C.advance(); // Skip the '@'
165   // TODO: add support for quoted names.
166   if (!isdigit(C.peek())) {
167     while (isIdentifierChar(C.peek()))
168       C.advance();
169     Token = MIToken(MIToken::NamedGlobalValue, Range.upto(C),
170                     /*StringOffset=*/1); // Drop the '@'
171     return C;
172   }
173   auto NumberRange = C;
174   while (isdigit(C.peek()))
175     C.advance();
176   Token =
177       MIToken(MIToken::GlobalValue, Range.upto(C), APSInt(NumberRange.upto(C)));
178   return C;
179 }
180
181 static Cursor maybeLexIntegerLiteral(Cursor C, MIToken &Token) {
182   if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
183     return None;
184   auto Range = C;
185   C.advance();
186   while (isdigit(C.peek()))
187     C.advance();
188   StringRef StrVal = Range.upto(C);
189   Token = MIToken(MIToken::IntegerLiteral, StrVal, APSInt(StrVal));
190   return C;
191 }
192
193 static MIToken::TokenKind symbolToken(char C) {
194   switch (C) {
195   case ',':
196     return MIToken::comma;
197   case '=':
198     return MIToken::equal;
199   case ':':
200     return MIToken::colon;
201   default:
202     return MIToken::Error;
203   }
204 }
205
206 static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
207   auto Kind = symbolToken(C.peek());
208   if (Kind == MIToken::Error)
209     return None;
210   auto Range = C;
211   C.advance();
212   Token = MIToken(Kind, Range.upto(C));
213   return C;
214 }
215
216 StringRef llvm::lexMIToken(
217     StringRef Source, MIToken &Token,
218     function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
219   auto C = skipWhitespace(Cursor(Source));
220   if (C.isEOF()) {
221     Token = MIToken(MIToken::Eof, C.remaining());
222     return C.remaining();
223   }
224
225   if (Cursor R = maybeLexIdentifier(C, Token))
226     return R.remaining();
227   if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
228     return R.remaining();
229   if (Cursor R = maybeLexJumpTableIndex(C, Token))
230     return R.remaining();
231   if (Cursor R = maybeLexRegister(C, Token))
232     return R.remaining();
233   if (Cursor R = maybeLexGlobalValue(C, Token))
234     return R.remaining();
235   if (Cursor R = maybeLexIntegerLiteral(C, Token))
236     return R.remaining();
237   if (Cursor R = maybeLexSymbol(C, Token))
238     return R.remaining();
239
240   Token = MIToken(MIToken::Error, C.remaining());
241   ErrorCallback(C.location(),
242                 Twine("unexpected character '") + Twine(C.peek()) + "'");
243   return C.remaining();
244 }