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