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