1 //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===// 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 // Implement the Lexer for .ll files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "LLLexer.h" 14 #include "llvm/ADT/APInt.h" 15 #include "llvm/ADT/STLExtras.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/ADT/Twine.h" 18 #include "llvm/IR/DerivedTypes.h" 19 #include "llvm/IR/Instruction.h" 20 #include "llvm/Support/ErrorHandling.h" 21 #include "llvm/Support/SourceMgr.h" 22 #include <cassert> 23 #include <cctype> 24 #include <cstdio> 25 26 using namespace llvm; 27 28 bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const { 29 ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg); 30 return true; 31 } 32 33 void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const { 34 SM.PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg); 35 } 36 37 //===----------------------------------------------------------------------===// 38 // Helper functions. 39 //===----------------------------------------------------------------------===// 40 41 // atoull - Convert an ascii string of decimal digits into the unsigned long 42 // long representation... this does not have to do input error checking, 43 // because we know that the input will be matched by a suitable regex... 44 // 45 uint64_t LLLexer::atoull(const char *Buffer, const char *End) { 46 uint64_t Result = 0; 47 for (; Buffer != End; Buffer++) { 48 uint64_t OldRes = Result; 49 Result *= 10; 50 Result += *Buffer-'0'; 51 if (Result < OldRes) { // Uh, oh, overflow detected!!! 52 Error("constant bigger than 64 bits detected!"); 53 return 0; 54 } 55 } 56 return Result; 57 } 58 59 uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) { 60 uint64_t Result = 0; 61 for (; Buffer != End; ++Buffer) { 62 uint64_t OldRes = Result; 63 Result *= 16; 64 Result += hexDigitValue(*Buffer); 65 66 if (Result < OldRes) { // Uh, oh, overflow detected!!! 67 Error("constant bigger than 64 bits detected!"); 68 return 0; 69 } 70 } 71 return Result; 72 } 73 74 void LLLexer::HexToIntPair(const char *Buffer, const char *End, 75 uint64_t Pair[2]) { 76 Pair[0] = 0; 77 if (End - Buffer >= 16) { 78 for (int i = 0; i < 16; i++, Buffer++) { 79 assert(Buffer != End); 80 Pair[0] *= 16; 81 Pair[0] += hexDigitValue(*Buffer); 82 } 83 } 84 Pair[1] = 0; 85 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) { 86 Pair[1] *= 16; 87 Pair[1] += hexDigitValue(*Buffer); 88 } 89 if (Buffer != End) 90 Error("constant bigger than 128 bits detected!"); 91 } 92 93 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into 94 /// { low64, high16 } as usual for an APInt. 95 void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End, 96 uint64_t Pair[2]) { 97 Pair[1] = 0; 98 for (int i=0; i<4 && Buffer != End; i++, Buffer++) { 99 assert(Buffer != End); 100 Pair[1] *= 16; 101 Pair[1] += hexDigitValue(*Buffer); 102 } 103 Pair[0] = 0; 104 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) { 105 Pair[0] *= 16; 106 Pair[0] += hexDigitValue(*Buffer); 107 } 108 if (Buffer != End) 109 Error("constant bigger than 128 bits detected!"); 110 } 111 112 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the 113 // appropriate character. 114 static void UnEscapeLexed(std::string &Str) { 115 if (Str.empty()) return; 116 117 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size(); 118 char *BOut = Buffer; 119 for (char *BIn = Buffer; BIn != EndBuffer; ) { 120 if (BIn[0] == '\\') { 121 if (BIn < EndBuffer-1 && BIn[1] == '\\') { 122 *BOut++ = '\\'; // Two \ becomes one 123 BIn += 2; 124 } else if (BIn < EndBuffer-2 && 125 isxdigit(static_cast<unsigned char>(BIn[1])) && 126 isxdigit(static_cast<unsigned char>(BIn[2]))) { 127 *BOut = hexDigitValue(BIn[1]) * 16 + hexDigitValue(BIn[2]); 128 BIn += 3; // Skip over handled chars 129 ++BOut; 130 } else { 131 *BOut++ = *BIn++; 132 } 133 } else { 134 *BOut++ = *BIn++; 135 } 136 } 137 Str.resize(BOut-Buffer); 138 } 139 140 /// isLabelChar - Return true for [-a-zA-Z$._0-9]. 141 static bool isLabelChar(char C) { 142 return isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' || 143 C == '.' || C == '_'; 144 } 145 146 /// isLabelTail - Return true if this pointer points to a valid end of a label. 147 static const char *isLabelTail(const char *CurPtr) { 148 while (true) { 149 if (CurPtr[0] == ':') return CurPtr+1; 150 if (!isLabelChar(CurPtr[0])) return nullptr; 151 ++CurPtr; 152 } 153 } 154 155 //===----------------------------------------------------------------------===// 156 // Lexer definition. 157 //===----------------------------------------------------------------------===// 158 159 LLLexer::LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &Err, 160 LLVMContext &C) 161 : CurBuf(StartBuf), ErrorInfo(Err), SM(SM), Context(C), APFloatVal(0.0), 162 IgnoreColonInIdentifiers(false) { 163 CurPtr = CurBuf.begin(); 164 } 165 166 int LLLexer::getNextChar() { 167 char CurChar = *CurPtr++; 168 switch (CurChar) { 169 default: return (unsigned char)CurChar; 170 case 0: 171 // A nul character in the stream is either the end of the current buffer or 172 // a random nul in the file. Disambiguate that here. 173 if (CurPtr-1 != CurBuf.end()) 174 return 0; // Just whitespace. 175 176 // Otherwise, return end of file. 177 --CurPtr; // Another call to lex will return EOF again. 178 return EOF; 179 } 180 } 181 182 lltok::Kind LLLexer::LexToken() { 183 while (true) { 184 TokStart = CurPtr; 185 186 int CurChar = getNextChar(); 187 switch (CurChar) { 188 default: 189 // Handle letters: [a-zA-Z_] 190 if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_') 191 return LexIdentifier(); 192 193 return lltok::Error; 194 case EOF: return lltok::Eof; 195 case 0: 196 case ' ': 197 case '\t': 198 case '\n': 199 case '\r': 200 // Ignore whitespace. 201 continue; 202 case '+': return LexPositive(); 203 case '@': return LexAt(); 204 case '$': return LexDollar(); 205 case '%': return LexPercent(); 206 case '"': return LexQuote(); 207 case '.': 208 if (const char *Ptr = isLabelTail(CurPtr)) { 209 CurPtr = Ptr; 210 StrVal.assign(TokStart, CurPtr-1); 211 return lltok::LabelStr; 212 } 213 if (CurPtr[0] == '.' && CurPtr[1] == '.') { 214 CurPtr += 2; 215 return lltok::dotdotdot; 216 } 217 return lltok::Error; 218 case ';': 219 SkipLineComment(); 220 continue; 221 case '!': return LexExclaim(); 222 case '^': 223 return LexCaret(); 224 case ':': 225 return lltok::colon; 226 case '#': return LexHash(); 227 case '0': case '1': case '2': case '3': case '4': 228 case '5': case '6': case '7': case '8': case '9': 229 case '-': 230 return LexDigitOrNegative(); 231 case '=': return lltok::equal; 232 case '[': return lltok::lsquare; 233 case ']': return lltok::rsquare; 234 case '{': return lltok::lbrace; 235 case '}': return lltok::rbrace; 236 case '<': return lltok::less; 237 case '>': return lltok::greater; 238 case '(': return lltok::lparen; 239 case ')': return lltok::rparen; 240 case ',': return lltok::comma; 241 case '*': return lltok::star; 242 case '|': return lltok::bar; 243 } 244 } 245 } 246 247 void LLLexer::SkipLineComment() { 248 while (true) { 249 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF) 250 return; 251 } 252 } 253 254 /// Lex all tokens that start with an @ character. 255 /// GlobalVar @\"[^\"]*\" 256 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]* 257 /// GlobalVarID @[0-9]+ 258 lltok::Kind LLLexer::LexAt() { 259 return LexVar(lltok::GlobalVar, lltok::GlobalID); 260 } 261 262 lltok::Kind LLLexer::LexDollar() { 263 if (const char *Ptr = isLabelTail(TokStart)) { 264 CurPtr = Ptr; 265 StrVal.assign(TokStart, CurPtr - 1); 266 return lltok::LabelStr; 267 } 268 269 // Handle DollarStringConstant: $\"[^\"]*\" 270 if (CurPtr[0] == '"') { 271 ++CurPtr; 272 273 while (true) { 274 int CurChar = getNextChar(); 275 276 if (CurChar == EOF) { 277 Error("end of file in COMDAT variable name"); 278 return lltok::Error; 279 } 280 if (CurChar == '"') { 281 StrVal.assign(TokStart + 2, CurPtr - 1); 282 UnEscapeLexed(StrVal); 283 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) { 284 Error("Null bytes are not allowed in names"); 285 return lltok::Error; 286 } 287 return lltok::ComdatVar; 288 } 289 } 290 } 291 292 // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]* 293 if (ReadVarName()) 294 return lltok::ComdatVar; 295 296 return lltok::Error; 297 } 298 299 /// ReadString - Read a string until the closing quote. 300 lltok::Kind LLLexer::ReadString(lltok::Kind kind) { 301 const char *Start = CurPtr; 302 while (true) { 303 int CurChar = getNextChar(); 304 305 if (CurChar == EOF) { 306 Error("end of file in string constant"); 307 return lltok::Error; 308 } 309 if (CurChar == '"') { 310 StrVal.assign(Start, CurPtr-1); 311 UnEscapeLexed(StrVal); 312 return kind; 313 } 314 } 315 } 316 317 /// ReadVarName - Read the rest of a token containing a variable name. 318 bool LLLexer::ReadVarName() { 319 const char *NameStart = CurPtr; 320 if (isalpha(static_cast<unsigned char>(CurPtr[0])) || 321 CurPtr[0] == '-' || CurPtr[0] == '$' || 322 CurPtr[0] == '.' || CurPtr[0] == '_') { 323 ++CurPtr; 324 while (isalnum(static_cast<unsigned char>(CurPtr[0])) || 325 CurPtr[0] == '-' || CurPtr[0] == '$' || 326 CurPtr[0] == '.' || CurPtr[0] == '_') 327 ++CurPtr; 328 329 StrVal.assign(NameStart, CurPtr); 330 return true; 331 } 332 return false; 333 } 334 335 // Lex an ID: [0-9]+. On success, the ID is stored in UIntVal and Token is 336 // returned, otherwise the Error token is returned. 337 lltok::Kind LLLexer::LexUIntID(lltok::Kind Token) { 338 if (!isdigit(static_cast<unsigned char>(CurPtr[0]))) 339 return lltok::Error; 340 341 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr) 342 /*empty*/; 343 344 uint64_t Val = atoull(TokStart + 1, CurPtr); 345 if ((unsigned)Val != Val) 346 Error("invalid value number (too large)!"); 347 UIntVal = unsigned(Val); 348 return Token; 349 } 350 351 lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) { 352 // Handle StringConstant: \"[^\"]*\" 353 if (CurPtr[0] == '"') { 354 ++CurPtr; 355 356 while (true) { 357 int CurChar = getNextChar(); 358 359 if (CurChar == EOF) { 360 Error("end of file in global variable name"); 361 return lltok::Error; 362 } 363 if (CurChar == '"') { 364 StrVal.assign(TokStart+2, CurPtr-1); 365 UnEscapeLexed(StrVal); 366 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) { 367 Error("Null bytes are not allowed in names"); 368 return lltok::Error; 369 } 370 return Var; 371 } 372 } 373 } 374 375 // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]* 376 if (ReadVarName()) 377 return Var; 378 379 // Handle VarID: [0-9]+ 380 return LexUIntID(VarID); 381 } 382 383 /// Lex all tokens that start with a % character. 384 /// LocalVar ::= %\"[^\"]*\" 385 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]* 386 /// LocalVarID ::= %[0-9]+ 387 lltok::Kind LLLexer::LexPercent() { 388 return LexVar(lltok::LocalVar, lltok::LocalVarID); 389 } 390 391 /// Lex all tokens that start with a " character. 392 /// QuoteLabel "[^"]+": 393 /// StringConstant "[^"]*" 394 lltok::Kind LLLexer::LexQuote() { 395 lltok::Kind kind = ReadString(lltok::StringConstant); 396 if (kind == lltok::Error || kind == lltok::Eof) 397 return kind; 398 399 if (CurPtr[0] == ':') { 400 ++CurPtr; 401 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) { 402 Error("Null bytes are not allowed in names"); 403 kind = lltok::Error; 404 } else { 405 kind = lltok::LabelStr; 406 } 407 } 408 409 return kind; 410 } 411 412 /// Lex all tokens that start with a ! character. 413 /// !foo 414 /// ! 415 lltok::Kind LLLexer::LexExclaim() { 416 // Lex a metadata name as a MetadataVar. 417 if (isalpha(static_cast<unsigned char>(CurPtr[0])) || 418 CurPtr[0] == '-' || CurPtr[0] == '$' || 419 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') { 420 ++CurPtr; 421 while (isalnum(static_cast<unsigned char>(CurPtr[0])) || 422 CurPtr[0] == '-' || CurPtr[0] == '$' || 423 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') 424 ++CurPtr; 425 426 StrVal.assign(TokStart+1, CurPtr); // Skip ! 427 UnEscapeLexed(StrVal); 428 return lltok::MetadataVar; 429 } 430 return lltok::exclaim; 431 } 432 433 /// Lex all tokens that start with a ^ character. 434 /// SummaryID ::= ^[0-9]+ 435 lltok::Kind LLLexer::LexCaret() { 436 // Handle SummaryID: ^[0-9]+ 437 return LexUIntID(lltok::SummaryID); 438 } 439 440 /// Lex all tokens that start with a # character. 441 /// AttrGrpID ::= #[0-9]+ 442 lltok::Kind LLLexer::LexHash() { 443 // Handle AttrGrpID: #[0-9]+ 444 return LexUIntID(lltok::AttrGrpID); 445 } 446 447 /// Lex a label, integer type, keyword, or hexadecimal integer constant. 448 /// Label [-a-zA-Z$._0-9]+: 449 /// IntegerType i[0-9]+ 450 /// Keyword sdiv, float, ... 451 /// HexIntConstant [us]0x[0-9A-Fa-f]+ 452 lltok::Kind LLLexer::LexIdentifier() { 453 const char *StartChar = CurPtr; 454 const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar; 455 const char *KeywordEnd = nullptr; 456 457 for (; isLabelChar(*CurPtr); ++CurPtr) { 458 // If we decide this is an integer, remember the end of the sequence. 459 if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr))) 460 IntEnd = CurPtr; 461 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) && 462 *CurPtr != '_') 463 KeywordEnd = CurPtr; 464 } 465 466 // If we stopped due to a colon, unless we were directed to ignore it, 467 // this really is a label. 468 if (!IgnoreColonInIdentifiers && *CurPtr == ':') { 469 StrVal.assign(StartChar-1, CurPtr++); 470 return lltok::LabelStr; 471 } 472 473 // Otherwise, this wasn't a label. If this was valid as an integer type, 474 // return it. 475 if (!IntEnd) IntEnd = CurPtr; 476 if (IntEnd != StartChar) { 477 CurPtr = IntEnd; 478 uint64_t NumBits = atoull(StartChar, CurPtr); 479 if (NumBits < IntegerType::MIN_INT_BITS || 480 NumBits > IntegerType::MAX_INT_BITS) { 481 Error("bitwidth for integer type out of range!"); 482 return lltok::Error; 483 } 484 TyVal = IntegerType::get(Context, NumBits); 485 return lltok::Type; 486 } 487 488 // Otherwise, this was a letter sequence. See which keyword this is. 489 if (!KeywordEnd) KeywordEnd = CurPtr; 490 CurPtr = KeywordEnd; 491 --StartChar; 492 StringRef Keyword(StartChar, CurPtr - StartChar); 493 494 #define KEYWORD(STR) \ 495 do { \ 496 if (Keyword == #STR) \ 497 return lltok::kw_##STR; \ 498 } while (false) 499 500 KEYWORD(true); KEYWORD(false); 501 KEYWORD(declare); KEYWORD(define); 502 KEYWORD(global); KEYWORD(constant); 503 504 KEYWORD(dso_local); 505 KEYWORD(dso_preemptable); 506 507 KEYWORD(private); 508 KEYWORD(internal); 509 KEYWORD(available_externally); 510 KEYWORD(linkonce); 511 KEYWORD(linkonce_odr); 512 KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg". 513 KEYWORD(weak_odr); 514 KEYWORD(appending); 515 KEYWORD(dllimport); 516 KEYWORD(dllexport); 517 KEYWORD(common); 518 KEYWORD(default); 519 KEYWORD(hidden); 520 KEYWORD(protected); 521 KEYWORD(unnamed_addr); 522 KEYWORD(local_unnamed_addr); 523 KEYWORD(externally_initialized); 524 KEYWORD(extern_weak); 525 KEYWORD(external); 526 KEYWORD(thread_local); 527 KEYWORD(localdynamic); 528 KEYWORD(initialexec); 529 KEYWORD(localexec); 530 KEYWORD(zeroinitializer); 531 KEYWORD(undef); 532 KEYWORD(null); 533 KEYWORD(none); 534 KEYWORD(poison); 535 KEYWORD(to); 536 KEYWORD(caller); 537 KEYWORD(within); 538 KEYWORD(from); 539 KEYWORD(tail); 540 KEYWORD(musttail); 541 KEYWORD(notail); 542 KEYWORD(target); 543 KEYWORD(triple); 544 KEYWORD(source_filename); 545 KEYWORD(unwind); 546 KEYWORD(deplibs); // FIXME: Remove in 4.0. 547 KEYWORD(datalayout); 548 KEYWORD(volatile); 549 KEYWORD(atomic); 550 KEYWORD(unordered); 551 KEYWORD(monotonic); 552 KEYWORD(acquire); 553 KEYWORD(release); 554 KEYWORD(acq_rel); 555 KEYWORD(seq_cst); 556 KEYWORD(syncscope); 557 558 KEYWORD(nnan); 559 KEYWORD(ninf); 560 KEYWORD(nsz); 561 KEYWORD(arcp); 562 KEYWORD(contract); 563 KEYWORD(reassoc); 564 KEYWORD(afn); 565 KEYWORD(fast); 566 KEYWORD(nuw); 567 KEYWORD(nsw); 568 KEYWORD(exact); 569 KEYWORD(inbounds); 570 KEYWORD(inrange); 571 KEYWORD(align); 572 KEYWORD(addrspace); 573 KEYWORD(section); 574 KEYWORD(partition); 575 KEYWORD(alias); 576 KEYWORD(ifunc); 577 KEYWORD(module); 578 KEYWORD(asm); 579 KEYWORD(sideeffect); 580 KEYWORD(alignstack); 581 KEYWORD(inteldialect); 582 KEYWORD(gc); 583 KEYWORD(prefix); 584 KEYWORD(prologue); 585 586 KEYWORD(ccc); 587 KEYWORD(fastcc); 588 KEYWORD(coldcc); 589 KEYWORD(cfguard_checkcc); 590 KEYWORD(x86_stdcallcc); 591 KEYWORD(x86_fastcallcc); 592 KEYWORD(x86_thiscallcc); 593 KEYWORD(x86_vectorcallcc); 594 KEYWORD(arm_apcscc); 595 KEYWORD(arm_aapcscc); 596 KEYWORD(arm_aapcs_vfpcc); 597 KEYWORD(aarch64_vector_pcs); 598 KEYWORD(aarch64_sve_vector_pcs); 599 KEYWORD(msp430_intrcc); 600 KEYWORD(avr_intrcc); 601 KEYWORD(avr_signalcc); 602 KEYWORD(ptx_kernel); 603 KEYWORD(ptx_device); 604 KEYWORD(spir_kernel); 605 KEYWORD(spir_func); 606 KEYWORD(intel_ocl_bicc); 607 KEYWORD(x86_64_sysvcc); 608 KEYWORD(win64cc); 609 KEYWORD(x86_regcallcc); 610 KEYWORD(webkit_jscc); 611 KEYWORD(swiftcc); 612 KEYWORD(anyregcc); 613 KEYWORD(preserve_mostcc); 614 KEYWORD(preserve_allcc); 615 KEYWORD(ghccc); 616 KEYWORD(x86_intrcc); 617 KEYWORD(hhvmcc); 618 KEYWORD(hhvm_ccc); 619 KEYWORD(cxx_fast_tlscc); 620 KEYWORD(amdgpu_vs); 621 KEYWORD(amdgpu_ls); 622 KEYWORD(amdgpu_hs); 623 KEYWORD(amdgpu_es); 624 KEYWORD(amdgpu_gs); 625 KEYWORD(amdgpu_ps); 626 KEYWORD(amdgpu_cs); 627 KEYWORD(amdgpu_kernel); 628 KEYWORD(amdgpu_gfx); 629 KEYWORD(tailcc); 630 631 KEYWORD(cc); 632 KEYWORD(c); 633 634 KEYWORD(attributes); 635 636 KEYWORD(alwaysinline); 637 KEYWORD(allocsize); 638 KEYWORD(argmemonly); 639 KEYWORD(builtin); 640 KEYWORD(byval); 641 KEYWORD(inalloca); 642 KEYWORD(cold); 643 KEYWORD(convergent); 644 KEYWORD(dereferenceable); 645 KEYWORD(dereferenceable_or_null); 646 KEYWORD(inaccessiblememonly); 647 KEYWORD(inaccessiblemem_or_argmemonly); 648 KEYWORD(inlinehint); 649 KEYWORD(inreg); 650 KEYWORD(jumptable); 651 KEYWORD(minsize); 652 KEYWORD(naked); 653 KEYWORD(nest); 654 KEYWORD(noalias); 655 KEYWORD(nobuiltin); 656 KEYWORD(nocallback); 657 KEYWORD(nocapture); 658 KEYWORD(noduplicate); 659 KEYWORD(nofree); 660 KEYWORD(noimplicitfloat); 661 KEYWORD(noinline); 662 KEYWORD(norecurse); 663 KEYWORD(nonlazybind); 664 KEYWORD(nomerge); 665 KEYWORD(nonnull); 666 KEYWORD(noprofile); 667 KEYWORD(noredzone); 668 KEYWORD(noreturn); 669 KEYWORD(nosync); 670 KEYWORD(nocf_check); 671 KEYWORD(noundef); 672 KEYWORD(nounwind); 673 KEYWORD(null_pointer_is_valid); 674 KEYWORD(optforfuzzing); 675 KEYWORD(optnone); 676 KEYWORD(optsize); 677 KEYWORD(preallocated); 678 KEYWORD(readnone); 679 KEYWORD(readonly); 680 KEYWORD(returned); 681 KEYWORD(returns_twice); 682 KEYWORD(signext); 683 KEYWORD(speculatable); 684 KEYWORD(sret); 685 KEYWORD(ssp); 686 KEYWORD(sspreq); 687 KEYWORD(sspstrong); 688 KEYWORD(strictfp); 689 KEYWORD(safestack); 690 KEYWORD(shadowcallstack); 691 KEYWORD(sanitize_address); 692 KEYWORD(sanitize_hwaddress); 693 KEYWORD(sanitize_memtag); 694 KEYWORD(sanitize_thread); 695 KEYWORD(sanitize_memory); 696 KEYWORD(speculative_load_hardening); 697 KEYWORD(swifterror); 698 KEYWORD(swiftself); 699 KEYWORD(uwtable); 700 KEYWORD(willreturn); 701 KEYWORD(writeonly); 702 KEYWORD(zeroext); 703 KEYWORD(immarg); 704 KEYWORD(byref); 705 KEYWORD(mustprogress); 706 707 KEYWORD(type); 708 KEYWORD(opaque); 709 710 KEYWORD(comdat); 711 712 // Comdat types 713 KEYWORD(any); 714 KEYWORD(exactmatch); 715 KEYWORD(largest); 716 KEYWORD(noduplicates); 717 KEYWORD(samesize); 718 719 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle); 720 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge); 721 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole); 722 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une); 723 724 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax); 725 KEYWORD(umin); 726 727 KEYWORD(vscale); 728 KEYWORD(x); 729 KEYWORD(blockaddress); 730 KEYWORD(dso_local_equivalent); 731 732 // Metadata types. 733 KEYWORD(distinct); 734 735 // Use-list order directives. 736 KEYWORD(uselistorder); 737 KEYWORD(uselistorder_bb); 738 739 KEYWORD(personality); 740 KEYWORD(cleanup); 741 KEYWORD(catch); 742 KEYWORD(filter); 743 744 // Summary index keywords. 745 KEYWORD(path); 746 KEYWORD(hash); 747 KEYWORD(gv); 748 KEYWORD(guid); 749 KEYWORD(name); 750 KEYWORD(summaries); 751 KEYWORD(flags); 752 KEYWORD(blockcount); 753 KEYWORD(linkage); 754 KEYWORD(notEligibleToImport); 755 KEYWORD(live); 756 KEYWORD(dsoLocal); 757 KEYWORD(canAutoHide); 758 KEYWORD(function); 759 KEYWORD(insts); 760 KEYWORD(funcFlags); 761 KEYWORD(readNone); 762 KEYWORD(readOnly); 763 KEYWORD(noRecurse); 764 KEYWORD(returnDoesNotAlias); 765 KEYWORD(noInline); 766 KEYWORD(alwaysInline); 767 KEYWORD(calls); 768 KEYWORD(callee); 769 KEYWORD(params); 770 KEYWORD(param); 771 KEYWORD(hotness); 772 KEYWORD(unknown); 773 KEYWORD(hot); 774 KEYWORD(critical); 775 KEYWORD(relbf); 776 KEYWORD(variable); 777 KEYWORD(vTableFuncs); 778 KEYWORD(virtFunc); 779 KEYWORD(aliasee); 780 KEYWORD(refs); 781 KEYWORD(typeIdInfo); 782 KEYWORD(typeTests); 783 KEYWORD(typeTestAssumeVCalls); 784 KEYWORD(typeCheckedLoadVCalls); 785 KEYWORD(typeTestAssumeConstVCalls); 786 KEYWORD(typeCheckedLoadConstVCalls); 787 KEYWORD(vFuncId); 788 KEYWORD(offset); 789 KEYWORD(args); 790 KEYWORD(typeid); 791 KEYWORD(typeidCompatibleVTable); 792 KEYWORD(summary); 793 KEYWORD(typeTestRes); 794 KEYWORD(kind); 795 KEYWORD(unsat); 796 KEYWORD(byteArray); 797 KEYWORD(inline); 798 KEYWORD(single); 799 KEYWORD(allOnes); 800 KEYWORD(sizeM1BitWidth); 801 KEYWORD(alignLog2); 802 KEYWORD(sizeM1); 803 KEYWORD(bitMask); 804 KEYWORD(inlineBits); 805 KEYWORD(vcall_visibility); 806 KEYWORD(wpdResolutions); 807 KEYWORD(wpdRes); 808 KEYWORD(indir); 809 KEYWORD(singleImpl); 810 KEYWORD(branchFunnel); 811 KEYWORD(singleImplName); 812 KEYWORD(resByArg); 813 KEYWORD(byArg); 814 KEYWORD(uniformRetVal); 815 KEYWORD(uniqueRetVal); 816 KEYWORD(virtualConstProp); 817 KEYWORD(info); 818 KEYWORD(byte); 819 KEYWORD(bit); 820 KEYWORD(varFlags); 821 822 #undef KEYWORD 823 824 // Keywords for types. 825 #define TYPEKEYWORD(STR, LLVMTY) \ 826 do { \ 827 if (Keyword == STR) { \ 828 TyVal = LLVMTY; \ 829 return lltok::Type; \ 830 } \ 831 } while (false) 832 833 TYPEKEYWORD("void", Type::getVoidTy(Context)); 834 TYPEKEYWORD("half", Type::getHalfTy(Context)); 835 TYPEKEYWORD("bfloat", Type::getBFloatTy(Context)); 836 TYPEKEYWORD("float", Type::getFloatTy(Context)); 837 TYPEKEYWORD("double", Type::getDoubleTy(Context)); 838 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context)); 839 TYPEKEYWORD("fp128", Type::getFP128Ty(Context)); 840 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context)); 841 TYPEKEYWORD("label", Type::getLabelTy(Context)); 842 TYPEKEYWORD("metadata", Type::getMetadataTy(Context)); 843 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context)); 844 TYPEKEYWORD("x86_amx", Type::getX86_AMXTy(Context)); 845 TYPEKEYWORD("token", Type::getTokenTy(Context)); 846 847 #undef TYPEKEYWORD 848 849 // Keywords for instructions. 850 #define INSTKEYWORD(STR, Enum) \ 851 do { \ 852 if (Keyword == #STR) { \ 853 UIntVal = Instruction::Enum; \ 854 return lltok::kw_##STR; \ 855 } \ 856 } while (false) 857 858 INSTKEYWORD(fneg, FNeg); 859 860 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd); 861 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub); 862 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul); 863 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv); 864 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem); 865 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr); 866 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor); 867 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp); 868 869 INSTKEYWORD(phi, PHI); 870 INSTKEYWORD(call, Call); 871 INSTKEYWORD(trunc, Trunc); 872 INSTKEYWORD(zext, ZExt); 873 INSTKEYWORD(sext, SExt); 874 INSTKEYWORD(fptrunc, FPTrunc); 875 INSTKEYWORD(fpext, FPExt); 876 INSTKEYWORD(uitofp, UIToFP); 877 INSTKEYWORD(sitofp, SIToFP); 878 INSTKEYWORD(fptoui, FPToUI); 879 INSTKEYWORD(fptosi, FPToSI); 880 INSTKEYWORD(inttoptr, IntToPtr); 881 INSTKEYWORD(ptrtoint, PtrToInt); 882 INSTKEYWORD(bitcast, BitCast); 883 INSTKEYWORD(addrspacecast, AddrSpaceCast); 884 INSTKEYWORD(select, Select); 885 INSTKEYWORD(va_arg, VAArg); 886 INSTKEYWORD(ret, Ret); 887 INSTKEYWORD(br, Br); 888 INSTKEYWORD(switch, Switch); 889 INSTKEYWORD(indirectbr, IndirectBr); 890 INSTKEYWORD(invoke, Invoke); 891 INSTKEYWORD(resume, Resume); 892 INSTKEYWORD(unreachable, Unreachable); 893 INSTKEYWORD(callbr, CallBr); 894 895 INSTKEYWORD(alloca, Alloca); 896 INSTKEYWORD(load, Load); 897 INSTKEYWORD(store, Store); 898 INSTKEYWORD(cmpxchg, AtomicCmpXchg); 899 INSTKEYWORD(atomicrmw, AtomicRMW); 900 INSTKEYWORD(fence, Fence); 901 INSTKEYWORD(getelementptr, GetElementPtr); 902 903 INSTKEYWORD(extractelement, ExtractElement); 904 INSTKEYWORD(insertelement, InsertElement); 905 INSTKEYWORD(shufflevector, ShuffleVector); 906 INSTKEYWORD(extractvalue, ExtractValue); 907 INSTKEYWORD(insertvalue, InsertValue); 908 INSTKEYWORD(landingpad, LandingPad); 909 INSTKEYWORD(cleanupret, CleanupRet); 910 INSTKEYWORD(catchret, CatchRet); 911 INSTKEYWORD(catchswitch, CatchSwitch); 912 INSTKEYWORD(catchpad, CatchPad); 913 INSTKEYWORD(cleanuppad, CleanupPad); 914 915 INSTKEYWORD(freeze, Freeze); 916 917 #undef INSTKEYWORD 918 919 #define DWKEYWORD(TYPE, TOKEN) \ 920 do { \ 921 if (Keyword.startswith("DW_" #TYPE "_")) { \ 922 StrVal.assign(Keyword.begin(), Keyword.end()); \ 923 return lltok::TOKEN; \ 924 } \ 925 } while (false) 926 927 DWKEYWORD(TAG, DwarfTag); 928 DWKEYWORD(ATE, DwarfAttEncoding); 929 DWKEYWORD(VIRTUALITY, DwarfVirtuality); 930 DWKEYWORD(LANG, DwarfLang); 931 DWKEYWORD(CC, DwarfCC); 932 DWKEYWORD(OP, DwarfOp); 933 DWKEYWORD(MACINFO, DwarfMacinfo); 934 935 #undef DWKEYWORD 936 937 if (Keyword.startswith("DIFlag")) { 938 StrVal.assign(Keyword.begin(), Keyword.end()); 939 return lltok::DIFlag; 940 } 941 942 if (Keyword.startswith("DISPFlag")) { 943 StrVal.assign(Keyword.begin(), Keyword.end()); 944 return lltok::DISPFlag; 945 } 946 947 if (Keyword.startswith("CSK_")) { 948 StrVal.assign(Keyword.begin(), Keyword.end()); 949 return lltok::ChecksumKind; 950 } 951 952 if (Keyword == "NoDebug" || Keyword == "FullDebug" || 953 Keyword == "LineTablesOnly" || Keyword == "DebugDirectivesOnly") { 954 StrVal.assign(Keyword.begin(), Keyword.end()); 955 return lltok::EmissionKind; 956 } 957 958 if (Keyword == "GNU" || Keyword == "None" || Keyword == "Default") { 959 StrVal.assign(Keyword.begin(), Keyword.end()); 960 return lltok::NameTableKind; 961 } 962 963 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by 964 // the CFE to avoid forcing it to deal with 64-bit numbers. 965 if ((TokStart[0] == 'u' || TokStart[0] == 's') && 966 TokStart[1] == '0' && TokStart[2] == 'x' && 967 isxdigit(static_cast<unsigned char>(TokStart[3]))) { 968 int len = CurPtr-TokStart-3; 969 uint32_t bits = len * 4; 970 StringRef HexStr(TokStart + 3, len); 971 if (!all_of(HexStr, isxdigit)) { 972 // Bad token, return it as an error. 973 CurPtr = TokStart+3; 974 return lltok::Error; 975 } 976 APInt Tmp(bits, HexStr, 16); 977 uint32_t activeBits = Tmp.getActiveBits(); 978 if (activeBits > 0 && activeBits < bits) 979 Tmp = Tmp.trunc(activeBits); 980 APSIntVal = APSInt(Tmp, TokStart[0] == 'u'); 981 return lltok::APSInt; 982 } 983 984 // If this is "cc1234", return this as just "cc". 985 if (TokStart[0] == 'c' && TokStart[1] == 'c') { 986 CurPtr = TokStart+2; 987 return lltok::kw_cc; 988 } 989 990 // Finally, if this isn't known, return an error. 991 CurPtr = TokStart+1; 992 return lltok::Error; 993 } 994 995 /// Lex all tokens that start with a 0x prefix, knowing they match and are not 996 /// labels. 997 /// HexFPConstant 0x[0-9A-Fa-f]+ 998 /// HexFP80Constant 0xK[0-9A-Fa-f]+ 999 /// HexFP128Constant 0xL[0-9A-Fa-f]+ 1000 /// HexPPC128Constant 0xM[0-9A-Fa-f]+ 1001 /// HexHalfConstant 0xH[0-9A-Fa-f]+ 1002 /// HexBFloatConstant 0xR[0-9A-Fa-f]+ 1003 lltok::Kind LLLexer::Lex0x() { 1004 CurPtr = TokStart + 2; 1005 1006 char Kind; 1007 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H' || 1008 CurPtr[0] == 'R') { 1009 Kind = *CurPtr++; 1010 } else { 1011 Kind = 'J'; 1012 } 1013 1014 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) { 1015 // Bad token, return it as an error. 1016 CurPtr = TokStart+1; 1017 return lltok::Error; 1018 } 1019 1020 while (isxdigit(static_cast<unsigned char>(CurPtr[0]))) 1021 ++CurPtr; 1022 1023 if (Kind == 'J') { 1024 // HexFPConstant - Floating point constant represented in IEEE format as a 1025 // hexadecimal number for when exponential notation is not precise enough. 1026 // Half, BFloat, Float, and double only. 1027 APFloatVal = APFloat(APFloat::IEEEdouble(), 1028 APInt(64, HexIntToVal(TokStart + 2, CurPtr))); 1029 return lltok::APFloat; 1030 } 1031 1032 uint64_t Pair[2]; 1033 switch (Kind) { 1034 default: llvm_unreachable("Unknown kind!"); 1035 case 'K': 1036 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes) 1037 FP80HexToIntPair(TokStart+3, CurPtr, Pair); 1038 APFloatVal = APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair)); 1039 return lltok::APFloat; 1040 case 'L': 1041 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes) 1042 HexToIntPair(TokStart+3, CurPtr, Pair); 1043 APFloatVal = APFloat(APFloat::IEEEquad(), APInt(128, Pair)); 1044 return lltok::APFloat; 1045 case 'M': 1046 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes) 1047 HexToIntPair(TokStart+3, CurPtr, Pair); 1048 APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair)); 1049 return lltok::APFloat; 1050 case 'H': 1051 APFloatVal = APFloat(APFloat::IEEEhalf(), 1052 APInt(16,HexIntToVal(TokStart+3, CurPtr))); 1053 return lltok::APFloat; 1054 case 'R': 1055 // Brain floating point 1056 APFloatVal = APFloat(APFloat::BFloat(), 1057 APInt(16, HexIntToVal(TokStart + 3, CurPtr))); 1058 return lltok::APFloat; 1059 } 1060 } 1061 1062 /// Lex tokens for a label or a numeric constant, possibly starting with -. 1063 /// Label [-a-zA-Z$._0-9]+: 1064 /// NInteger -[0-9]+ 1065 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? 1066 /// PInteger [0-9]+ 1067 /// HexFPConstant 0x[0-9A-Fa-f]+ 1068 /// HexFP80Constant 0xK[0-9A-Fa-f]+ 1069 /// HexFP128Constant 0xL[0-9A-Fa-f]+ 1070 /// HexPPC128Constant 0xM[0-9A-Fa-f]+ 1071 lltok::Kind LLLexer::LexDigitOrNegative() { 1072 // If the letter after the negative is not a number, this is probably a label. 1073 if (!isdigit(static_cast<unsigned char>(TokStart[0])) && 1074 !isdigit(static_cast<unsigned char>(CurPtr[0]))) { 1075 // Okay, this is not a number after the -, it's probably a label. 1076 if (const char *End = isLabelTail(CurPtr)) { 1077 StrVal.assign(TokStart, End-1); 1078 CurPtr = End; 1079 return lltok::LabelStr; 1080 } 1081 1082 return lltok::Error; 1083 } 1084 1085 // At this point, it is either a label, int or fp constant. 1086 1087 // Skip digits, we have at least one. 1088 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr) 1089 /*empty*/; 1090 1091 // Check if this is a fully-numeric label: 1092 if (isdigit(TokStart[0]) && CurPtr[0] == ':') { 1093 uint64_t Val = atoull(TokStart, CurPtr); 1094 ++CurPtr; // Skip the colon. 1095 if ((unsigned)Val != Val) 1096 Error("invalid value number (too large)!"); 1097 UIntVal = unsigned(Val); 1098 return lltok::LabelID; 1099 } 1100 1101 // Check to see if this really is a string label, e.g. "-1:". 1102 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') { 1103 if (const char *End = isLabelTail(CurPtr)) { 1104 StrVal.assign(TokStart, End-1); 1105 CurPtr = End; 1106 return lltok::LabelStr; 1107 } 1108 } 1109 1110 // If the next character is a '.', then it is a fp value, otherwise its 1111 // integer. 1112 if (CurPtr[0] != '.') { 1113 if (TokStart[0] == '0' && TokStart[1] == 'x') 1114 return Lex0x(); 1115 APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart)); 1116 return lltok::APSInt; 1117 } 1118 1119 ++CurPtr; 1120 1121 // Skip over [0-9]*([eE][-+]?[0-9]+)? 1122 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr; 1123 1124 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') { 1125 if (isdigit(static_cast<unsigned char>(CurPtr[1])) || 1126 ((CurPtr[1] == '-' || CurPtr[1] == '+') && 1127 isdigit(static_cast<unsigned char>(CurPtr[2])))) { 1128 CurPtr += 2; 1129 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr; 1130 } 1131 } 1132 1133 APFloatVal = APFloat(APFloat::IEEEdouble(), 1134 StringRef(TokStart, CurPtr - TokStart)); 1135 return lltok::APFloat; 1136 } 1137 1138 /// Lex a floating point constant starting with +. 1139 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? 1140 lltok::Kind LLLexer::LexPositive() { 1141 // If the letter after the negative is a number, this is probably not a 1142 // label. 1143 if (!isdigit(static_cast<unsigned char>(CurPtr[0]))) 1144 return lltok::Error; 1145 1146 // Skip digits. 1147 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr) 1148 /*empty*/; 1149 1150 // At this point, we need a '.'. 1151 if (CurPtr[0] != '.') { 1152 CurPtr = TokStart+1; 1153 return lltok::Error; 1154 } 1155 1156 ++CurPtr; 1157 1158 // Skip over [0-9]*([eE][-+]?[0-9]+)? 1159 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr; 1160 1161 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') { 1162 if (isdigit(static_cast<unsigned char>(CurPtr[1])) || 1163 ((CurPtr[1] == '-' || CurPtr[1] == '+') && 1164 isdigit(static_cast<unsigned char>(CurPtr[2])))) { 1165 CurPtr += 2; 1166 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr; 1167 } 1168 } 1169 1170 APFloatVal = APFloat(APFloat::IEEEdouble(), 1171 StringRef(TokStart, CurPtr - TokStart)); 1172 return lltok::APFloat; 1173 } 1174