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