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