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