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(webkit_jscc); 589 KEYWORD(swiftcc); 590 KEYWORD(anyregcc); 591 KEYWORD(preserve_mostcc); 592 KEYWORD(preserve_allcc); 593 KEYWORD(ghccc); 594 KEYWORD(x86_intrcc); 595 KEYWORD(hhvmcc); 596 KEYWORD(hhvm_ccc); 597 KEYWORD(cxx_fast_tlscc); 598 KEYWORD(amdgpu_vs); 599 KEYWORD(amdgpu_gs); 600 KEYWORD(amdgpu_ps); 601 KEYWORD(amdgpu_cs); 602 KEYWORD(amdgpu_kernel); 603 604 KEYWORD(cc); 605 KEYWORD(c); 606 607 KEYWORD(attributes); 608 609 KEYWORD(alwaysinline); 610 KEYWORD(allocsize); 611 KEYWORD(argmemonly); 612 KEYWORD(builtin); 613 KEYWORD(byval); 614 KEYWORD(inalloca); 615 KEYWORD(cold); 616 KEYWORD(convergent); 617 KEYWORD(dereferenceable); 618 KEYWORD(dereferenceable_or_null); 619 KEYWORD(inaccessiblememonly); 620 KEYWORD(inaccessiblemem_or_argmemonly); 621 KEYWORD(inlinehint); 622 KEYWORD(inreg); 623 KEYWORD(jumptable); 624 KEYWORD(minsize); 625 KEYWORD(naked); 626 KEYWORD(nest); 627 KEYWORD(noalias); 628 KEYWORD(nobuiltin); 629 KEYWORD(nocapture); 630 KEYWORD(noduplicate); 631 KEYWORD(noimplicitfloat); 632 KEYWORD(noinline); 633 KEYWORD(norecurse); 634 KEYWORD(nonlazybind); 635 KEYWORD(nonnull); 636 KEYWORD(noredzone); 637 KEYWORD(noreturn); 638 KEYWORD(nounwind); 639 KEYWORD(optnone); 640 KEYWORD(optsize); 641 KEYWORD(readnone); 642 KEYWORD(readonly); 643 KEYWORD(returned); 644 KEYWORD(returns_twice); 645 KEYWORD(signext); 646 KEYWORD(sret); 647 KEYWORD(ssp); 648 KEYWORD(sspreq); 649 KEYWORD(sspstrong); 650 KEYWORD(safestack); 651 KEYWORD(sanitize_address); 652 KEYWORD(sanitize_thread); 653 KEYWORD(sanitize_memory); 654 KEYWORD(swifterror); 655 KEYWORD(swiftself); 656 KEYWORD(uwtable); 657 KEYWORD(writeonly); 658 KEYWORD(zeroext); 659 660 KEYWORD(type); 661 KEYWORD(opaque); 662 663 KEYWORD(comdat); 664 665 // Comdat types 666 KEYWORD(any); 667 KEYWORD(exactmatch); 668 KEYWORD(largest); 669 KEYWORD(noduplicates); 670 KEYWORD(samesize); 671 672 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle); 673 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge); 674 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole); 675 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une); 676 677 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax); 678 KEYWORD(umin); 679 680 KEYWORD(x); 681 KEYWORD(blockaddress); 682 683 // Metadata types. 684 KEYWORD(distinct); 685 686 // Use-list order directives. 687 KEYWORD(uselistorder); 688 KEYWORD(uselistorder_bb); 689 690 KEYWORD(personality); 691 KEYWORD(cleanup); 692 KEYWORD(catch); 693 KEYWORD(filter); 694 695 #undef KEYWORD 696 697 // Keywords for types. 698 #define TYPEKEYWORD(STR, LLVMTY) \ 699 do { \ 700 if (Keyword == STR) { \ 701 TyVal = LLVMTY; \ 702 return lltok::Type; \ 703 } \ 704 } while (false) 705 706 TYPEKEYWORD("void", Type::getVoidTy(Context)); 707 TYPEKEYWORD("half", Type::getHalfTy(Context)); 708 TYPEKEYWORD("float", Type::getFloatTy(Context)); 709 TYPEKEYWORD("double", Type::getDoubleTy(Context)); 710 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context)); 711 TYPEKEYWORD("fp128", Type::getFP128Ty(Context)); 712 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context)); 713 TYPEKEYWORD("label", Type::getLabelTy(Context)); 714 TYPEKEYWORD("metadata", Type::getMetadataTy(Context)); 715 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context)); 716 TYPEKEYWORD("token", Type::getTokenTy(Context)); 717 718 #undef TYPEKEYWORD 719 720 // Keywords for instructions. 721 #define INSTKEYWORD(STR, Enum) \ 722 do { \ 723 if (Keyword == #STR) { \ 724 UIntVal = Instruction::Enum; \ 725 return lltok::kw_##STR; \ 726 } \ 727 } while (false) 728 729 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd); 730 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub); 731 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul); 732 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv); 733 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem); 734 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr); 735 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor); 736 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp); 737 738 INSTKEYWORD(phi, PHI); 739 INSTKEYWORD(call, Call); 740 INSTKEYWORD(trunc, Trunc); 741 INSTKEYWORD(zext, ZExt); 742 INSTKEYWORD(sext, SExt); 743 INSTKEYWORD(fptrunc, FPTrunc); 744 INSTKEYWORD(fpext, FPExt); 745 INSTKEYWORD(uitofp, UIToFP); 746 INSTKEYWORD(sitofp, SIToFP); 747 INSTKEYWORD(fptoui, FPToUI); 748 INSTKEYWORD(fptosi, FPToSI); 749 INSTKEYWORD(inttoptr, IntToPtr); 750 INSTKEYWORD(ptrtoint, PtrToInt); 751 INSTKEYWORD(bitcast, BitCast); 752 INSTKEYWORD(addrspacecast, AddrSpaceCast); 753 INSTKEYWORD(select, Select); 754 INSTKEYWORD(va_arg, VAArg); 755 INSTKEYWORD(ret, Ret); 756 INSTKEYWORD(br, Br); 757 INSTKEYWORD(switch, Switch); 758 INSTKEYWORD(indirectbr, IndirectBr); 759 INSTKEYWORD(invoke, Invoke); 760 INSTKEYWORD(resume, Resume); 761 INSTKEYWORD(unreachable, Unreachable); 762 763 INSTKEYWORD(alloca, Alloca); 764 INSTKEYWORD(load, Load); 765 INSTKEYWORD(store, Store); 766 INSTKEYWORD(cmpxchg, AtomicCmpXchg); 767 INSTKEYWORD(atomicrmw, AtomicRMW); 768 INSTKEYWORD(fence, Fence); 769 INSTKEYWORD(getelementptr, GetElementPtr); 770 771 INSTKEYWORD(extractelement, ExtractElement); 772 INSTKEYWORD(insertelement, InsertElement); 773 INSTKEYWORD(shufflevector, ShuffleVector); 774 INSTKEYWORD(extractvalue, ExtractValue); 775 INSTKEYWORD(insertvalue, InsertValue); 776 INSTKEYWORD(landingpad, LandingPad); 777 INSTKEYWORD(cleanupret, CleanupRet); 778 INSTKEYWORD(catchret, CatchRet); 779 INSTKEYWORD(catchswitch, CatchSwitch); 780 INSTKEYWORD(catchpad, CatchPad); 781 INSTKEYWORD(cleanuppad, CleanupPad); 782 783 #undef INSTKEYWORD 784 785 #define DWKEYWORD(TYPE, TOKEN) \ 786 do { \ 787 if (Keyword.startswith("DW_" #TYPE "_")) { \ 788 StrVal.assign(Keyword.begin(), Keyword.end()); \ 789 return lltok::TOKEN; \ 790 } \ 791 } while (false) 792 793 DWKEYWORD(TAG, DwarfTag); 794 DWKEYWORD(ATE, DwarfAttEncoding); 795 DWKEYWORD(VIRTUALITY, DwarfVirtuality); 796 DWKEYWORD(LANG, DwarfLang); 797 DWKEYWORD(CC, DwarfCC); 798 DWKEYWORD(OP, DwarfOp); 799 DWKEYWORD(MACINFO, DwarfMacinfo); 800 801 #undef DWKEYWORD 802 803 if (Keyword.startswith("DIFlag")) { 804 StrVal.assign(Keyword.begin(), Keyword.end()); 805 return lltok::DIFlag; 806 } 807 if (Keyword == "NoDebug" || Keyword == "FullDebug" || 808 Keyword == "LineTablesOnly") { 809 StrVal.assign(Keyword.begin(), Keyword.end()); 810 return lltok::EmissionKind; 811 } 812 813 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by 814 // the CFE to avoid forcing it to deal with 64-bit numbers. 815 if ((TokStart[0] == 'u' || TokStart[0] == 's') && 816 TokStart[1] == '0' && TokStart[2] == 'x' && 817 isxdigit(static_cast<unsigned char>(TokStart[3]))) { 818 int len = CurPtr-TokStart-3; 819 uint32_t bits = len * 4; 820 StringRef HexStr(TokStart + 3, len); 821 if (!all_of(HexStr, isxdigit)) { 822 // Bad token, return it as an error. 823 CurPtr = TokStart+3; 824 return lltok::Error; 825 } 826 APInt Tmp(bits, HexStr, 16); 827 uint32_t activeBits = Tmp.getActiveBits(); 828 if (activeBits > 0 && activeBits < bits) 829 Tmp = Tmp.trunc(activeBits); 830 APSIntVal = APSInt(Tmp, TokStart[0] == 'u'); 831 return lltok::APSInt; 832 } 833 834 // If this is "cc1234", return this as just "cc". 835 if (TokStart[0] == 'c' && TokStart[1] == 'c') { 836 CurPtr = TokStart+2; 837 return lltok::kw_cc; 838 } 839 840 // Finally, if this isn't known, return an error. 841 CurPtr = TokStart+1; 842 return lltok::Error; 843 } 844 845 /// Lex all tokens that start with a 0x prefix, knowing they match and are not 846 /// labels. 847 /// HexFPConstant 0x[0-9A-Fa-f]+ 848 /// HexFP80Constant 0xK[0-9A-Fa-f]+ 849 /// HexFP128Constant 0xL[0-9A-Fa-f]+ 850 /// HexPPC128Constant 0xM[0-9A-Fa-f]+ 851 /// HexHalfConstant 0xH[0-9A-Fa-f]+ 852 lltok::Kind LLLexer::Lex0x() { 853 CurPtr = TokStart + 2; 854 855 char Kind; 856 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') { 857 Kind = *CurPtr++; 858 } else { 859 Kind = 'J'; 860 } 861 862 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) { 863 // Bad token, return it as an error. 864 CurPtr = TokStart+1; 865 return lltok::Error; 866 } 867 868 while (isxdigit(static_cast<unsigned char>(CurPtr[0]))) 869 ++CurPtr; 870 871 if (Kind == 'J') { 872 // HexFPConstant - Floating point constant represented in IEEE format as a 873 // hexadecimal number for when exponential notation is not precise enough. 874 // Half, Float, and double only. 875 APFloatVal = APFloat(APFloat::IEEEdouble, 876 APInt(64, HexIntToVal(TokStart + 2, CurPtr))); 877 return lltok::APFloat; 878 } 879 880 uint64_t Pair[2]; 881 switch (Kind) { 882 default: llvm_unreachable("Unknown kind!"); 883 case 'K': 884 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes) 885 FP80HexToIntPair(TokStart+3, CurPtr, Pair); 886 APFloatVal = APFloat(APFloat::x87DoubleExtended, APInt(80, Pair)); 887 return lltok::APFloat; 888 case 'L': 889 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes) 890 HexToIntPair(TokStart+3, CurPtr, Pair); 891 APFloatVal = APFloat(APFloat::IEEEquad, APInt(128, Pair)); 892 return lltok::APFloat; 893 case 'M': 894 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes) 895 HexToIntPair(TokStart+3, CurPtr, Pair); 896 APFloatVal = APFloat(APFloat::PPCDoubleDouble, APInt(128, Pair)); 897 return lltok::APFloat; 898 case 'H': 899 APFloatVal = APFloat(APFloat::IEEEhalf, 900 APInt(16,HexIntToVal(TokStart+3, CurPtr))); 901 return lltok::APFloat; 902 } 903 } 904 905 /// Lex tokens for a label or a numeric constant, possibly starting with -. 906 /// Label [-a-zA-Z$._0-9]+: 907 /// NInteger -[0-9]+ 908 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? 909 /// PInteger [0-9]+ 910 /// HexFPConstant 0x[0-9A-Fa-f]+ 911 /// HexFP80Constant 0xK[0-9A-Fa-f]+ 912 /// HexFP128Constant 0xL[0-9A-Fa-f]+ 913 /// HexPPC128Constant 0xM[0-9A-Fa-f]+ 914 lltok::Kind LLLexer::LexDigitOrNegative() { 915 // If the letter after the negative is not a number, this is probably a label. 916 if (!isdigit(static_cast<unsigned char>(TokStart[0])) && 917 !isdigit(static_cast<unsigned char>(CurPtr[0]))) { 918 // Okay, this is not a number after the -, it's probably a label. 919 if (const char *End = isLabelTail(CurPtr)) { 920 StrVal.assign(TokStart, End-1); 921 CurPtr = End; 922 return lltok::LabelStr; 923 } 924 925 return lltok::Error; 926 } 927 928 // At this point, it is either a label, int or fp constant. 929 930 // Skip digits, we have at least one. 931 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr) 932 /*empty*/; 933 934 // Check to see if this really is a label afterall, e.g. "-1:". 935 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') { 936 if (const char *End = isLabelTail(CurPtr)) { 937 StrVal.assign(TokStart, End-1); 938 CurPtr = End; 939 return lltok::LabelStr; 940 } 941 } 942 943 // If the next character is a '.', then it is a fp value, otherwise its 944 // integer. 945 if (CurPtr[0] != '.') { 946 if (TokStart[0] == '0' && TokStart[1] == 'x') 947 return Lex0x(); 948 APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart)); 949 return lltok::APSInt; 950 } 951 952 ++CurPtr; 953 954 // Skip over [0-9]*([eE][-+]?[0-9]+)? 955 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr; 956 957 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') { 958 if (isdigit(static_cast<unsigned char>(CurPtr[1])) || 959 ((CurPtr[1] == '-' || CurPtr[1] == '+') && 960 isdigit(static_cast<unsigned char>(CurPtr[2])))) { 961 CurPtr += 2; 962 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr; 963 } 964 } 965 966 APFloatVal = APFloat(APFloat::IEEEdouble, 967 StringRef(TokStart, CurPtr - TokStart)); 968 return lltok::APFloat; 969 } 970 971 /// Lex a floating point constant starting with +. 972 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? 973 lltok::Kind LLLexer::LexPositive() { 974 // If the letter after the negative is a number, this is probably not a 975 // label. 976 if (!isdigit(static_cast<unsigned char>(CurPtr[0]))) 977 return lltok::Error; 978 979 // Skip digits. 980 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr) 981 /*empty*/; 982 983 // At this point, we need a '.'. 984 if (CurPtr[0] != '.') { 985 CurPtr = TokStart+1; 986 return lltok::Error; 987 } 988 989 ++CurPtr; 990 991 // Skip over [0-9]*([eE][-+]?[0-9]+)? 992 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr; 993 994 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') { 995 if (isdigit(static_cast<unsigned char>(CurPtr[1])) || 996 ((CurPtr[1] == '-' || CurPtr[1] == '+') && 997 isdigit(static_cast<unsigned char>(CurPtr[2])))) { 998 CurPtr += 2; 999 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr; 1000 } 1001 } 1002 1003 APFloatVal = APFloat(APFloat::IEEEdouble, 1004 StringRef(TokStart, CurPtr - TokStart)); 1005 return lltok::APFloat; 1006 } 1007