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