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