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("debug-instr-number", MIToken::kw_debug_instr_number) 221 .Case("same_value", MIToken::kw_cfi_same_value) 222 .Case("offset", MIToken::kw_cfi_offset) 223 .Case("rel_offset", MIToken::kw_cfi_rel_offset) 224 .Case("def_cfa_register", MIToken::kw_cfi_def_cfa_register) 225 .Case("def_cfa_offset", MIToken::kw_cfi_def_cfa_offset) 226 .Case("adjust_cfa_offset", MIToken::kw_cfi_adjust_cfa_offset) 227 .Case("escape", MIToken::kw_cfi_escape) 228 .Case("def_cfa", MIToken::kw_cfi_def_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_align) 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("address-taken", MIToken::kw_address_taken) 262 .Case("landing-pad", MIToken::kw_landing_pad) 263 .Case("ehfunclet-entry", MIToken::kw_ehfunclet_entry) 264 .Case("liveins", MIToken::kw_liveins) 265 .Case("successors", MIToken::kw_successors) 266 .Case("floatpred", MIToken::kw_floatpred) 267 .Case("intpred", MIToken::kw_intpred) 268 .Case("shufflemask", MIToken::kw_shufflemask) 269 .Case("pre-instr-symbol", MIToken::kw_pre_instr_symbol) 270 .Case("post-instr-symbol", MIToken::kw_post_instr_symbol) 271 .Case("heap-alloc-marker", MIToken::kw_heap_alloc_marker) 272 .Case("bbsections", MIToken::kw_bbsections) 273 .Case("unknown-size", MIToken::kw_unknown_size) 274 .Default(MIToken::Identifier); 275 } 276 277 static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) { 278 if (!isalpha(C.peek()) && C.peek() != '_') 279 return None; 280 auto Range = C; 281 while (isIdentifierChar(C.peek())) 282 C.advance(); 283 auto Identifier = Range.upto(C); 284 Token.reset(getIdentifierKind(Identifier), Identifier) 285 .setStringValue(Identifier); 286 return C; 287 } 288 289 static Cursor maybeLexMachineBasicBlock(Cursor C, MIToken &Token, 290 ErrorCallbackType ErrorCallback) { 291 bool IsReference = C.remaining().startswith("%bb."); 292 if (!IsReference && !C.remaining().startswith("bb.")) 293 return None; 294 auto Range = C; 295 unsigned PrefixLength = IsReference ? 4 : 3; 296 C.advance(PrefixLength); // Skip '%bb.' or 'bb.' 297 if (!isdigit(C.peek())) { 298 Token.reset(MIToken::Error, C.remaining()); 299 ErrorCallback(C.location(), "expected a number after '%bb.'"); 300 return C; 301 } 302 auto NumberRange = C; 303 while (isdigit(C.peek())) 304 C.advance(); 305 StringRef Number = NumberRange.upto(C); 306 unsigned StringOffset = PrefixLength + Number.size(); // Drop '%bb.<id>' 307 // TODO: The format bb.<id>.<irname> is supported only when it's not a 308 // reference. Once we deprecate the format where the irname shows up, we 309 // should only lex forward if it is a reference. 310 if (C.peek() == '.') { 311 C.advance(); // Skip '.' 312 ++StringOffset; 313 while (isIdentifierChar(C.peek())) 314 C.advance(); 315 } 316 Token.reset(IsReference ? MIToken::MachineBasicBlock 317 : MIToken::MachineBasicBlockLabel, 318 Range.upto(C)) 319 .setIntegerValue(APSInt(Number)) 320 .setStringValue(Range.upto(C).drop_front(StringOffset)); 321 return C; 322 } 323 324 static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule, 325 MIToken::TokenKind Kind) { 326 if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size()))) 327 return None; 328 auto Range = C; 329 C.advance(Rule.size()); 330 auto NumberRange = C; 331 while (isdigit(C.peek())) 332 C.advance(); 333 Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C))); 334 return C; 335 } 336 337 static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule, 338 MIToken::TokenKind Kind) { 339 if (!C.remaining().startswith(Rule) || !isdigit(C.peek(Rule.size()))) 340 return None; 341 auto Range = C; 342 C.advance(Rule.size()); 343 auto NumberRange = C; 344 while (isdigit(C.peek())) 345 C.advance(); 346 StringRef Number = NumberRange.upto(C); 347 unsigned StringOffset = Rule.size() + Number.size(); 348 if (C.peek() == '.') { 349 C.advance(); 350 ++StringOffset; 351 while (isIdentifierChar(C.peek())) 352 C.advance(); 353 } 354 Token.reset(Kind, Range.upto(C)) 355 .setIntegerValue(APSInt(Number)) 356 .setStringValue(Range.upto(C).drop_front(StringOffset)); 357 return C; 358 } 359 360 static Cursor maybeLexJumpTableIndex(Cursor C, MIToken &Token) { 361 return maybeLexIndex(C, Token, "%jump-table.", MIToken::JumpTableIndex); 362 } 363 364 static Cursor maybeLexStackObject(Cursor C, MIToken &Token) { 365 return maybeLexIndexAndName(C, Token, "%stack.", MIToken::StackObject); 366 } 367 368 static Cursor maybeLexFixedStackObject(Cursor C, MIToken &Token) { 369 return maybeLexIndex(C, Token, "%fixed-stack.", MIToken::FixedStackObject); 370 } 371 372 static Cursor maybeLexConstantPoolItem(Cursor C, MIToken &Token) { 373 return maybeLexIndex(C, Token, "%const.", MIToken::ConstantPoolItem); 374 } 375 376 static Cursor maybeLexSubRegisterIndex(Cursor C, MIToken &Token, 377 ErrorCallbackType ErrorCallback) { 378 const StringRef Rule = "%subreg."; 379 if (!C.remaining().startswith(Rule)) 380 return None; 381 return lexName(C, Token, MIToken::SubRegisterIndex, Rule.size(), 382 ErrorCallback); 383 } 384 385 static Cursor maybeLexIRBlock(Cursor C, MIToken &Token, 386 ErrorCallbackType ErrorCallback) { 387 const StringRef Rule = "%ir-block."; 388 if (!C.remaining().startswith(Rule)) 389 return None; 390 if (isdigit(C.peek(Rule.size()))) 391 return maybeLexIndex(C, Token, Rule, MIToken::IRBlock); 392 return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback); 393 } 394 395 static Cursor maybeLexIRValue(Cursor C, MIToken &Token, 396 ErrorCallbackType ErrorCallback) { 397 const StringRef Rule = "%ir."; 398 if (!C.remaining().startswith(Rule)) 399 return None; 400 if (isdigit(C.peek(Rule.size()))) 401 return maybeLexIndex(C, Token, Rule, MIToken::IRValue); 402 return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback); 403 } 404 405 static Cursor maybeLexStringConstant(Cursor C, MIToken &Token, 406 ErrorCallbackType ErrorCallback) { 407 if (C.peek() != '"') 408 return None; 409 return lexName(C, Token, MIToken::StringConstant, /*PrefixLength=*/0, 410 ErrorCallback); 411 } 412 413 static Cursor lexVirtualRegister(Cursor C, MIToken &Token) { 414 auto Range = C; 415 C.advance(); // Skip '%' 416 auto NumberRange = C; 417 while (isdigit(C.peek())) 418 C.advance(); 419 Token.reset(MIToken::VirtualRegister, Range.upto(C)) 420 .setIntegerValue(APSInt(NumberRange.upto(C))); 421 return C; 422 } 423 424 /// Returns true for a character allowed in a register name. 425 static bool isRegisterChar(char C) { 426 return isIdentifierChar(C) && C != '.'; 427 } 428 429 static Cursor lexNamedVirtualRegister(Cursor C, MIToken &Token) { 430 Cursor Range = C; 431 C.advance(); // Skip '%' 432 while (isRegisterChar(C.peek())) 433 C.advance(); 434 Token.reset(MIToken::NamedVirtualRegister, Range.upto(C)) 435 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%' 436 return C; 437 } 438 439 static Cursor maybeLexRegister(Cursor C, MIToken &Token, 440 ErrorCallbackType ErrorCallback) { 441 if (C.peek() != '%' && C.peek() != '$') 442 return None; 443 444 if (C.peek() == '%') { 445 if (isdigit(C.peek(1))) 446 return lexVirtualRegister(C, Token); 447 448 if (isRegisterChar(C.peek(1))) 449 return lexNamedVirtualRegister(C, Token); 450 451 return None; 452 } 453 454 assert(C.peek() == '$'); 455 auto Range = C; 456 C.advance(); // Skip '$' 457 while (isRegisterChar(C.peek())) 458 C.advance(); 459 Token.reset(MIToken::NamedRegister, Range.upto(C)) 460 .setStringValue(Range.upto(C).drop_front(1)); // Drop the '$' 461 return C; 462 } 463 464 static Cursor maybeLexGlobalValue(Cursor C, MIToken &Token, 465 ErrorCallbackType ErrorCallback) { 466 if (C.peek() != '@') 467 return None; 468 if (!isdigit(C.peek(1))) 469 return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1, 470 ErrorCallback); 471 auto Range = C; 472 C.advance(1); // Skip the '@' 473 auto NumberRange = C; 474 while (isdigit(C.peek())) 475 C.advance(); 476 Token.reset(MIToken::GlobalValue, Range.upto(C)) 477 .setIntegerValue(APSInt(NumberRange.upto(C))); 478 return C; 479 } 480 481 static Cursor maybeLexExternalSymbol(Cursor C, MIToken &Token, 482 ErrorCallbackType ErrorCallback) { 483 if (C.peek() != '&') 484 return None; 485 return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1, 486 ErrorCallback); 487 } 488 489 static Cursor maybeLexMCSymbol(Cursor C, MIToken &Token, 490 ErrorCallbackType ErrorCallback) { 491 const StringRef Rule = "<mcsymbol "; 492 if (!C.remaining().startswith(Rule)) 493 return None; 494 auto Start = C; 495 C.advance(Rule.size()); 496 497 // Try a simple unquoted name. 498 if (C.peek() != '"') { 499 while (isIdentifierChar(C.peek())) 500 C.advance(); 501 StringRef String = Start.upto(C).drop_front(Rule.size()); 502 if (C.peek() != '>') { 503 ErrorCallback(C.location(), 504 "expected the '<mcsymbol ...' to be closed by a '>'"); 505 Token.reset(MIToken::Error, Start.remaining()); 506 return Start; 507 } 508 C.advance(); 509 510 Token.reset(MIToken::MCSymbol, Start.upto(C)).setStringValue(String); 511 return C; 512 } 513 514 // Otherwise lex out a quoted name. 515 Cursor R = lexStringConstant(C, ErrorCallback); 516 if (!R) { 517 ErrorCallback(C.location(), 518 "unable to parse quoted string from opening quote"); 519 Token.reset(MIToken::Error, Start.remaining()); 520 return Start; 521 } 522 StringRef String = Start.upto(R).drop_front(Rule.size()); 523 if (R.peek() != '>') { 524 ErrorCallback(R.location(), 525 "expected the '<mcsymbol ...' to be closed by a '>'"); 526 Token.reset(MIToken::Error, Start.remaining()); 527 return Start; 528 } 529 R.advance(); 530 531 Token.reset(MIToken::MCSymbol, Start.upto(R)) 532 .setOwnedStringValue(unescapeQuotedString(String)); 533 return R; 534 } 535 536 static bool isValidHexFloatingPointPrefix(char C) { 537 return C == 'H' || C == 'K' || C == 'L' || C == 'M' || C == 'R'; 538 } 539 540 static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) { 541 C.advance(); 542 // Skip over [0-9]*([eE][-+]?[0-9]+)? 543 while (isdigit(C.peek())) 544 C.advance(); 545 if ((C.peek() == 'e' || C.peek() == 'E') && 546 (isdigit(C.peek(1)) || 547 ((C.peek(1) == '-' || C.peek(1) == '+') && isdigit(C.peek(2))))) { 548 C.advance(2); 549 while (isdigit(C.peek())) 550 C.advance(); 551 } 552 Token.reset(MIToken::FloatingPointLiteral, Range.upto(C)); 553 return C; 554 } 555 556 static Cursor maybeLexHexadecimalLiteral(Cursor C, MIToken &Token) { 557 if (C.peek() != '0' || (C.peek(1) != 'x' && C.peek(1) != 'X')) 558 return None; 559 Cursor Range = C; 560 C.advance(2); 561 unsigned PrefLen = 2; 562 if (isValidHexFloatingPointPrefix(C.peek())) { 563 C.advance(); 564 PrefLen++; 565 } 566 while (isxdigit(C.peek())) 567 C.advance(); 568 StringRef StrVal = Range.upto(C); 569 if (StrVal.size() <= PrefLen) 570 return None; 571 if (PrefLen == 2) 572 Token.reset(MIToken::HexLiteral, Range.upto(C)); 573 else // It must be 3, which means that there was a floating-point prefix. 574 Token.reset(MIToken::FloatingPointLiteral, Range.upto(C)); 575 return C; 576 } 577 578 static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) { 579 if (!isdigit(C.peek()) && (C.peek() != '-' || !isdigit(C.peek(1)))) 580 return None; 581 auto Range = C; 582 C.advance(); 583 while (isdigit(C.peek())) 584 C.advance(); 585 if (C.peek() == '.') 586 return lexFloatingPointLiteral(Range, C, Token); 587 StringRef StrVal = Range.upto(C); 588 Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal)); 589 return C; 590 } 591 592 static MIToken::TokenKind getMetadataKeywordKind(StringRef Identifier) { 593 return StringSwitch<MIToken::TokenKind>(Identifier) 594 .Case("!tbaa", MIToken::md_tbaa) 595 .Case("!alias.scope", MIToken::md_alias_scope) 596 .Case("!noalias", MIToken::md_noalias) 597 .Case("!range", MIToken::md_range) 598 .Case("!DIExpression", MIToken::md_diexpr) 599 .Case("!DILocation", MIToken::md_dilocation) 600 .Default(MIToken::Error); 601 } 602 603 static Cursor maybeLexExclaim(Cursor C, MIToken &Token, 604 ErrorCallbackType ErrorCallback) { 605 if (C.peek() != '!') 606 return None; 607 auto Range = C; 608 C.advance(1); 609 if (isdigit(C.peek()) || !isIdentifierChar(C.peek())) { 610 Token.reset(MIToken::exclaim, Range.upto(C)); 611 return C; 612 } 613 while (isIdentifierChar(C.peek())) 614 C.advance(); 615 StringRef StrVal = Range.upto(C); 616 Token.reset(getMetadataKeywordKind(StrVal), StrVal); 617 if (Token.isError()) 618 ErrorCallback(Token.location(), 619 "use of unknown metadata keyword '" + StrVal + "'"); 620 return C; 621 } 622 623 static MIToken::TokenKind symbolToken(char C) { 624 switch (C) { 625 case ',': 626 return MIToken::comma; 627 case '.': 628 return MIToken::dot; 629 case '=': 630 return MIToken::equal; 631 case ':': 632 return MIToken::colon; 633 case '(': 634 return MIToken::lparen; 635 case ')': 636 return MIToken::rparen; 637 case '{': 638 return MIToken::lbrace; 639 case '}': 640 return MIToken::rbrace; 641 case '+': 642 return MIToken::plus; 643 case '-': 644 return MIToken::minus; 645 case '<': 646 return MIToken::less; 647 case '>': 648 return MIToken::greater; 649 default: 650 return MIToken::Error; 651 } 652 } 653 654 static Cursor maybeLexSymbol(Cursor C, MIToken &Token) { 655 MIToken::TokenKind Kind; 656 unsigned Length = 1; 657 if (C.peek() == ':' && C.peek(1) == ':') { 658 Kind = MIToken::coloncolon; 659 Length = 2; 660 } else 661 Kind = symbolToken(C.peek()); 662 if (Kind == MIToken::Error) 663 return None; 664 auto Range = C; 665 C.advance(Length); 666 Token.reset(Kind, Range.upto(C)); 667 return C; 668 } 669 670 static Cursor maybeLexNewline(Cursor C, MIToken &Token) { 671 if (!isNewlineChar(C.peek())) 672 return None; 673 auto Range = C; 674 C.advance(); 675 Token.reset(MIToken::Newline, Range.upto(C)); 676 return C; 677 } 678 679 static Cursor maybeLexEscapedIRValue(Cursor C, MIToken &Token, 680 ErrorCallbackType ErrorCallback) { 681 if (C.peek() != '`') 682 return None; 683 auto Range = C; 684 C.advance(); 685 auto StrRange = C; 686 while (C.peek() != '`') { 687 if (C.isEOF() || isNewlineChar(C.peek())) { 688 ErrorCallback( 689 C.location(), 690 "end of machine instruction reached before the closing '`'"); 691 Token.reset(MIToken::Error, Range.remaining()); 692 return C; 693 } 694 C.advance(); 695 } 696 StringRef Value = StrRange.upto(C); 697 C.advance(); 698 Token.reset(MIToken::QuotedIRValue, Range.upto(C)).setStringValue(Value); 699 return C; 700 } 701 702 StringRef llvm::lexMIToken(StringRef Source, MIToken &Token, 703 ErrorCallbackType ErrorCallback) { 704 auto C = skipComment(skipWhitespace(Cursor(Source))); 705 if (C.isEOF()) { 706 Token.reset(MIToken::Eof, C.remaining()); 707 return C.remaining(); 708 } 709 710 C = skipMachineOperandComment(C); 711 712 if (Cursor R = maybeLexMachineBasicBlock(C, Token, ErrorCallback)) 713 return R.remaining(); 714 if (Cursor R = maybeLexIdentifier(C, Token)) 715 return R.remaining(); 716 if (Cursor R = maybeLexJumpTableIndex(C, Token)) 717 return R.remaining(); 718 if (Cursor R = maybeLexStackObject(C, Token)) 719 return R.remaining(); 720 if (Cursor R = maybeLexFixedStackObject(C, Token)) 721 return R.remaining(); 722 if (Cursor R = maybeLexConstantPoolItem(C, Token)) 723 return R.remaining(); 724 if (Cursor R = maybeLexSubRegisterIndex(C, Token, ErrorCallback)) 725 return R.remaining(); 726 if (Cursor R = maybeLexIRBlock(C, Token, ErrorCallback)) 727 return R.remaining(); 728 if (Cursor R = maybeLexIRValue(C, Token, ErrorCallback)) 729 return R.remaining(); 730 if (Cursor R = maybeLexRegister(C, Token, ErrorCallback)) 731 return R.remaining(); 732 if (Cursor R = maybeLexGlobalValue(C, Token, ErrorCallback)) 733 return R.remaining(); 734 if (Cursor R = maybeLexExternalSymbol(C, Token, ErrorCallback)) 735 return R.remaining(); 736 if (Cursor R = maybeLexMCSymbol(C, Token, ErrorCallback)) 737 return R.remaining(); 738 if (Cursor R = maybeLexHexadecimalLiteral(C, Token)) 739 return R.remaining(); 740 if (Cursor R = maybeLexNumericalLiteral(C, Token)) 741 return R.remaining(); 742 if (Cursor R = maybeLexExclaim(C, Token, ErrorCallback)) 743 return R.remaining(); 744 if (Cursor R = maybeLexSymbol(C, Token)) 745 return R.remaining(); 746 if (Cursor R = maybeLexNewline(C, Token)) 747 return R.remaining(); 748 if (Cursor R = maybeLexEscapedIRValue(C, Token, ErrorCallback)) 749 return R.remaining(); 750 if (Cursor R = maybeLexStringConstant(C, Token, ErrorCallback)) 751 return R.remaining(); 752 753 Token.reset(MIToken::Error, C.remaining()); 754 ErrorCallback(C.location(), 755 Twine("unexpected character '") + Twine(C.peek()) + "'"); 756 return C.remaining(); 757 } 758