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