1 //===- MILexer.cpp - Machine instructions lexer implementation ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the lexing of machine instructions.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/ADT/Twine.h"
24 /// This class provides a way to iterate and get characters from the source
31 Cursor(NoneType) : Ptr(nullptr), End(nullptr) {}
33 explicit Cursor(StringRef Str) {
35 End = Ptr + Str.size();
38 bool isEOF() const { return Ptr == End; }
40 char peek(int I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
42 void advance(unsigned I = 1) { Ptr += I; }
44 StringRef remaining() const { return StringRef(Ptr, End - Ptr); }
46 StringRef upto(Cursor C) const {
47 assert(C.Ptr >= Ptr && C.Ptr <= End);
48 return StringRef(Ptr, C.Ptr - Ptr);
51 StringRef::iterator location() const { return Ptr; }
53 operator bool() const { return Ptr != nullptr; }
56 } // end anonymous namespace
58 /// Skip the leading whitespace characters and return the updated cursor.
59 static Cursor skipWhitespace(Cursor C) {
60 while (isspace(C.peek()))
65 /// Return true if the given character satisfies the following regular
66 /// expression: [-a-zA-Z$._0-9]
67 static bool isIdentifierChar(char C) {
68 return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.' ||
72 void MIToken::unescapeQuotedStringValue(std::string &Str) const {
73 assert(isStringValueQuoted() && "String value isn't quoted");
74 StringRef Value = Range.drop_front(StringOffset);
75 assert(Value.front() == '"' && Value.back() == '"');
76 Cursor C = Cursor(Value.substr(1, Value.size() - 2));
79 Str.reserve(C.remaining().size());
83 if (C.peek(1) == '\\') {
89 if (isxdigit(C.peek(1)) && isxdigit(C.peek(2))) {
90 Str += hexDigitValue(C.peek(1)) * 16 + hexDigitValue(C.peek(2));
100 /// Lex a string constant using the following regular expression: \"[^\"]*\"
101 static Cursor lexStringConstant(
103 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
104 assert(C.peek() == '"');
105 for (C.advance(); C.peek() != '"'; C.advance()) {
109 "end of machine instruction reached before the closing '\"'");
117 static Cursor lexName(
118 Cursor C, MIToken &Token, MIToken::TokenKind Type,
119 MIToken::TokenKind QuotedType, unsigned PrefixLength,
120 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
122 C.advance(PrefixLength);
123 if (C.peek() == '"') {
124 if (Cursor R = lexStringConstant(C, ErrorCallback)) {
125 Token = MIToken(QuotedType, Range.upto(R), PrefixLength);
128 Token = MIToken(MIToken::Error, Range.remaining());
131 while (isIdentifierChar(C.peek()))
133 Token = MIToken(Type, Range.upto(C), PrefixLength);
137 static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
138 return StringSwitch<MIToken::TokenKind>(Identifier)
139 .Case("_", MIToken::underscore)
140 .Case("implicit", MIToken::kw_implicit)
141 .Case("implicit-def", MIToken::kw_implicit_define)
142 .Case("dead", MIToken::kw_dead)
143 .Case("killed", MIToken::kw_killed)
144 .Case("undef", MIToken::kw_undef)
145 .Case("frame-setup", MIToken::kw_frame_setup)
146 .Case("debug-location", MIToken::kw_debug_location)
147 .Case(".cfi_offset", MIToken::kw_cfi_offset)
148 .Case(".cfi_def_cfa_register", MIToken::kw_cfi_def_cfa_register)
149 .Case(".cfi_def_cfa_offset", MIToken::kw_cfi_def_cfa_offset)
150 .Default(MIToken::Identifier);
153 static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
154 if (!isalpha(C.peek()) && C.peek() != '_' && C.peek() != '.')
157 while (isIdentifierChar(C.peek()))
159 auto Identifier = Range.upto(C);
160 Token = MIToken(getIdentifierKind(Identifier), Identifier);
164 static Cursor maybeLexMachineBasicBlock(
165 Cursor C, MIToken &Token,
166 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
167 if (!C.remaining().startswith("%bb."))
170 C.advance(4); // Skip '%bb.'
171 if (!isdigit(C.peek())) {
172 Token = MIToken(MIToken::Error, C.remaining());
173 ErrorCallback(C.location(), "expected a number after '%bb.'");
176 auto NumberRange = C;
177 while (isdigit(C.peek()))
179 StringRef Number = NumberRange.upto(C);
180 unsigned StringOffset = 4 + Number.size(); // Drop '%bb.<id>'
181 if (C.peek() == '.') {
182 C.advance(); // Skip '.'
184 while (isIdentifierChar(C.peek()))
187 Token = MIToken(MIToken::MachineBasicBlock, Range.upto(C), APSInt(Number),
192 static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
193 MIToken::TokenKind Kind) {
194 if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
197 C.advance(Rule.size());
198 auto NumberRange = C;
199 while (isdigit(C.peek()))
201 Token = MIToken(Kind, Range.upto(C), APSInt(NumberRange.upto(C)));
205 static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
206 MIToken::TokenKind Kind) {
207 if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
210 C.advance(Rule.size());
211 auto NumberRange = C;
212 while (isdigit(C.peek()))
214 StringRef Number = NumberRange.upto(C);
215 unsigned StringOffset = Rule.size() + Number.size();
216 if (C.peek() == '.') {
219 while (isIdentifierChar(C.peek()))
222 Token = MIToken(Kind, Range.upto(C), APSInt(Number), StringOffset);
226 static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
227 return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
230 static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
231 return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
234 static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
235 return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
238 static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
239 return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
242 static Cursor maybeLexIRBlock(Cursor C, MIToken &Token) {
243 return maybeLexIndex(C, Token, "%ir-block.", MIToken::IRBlock);
246 static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
248 C.advance(); // Skip '%'
249 auto NumberRange = C;
250 while (isdigit(C.peek()))
252 Token = MIToken(MIToken::VirtualRegister, Range.upto(C),
253 APSInt(NumberRange.upto(C)));
257 static Cursor maybeLexRegister(Cursor C, MIToken &Token) {
260 if (isdigit(C.peek(1)))
261 return lexVirtualRegister(C, Token);
263 C.advance(); // Skip '%'
264 while (isIdentifierChar(C.peek()))
266 Token = MIToken(MIToken::NamedRegister, Range.upto(C),
267 /*StringOffset=*/1); // Drop the '%'
271 static Cursor maybeLexGlobalValue(
272 Cursor C, MIToken &Token,
273 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
276 if (!isdigit(C.peek(1)))
277 return lexName(C, Token, MIToken::NamedGlobalValue,
278 MIToken::QuotedNamedGlobalValue, /*PrefixLength=*/1,
281 C.advance(1); // Skip the '@'
282 auto NumberRange = C;
283 while (isdigit(C.peek()))
286 MIToken(MIToken::GlobalValue, Range.upto(C), APSInt(NumberRange.upto(C)));
290 static Cursor maybeLexExternalSymbol(
291 Cursor C, MIToken &Token,
292 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
295 return lexName(C, Token, MIToken::ExternalSymbol,
296 MIToken::QuotedExternalSymbol,
297 /*PrefixLength=*/1, ErrorCallback);
300 static Cursor maybeLexIntegerLiteral(Cursor C, MIToken &Token) {
301 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
305 while (isdigit(C.peek()))
307 StringRef StrVal = Range.upto(C);
308 Token = MIToken(MIToken::IntegerLiteral, StrVal, APSInt(StrVal));
312 static MIToken::TokenKind symbolToken(char C) {
315 return MIToken::comma;
317 return MIToken::equal;
319 return MIToken::colon;
321 return MIToken::exclaim;
323 return MIToken::Error;
327 static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
328 auto Kind = symbolToken(C.peek());
329 if (Kind == MIToken::Error)
333 Token = MIToken(Kind, Range.upto(C));
337 StringRef llvm::lexMIToken(
338 StringRef Source, MIToken &Token,
339 function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
340 auto C = skipWhitespace(Cursor(Source));
342 Token = MIToken(MIToken::Eof, C.remaining());
343 return C.remaining();
346 if (Cursor R = maybeLexIdentifier(C, Token))
347 return R.remaining();
348 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
349 return R.remaining();
350 if (Cursor R = maybeLexJumpTableIndex(C, Token))
351 return R.remaining();
352 if (Cursor R = maybeLexStackObject(C, Token))
353 return R.remaining();
354 if (Cursor R = maybeLexFixedStackObject(C, Token))
355 return R.remaining();
356 if (Cursor R = maybeLexConstantPoolItem(C, Token))
357 return R.remaining();
358 if (Cursor R = maybeLexIRBlock(C, Token))
359 return R.remaining();
360 if (Cursor R = maybeLexRegister(C, Token))
361 return R.remaining();
362 if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
363 return R.remaining();
364 if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
365 return R.remaining();
366 if (Cursor R = maybeLexIntegerLiteral(C, Token))
367 return R.remaining();
368 if (Cursor R = maybeLexSymbol(C, Token))
369 return R.remaining();
371 Token = MIToken(MIToken::Error, C.remaining());
372 ErrorCallback(C.location(),
373 Twine("unexpected character '") + Twine(C.peek()) + "'");
374 return C.remaining();