[llvm-readobj] - Simplify stack-sizes.test test case.
[llvm-complete.git] / lib / CodeGen / MIRParser / MILexer.cpp
blobad5c617623f2eb8edcbe6e1077bf5f832727f91f
1 //===- MILexer.cpp - Machine instructions lexer implementation ------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the lexing of machine instructions.
11 //===----------------------------------------------------------------------===//
13 #include "MILexer.h"
14 #include "llvm/ADT/APSInt.h"
15 #include "llvm/ADT/None.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include <algorithm>
22 #include <cassert>
23 #include <cctype>
24 #include <string>
26 using namespace llvm;
28 namespace {
30 using ErrorCallbackType =
31 function_ref<void(StringRef::iterator Loc, const Twine &)>;
33 /// This class provides a way to iterate and get characters from the source
34 /// string.
35 class Cursor {
36 const char *Ptr = nullptr;
37 const char *End = nullptr;
39 public:
40 Cursor(NoneType) {}
42 explicit Cursor(StringRef Str) {
43 Ptr = Str.data();
44 End = Ptr + Str.size();
47 bool isEOF() const { return Ptr == End; }
49 char peek(int I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
51 void advance(unsigned I = 1) { Ptr += I; }
53 StringRef remaining() const { return StringRef(Ptr, End - Ptr); }
55 StringRef upto(Cursor C) const {
56 assert(C.Ptr >= Ptr && C.Ptr <= End);
57 return StringRef(Ptr, C.Ptr - Ptr);
60 StringRef::iterator location() const { return Ptr; }
62 operator bool() const { return Ptr != nullptr; }
65 } // end anonymous namespace
67 MIToken &MIToken::reset(TokenKind Kind, StringRef Range) {
68 this->Kind = Kind;
69 this->Range = Range;
70 return *this;
73 MIToken &MIToken::setStringValue(StringRef StrVal) {
74 StringValue = StrVal;
75 return *this;
78 MIToken &MIToken::setOwnedStringValue(std::string StrVal) {
79 StringValueStorage = std::move(StrVal);
80 StringValue = StringValueStorage;
81 return *this;
84 MIToken &MIToken::setIntegerValue(APSInt IntVal) {
85 this->IntVal = std::move(IntVal);
86 return *this;
89 /// Skip the leading whitespace characters and return the updated cursor.
90 static Cursor skipWhitespace(Cursor C) {
91 while (isblank(C.peek()))
92 C.advance();
93 return C;
96 static bool isNewlineChar(char C) { return C == '\n' || C == '\r'; }
98 /// Skip a line comment and return the updated cursor.
99 static Cursor skipComment(Cursor C) {
100 if (C.peek() != ';')
101 return C;
102 while (!isNewlineChar(C.peek()) && !C.isEOF())
103 C.advance();
104 return C;
107 /// Return true if the given character satisfies the following regular
108 /// expression: [-a-zA-Z$._0-9]
109 static bool isIdentifierChar(char C) {
110 return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.' ||
111 C == '$';
114 /// Unescapes the given string value.
116 /// Expects the string value to be quoted.
117 static std::string unescapeQuotedString(StringRef Value) {
118 assert(Value.front() == '"' && Value.back() == '"');
119 Cursor C = Cursor(Value.substr(1, Value.size() - 2));
121 std::string Str;
122 Str.reserve(C.remaining().size());
123 while (!C.isEOF()) {
124 char Char = C.peek();
125 if (Char == '\\') {
126 if (C.peek(1) == '\\') {
127 // Two '\' become one
128 Str += '\\';
129 C.advance(2);
130 continue;
132 if (isxdigit(C.peek(1)) && isxdigit(C.peek(2))) {
133 Str += hexDigitValue(C.peek(1)) * 16 + hexDigitValue(C.peek(2));
134 C.advance(3);
135 continue;
138 Str += Char;
139 C.advance();
141 return Str;
144 /// Lex a string constant using the following regular expression: \"[^\"]*\"
145 static Cursor lexStringConstant(Cursor C, ErrorCallbackType ErrorCallback) {
146 assert(C.peek() == '"');
147 for (C.advance(); C.peek() != '"'; C.advance()) {
148 if (C.isEOF() || isNewlineChar(C.peek())) {
149 ErrorCallback(
150 C.location(),
151 "end of machine instruction reached before the closing '\"'");
152 return None;
155 C.advance();
156 return C;
159 static Cursor lexName(Cursor C, MIToken &Token, MIToken::TokenKind Type,
160 unsigned PrefixLength, ErrorCallbackType ErrorCallback) {
161 auto Range = C;
162 C.advance(PrefixLength);
163 if (C.peek() == '"') {
164 if (Cursor R = lexStringConstant(C, ErrorCallback)) {
165 StringRef String = Range.upto(R);
166 Token.reset(Type, String)
167 .setOwnedStringValue(
168 unescapeQuotedString(String.drop_front(PrefixLength)));
169 return R;
171 Token.reset(MIToken::Error, Range.remaining());
172 return Range;
174 while (isIdentifierChar(C.peek()))
175 C.advance();
176 Token.reset(Type, Range.upto(C))
177 .setStringValue(Range.upto(C).drop_front(PrefixLength));
178 return C;
181 static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
182 return StringSwitch<MIToken::TokenKind>(Identifier)
183 .Case("_", MIToken::underscore)
184 .Case("implicit", MIToken::kw_implicit)
185 .Case("implicit-def", MIToken::kw_implicit_define)
186 .Case("def", MIToken::kw_def)
187 .Case("dead", MIToken::kw_dead)
188 .Case("killed", MIToken::kw_killed)
189 .Case("undef", MIToken::kw_undef)
190 .Case("internal", MIToken::kw_internal)
191 .Case("early-clobber", MIToken::kw_early_clobber)
192 .Case("debug-use", MIToken::kw_debug_use)
193 .Case("renamable", MIToken::kw_renamable)
194 .Case("tied-def", MIToken::kw_tied_def)
195 .Case("frame-setup", MIToken::kw_frame_setup)
196 .Case("frame-destroy", MIToken::kw_frame_destroy)
197 .Case("nnan", MIToken::kw_nnan)
198 .Case("ninf", MIToken::kw_ninf)
199 .Case("nsz", MIToken::kw_nsz)
200 .Case("arcp", MIToken::kw_arcp)
201 .Case("contract", MIToken::kw_contract)
202 .Case("afn", MIToken::kw_afn)
203 .Case("reassoc", MIToken::kw_reassoc)
204 .Case("nuw" , MIToken::kw_nuw)
205 .Case("nsw" , MIToken::kw_nsw)
206 .Case("exact" , MIToken::kw_exact)
207 .Case("fpexcept", MIToken::kw_fpexcept)
208 .Case("debug-location", MIToken::kw_debug_location)
209 .Case("same_value", MIToken::kw_cfi_same_value)
210 .Case("offset", MIToken::kw_cfi_offset)
211 .Case("rel_offset", MIToken::kw_cfi_rel_offset)
212 .Case("def_cfa_register", MIToken::kw_cfi_def_cfa_register)
213 .Case("def_cfa_offset", MIToken::kw_cfi_def_cfa_offset)
214 .Case("adjust_cfa_offset", MIToken::kw_cfi_adjust_cfa_offset)
215 .Case("escape", MIToken::kw_cfi_escape)
216 .Case("def_cfa", MIToken::kw_cfi_def_cfa)
217 .Case("remember_state", MIToken::kw_cfi_remember_state)
218 .Case("restore", MIToken::kw_cfi_restore)
219 .Case("restore_state", MIToken::kw_cfi_restore_state)
220 .Case("undefined", MIToken::kw_cfi_undefined)
221 .Case("register", MIToken::kw_cfi_register)
222 .Case("window_save", MIToken::kw_cfi_window_save)
223 .Case("negate_ra_sign_state", MIToken::kw_cfi_aarch64_negate_ra_sign_state)
224 .Case("blockaddress", MIToken::kw_blockaddress)
225 .Case("intrinsic", MIToken::kw_intrinsic)
226 .Case("target-index", MIToken::kw_target_index)
227 .Case("half", MIToken::kw_half)
228 .Case("float", MIToken::kw_float)
229 .Case("double", MIToken::kw_double)
230 .Case("x86_fp80", MIToken::kw_x86_fp80)
231 .Case("fp128", MIToken::kw_fp128)
232 .Case("ppc_fp128", MIToken::kw_ppc_fp128)
233 .Case("target-flags", MIToken::kw_target_flags)
234 .Case("volatile", MIToken::kw_volatile)
235 .Case("non-temporal", MIToken::kw_non_temporal)
236 .Case("dereferenceable", MIToken::kw_dereferenceable)
237 .Case("invariant", MIToken::kw_invariant)
238 .Case("align", MIToken::kw_align)
239 .Case("addrspace", MIToken::kw_addrspace)
240 .Case("stack", MIToken::kw_stack)
241 .Case("got", MIToken::kw_got)
242 .Case("jump-table", MIToken::kw_jump_table)
243 .Case("constant-pool", MIToken::kw_constant_pool)
244 .Case("call-entry", MIToken::kw_call_entry)
245 .Case("liveout", MIToken::kw_liveout)
246 .Case("address-taken", MIToken::kw_address_taken)
247 .Case("landing-pad", MIToken::kw_landing_pad)
248 .Case("liveins", MIToken::kw_liveins)
249 .Case("successors", MIToken::kw_successors)
250 .Case("floatpred", MIToken::kw_floatpred)
251 .Case("intpred", MIToken::kw_intpred)
252 .Case("shufflemask", MIToken::kw_shufflemask)
253 .Case("pre-instr-symbol", MIToken::kw_pre_instr_symbol)
254 .Case("post-instr-symbol", MIToken::kw_post_instr_symbol)
255 .Case("unknown-size", MIToken::kw_unknown_size)
256 .Default(MIToken::Identifier);
259 static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
260 if (!isalpha(C.peek()) && C.peek() != '_')
261 return None;
262 auto Range = C;
263 while (isIdentifierChar(C.peek()))
264 C.advance();
265 auto Identifier = Range.upto(C);
266 Token.reset(getIdentifierKind(Identifier), Identifier)
267 .setStringValue(Identifier);
268 return C;
271 static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token,
272 ErrorCallbackType ErrorCallback) {
273 bool IsReference = C.remaining().startswith("%bb.");
274 if (!IsReference && !C.remaining().startswith("bb."))
275 return None;
276 auto Range = C;
277 unsigned PrefixLength = IsReference ? 4 : 3;
278 C.advance(PrefixLength); // Skip '%bb.' or 'bb.'
279 if (!isdigit(C.peek())) {
280 Token.reset(MIToken::Error, C.remaining());
281 ErrorCallback(C.location(), "expected a number after '%bb.'");
282 return C;
284 auto NumberRange = C;
285 while (isdigit(C.peek()))
286 C.advance();
287 StringRef Number = NumberRange.upto(C);
288 unsigned StringOffset = PrefixLength + Number.size(); // Drop '%bb.<id>'
289 // TODO: The format bb.<id>.<irname> is supported only when it's not a
290 // reference. Once we deprecate the format where the irname shows up, we
291 // should only lex forward if it is a reference.
292 if (C.peek() == '.') {
293 C.advance(); // Skip '.'
294 ++StringOffset;
295 while (isIdentifierChar(C.peek()))
296 C.advance();
298 Token.reset(IsReference ? MIToken::MachineBasicBlock
299 : MIToken::MachineBasicBlockLabel,
300 Range.upto(C))
301 .setIntegerValue(APSInt(Number))
302 .setStringValue(Range.upto(C).drop_front(StringOffset));
303 return C;
306 static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
307 MIToken::TokenKind Kind) {
308 if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
309 return None;
310 auto Range = C;
311 C.advance(Rule.size());
312 auto NumberRange = C;
313 while (isdigit(C.peek()))
314 C.advance();
315 Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C)));
316 return C;
319 static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
320 MIToken::TokenKind Kind) {
321 if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
322 return None;
323 auto Range = C;
324 C.advance(Rule.size());
325 auto NumberRange = C;
326 while (isdigit(C.peek()))
327 C.advance();
328 StringRef Number = NumberRange.upto(C);
329 unsigned StringOffset = Rule.size() + Number.size();
330 if (C.peek() == '.') {
331 C.advance();
332 ++StringOffset;
333 while (isIdentifierChar(C.peek()))
334 C.advance();
336 Token.reset(Kind, Range.upto(C))
337 .setIntegerValue(APSInt(Number))
338 .setStringValue(Range.upto(C).drop_front(StringOffset));
339 return C;
342 static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
343 return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
346 static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
347 return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
350 static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
351 return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
354 static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
355 return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
358 static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token,
359 ErrorCallbackType ErrorCallback) {
360 const StringRef Rule = "%subreg.";
361 if (!C.remaining().startswith(Rule))
362 return None;
363 return lexName(C, Token, MIToken::SubRegisterIndex, Rule.size(),
364 ErrorCallback);
367 static Cursor maybeLexIRBlock(Cursor C, MIToken &Token,
368 ErrorCallbackType ErrorCallback) {
369 const StringRef Rule = "%ir-block.";
370 if (!C.remaining().startswith(Rule))
371 return None;
372 if (isdigit(C.peek(Rule.size())))
373 return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
374 return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
377 static Cursor maybeLexIRValue(Cursor C, MIToken &Token,
378 ErrorCallbackType ErrorCallback) {
379 const StringRef Rule = "%ir.";
380 if (!C.remaining().startswith(Rule))
381 return None;
382 if (isdigit(C.peek(Rule.size())))
383 return maybeLexIndex(C, Token, Rule, MIToken::IRValue);
384 return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
387 static Cursor maybeLexStringConstant(Cursor C, MIToken &Token,
388 ErrorCallbackType ErrorCallback) {
389 if (C.peek() != '"')
390 return None;
391 return lexName(C, Token, MIToken::StringConstant, /*PrefixLength=*/0,
392 ErrorCallback);
395 static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
396 auto Range = C;
397 C.advance(); // Skip '%'
398 auto NumberRange = C;
399 while (isdigit(C.peek()))
400 C.advance();
401 Token.reset(MIToken::VirtualRegister, Range.upto(C))
402 .setIntegerValue(APSInt(NumberRange.upto(C)));
403 return C;
406 /// Returns true for a character allowed in a register name.
407 static bool isRegisterChar(char C) {
408 return isIdentifierChar(C) && C != '.';
411 static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token) {
412 Cursor Range = C;
413 C.advance(); // Skip '%'
414 while (isRegisterChar(C.peek()))
415 C.advance();
416 Token.reset(MIToken::NamedVirtualRegister, Range.upto(C))
417 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%'
418 return C;
421 static Cursor maybeLexRegister(Cursor C, MIToken &Token,
422 ErrorCallbackType ErrorCallback) {
423 if (C.peek() != '%' && C.peek() != '$')
424 return None;
426 if (C.peek() == '%') {
427 if (isdigit(C.peek(1)))
428 return lexVirtualRegister(C, Token);
430 if (isRegisterChar(C.peek(1)))
431 return lexNamedVirtualRegister(C, Token);
433 return None;
436 assert(C.peek() == '$');
437 auto Range = C;
438 C.advance(); // Skip '$'
439 while (isRegisterChar(C.peek()))
440 C.advance();
441 Token.reset(MIToken::NamedRegister, Range.upto(C))
442 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '$'
443 return C;
446 static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token,
447 ErrorCallbackType ErrorCallback) {
448 if (C.peek() != '@')
449 return None;
450 if (!isdigit(C.peek(1)))
451 return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
452 ErrorCallback);
453 auto Range = C;
454 C.advance(1); // Skip the '@'
455 auto NumberRange = C;
456 while (isdigit(C.peek()))
457 C.advance();
458 Token.reset(MIToken::GlobalValue, Range.upto(C))
459 .setIntegerValue(APSInt(NumberRange.upto(C)));
460 return C;
463 static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token,
464 ErrorCallbackType ErrorCallback) {
465 if (C.peek() != '&')
466 return None;
467 return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
468 ErrorCallback);
471 static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token,
472 ErrorCallbackType ErrorCallback) {
473 const StringRef Rule = "<mcsymbol ";
474 if (!C.remaining().startswith(Rule))
475 return None;
476 auto Start = C;
477 C.advance(Rule.size());
479 // Try a simple unquoted name.
480 if (C.peek() != '"') {
481 while (isIdentifierChar(C.peek()))
482 C.advance();
483 StringRef String = Start.upto(C).drop_front(Rule.size());
484 if (C.peek() != '>') {
485 ErrorCallback(C.location(),
486 "expected the '<mcsymbol ...' to be closed by a '>'");
487 Token.reset(MIToken::Error, Start.remaining());
488 return Start;
490 C.advance();
492 Token.reset(MIToken::MCSymbol, Start.upto(C)).setStringValue(String);
493 return C;
496 // Otherwise lex out a quoted name.
497 Cursor R = lexStringConstant(C, ErrorCallback);
498 if (!R) {
499 ErrorCallback(C.location(),
500 "unable to parse quoted string from opening quote");
501 Token.reset(MIToken::Error, Start.remaining());
502 return Start;
504 StringRef String = Start.upto(R).drop_front(Rule.size());
505 if (R.peek() != '>') {
506 ErrorCallback(R.location(),
507 "expected the '<mcsymbol ...' to be closed by a '>'");
508 Token.reset(MIToken::Error, Start.remaining());
509 return Start;
511 R.advance();
513 Token.reset(MIToken::MCSymbol, Start.upto(R))
514 .setOwnedStringValue(unescapeQuotedString(String));
515 return R;
518 static bool isValidHexFloatingPointPrefix(char C) {
519 return C == 'H' || C == 'K' || C == 'L' || C == 'M';
522 static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
523 C.advance();
524 // Skip over [0-9]*([eE][-+]?[0-9]+)?
525 while (isdigit(C.peek()))
526 C.advance();
527 if ((C.peek() == 'e' || C.peek() == 'E') &&
528 (isdigit(C.peek(1)) ||
529 ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
530 C.advance(2);
531 while (isdigit(C.peek()))
532 C.advance();
534 Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
535 return C;
538 static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token) {
539 if (C.peek() != '0' || (C.peek(1) != 'x' && C.peek(1) != 'X'))
540 return None;
541 Cursor Range = C;
542 C.advance(2);
543 unsigned PrefLen = 2;
544 if (isValidHexFloatingPointPrefix(C.peek())) {
545 C.advance();
546 PrefLen++;
548 while (isxdigit(C.peek()))
549 C.advance();
550 StringRef StrVal = Range.upto(C);
551 if (StrVal.size() <= PrefLen)
552 return None;
553 if (PrefLen == 2)
554 Token.reset(MIToken::HexLiteral, Range.upto(C));
555 else // It must be 3, which means that there was a floating-point prefix.
556 Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
557 return C;
560 static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
561 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
562 return None;
563 auto Range = C;
564 C.advance();
565 while (isdigit(C.peek()))
566 C.advance();
567 if (C.peek() == '.')
568 return lexFloatingPointLiteral(Range, C, Token);
569 StringRef StrVal = Range.upto(C);
570 Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal));
571 return C;
574 static MIToken::TokenKind getMetadataKeywordKind(StringRef Identifier) {
575 return StringSwitch<MIToken::TokenKind>(Identifier)
576 .Case("!tbaa", MIToken::md_tbaa)
577 .Case("!alias.scope", MIToken::md_alias_scope)
578 .Case("!noalias", MIToken::md_noalias)
579 .Case("!range", MIToken::md_range)
580 .Case("!DIExpression", MIToken::md_diexpr)
581 .Case("!DILocation", MIToken::md_dilocation)
582 .Default(MIToken::Error);
585 static Cursor maybeLexExlaim(Cursor C, MIToken &Token,
586 ErrorCallbackType ErrorCallback) {
587 if (C.peek() != '!')
588 return None;
589 auto Range = C;
590 C.advance(1);
591 if (isdigit(C.peek()) || !isIdentifierChar(C.peek())) {
592 Token.reset(MIToken::exclaim, Range.upto(C));
593 return C;
595 while (isIdentifierChar(C.peek()))
596 C.advance();
597 StringRef StrVal = Range.upto(C);
598 Token.reset(getMetadataKeywordKind(StrVal), StrVal);
599 if (Token.isError())
600 ErrorCallback(Token.location(),
601 "use of unknown metadata keyword '" + StrVal + "'");
602 return C;
605 static MIToken::TokenKind symbolToken(char C) {
606 switch (C) {
607 case ',':
608 return MIToken::comma;
609 case '.':
610 return MIToken::dot;
611 case '=':
612 return MIToken::equal;
613 case ':':
614 return MIToken::colon;
615 case '(':
616 return MIToken::lparen;
617 case ')':
618 return MIToken::rparen;
619 case '{':
620 return MIToken::lbrace;
621 case '}':
622 return MIToken::rbrace;
623 case '+':
624 return MIToken::plus;
625 case '-':
626 return MIToken::minus;
627 case '<':
628 return MIToken::less;
629 case '>':
630 return MIToken::greater;
631 default:
632 return MIToken::Error;
636 static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
637 MIToken::TokenKind Kind;
638 unsigned Length = 1;
639 if (C.peek() == ':' && C.peek(1) == ':') {
640 Kind = MIToken::coloncolon;
641 Length = 2;
642 } else
643 Kind = symbolToken(C.peek());
644 if (Kind == MIToken::Error)
645 return None;
646 auto Range = C;
647 C.advance(Length);
648 Token.reset(Kind, Range.upto(C));
649 return C;
652 static Cursor maybeLexNewline(Cursor C, MIToken &Token) {
653 if (!isNewlineChar(C.peek()))
654 return None;
655 auto Range = C;
656 C.advance();
657 Token.reset(MIToken::Newline, Range.upto(C));
658 return C;
661 static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token,
662 ErrorCallbackType ErrorCallback) {
663 if (C.peek() != '`')
664 return None;
665 auto Range = C;
666 C.advance();
667 auto StrRange = C;
668 while (C.peek() != '`') {
669 if (C.isEOF() || isNewlineChar(C.peek())) {
670 ErrorCallback(
671 C.location(),
672 "end of machine instruction reached before the closing '`'");
673 Token.reset(MIToken::Error, Range.remaining());
674 return C;
676 C.advance();
678 StringRef Value = StrRange.upto(C);
679 C.advance();
680 Token.reset(MIToken::QuotedIRValue, Range.upto(C)).setStringValue(Value);
681 return C;
684 StringRef llvm::lexMIToken(StringRef Source, MIToken &Token,
685 ErrorCallbackType ErrorCallback) {
686 auto C = skipComment(skipWhitespace(Cursor(Source)));
687 if (C.isEOF()) {
688 Token.reset(MIToken::Eof, C.remaining());
689 return C.remaining();
692 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
693 return R.remaining();
694 if (Cursor R = maybeLexIdentifier(C, Token))
695 return R.remaining();
696 if (Cursor R = maybeLexJumpTableIndex(C, Token))
697 return R.remaining();
698 if (Cursor R = maybeLexStackObject(C, Token))
699 return R.remaining();
700 if (Cursor R = maybeLexFixedStackObject(C, Token))
701 return R.remaining();
702 if (Cursor R = maybeLexConstantPoolItem(C, Token))
703 return R.remaining();
704 if (Cursor R = maybeLexSubRegisterIndex(C, Token, ErrorCallback))
705 return R.remaining();
706 if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
707 return R.remaining();
708 if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
709 return R.remaining();
710 if (Cursor R = maybeLexRegister(C, Token, ErrorCallback))
711 return R.remaining();
712 if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
713 return R.remaining();
714 if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
715 return R.remaining();
716 if (Cursor R = maybeLexMCSymbol(C, Token, ErrorCallback))
717 return R.remaining();
718 if (Cursor R = maybeLexHexadecimalLiteral(C, Token))
719 return R.remaining();
720 if (Cursor R = maybeLexNumericalLiteral(C, Token))
721 return R.remaining();
722 if (Cursor R = maybeLexExlaim(C, Token, ErrorCallback))
723 return R.remaining();
724 if (Cursor R = maybeLexSymbol(C, Token))
725 return R.remaining();
726 if (Cursor R = maybeLexNewline(C, Token))
727 return R.remaining();
728 if (Cursor R = maybeLexEscapedIRValue(C, Token, ErrorCallback))
729 return R.remaining();
730 if (Cursor R = maybeLexStringConstant(C, Token, ErrorCallback))
731 return R.remaining();
733 Token.reset(MIToken::Error, C.remaining());
734 ErrorCallback(C.location(),
735 Twine("unexpected character '") + Twine(C.peek()) + "'");
736 return C.remaining();