LLVM  10.0.0
MILexer.cpp
Go to the documentation of this file.
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/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>
25 
26 using namespace llvm;
27 
28 namespace {
29 
30 using ErrorCallbackType =
32 
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;
38 
39 public:
40  Cursor(NoneType) {}
41 
42  explicit Cursor(StringRef Str) {
43  Ptr = Str.data();
44  End = Ptr + Str.size();
45  }
46 
47  bool isEOF() const { return Ptr == End; }
48 
49  char peek(int I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
50 
51  void advance(unsigned I = 1) { Ptr += I; }
52 
53  StringRef remaining() const { return StringRef(Ptr, End - Ptr); }
54 
55  StringRef upto(Cursor C) const {
56  assert(C.Ptr >= Ptr && C.Ptr <= End);
57  return StringRef(Ptr, C.Ptr - Ptr);
58  }
59 
60  StringRef::iterator location() const { return Ptr; }
61 
62  operator bool() const { return Ptr != nullptr; }
63 };
64 
65 } // end anonymous namespace
66 
68  this->Kind = Kind;
69  this->Range = Range;
70  return *this;
71 }
72 
74  StringValue = StrVal;
75  return *this;
76 }
77 
79  StringValueStorage = std::move(StrVal);
80  StringValue = StringValueStorage;
81  return *this;
82 }
83 
85  this->IntVal = std::move(IntVal);
86  return *this;
87 }
88 
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;
94 }
95 
96 static bool isNewlineChar(char C) { return C == '\n' || C == '\r'; }
97 
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;
105 }
106 
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 == '$';
112 }
113 
114 /// Unescapes the given string value.
115 ///
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));
120 
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;
131  }
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;
136  }
137  }
138  Str += Char;
139  C.advance();
140  }
141  return Str;
142 }
143 
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;
153  }
154  }
155  C.advance();
156  return C;
157 }
158 
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)
168  unescapeQuotedString(String.drop_front(PrefixLength)));
169  return R;
170  }
171  Token.reset(MIToken::Error, Range.remaining());
172  return Range;
173  }
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;
179 }
180 
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("nofpexcept", MIToken::kw_nofpexcept)
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("custom", MIToken::kw_custom)
246  .Case("liveout", MIToken::kw_liveout)
247  .Case("address-taken", MIToken::kw_address_taken)
248  .Case("landing-pad", MIToken::kw_landing_pad)
249  .Case("liveins", MIToken::kw_liveins)
250  .Case("successors", MIToken::kw_successors)
251  .Case("floatpred", MIToken::kw_floatpred)
252  .Case("intpred", MIToken::kw_intpred)
253  .Case("shufflemask", MIToken::kw_shufflemask)
254  .Case("pre-instr-symbol", MIToken::kw_pre_instr_symbol)
255  .Case("post-instr-symbol", MIToken::kw_post_instr_symbol)
256  .Case("heap-alloc-marker", MIToken::kw_heap_alloc_marker)
257  .Case("unknown-size", MIToken::kw_unknown_size)
259 }
260 
261 static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) {
262  if (!isalpha(C.peek()) && C.peek() != '_')
263  return None;
264  auto Range = C;
265  while (isIdentifierChar(C.peek()))
266  C.advance();
267  auto Identifier = Range.upto(C);
268  Token.reset(getIdentifierKind(Identifier), Identifier)
269  .setStringValue(Identifier);
270  return C;
271 }
272 
273 static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token,
274  ErrorCallbackType ErrorCallback) {
275  bool IsReference = C.remaining().startswith("%bb.");
276  if (!IsReference && !C.remaining().startswith("bb."))
277  return None;
278  auto Range = C;
279  unsigned PrefixLength = IsReference ? 4 : 3;
280  C.advance(PrefixLength); // Skip '%bb.' or 'bb.'
281  if (!isdigit(C.peek())) {
282  Token.reset(MIToken::Error, C.remaining());
283  ErrorCallback(C.location(), "expected a number after '%bb.'");
284  return C;
285  }
286  auto NumberRange = C;
287  while (isdigit(C.peek()))
288  C.advance();
289  StringRef Number = NumberRange.upto(C);
290  unsigned StringOffset = PrefixLength + Number.size(); // Drop '%bb.<id>'
291  // TODO: The format bb.<id>.<irname> is supported only when it's not a
292  // reference. Once we deprecate the format where the irname shows up, we
293  // should only lex forward if it is a reference.
294  if (C.peek() == '.') {
295  C.advance(); // Skip '.'
296  ++StringOffset;
297  while (isIdentifierChar(C.peek()))
298  C.advance();
299  }
300  Token.reset(IsReference ? MIToken::MachineBasicBlock
302  Range.upto(C))
303  .setIntegerValue(APSInt(Number))
304  .setStringValue(Range.upto(C).drop_front(StringOffset));
305  return C;
306 }
307 
308 static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule,
310  if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size())))
311  return None;
312  auto Range = C;
313  C.advance(Rule.size());
314  auto NumberRange = C;
315  while (isdigit(C.peek()))
316  C.advance();
317  Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C)));
318  return C;
319 }
320 
321 static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule,
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  StringRef Number = NumberRange.upto(C);
331  unsigned StringOffset = Rule.size() + Number.size();
332  if (C.peek() == '.') {
333  C.advance();
334  ++StringOffset;
335  while (isIdentifierChar(C.peek()))
336  C.advance();
337  }
338  Token.reset(Kind, Range.upto(C))
339  .setIntegerValue(APSInt(Number))
340  .setStringValue(Range.upto(C).drop_front(StringOffset));
341  return C;
342 }
343 
344 static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) {
345  return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex);
346 }
347 
348 static Cursor maybeLexStackObject(Cursor C, MIToken &Token) {
349  return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject);
350 }
351 
352 static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) {
353  return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject);
354 }
355 
356 static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) {
357  return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem);
358 }
359 
360 static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token,
361  ErrorCallbackType ErrorCallback) {
362  const StringRef Rule = "%subreg.";
363  if (!C.remaining().startswith(Rule))
364  return None;
365  return lexName(C, Token, MIToken::SubRegisterIndex, Rule.size(),
366  ErrorCallback);
367 }
368 
369 static Cursor maybeLexIRBlock(Cursor C, MIToken &Token,
370  ErrorCallbackType ErrorCallback) {
371  const StringRef Rule = "%ir-block.";
372  if (!C.remaining().startswith(Rule))
373  return None;
374  if (isdigit(C.peek(Rule.size())))
375  return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
376  return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
377 }
378 
379 static Cursor maybeLexIRValue(Cursor C, MIToken &Token,
380  ErrorCallbackType ErrorCallback) {
381  const StringRef Rule = "%ir.";
382  if (!C.remaining().startswith(Rule))
383  return None;
384  if (isdigit(C.peek(Rule.size())))
385  return maybeLexIndex(C, Token, Rule, MIToken::IRValue);
386  return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
387 }
388 
389 static Cursor maybeLexStringConstant(Cursor C, MIToken &Token,
390  ErrorCallbackType ErrorCallback) {
391  if (C.peek() != '"')
392  return None;
393  return lexName(C, Token, MIToken::StringConstant, /*PrefixLength=*/0,
394  ErrorCallback);
395 }
396 
397 static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
398  auto Range = C;
399  C.advance(); // Skip '%'
400  auto NumberRange = C;
401  while (isdigit(C.peek()))
402  C.advance();
403  Token.reset(MIToken::VirtualRegister, Range.upto(C))
404  .setIntegerValue(APSInt(NumberRange.upto(C)));
405  return C;
406 }
407 
408 /// Returns true for a character allowed in a register name.
409 static bool isRegisterChar(char C) {
410  return isIdentifierChar(C) && C != '.';
411 }
412 
413 static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token) {
414  Cursor Range = C;
415  C.advance(); // Skip '%'
416  while (isRegisterChar(C.peek()))
417  C.advance();
418  Token.reset(MIToken::NamedVirtualRegister, Range.upto(C))
419  .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%'
420  return C;
421 }
422 
423 static Cursor maybeLexRegister(Cursor C, MIToken &Token,
424  ErrorCallbackType ErrorCallback) {
425  if (C.peek() != '%' && C.peek() != '$')
426  return None;
427 
428  if (C.peek() == '%') {
429  if (isdigit(C.peek(1)))
430  return lexVirtualRegister(C, Token);
431 
432  if (isRegisterChar(C.peek(1)))
433  return lexNamedVirtualRegister(C, Token);
434 
435  return None;
436  }
437 
438  assert(C.peek() == '$');
439  auto Range = C;
440  C.advance(); // Skip '$'
441  while (isRegisterChar(C.peek()))
442  C.advance();
443  Token.reset(MIToken::NamedRegister, Range.upto(C))
444  .setStringValue(Range.upto(C).drop_front(1)); // Drop the '$'
445  return C;
446 }
447 
448 static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token,
449  ErrorCallbackType ErrorCallback) {
450  if (C.peek() != '@')
451  return None;
452  if (!isdigit(C.peek(1)))
453  return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
454  ErrorCallback);
455  auto Range = C;
456  C.advance(1); // Skip the '@'
457  auto NumberRange = C;
458  while (isdigit(C.peek()))
459  C.advance();
460  Token.reset(MIToken::GlobalValue, Range.upto(C))
461  .setIntegerValue(APSInt(NumberRange.upto(C)));
462  return C;
463 }
464 
465 static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token,
466  ErrorCallbackType ErrorCallback) {
467  if (C.peek() != '&')
468  return None;
469  return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
470  ErrorCallback);
471 }
472 
473 static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token,
474  ErrorCallbackType ErrorCallback) {
475  const StringRef Rule = "<mcsymbol ";
476  if (!C.remaining().startswith(Rule))
477  return None;
478  auto Start = C;
479  C.advance(Rule.size());
480 
481  // Try a simple unquoted name.
482  if (C.peek() != '"') {
483  while (isIdentifierChar(C.peek()))
484  C.advance();
485  StringRef String = Start.upto(C).drop_front(Rule.size());
486  if (C.peek() != '>') {
487  ErrorCallback(C.location(),
488  "expected the '<mcsymbol ...' to be closed by a '>'");
489  Token.reset(MIToken::Error, Start.remaining());
490  return Start;
491  }
492  C.advance();
493 
494  Token.reset(MIToken::MCSymbol, Start.upto(C)).setStringValue(String);
495  return C;
496  }
497 
498  // Otherwise lex out a quoted name.
499  Cursor R = lexStringConstant(C, ErrorCallback);
500  if (!R) {
501  ErrorCallback(C.location(),
502  "unable to parse quoted string from opening quote");
503  Token.reset(MIToken::Error, Start.remaining());
504  return Start;
505  }
506  StringRef String = Start.upto(R).drop_front(Rule.size());
507  if (R.peek() != '>') {
508  ErrorCallback(R.location(),
509  "expected the '<mcsymbol ...' to be closed by a '>'");
510  Token.reset(MIToken::Error, Start.remaining());
511  return Start;
512  }
513  R.advance();
514 
515  Token.reset(MIToken::MCSymbol, Start.upto(R))
516  .setOwnedStringValue(unescapeQuotedString(String));
517  return R;
518 }
519 
520 static bool isValidHexFloatingPointPrefix(char C) {
521  return C == 'H' || C == 'K' || C == 'L' || C == 'M';
522 }
523 
524 static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) {
525  C.advance();
526  // Skip over [0-9]*([eE][-+]?[0-9]+)?
527  while (isdigit(C.peek()))
528  C.advance();
529  if ((C.peek() == 'e' || C.peek() == 'E') &&
530  (isdigit(C.peek(1)) ||
531  ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) {
532  C.advance(2);
533  while (isdigit(C.peek()))
534  C.advance();
535  }
536  Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
537  return C;
538 }
539 
540 static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token) {
541  if (C.peek() != '0' || (C.peek(1) != 'x' && C.peek(1) != 'X'))
542  return None;
543  Cursor Range = C;
544  C.advance(2);
545  unsigned PrefLen = 2;
546  if (isValidHexFloatingPointPrefix(C.peek())) {
547  C.advance();
548  PrefLen++;
549  }
550  while (isxdigit(C.peek()))
551  C.advance();
552  StringRef StrVal = Range.upto(C);
553  if (StrVal.size() <= PrefLen)
554  return None;
555  if (PrefLen == 2)
556  Token.reset(MIToken::HexLiteral, Range.upto(C));
557  else // It must be 3, which means that there was a floating-point prefix.
558  Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
559  return C;
560 }
561 
562 static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) {
563  if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1))))
564  return None;
565  auto Range = C;
566  C.advance();
567  while (isdigit(C.peek()))
568  C.advance();
569  if (C.peek() == '.')
570  return lexFloatingPointLiteral(Range, C, Token);
571  StringRef StrVal = Range.upto(C);
572  Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal));
573  return C;
574 }
575 
577  return StringSwitch<MIToken::TokenKind>(Identifier)
578  .Case("!tbaa", MIToken::md_tbaa)
579  .Case("!alias.scope", MIToken::md_alias_scope)
580  .Case("!noalias", MIToken::md_noalias)
581  .Case("!range", MIToken::md_range)
582  .Case("!DIExpression", MIToken::md_diexpr)
583  .Case("!DILocation", MIToken::md_dilocation)
585 }
586 
587 static Cursor maybeLexExclaim(Cursor C, MIToken &Token,
588  ErrorCallbackType ErrorCallback) {
589  if (C.peek() != '!')
590  return None;
591  auto Range = C;
592  C.advance(1);
593  if (isdigit(C.peek()) || !isIdentifierChar(C.peek())) {
594  Token.reset(MIToken::exclaim, Range.upto(C));
595  return C;
596  }
597  while (isIdentifierChar(C.peek()))
598  C.advance();
599  StringRef StrVal = Range.upto(C);
600  Token.reset(getMetadataKeywordKind(StrVal), StrVal);
601  if (Token.isError())
602  ErrorCallback(Token.location(),
603  "use of unknown metadata keyword '" + StrVal + "'");
604  return C;
605 }
606 
608  switch (C) {
609  case ',':
610  return MIToken::comma;
611  case '.':
612  return MIToken::dot;
613  case '=':
614  return MIToken::equal;
615  case ':':
616  return MIToken::colon;
617  case '(':
618  return MIToken::lparen;
619  case ')':
620  return MIToken::rparen;
621  case '{':
622  return MIToken::lbrace;
623  case '}':
624  return MIToken::rbrace;
625  case '+':
626  return MIToken::plus;
627  case '-':
628  return MIToken::minus;
629  case '<':
630  return MIToken::less;
631  case '>':
632  return MIToken::greater;
633  default:
634  return MIToken::Error;
635  }
636 }
637 
638 static Cursor maybeLexSymbol(Cursor C, MIToken &Token) {
640  unsigned Length = 1;
641  if (C.peek() == ':' && C.peek(1) == ':') {
642  Kind = MIToken::coloncolon;
643  Length = 2;
644  } else
645  Kind = symbolToken(C.peek());
646  if (Kind == MIToken::Error)
647  return None;
648  auto Range = C;
649  C.advance(Length);
650  Token.reset(Kind, Range.upto(C));
651  return C;
652 }
653 
654 static Cursor maybeLexNewline(Cursor C, MIToken &Token) {
655  if (!isNewlineChar(C.peek()))
656  return None;
657  auto Range = C;
658  C.advance();
659  Token.reset(MIToken::Newline, Range.upto(C));
660  return C;
661 }
662 
663 static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token,
664  ErrorCallbackType ErrorCallback) {
665  if (C.peek() != '`')
666  return None;
667  auto Range = C;
668  C.advance();
669  auto StrRange = C;
670  while (C.peek() != '`') {
671  if (C.isEOF() || isNewlineChar(C.peek())) {
672  ErrorCallback(
673  C.location(),
674  "end of machine instruction reached before the closing '`'");
675  Token.reset(MIToken::Error, Range.remaining());
676  return C;
677  }
678  C.advance();
679  }
680  StringRef Value = StrRange.upto(C);
681  C.advance();
682  Token.reset(MIToken::QuotedIRValue, Range.upto(C)).setStringValue(Value);
683  return C;
684 }
685 
687  ErrorCallbackType ErrorCallback) {
688  auto C = skipComment(skipWhitespace(Cursor(Source)));
689  if (C.isEOF()) {
690  Token.reset(MIToken::Eof, C.remaining());
691  return C.remaining();
692  }
693 
694  if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback))
695  return R.remaining();
696  if (Cursor R = maybeLexIdentifier(C, Token))
697  return R.remaining();
698  if (Cursor R = maybeLexJumpTableIndex(C, Token))
699  return R.remaining();
700  if (Cursor R = maybeLexStackObject(C, Token))
701  return R.remaining();
702  if (Cursor R = maybeLexFixedStackObject(C, Token))
703  return R.remaining();
704  if (Cursor R = maybeLexConstantPoolItem(C, Token))
705  return R.remaining();
706  if (Cursor R = maybeLexSubRegisterIndex(C, Token, ErrorCallback))
707  return R.remaining();
708  if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback))
709  return R.remaining();
710  if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback))
711  return R.remaining();
712  if (Cursor R = maybeLexRegister(C, Token, ErrorCallback))
713  return R.remaining();
714  if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback))
715  return R.remaining();
716  if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback))
717  return R.remaining();
718  if (Cursor R = maybeLexMCSymbol(C, Token, ErrorCallback))
719  return R.remaining();
720  if (Cursor R = maybeLexHexadecimalLiteral(C, Token))
721  return R.remaining();
722  if (Cursor R = maybeLexNumericalLiteral(C, Token))
723  return R.remaining();
724  if (Cursor R = maybeLexExclaim(C, Token, ErrorCallback))
725  return R.remaining();
726  if (Cursor R = maybeLexSymbol(C, Token))
727  return R.remaining();
728  if (Cursor R = maybeLexNewline(C, Token))
729  return R.remaining();
730  if (Cursor R = maybeLexEscapedIRValue(C, Token, ErrorCallback))
731  return R.remaining();
732  if (Cursor R = maybeLexStringConstant(C, Token, ErrorCallback))
733  return R.remaining();
734 
735  Token.reset(MIToken::Error, C.remaining());
736  ErrorCallback(C.location(),
737  Twine("unexpected character '") + Twine(C.peek()) + "'");
738  return C.remaining();
739 }
static Cursor maybeLexStringConstant(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:389
uint64_t CallInst * C
static Cursor maybeLexIRBlock(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:369
static MIToken::TokenKind getMetadataKeywordKind(StringRef Identifier)
Definition: MILexer.cpp:576
bool isError() const
Definition: MILexer.h:183
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static bool isRegisterChar(char C)
Returns true for a character allowed in a register name.
Definition: MILexer.cpp:409
static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token)
Definition: MILexer.cpp:413
NoneType
A simple null object to allow implicit construction of Optional<T> and similar types without having t...
Definition: None.h:22
static Cursor lexName(Cursor C, MIToken &Token, MIToken::TokenKind Type, unsigned PrefixLength, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:159
static Cursor lexStringConstant(Cursor C, ErrorCallbackType ErrorCallback)
Lex a string constant using the following regular expression: "[^"]*".
Definition: MILexer.cpp:145
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:99
static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:663
StringRef lexMIToken(StringRef Source, MIToken &Token, function_ref< void(StringRef::iterator, const Twine &)> ErrorCallback)
Consume a single machine instruction token in the given source and return the remaining source string...
MIToken & reset(TokenKind Kind, StringRef Range)
Definition: MILexer.cpp:67
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
static bool isNewlineChar(char C)
Definition: MILexer.cpp:96
LLVM_NODISCARD StringRef drop_front(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with the first N elements dropped.
Definition: StringRef.h:642
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
static bool isValidHexFloatingPointPrefix(char C)
Definition: MILexer.cpp:520
static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token)
Definition: MILexer.cpp:562
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:600
static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:273
static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token)
Definition: MILexer.cpp:524
static Cursor skipWhitespace(Cursor C)
Skip the leading whitespace characters and return the updated cursor.
Definition: MILexer.cpp:90
static Cursor maybeLexIdentifier(Cursor C, MIToken &Token)
Definition: MILexer.cpp:261
static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:448
static Cursor maybeLexExclaim(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:587
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:151
static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:473
static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:465
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
static Cursor maybeLexNewline(Cursor C, MIToken &Token)
Definition: MILexer.cpp:654
static Cursor maybeLexStackObject(Cursor C, MIToken &Token)
Definition: MILexer.cpp:348
static Cursor lexVirtualRegister(Cursor C, MIToken &Token)
Definition: MILexer.cpp:397
static MIToken::TokenKind symbolToken(char C)
Definition: MILexer.cpp:607
MIToken & setOwnedStringValue(std::string StrVal)
Definition: MILexer.cpp:78
LLVM_NODISCARD char back() const
back - Get the last character in the string.
Definition: StringRef.h:162
static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token)
Definition: MILexer.cpp:352
static Cursor skipComment(Cursor C)
Skip a line comment and return the updated cursor.
Definition: MILexer.cpp:99
MIToken & setStringValue(StringRef StrVal)
Definition: MILexer.cpp:73
static Cursor maybeLexSymbol(Cursor C, MIToken &Token)
Definition: MILexer.cpp:638
StringRef::iterator location() const
Definition: MILexer.h:212
static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule, MIToken::TokenKind Kind)
Definition: MILexer.cpp:321
static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token)
Definition: MILexer.cpp:540
static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token)
Definition: MILexer.cpp:344
unsigned hexDigitValue(char C)
Interpret the given character C as a hexadecimal digit and return its value.
Definition: StringExtras.h:68
static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token)
Definition: MILexer.cpp:356
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:143
static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule, MIToken::TokenKind Kind)
Definition: MILexer.cpp:308
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM_NODISCARD char front() const
front - Get the first character in the string.
Definition: StringRef.h:155
LLVM Value Representation.
Definition: Value.h:74
A token produced by the machine instruction lexer.
Definition: MILexer.h:27
static Cursor maybeLexRegister(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:423
static Cursor maybeLexIRValue(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:379
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
MIToken & setIntegerValue(APSInt IntVal)
Definition: MILexer.cpp:84
static MIToken::TokenKind getIdentifierKind(StringRef Identifier)
Definition: MILexer.cpp:181
static bool isIdentifierChar(char C)
Return true if the given character satisfies the following regular expression: [-a-zA-Z$._0-9].
Definition: MILexer.cpp:109
static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token, ErrorCallbackType ErrorCallback)
Definition: MILexer.cpp:360
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:451
static bool peek(struct InternalInstruction *insn, uint8_t &byte)
static std::string unescapeQuotedString(StringRef Value)
Unescapes the given string value.
Definition: MILexer.cpp:117