xref: /llvm-project/llvm/lib/CodeGen/MIRParser/MILexer.cpp (revision 2b58458225fb0f9cce6dabce7e4451f86c8c73a5)
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.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "MILexer.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/Twine.h"
17 #include <cassert>
18 #include <cctype>
19 #include <string>
20 
21 using namespace llvm;
22 
23 namespace {
24 
25 using ErrorCallbackType =
26     function_ref<void(StringRef::iterator Loc, const Twine &)>;
27 
28 /// This class provides a way to iterate and get characters from the source
29 /// string.
30 class Cursor {
31   const char *Ptr = nullptr;
32   const char *End = nullptr;
33 
34 public:
35   Cursor(std::nullopt_t) {}
36 
37   explicit Cursor(StringRef Str) {
38     Ptr = Str.data();
39     End = Ptr + Str.size();
40   }
41 
42   bool isEOF() const { return Ptr == End; }
43 
44   char peek(int I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
45 
46   void advance(unsigned I = 1) { Ptr += I; }
47 
48   StringRef remaining() const { return StringRef(Ptr, End - Ptr); }
49 
50   StringRef upto(Cursor C) const {
51     assert(C.Ptr >= Ptr && C.Ptr <= End);
52     return StringRef(Ptr, C.Ptr - Ptr);
53   }
54 
55   StringRef::iterator location() const { return Ptr; }
56 
57   operator bool() const { return Ptr != nullptr; }
58 };
59 
60 } // end anonymous namespace
61 
62 MIToken &MIToken::reset(TokenKind Kind, StringRef Range) {
63   this->Kind = Kind;
64   this->Range = Range;
65   return *this;
66 }
67 
68 MIToken &MIToken::setStringValue(StringRef StrVal) {
69   StringValue = StrVal;
70   return *this;
71 }
72 
73 MIToken &MIToken::setOwnedStringValue(std::string StrVal) {
74   StringValueStorage = std::move(StrVal);
75   StringValue = StringValueStorage;
76   return *this;
77 }
78 
79 MIToken &MIToken::setIntegerValue(APSInt IntVal) {
80   this->IntVal = std::move(IntVal);
81   return *this;
82 }
83 
84 /// Skip the leading whitespace characters and return the updated cursor.
85 static Cursor skipWhitespace(Cursor C) {
86   while (isblank(C.peek()))
87     C.advance();
88   return C;
89 }
90 
91 static bool isNewlineChar(char C) { return C == '\n' || C == '\r'; }
92 
93 /// Skip a line comment and return the updated cursor.
94 static Cursor skipComment(Cursor C) {
95   if (C.peek() != ';')
96     return C;
97   while (!isNewlineChar(C.peek()) && !C.isEOF())
98     C.advance();
99   return C;
100 }
101 
102 /// Machine operands can have comments, enclosed between /* and */.
103 /// This eats up all tokens, including /* and */.
104 static Cursor skipMachineOperandComment(Cursor C) {
105   if (C.peek() != '/' || C.peek(1) != '*')
106     return C;
107 
108   while (C.peek() != '*' || C.peek(1) != '/')
109     C.advance();
110 
111   C.advance();
112   C.advance();
113   return C;
114 }
115 
116 /// Return true if the given character satisfies the following regular
117 /// expression: [-a-zA-Z$._0-9]
118 static bool isIdentifierChar(char C) {
119   return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.' ||
120          C == '$';
121 }
122 
123 /// Unescapes the given string value.
124 ///
125 /// Expects the string value to be quoted.
126 static std::string unescapeQuotedString(StringRef Value) {
127   assert(Value.front() == '"' && Value.back() == '"');
128   Cursor C = Cursor(Value.substr(1, Value.size() - 2));
129 
130   std::string Str;
131   Str.reserve(C.remaining().size());
132   while (!C.isEOF()) {
133     char Char = C.peek();
134     if (Char == '\\') {
135       if (C.peek(1) == '\\') {
136         // Two '\' become one
137         Str += '\\';
138         C.advance(2);
139         continue;
140       }
141       if (isxdigit(C.peek(1)) && isxdigit(C.peek(2))) {
142         Str += hexDigitValue(C.peek(1)) * 16 + hexDigitValue(C.peek(2));
143         C.advance(3);
144         continue;
145       }
146     }
147     Str += Char;
148     C.advance();
149   }
150   return Str;
151 }
152 
153 /// Lex a string constant using the following regular expression: \"[^\"]*\"
154 static Cursor lexStringConstant(Cursor C, ErrorCallbackType ErrorCallback) {
155   assert(C.peek() == '"');
156   for (C.advance(); C.peek() != '"'; C.advance()) {
157     if (C.isEOF() || isNewlineChar(C.peek())) {
158       ErrorCallback(
159           C.location(),
160           "end of machine instruction reached before the closing '\"'");
161       return std::nullopt;
162     }
163   }
164   C.advance();
165   return C;
166 }
167 
168 static Cursor lexName(Cursor C, MIToken &Token, MIToken::TokenKind Type,
169                       unsigned PrefixLength, ErrorCallbackType ErrorCallback) {
170   auto Range = C;
171   C.advance(PrefixLength);
172   if (C.peek() == '"') {
173     if (Cursor R = lexStringConstant(C, ErrorCallback)) {
174       StringRef String = Range.upto(R);
175       Token.reset(Type, String)
176           .setOwnedStringValue(
177               unescapeQuotedString(String.drop_front(PrefixLength)));
178       return R;
179     }
180     Token.reset(MIToken::Error, Range.remaining());
181     return Range;
182   }
183   while (isIdentifierChar(C.peek()))
184     C.advance();
185   Token.reset(Type, Range.upto(C))
186       .setStringValue(Range.upto(C).drop_front(PrefixLength));
187   return C;
188 }
189 
190 static MIToken::TokenKind getIdentifierKind(StringRef Identifier) {
191   return StringSwitch<MIToken::TokenKind>(Identifier)
192       .Case("_", MIToken::underscore)
193       .Case("implicit", MIToken::kw_implicit)
194       .Case("implicit-def", MIToken::kw_implicit_define)
195       .Case("def", MIToken::kw_def)
196       .Case("dead", MIToken::kw_dead)
197       .Case("killed", MIToken::kw_killed)
198       .Case("undef", MIToken::kw_undef)
199       .Case("internal", MIToken::kw_internal)
200       .Case("early-clobber", MIToken::kw_early_clobber)
201       .Case("debug-use", MIToken::kw_debug_use)
202       .Case("renamable", MIToken::kw_renamable)
203       .Case("tied-def", MIToken::kw_tied_def)
204       .Case("frame-setup", MIToken::kw_frame_setup)
205       .Case("frame-destroy", MIToken::kw_frame_destroy)
206       .Case("nnan", MIToken::kw_nnan)
207       .Case("ninf", MIToken::kw_ninf)
208       .Case("nsz", MIToken::kw_nsz)
209       .Case("arcp", MIToken::kw_arcp)
210       .Case("contract", MIToken::kw_contract)
211       .Case("afn", MIToken::kw_afn)
212       .Case("reassoc", MIToken::kw_reassoc)
213       .Case("nuw", MIToken::kw_nuw)
214       .Case("nsw", MIToken::kw_nsw)
215       .Case("nusw", MIToken::kw_nusw)
216       .Case("exact", MIToken::kw_exact)
217       .Case("nneg", MIToken::kw_nneg)
218       .Case("disjoint", MIToken::kw_disjoint)
219       .Case("samesign", MIToken::kw_samesign)
220       .Case("nofpexcept", MIToken::kw_nofpexcept)
221       .Case("unpredictable", MIToken::kw_unpredictable)
222       .Case("debug-location", MIToken::kw_debug_location)
223       .Case("debug-instr-number", MIToken::kw_debug_instr_number)
224       .Case("dbg-instr-ref", MIToken::kw_dbg_instr_ref)
225       .Case("same_value", MIToken::kw_cfi_same_value)
226       .Case("offset", MIToken::kw_cfi_offset)
227       .Case("rel_offset", MIToken::kw_cfi_rel_offset)
228       .Case("def_cfa_register", MIToken::kw_cfi_def_cfa_register)
229       .Case("def_cfa_offset", MIToken::kw_cfi_def_cfa_offset)
230       .Case("adjust_cfa_offset", MIToken::kw_cfi_adjust_cfa_offset)
231       .Case("escape", MIToken::kw_cfi_escape)
232       .Case("def_cfa", MIToken::kw_cfi_def_cfa)
233       .Case("llvm_def_aspace_cfa", MIToken::kw_cfi_llvm_def_aspace_cfa)
234       .Case("remember_state", MIToken::kw_cfi_remember_state)
235       .Case("restore", MIToken::kw_cfi_restore)
236       .Case("restore_state", MIToken::kw_cfi_restore_state)
237       .Case("undefined", MIToken::kw_cfi_undefined)
238       .Case("register", MIToken::kw_cfi_register)
239       .Case("window_save", MIToken::kw_cfi_window_save)
240       .Case("negate_ra_sign_state",
241             MIToken::kw_cfi_aarch64_negate_ra_sign_state)
242       .Case("negate_ra_sign_state_with_pc",
243             MIToken::kw_cfi_aarch64_negate_ra_sign_state_with_pc)
244       .Case("blockaddress", MIToken::kw_blockaddress)
245       .Case("intrinsic", MIToken::kw_intrinsic)
246       .Case("target-index", MIToken::kw_target_index)
247       .Case("half", MIToken::kw_half)
248       .Case("bfloat", MIToken::kw_bfloat)
249       .Case("float", MIToken::kw_float)
250       .Case("double", MIToken::kw_double)
251       .Case("x86_fp80", MIToken::kw_x86_fp80)
252       .Case("fp128", MIToken::kw_fp128)
253       .Case("ppc_fp128", MIToken::kw_ppc_fp128)
254       .Case("target-flags", MIToken::kw_target_flags)
255       .Case("volatile", MIToken::kw_volatile)
256       .Case("non-temporal", MIToken::kw_non_temporal)
257       .Case("dereferenceable", MIToken::kw_dereferenceable)
258       .Case("invariant", MIToken::kw_invariant)
259       .Case("align", MIToken::kw_align)
260       .Case("basealign", MIToken::kw_basealign)
261       .Case("addrspace", MIToken::kw_addrspace)
262       .Case("stack", MIToken::kw_stack)
263       .Case("got", MIToken::kw_got)
264       .Case("jump-table", MIToken::kw_jump_table)
265       .Case("constant-pool", MIToken::kw_constant_pool)
266       .Case("call-entry", MIToken::kw_call_entry)
267       .Case("custom", MIToken::kw_custom)
268       .Case("liveout", MIToken::kw_liveout)
269       .Case("landing-pad", MIToken::kw_landing_pad)
270       .Case("inlineasm-br-indirect-target",
271             MIToken::kw_inlineasm_br_indirect_target)
272       .Case("ehfunclet-entry", MIToken::kw_ehfunclet_entry)
273       .Case("liveins", MIToken::kw_liveins)
274       .Case("successors", MIToken::kw_successors)
275       .Case("floatpred", MIToken::kw_floatpred)
276       .Case("intpred", MIToken::kw_intpred)
277       .Case("shufflemask", MIToken::kw_shufflemask)
278       .Case("pre-instr-symbol", MIToken::kw_pre_instr_symbol)
279       .Case("post-instr-symbol", MIToken::kw_post_instr_symbol)
280       .Case("heap-alloc-marker", MIToken::kw_heap_alloc_marker)
281       .Case("pcsections", MIToken::kw_pcsections)
282       .Case("cfi-type", MIToken::kw_cfi_type)
283       .Case("bbsections", MIToken::kw_bbsections)
284       .Case("bb_id", MIToken::kw_bb_id)
285       .Case("unknown-size", MIToken::kw_unknown_size)
286       .Case("unknown-address", MIToken::kw_unknown_address)
287       .Case("distinct", MIToken::kw_distinct)
288       .Case("ir-block-address-taken", MIToken::kw_ir_block_address_taken)
289       .Case("machine-block-address-taken",
290             MIToken::kw_machine_block_address_taken)
291       .Case("call-frame-size", MIToken::kw_call_frame_size)
292       .Case("noconvergent", MIToken::kw_noconvergent)
293       .Default(MIToken::Identifier);
294 }
295 
296 static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
297   if (!isalpha(C.peek()) && C.peek() != '_')
298     return std::nullopt;
299   auto Range = C;
300   while (isIdentifierChar(C.peek()))
301     C.advance();
302   auto Identifier = Range.upto(C);
303   Token.reset(getIdentifierKind(Identifier), Identifier)
304       .setStringValue(Identifier);
305   return C;
306 }
307 
308 static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token,
309                                         ErrorCallbackType ErrorCallback) {
310   bool IsReference = C.remaining().starts_with("%bb.");
311   if (!IsReference && !C.remaining().starts_with("bb."))
312     return std::nullopt;
313   auto Range = C;
314   unsigned PrefixLength = IsReference ? 4 : 3;
315   C.advance(PrefixLength); // Skip '%bb.' or 'bb.'
316   if (!isdigit(C.peek())) {
317     Token.reset(MIToken::Error, C.remaining());
318     ErrorCallback(C.location(), "expected a number after '%bb.'");
319     return C;
320   }
321   auto NumberRange = C;
322   while (isdigit(C.peek()))
323     C.advance();
324   StringRef Number = NumberRange.upto(C);
325   unsigned StringOffset = PrefixLength + Number.size(); // Drop '%bb.<id>'
326   // TODO: The format bb.<id>.<irname> is supported only when it's not a
327   // reference. Once we deprecate the format where the irname shows up, we
328   // should only lex forward if it is a reference.
329   if (C.peek() == '.') {
330     C.advance(); // Skip '.'
331     ++StringOffset;
332     while (isIdentifierChar(C.peek()))
333       C.advance();
334   }
335   Token.reset(IsReference ? MIToken::MachineBasicBlock
336                           : MIToken::MachineBasicBlockLabel,
337               Range.upto(C))
338       .setIntegerValue(APSInt(Number))
339       .setStringValue(Range.upto(C).drop_front(StringOffset));
340   return C;
341 }
342 
343 static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
344                             MIToken::TokenKind Kind) {
345   if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
346     return std::nullopt;
347   auto Range = C;
348   C.advance(Rule.size());
349   auto NumberRange = C;
350   while (isdigit(C.peek()))
351     C.advance();
352   Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C)));
353   return C;
354 }
355 
356 static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
357                                    MIToken::TokenKind Kind) {
358   if (!C.remaining().starts_with(Rule) || !isdigit(C.peek(Rule.size())))
359     return std::nullopt;
360   auto Range = C;
361   C.advance(Rule.size());
362   auto NumberRange = C;
363   while (isdigit(C.peek()))
364     C.advance();
365   StringRef Number = NumberRange.upto(C);
366   unsigned StringOffset = Rule.size() + Number.size();
367   if (C.peek() == '.') {
368     C.advance();
369     ++StringOffset;
370     while (isIdentifierChar(C.peek()))
371       C.advance();
372   }
373   Token.reset(Kind, Range.upto(C))
374       .setIntegerValue(APSInt(Number))
375       .setStringValue(Range.upto(C).drop_front(StringOffset));
376   return C;
377 }
378 
379 static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
380   return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
381 }
382 
383 static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
384   return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
385 }
386 
387 static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
388   return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
389 }
390 
391 static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
392   return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
393 }
394 
395 static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token,
396                                        ErrorCallbackType ErrorCallback) {
397   const StringRef Rule = "%subreg.";
398   if (!C.remaining().starts_with(Rule))
399     return std::nullopt;
400   return lexName(C, Token, MIToken::SubRegisterIndex, Rule.size(),
401                  ErrorCallback);
402 }
403 
404 static Cursor maybeLexIRBlock(Cursor C, MIToken &Token,
405                               ErrorCallbackType ErrorCallback) {
406   const StringRef Rule = "%ir-block.";
407   if (!C.remaining().starts_with(Rule))
408     return std::nullopt;
409   if (isdigit(C.peek(Rule.size())))
410     return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
411   return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
412 }
413 
414 static Cursor maybeLexIRValue(Cursor C, MIToken &Token,
415                               ErrorCallbackType ErrorCallback) {
416   const StringRef Rule = "%ir.";
417   if (!C.remaining().starts_with(Rule))
418     return std::nullopt;
419   if (isdigit(C.peek(Rule.size())))
420     return maybeLexIndex(C, Token, Rule, MIToken::IRValue);
421   return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
422 }
423 
424 static Cursor maybeLexStringConstant(Cursor C, MIToken &Token,
425                                      ErrorCallbackType ErrorCallback) {
426   if (C.peek() != '"')
427     return std::nullopt;
428   return lexName(C, Token, MIToken::StringConstant, /*PrefixLength=*/0,
429                  ErrorCallback);
430 }
431 
432 static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
433   auto Range = C;
434   C.advance(); // Skip '%'
435   auto NumberRange = C;
436   while (isdigit(C.peek()))
437     C.advance();
438   Token.reset(MIToken::VirtualRegister, Range.upto(C))
439       .setIntegerValue(APSInt(NumberRange.upto(C)));
440   return C;
441 }
442 
443 /// Returns true for a character allowed in a register name.
444 static bool isRegisterChar(char C) {
445   return isIdentifierChar(C) && C != '.';
446 }
447 
448 static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token) {
449   Cursor Range = C;
450   C.advance(); // Skip '%'
451   while (isRegisterChar(C.peek()))
452     C.advance();
453   Token.reset(MIToken::NamedVirtualRegister, Range.upto(C))
454       .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%'
455   return C;
456 }
457 
458 static Cursor maybeLexRegister(Cursor C, MIToken &Token,
459                                ErrorCallbackType ErrorCallback) {
460   if (C.peek() != '%' && C.peek() != '$')
461     return std::nullopt;
462 
463   if (C.peek() == '%') {
464     if (isdigit(C.peek(1)))
465       return lexVirtualRegister(C, Token);
466 
467     if (isRegisterChar(C.peek(1)))
468       return lexNamedVirtualRegister(C, Token);
469 
470     return std::nullopt;
471   }
472 
473   assert(C.peek() == '$');
474   auto Range = C;
475   C.advance(); // Skip '$'
476   while (isRegisterChar(C.peek()))
477     C.advance();
478   Token.reset(MIToken::NamedRegister, Range.upto(C))
479       .setStringValue(Range.upto(C).drop_front(1)); // Drop the '$'
480   return C;
481 }
482 
483 static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token,
484                                   ErrorCallbackType ErrorCallback) {
485   if (C.peek() != '@')
486     return std::nullopt;
487   if (!isdigit(C.peek(1)))
488     return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
489                    ErrorCallback);
490   auto Range = C;
491   C.advance(1); // Skip the '@'
492   auto NumberRange = C;
493   while (isdigit(C.peek()))
494     C.advance();
495   Token.reset(MIToken::GlobalValue, Range.upto(C))
496       .setIntegerValue(APSInt(NumberRange.upto(C)));
497   return C;
498 }
499 
500 static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token,
501                                      ErrorCallbackType ErrorCallback) {
502   if (C.peek() != '&')
503     return std::nullopt;
504   return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
505                  ErrorCallback);
506 }
507 
508 static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token,
509                                ErrorCallbackType ErrorCallback) {
510   const StringRef Rule = "<mcsymbol ";
511   if (!C.remaining().starts_with(Rule))
512     return std::nullopt;
513   auto Start = C;
514   C.advance(Rule.size());
515 
516   // Try a simple unquoted name.
517   if (C.peek() != '"') {
518     while (isIdentifierChar(C.peek()))
519       C.advance();
520     StringRef String = Start.upto(C).drop_front(Rule.size());
521     if (C.peek() != '>') {
522       ErrorCallback(C.location(),
523                     "expected the '<mcsymbol ...' to be closed by a '>'");
524       Token.reset(MIToken::Error, Start.remaining());
525       return Start;
526     }
527     C.advance();
528 
529     Token.reset(MIToken::MCSymbol, Start.upto(C)).setStringValue(String);
530     return C;
531   }
532 
533   // Otherwise lex out a quoted name.
534   Cursor R = lexStringConstant(C, ErrorCallback);
535   if (!R) {
536     ErrorCallback(C.location(),
537                   "unable to parse quoted string from opening quote");
538     Token.reset(MIToken::Error, Start.remaining());
539     return Start;
540   }
541   StringRef String = Start.upto(R).drop_front(Rule.size());
542   if (R.peek() != '>') {
543     ErrorCallback(R.location(),
544                   "expected the '<mcsymbol ...' to be closed by a '>'");
545     Token.reset(MIToken::Error, Start.remaining());
546     return Start;
547   }
548   R.advance();
549 
550   Token.reset(MIToken::MCSymbol, Start.upto(R))
551       .setOwnedStringValue(unescapeQuotedString(String));
552   return R;
553 }
554 
555 static bool isValidHexFloatingPointPrefix(char C) {
556   return C == 'H' || C == 'K' || C == 'L' || C == 'M' || C == 'R';
557 }
558 
559 static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
560   C.advance();
561   // Skip over [0-9]*([eE][-+]?[0-9]+)?
562   while (isdigit(C.peek()))
563     C.advance();
564   if ((C.peek() == 'e' || C.peek() == 'E') &&
565       (isdigit(C.peek(1)) ||
566        ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
567     C.advance(2);
568     while (isdigit(C.peek()))
569       C.advance();
570   }
571   Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
572   return C;
573 }
574 
575 static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token) {
576   if (C.peek() != '0' || (C.peek(1) != 'x' && C.peek(1) != 'X'))
577     return std::nullopt;
578   Cursor Range = C;
579   C.advance(2);
580   unsigned PrefLen = 2;
581   if (isValidHexFloatingPointPrefix(C.peek())) {
582     C.advance();
583     PrefLen++;
584   }
585   while (isxdigit(C.peek()))
586     C.advance();
587   StringRef StrVal = Range.upto(C);
588   if (StrVal.size() <= PrefLen)
589     return std::nullopt;
590   if (PrefLen == 2)
591     Token.reset(MIToken::HexLiteral, Range.upto(C));
592   else // It must be 3, which means that there was a floating-point prefix.
593     Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
594   return C;
595 }
596 
597 static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
598   if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
599     return std::nullopt;
600   auto Range = C;
601   C.advance();
602   while (isdigit(C.peek()))
603     C.advance();
604   if (C.peek() == '.')
605     return lexFloatingPointLiteral(Range, C, Token);
606   StringRef StrVal = Range.upto(C);
607   Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal));
608   return C;
609 }
610 
611 static MIToken::TokenKind getMetadataKeywordKind(StringRef Identifier) {
612   return StringSwitch<MIToken::TokenKind>(Identifier)
613       .Case("!tbaa", MIToken::md_tbaa)
614       .Case("!alias.scope", MIToken::md_alias_scope)
615       .Case("!noalias", MIToken::md_noalias)
616       .Case("!range", MIToken::md_range)
617       .Case("!DIExpression", MIToken::md_diexpr)
618       .Case("!DILocation", MIToken::md_dilocation)
619       .Default(MIToken::Error);
620 }
621 
622 static Cursor maybeLexExclaim(Cursor C, MIToken &Token,
623                               ErrorCallbackType ErrorCallback) {
624   if (C.peek() != '!')
625     return std::nullopt;
626   auto Range = C;
627   C.advance(1);
628   if (isdigit(C.peek()) || !isIdentifierChar(C.peek())) {
629     Token.reset(MIToken::exclaim, Range.upto(C));
630     return C;
631   }
632   while (isIdentifierChar(C.peek()))
633     C.advance();
634   StringRef StrVal = Range.upto(C);
635   Token.reset(getMetadataKeywordKind(StrVal), StrVal);
636   if (Token.isError())
637     ErrorCallback(Token.location(),
638                   "use of unknown metadata keyword '" + StrVal + "'");
639   return C;
640 }
641 
642 static MIToken::TokenKind symbolToken(char C) {
643   switch (C) {
644   case ',':
645     return MIToken::comma;
646   case '.':
647     return MIToken::dot;
648   case '=':
649     return MIToken::equal;
650   case ':':
651     return MIToken::colon;
652   case '(':
653     return MIToken::lparen;
654   case ')':
655     return MIToken::rparen;
656   case '{':
657     return MIToken::lbrace;
658   case '}':
659     return MIToken::rbrace;
660   case '+':
661     return MIToken::plus;
662   case '-':
663     return MIToken::minus;
664   case '<':
665     return MIToken::less;
666   case '>':
667     return MIToken::greater;
668   default:
669     return MIToken::Error;
670   }
671 }
672 
673 static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
674   MIToken::TokenKind Kind;
675   unsigned Length = 1;
676   if (C.peek() == ':' && C.peek(1) == ':') {
677     Kind = MIToken::coloncolon;
678     Length = 2;
679   } else
680     Kind = symbolToken(C.peek());
681   if (Kind == MIToken::Error)
682     return std::nullopt;
683   auto Range = C;
684   C.advance(Length);
685   Token.reset(Kind, Range.upto(C));
686   return C;
687 }
688 
689 static Cursor maybeLexNewline(Cursor C, MIToken &Token) {
690   if (!isNewlineChar(C.peek()))
691     return std::nullopt;
692   auto Range = C;
693   C.advance();
694   Token.reset(MIToken::Newline, Range.upto(C));
695   return C;
696 }
697 
698 static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token,
699                                      ErrorCallbackType ErrorCallback) {
700   if (C.peek() != '`')
701     return std::nullopt;
702   auto Range = C;
703   C.advance();
704   auto StrRange = C;
705   while (C.peek() != '`') {
706     if (C.isEOF() || isNewlineChar(C.peek())) {
707       ErrorCallback(
708           C.location(),
709           "end of machine instruction reached before the closing '`'");
710       Token.reset(MIToken::Error, Range.remaining());
711       return C;
712     }
713     C.advance();
714   }
715   StringRef Value = StrRange.upto(C);
716   C.advance();
717   Token.reset(MIToken::QuotedIRValue, Range.upto(C)).setStringValue(Value);
718   return C;
719 }
720 
721 StringRef llvm::lexMIToken(StringRef Source, MIToken &Token,
722                            ErrorCallbackType ErrorCallback) {
723   auto C = skipComment(skipWhitespace(Cursor(Source)));
724   if (C.isEOF()) {
725     Token.reset(MIToken::Eof, C.remaining());
726     return C.remaining();
727   }
728 
729   C = skipWhitespace(skipMachineOperandComment(C));
730 
731   if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
732     return R.remaining();
733   if (Cursor R = maybeLexIdentifier(C, Token))
734     return R.remaining();
735   if (Cursor R = maybeLexJumpTableIndex(C, Token))
736     return R.remaining();
737   if (Cursor R = maybeLexStackObject(C, Token))
738     return R.remaining();
739   if (Cursor R = maybeLexFixedStackObject(C, Token))
740     return R.remaining();
741   if (Cursor R = maybeLexConstantPoolItem(C, Token))
742     return R.remaining();
743   if (Cursor R = maybeLexSubRegisterIndex(C, Token, ErrorCallback))
744     return R.remaining();
745   if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
746     return R.remaining();
747   if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
748     return R.remaining();
749   if (Cursor R = maybeLexRegister(C, Token, ErrorCallback))
750     return R.remaining();
751   if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
752     return R.remaining();
753   if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
754     return R.remaining();
755   if (Cursor R = maybeLexMCSymbol(C, Token, ErrorCallback))
756     return R.remaining();
757   if (Cursor R = maybeLexHexadecimalLiteral(C, Token))
758     return R.remaining();
759   if (Cursor R = maybeLexNumericalLiteral(C, Token))
760     return R.remaining();
761   if (Cursor R = maybeLexExclaim(C, Token, ErrorCallback))
762     return R.remaining();
763   if (Cursor R = maybeLexSymbol(C, Token))
764     return R.remaining();
765   if (Cursor R = maybeLexNewline(C, Token))
766     return R.remaining();
767   if (Cursor R = maybeLexEscapedIRValue(C, Token, ErrorCallback))
768     return R.remaining();
769   if (Cursor R = maybeLexStringConstant(C, Token, ErrorCallback))
770     return R.remaining();
771 
772   Token.reset(MIToken::Error, C.remaining());
773   ErrorCallback(C.location(),
774                 Twine("unexpected character '") + Twine(C.peek()) + "'");
775   return C.remaining();
776 }
777