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