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(nocapture); 657 KEYWORD(noduplicate); 658 KEYWORD(nofree); 659 KEYWORD(noimplicitfloat); 660 KEYWORD(noinline); 661 KEYWORD(norecurse); 662 KEYWORD(nonlazybind); 663 KEYWORD(nomerge); 664 KEYWORD(nonnull); 665 KEYWORD(noredzone); 666 KEYWORD(noreturn); 667 KEYWORD(nosync); 668 KEYWORD(nocf_check); 669 KEYWORD(noundef); 670 KEYWORD(nounwind); 671 KEYWORD(null_pointer_is_valid); 672 KEYWORD(optforfuzzing); 673 KEYWORD(optnone); 674 KEYWORD(optsize); 675 KEYWORD(preallocated); 676 KEYWORD(readnone); 677 KEYWORD(readonly); 678 KEYWORD(returned); 679 KEYWORD(returns_twice); 680 KEYWORD(signext); 681 KEYWORD(speculatable); 682 KEYWORD(sret); 683 KEYWORD(ssp); 684 KEYWORD(sspreq); 685 KEYWORD(sspstrong); 686 KEYWORD(strictfp); 687 KEYWORD(safestack); 688 KEYWORD(shadowcallstack); 689 KEYWORD(sanitize_address); 690 KEYWORD(sanitize_hwaddress); 691 KEYWORD(sanitize_memtag); 692 KEYWORD(sanitize_thread); 693 KEYWORD(sanitize_memory); 694 KEYWORD(speculative_load_hardening); 695 KEYWORD(swifterror); 696 KEYWORD(swiftself); 697 KEYWORD(uwtable); 698 KEYWORD(willreturn); 699 KEYWORD(writeonly); 700 KEYWORD(zeroext); 701 KEYWORD(immarg); 702 KEYWORD(byref); 703 KEYWORD(mustprogress); 704 705 KEYWORD(type); 706 KEYWORD(opaque); 707 708 KEYWORD(comdat); 709 710 // Comdat types 711 KEYWORD(any); 712 KEYWORD(exactmatch); 713 KEYWORD(largest); 714 KEYWORD(noduplicates); 715 KEYWORD(samesize); 716 717 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle); 718 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge); 719 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole); 720 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une); 721 722 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax); 723 KEYWORD(umin); 724 725 KEYWORD(vscale); 726 KEYWORD(x); 727 KEYWORD(blockaddress); 728 KEYWORD(dso_local_equivalent); 729 730 // Metadata types. 731 KEYWORD(distinct); 732 733 // Use-list order directives. 734 KEYWORD(uselistorder); 735 KEYWORD(uselistorder_bb); 736 737 KEYWORD(personality); 738 KEYWORD(cleanup); 739 KEYWORD(catch); 740 KEYWORD(filter); 741 742 // Summary index keywords. 743 KEYWORD(path); 744 KEYWORD(hash); 745 KEYWORD(gv); 746 KEYWORD(guid); 747 KEYWORD(name); 748 KEYWORD(summaries); 749 KEYWORD(flags); 750 KEYWORD(blockcount); 751 KEYWORD(linkage); 752 KEYWORD(notEligibleToImport); 753 KEYWORD(live); 754 KEYWORD(dsoLocal); 755 KEYWORD(canAutoHide); 756 KEYWORD(function); 757 KEYWORD(insts); 758 KEYWORD(funcFlags); 759 KEYWORD(readNone); 760 KEYWORD(readOnly); 761 KEYWORD(noRecurse); 762 KEYWORD(returnDoesNotAlias); 763 KEYWORD(noInline); 764 KEYWORD(alwaysInline); 765 KEYWORD(calls); 766 KEYWORD(callee); 767 KEYWORD(params); 768 KEYWORD(param); 769 KEYWORD(hotness); 770 KEYWORD(unknown); 771 KEYWORD(hot); 772 KEYWORD(critical); 773 KEYWORD(relbf); 774 KEYWORD(variable); 775 KEYWORD(vTableFuncs); 776 KEYWORD(virtFunc); 777 KEYWORD(aliasee); 778 KEYWORD(refs); 779 KEYWORD(typeIdInfo); 780 KEYWORD(typeTests); 781 KEYWORD(typeTestAssumeVCalls); 782 KEYWORD(typeCheckedLoadVCalls); 783 KEYWORD(typeTestAssumeConstVCalls); 784 KEYWORD(typeCheckedLoadConstVCalls); 785 KEYWORD(vFuncId); 786 KEYWORD(offset); 787 KEYWORD(args); 788 KEYWORD(typeid); 789 KEYWORD(typeidCompatibleVTable); 790 KEYWORD(summary); 791 KEYWORD(typeTestRes); 792 KEYWORD(kind); 793 KEYWORD(unsat); 794 KEYWORD(byteArray); 795 KEYWORD(inline); 796 KEYWORD(single); 797 KEYWORD(allOnes); 798 KEYWORD(sizeM1BitWidth); 799 KEYWORD(alignLog2); 800 KEYWORD(sizeM1); 801 KEYWORD(bitMask); 802 KEYWORD(inlineBits); 803 KEYWORD(vcall_visibility); 804 KEYWORD(wpdResolutions); 805 KEYWORD(wpdRes); 806 KEYWORD(indir); 807 KEYWORD(singleImpl); 808 KEYWORD(branchFunnel); 809 KEYWORD(singleImplName); 810 KEYWORD(resByArg); 811 KEYWORD(byArg); 812 KEYWORD(uniformRetVal); 813 KEYWORD(uniqueRetVal); 814 KEYWORD(virtualConstProp); 815 KEYWORD(info); 816 KEYWORD(byte); 817 KEYWORD(bit); 818 KEYWORD(varFlags); 819 820 #undef KEYWORD 821 822 // Keywords for types. 823 #define TYPEKEYWORD(STR, LLVMTY) \ 824 do { \ 825 if (Keyword == STR) { \ 826 TyVal = LLVMTY; \ 827 return lltok::Type; \ 828 } \ 829 } while (false) 830 831 TYPEKEYWORD("void", Type::getVoidTy(Context)); 832 TYPEKEYWORD("half", Type::getHalfTy(Context)); 833 TYPEKEYWORD("bfloat", Type::getBFloatTy(Context)); 834 TYPEKEYWORD("float", Type::getFloatTy(Context)); 835 TYPEKEYWORD("double", Type::getDoubleTy(Context)); 836 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context)); 837 TYPEKEYWORD("fp128", Type::getFP128Ty(Context)); 838 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context)); 839 TYPEKEYWORD("label", Type::getLabelTy(Context)); 840 TYPEKEYWORD("metadata", Type::getMetadataTy(Context)); 841 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context)); 842 TYPEKEYWORD("token", Type::getTokenTy(Context)); 843 844 #undef TYPEKEYWORD 845 846 // Keywords for instructions. 847 #define INSTKEYWORD(STR, Enum) \ 848 do { \ 849 if (Keyword == #STR) { \ 850 UIntVal = Instruction::Enum; \ 851 return lltok::kw_##STR; \ 852 } \ 853 } while (false) 854 855 INSTKEYWORD(fneg, FNeg); 856 857 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd); 858 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub); 859 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul); 860 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv); 861 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem); 862 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr); 863 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor); 864 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp); 865 866 INSTKEYWORD(phi, PHI); 867 INSTKEYWORD(call, Call); 868 INSTKEYWORD(trunc, Trunc); 869 INSTKEYWORD(zext, ZExt); 870 INSTKEYWORD(sext, SExt); 871 INSTKEYWORD(fptrunc, FPTrunc); 872 INSTKEYWORD(fpext, FPExt); 873 INSTKEYWORD(uitofp, UIToFP); 874 INSTKEYWORD(sitofp, SIToFP); 875 INSTKEYWORD(fptoui, FPToUI); 876 INSTKEYWORD(fptosi, FPToSI); 877 INSTKEYWORD(inttoptr, IntToPtr); 878 INSTKEYWORD(ptrtoint, PtrToInt); 879 INSTKEYWORD(bitcast, BitCast); 880 INSTKEYWORD(addrspacecast, AddrSpaceCast); 881 INSTKEYWORD(select, Select); 882 INSTKEYWORD(va_arg, VAArg); 883 INSTKEYWORD(ret, Ret); 884 INSTKEYWORD(br, Br); 885 INSTKEYWORD(switch, Switch); 886 INSTKEYWORD(indirectbr, IndirectBr); 887 INSTKEYWORD(invoke, Invoke); 888 INSTKEYWORD(resume, Resume); 889 INSTKEYWORD(unreachable, Unreachable); 890 INSTKEYWORD(callbr, CallBr); 891 892 INSTKEYWORD(alloca, Alloca); 893 INSTKEYWORD(load, Load); 894 INSTKEYWORD(store, Store); 895 INSTKEYWORD(cmpxchg, AtomicCmpXchg); 896 INSTKEYWORD(atomicrmw, AtomicRMW); 897 INSTKEYWORD(fence, Fence); 898 INSTKEYWORD(getelementptr, GetElementPtr); 899 900 INSTKEYWORD(extractelement, ExtractElement); 901 INSTKEYWORD(insertelement, InsertElement); 902 INSTKEYWORD(shufflevector, ShuffleVector); 903 INSTKEYWORD(extractvalue, ExtractValue); 904 INSTKEYWORD(insertvalue, InsertValue); 905 INSTKEYWORD(landingpad, LandingPad); 906 INSTKEYWORD(cleanupret, CleanupRet); 907 INSTKEYWORD(catchret, CatchRet); 908 INSTKEYWORD(catchswitch, CatchSwitch); 909 INSTKEYWORD(catchpad, CatchPad); 910 INSTKEYWORD(cleanuppad, CleanupPad); 911 912 INSTKEYWORD(freeze, Freeze); 913 914 #undef INSTKEYWORD 915 916 #define DWKEYWORD(TYPE, TOKEN) \ 917 do { \ 918 if (Keyword.startswith("DW_" #TYPE "_")) { \ 919 StrVal.assign(Keyword.begin(), Keyword.end()); \ 920 return lltok::TOKEN; \ 921 } \ 922 } while (false) 923 924 DWKEYWORD(TAG, DwarfTag); 925 DWKEYWORD(ATE, DwarfAttEncoding); 926 DWKEYWORD(VIRTUALITY, DwarfVirtuality); 927 DWKEYWORD(LANG, DwarfLang); 928 DWKEYWORD(CC, DwarfCC); 929 DWKEYWORD(OP, DwarfOp); 930 DWKEYWORD(MACINFO, DwarfMacinfo); 931 932 #undef DWKEYWORD 933 934 if (Keyword.startswith("DIFlag")) { 935 StrVal.assign(Keyword.begin(), Keyword.end()); 936 return lltok::DIFlag; 937 } 938 939 if (Keyword.startswith("DISPFlag")) { 940 StrVal.assign(Keyword.begin(), Keyword.end()); 941 return lltok::DISPFlag; 942 } 943 944 if (Keyword.startswith("CSK_")) { 945 StrVal.assign(Keyword.begin(), Keyword.end()); 946 return lltok::ChecksumKind; 947 } 948 949 if (Keyword == "NoDebug" || Keyword == "FullDebug" || 950 Keyword == "LineTablesOnly" || Keyword == "DebugDirectivesOnly") { 951 StrVal.assign(Keyword.begin(), Keyword.end()); 952 return lltok::EmissionKind; 953 } 954 955 if (Keyword == "GNU" || Keyword == "None" || Keyword == "Default") { 956 StrVal.assign(Keyword.begin(), Keyword.end()); 957 return lltok::NameTableKind; 958 } 959 960 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by 961 // the CFE to avoid forcing it to deal with 64-bit numbers. 962 if ((TokStart[0] == 'u' || TokStart[0] == 's') && 963 TokStart[1] == '0' && TokStart[2] == 'x' && 964 isxdigit(static_cast<unsigned char>(TokStart[3]))) { 965 int len = CurPtr-TokStart-3; 966 uint32_t bits = len * 4; 967 StringRef HexStr(TokStart + 3, len); 968 if (!all_of(HexStr, isxdigit)) { 969 // Bad token, return it as an error. 970 CurPtr = TokStart+3; 971 return lltok::Error; 972 } 973 APInt Tmp(bits, HexStr, 16); 974 uint32_t activeBits = Tmp.getActiveBits(); 975 if (activeBits > 0 && activeBits < bits) 976 Tmp = Tmp.trunc(activeBits); 977 APSIntVal = APSInt(Tmp, TokStart[0] == 'u'); 978 return lltok::APSInt; 979 } 980 981 // If this is "cc1234", return this as just "cc". 982 if (TokStart[0] == 'c' && TokStart[1] == 'c') { 983 CurPtr = TokStart+2; 984 return lltok::kw_cc; 985 } 986 987 // Finally, if this isn't known, return an error. 988 CurPtr = TokStart+1; 989 return lltok::Error; 990 } 991 992 /// Lex all tokens that start with a 0x prefix, knowing they match and are not 993 /// labels. 994 /// HexFPConstant 0x[0-9A-Fa-f]+ 995 /// HexFP80Constant 0xK[0-9A-Fa-f]+ 996 /// HexFP128Constant 0xL[0-9A-Fa-f]+ 997 /// HexPPC128Constant 0xM[0-9A-Fa-f]+ 998 /// HexHalfConstant 0xH[0-9A-Fa-f]+ 999 /// HexBFloatConstant 0xR[0-9A-Fa-f]+ 1000 lltok::Kind LLLexer::Lex0x() { 1001 CurPtr = TokStart + 2; 1002 1003 char Kind; 1004 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H' || 1005 CurPtr[0] == 'R') { 1006 Kind = *CurPtr++; 1007 } else { 1008 Kind = 'J'; 1009 } 1010 1011 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) { 1012 // Bad token, return it as an error. 1013 CurPtr = TokStart+1; 1014 return lltok::Error; 1015 } 1016 1017 while (isxdigit(static_cast<unsigned char>(CurPtr[0]))) 1018 ++CurPtr; 1019 1020 if (Kind == 'J') { 1021 // HexFPConstant - Floating point constant represented in IEEE format as a 1022 // hexadecimal number for when exponential notation is not precise enough. 1023 // Half, BFloat, Float, and double only. 1024 APFloatVal = APFloat(APFloat::IEEEdouble(), 1025 APInt(64, HexIntToVal(TokStart + 2, CurPtr))); 1026 return lltok::APFloat; 1027 } 1028 1029 uint64_t Pair[2]; 1030 switch (Kind) { 1031 default: llvm_unreachable("Unknown kind!"); 1032 case 'K': 1033 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes) 1034 FP80HexToIntPair(TokStart+3, CurPtr, Pair); 1035 APFloatVal = APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair)); 1036 return lltok::APFloat; 1037 case 'L': 1038 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes) 1039 HexToIntPair(TokStart+3, CurPtr, Pair); 1040 APFloatVal = APFloat(APFloat::IEEEquad(), APInt(128, Pair)); 1041 return lltok::APFloat; 1042 case 'M': 1043 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes) 1044 HexToIntPair(TokStart+3, CurPtr, Pair); 1045 APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair)); 1046 return lltok::APFloat; 1047 case 'H': 1048 APFloatVal = APFloat(APFloat::IEEEhalf(), 1049 APInt(16,HexIntToVal(TokStart+3, CurPtr))); 1050 return lltok::APFloat; 1051 case 'R': 1052 // Brain floating point 1053 APFloatVal = APFloat(APFloat::BFloat(), 1054 APInt(16, HexIntToVal(TokStart + 3, CurPtr))); 1055 return lltok::APFloat; 1056 } 1057 } 1058 1059 /// Lex tokens for a label or a numeric constant, possibly starting with -. 1060 /// Label [-a-zA-Z$._0-9]+: 1061 /// NInteger -[0-9]+ 1062 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? 1063 /// PInteger [0-9]+ 1064 /// HexFPConstant 0x[0-9A-Fa-f]+ 1065 /// HexFP80Constant 0xK[0-9A-Fa-f]+ 1066 /// HexFP128Constant 0xL[0-9A-Fa-f]+ 1067 /// HexPPC128Constant 0xM[0-9A-Fa-f]+ 1068 lltok::Kind LLLexer::LexDigitOrNegative() { 1069 // If the letter after the negative is not a number, this is probably a label. 1070 if (!isdigit(static_cast<unsigned char>(TokStart[0])) && 1071 !isdigit(static_cast<unsigned char>(CurPtr[0]))) { 1072 // Okay, this is not a number after the -, it's probably a label. 1073 if (const char *End = isLabelTail(CurPtr)) { 1074 StrVal.assign(TokStart, End-1); 1075 CurPtr = End; 1076 return lltok::LabelStr; 1077 } 1078 1079 return lltok::Error; 1080 } 1081 1082 // At this point, it is either a label, int or fp constant. 1083 1084 // Skip digits, we have at least one. 1085 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr) 1086 /*empty*/; 1087 1088 // Check if this is a fully-numeric label: 1089 if (isdigit(TokStart[0]) && CurPtr[0] == ':') { 1090 uint64_t Val = atoull(TokStart, CurPtr); 1091 ++CurPtr; // Skip the colon. 1092 if ((unsigned)Val != Val) 1093 Error("invalid value number (too large)!"); 1094 UIntVal = unsigned(Val); 1095 return lltok::LabelID; 1096 } 1097 1098 // Check to see if this really is a string label, e.g. "-1:". 1099 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') { 1100 if (const char *End = isLabelTail(CurPtr)) { 1101 StrVal.assign(TokStart, End-1); 1102 CurPtr = End; 1103 return lltok::LabelStr; 1104 } 1105 } 1106 1107 // If the next character is a '.', then it is a fp value, otherwise its 1108 // integer. 1109 if (CurPtr[0] != '.') { 1110 if (TokStart[0] == '0' && TokStart[1] == 'x') 1111 return Lex0x(); 1112 APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart)); 1113 return lltok::APSInt; 1114 } 1115 1116 ++CurPtr; 1117 1118 // Skip over [0-9]*([eE][-+]?[0-9]+)? 1119 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr; 1120 1121 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') { 1122 if (isdigit(static_cast<unsigned char>(CurPtr[1])) || 1123 ((CurPtr[1] == '-' || CurPtr[1] == '+') && 1124 isdigit(static_cast<unsigned char>(CurPtr[2])))) { 1125 CurPtr += 2; 1126 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr; 1127 } 1128 } 1129 1130 APFloatVal = APFloat(APFloat::IEEEdouble(), 1131 StringRef(TokStart, CurPtr - TokStart)); 1132 return lltok::APFloat; 1133 } 1134 1135 /// Lex a floating point constant starting with +. 1136 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? 1137 lltok::Kind LLLexer::LexPositive() { 1138 // If the letter after the negative is a number, this is probably not a 1139 // label. 1140 if (!isdigit(static_cast<unsigned char>(CurPtr[0]))) 1141 return lltok::Error; 1142 1143 // Skip digits. 1144 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr) 1145 /*empty*/; 1146 1147 // At this point, we need a '.'. 1148 if (CurPtr[0] != '.') { 1149 CurPtr = TokStart+1; 1150 return lltok::Error; 1151 } 1152 1153 ++CurPtr; 1154 1155 // Skip over [0-9]*([eE][-+]?[0-9]+)? 1156 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr; 1157 1158 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') { 1159 if (isdigit(static_cast<unsigned char>(CurPtr[1])) || 1160 ((CurPtr[1] == '-' || CurPtr[1] == '+') && 1161 isdigit(static_cast<unsigned char>(CurPtr[2])))) { 1162 CurPtr += 2; 1163 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr; 1164 } 1165 } 1166 1167 APFloatVal = APFloat(APFloat::IEEEdouble(), 1168 StringRef(TokStart, CurPtr - TokStart)); 1169 return lltok::APFloat; 1170 } 1171