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