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> 25 using namespace llvm; 26 27 bool 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 // 40 uint64_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 54 uint64_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 75 void 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. 106 void 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. 137 static 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]. 164 static 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. 170 static 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 184 LLLexer::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 190 std::string LLLexer::getFilename() const { 191 return CurBuf->getBufferIdentifier(); 192 } 193 194 int 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 211 lltok::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 275 void 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]+ 286 lltok::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]+ 338 lltok::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 "[^"]*" 388 lltok::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 412 static 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]+ 429 lltok::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 595 INSTKEYWORD(phi, PHI); 596 INSTKEYWORD(call, Call); 597 INSTKEYWORD(trunc, Trunc); 598 INSTKEYWORD(zext, ZExt); 599 INSTKEYWORD(sext, SExt); 600 INSTKEYWORD(fptrunc, FPTrunc); 601 INSTKEYWORD(fpext, FPExt); 602 INSTKEYWORD(uitofp, UIToFP); 603 INSTKEYWORD(sitofp, SIToFP); 604 INSTKEYWORD(fptoui, FPToUI); 605 INSTKEYWORD(fptosi, FPToSI); 606 INSTKEYWORD(inttoptr, IntToPtr); 607 INSTKEYWORD(ptrtoint, PtrToInt); 608 INSTKEYWORD(bitcast, BitCast); 609 INSTKEYWORD(select, Select); 610 INSTKEYWORD(va_arg, VAArg); 611 INSTKEYWORD(ret, Ret); 612 INSTKEYWORD(br, Br); 613 INSTKEYWORD(switch, Switch); 614 INSTKEYWORD(invoke, Invoke); 615 INSTKEYWORD(unwind, Unwind); 616 INSTKEYWORD(unreachable, Unreachable); 617 618 INSTKEYWORD(malloc, Malloc); 619 INSTKEYWORD(alloca, Alloca); 620 INSTKEYWORD(free, Free); 621 INSTKEYWORD(load, Load); 622 INSTKEYWORD(store, Store); 623 INSTKEYWORD(getelementptr, GetElementPtr); 624 625 INSTKEYWORD(extractelement, ExtractElement); 626 INSTKEYWORD(insertelement, InsertElement); 627 INSTKEYWORD(shufflevector, ShuffleVector); 628 INSTKEYWORD(getresult, ExtractValue); 629 INSTKEYWORD(extractvalue, ExtractValue); 630 INSTKEYWORD(insertvalue, InsertValue); 631 #undef INSTKEYWORD 632 633 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by 634 // the CFE to avoid forcing it to deal with 64-bit numbers. 635 if ((TokStart[0] == 'u' || TokStart[0] == 's') && 636 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) { 637 int len = CurPtr-TokStart-3; 638 uint32_t bits = len * 4; 639 APInt Tmp(bits, TokStart+3, len, 16); 640 uint32_t activeBits = Tmp.getActiveBits(); 641 if (activeBits > 0 && activeBits < bits) 642 Tmp.trunc(activeBits); 643 APSIntVal = APSInt(Tmp, TokStart[0] == 'u'); 644 return lltok::APSInt; 645 } 646 647 // If this is "cc1234", return this as just "cc". 648 if (TokStart[0] == 'c' && TokStart[1] == 'c') { 649 CurPtr = TokStart+2; 650 return lltok::kw_cc; 651 } 652 653 // If this starts with "call", return it as CALL. This is to support old 654 // broken .ll files. FIXME: remove this with LLVM 3.0. 655 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) { 656 CurPtr = TokStart+4; 657 UIntVal = Instruction::Call; 658 return lltok::kw_call; 659 } 660 661 // Finally, if this isn't known, return an error. 662 CurPtr = TokStart+1; 663 return lltok::Error; 664 } 665 666 667 /// Lex0x: Handle productions that start with 0x, knowing that it matches and 668 /// that this is not a label: 669 /// HexFPConstant 0x[0-9A-Fa-f]+ 670 /// HexFP80Constant 0xK[0-9A-Fa-f]+ 671 /// HexFP128Constant 0xL[0-9A-Fa-f]+ 672 /// HexPPC128Constant 0xM[0-9A-Fa-f]+ 673 lltok::Kind LLLexer::Lex0x() { 674 CurPtr = TokStart + 2; 675 676 char Kind; 677 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') { 678 Kind = *CurPtr++; 679 } else { 680 Kind = 'J'; 681 } 682 683 if (!isxdigit(CurPtr[0])) { 684 // Bad token, return it as an error. 685 CurPtr = TokStart+1; 686 return lltok::Error; 687 } 688 689 while (isxdigit(CurPtr[0])) 690 ++CurPtr; 691 692 if (Kind == 'J') { 693 // HexFPConstant - Floating point constant represented in IEEE format as a 694 // hexadecimal number for when exponential notation is not precise enough. 695 // Float and double only. 696 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr))); 697 return lltok::APFloat; 698 } 699 700 uint64_t Pair[2]; 701 switch (Kind) { 702 default: assert(0 && "Unknown kind!"); 703 case 'K': 704 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes) 705 FP80HexToIntPair(TokStart+3, CurPtr, Pair); 706 APFloatVal = APFloat(APInt(80, 2, Pair)); 707 return lltok::APFloat; 708 case 'L': 709 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes) 710 HexToIntPair(TokStart+3, CurPtr, Pair); 711 APFloatVal = APFloat(APInt(128, 2, Pair), true); 712 return lltok::APFloat; 713 case 'M': 714 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes) 715 HexToIntPair(TokStart+3, CurPtr, Pair); 716 APFloatVal = APFloat(APInt(128, 2, Pair)); 717 return lltok::APFloat; 718 } 719 } 720 721 /// LexIdentifier: Handle several related productions: 722 /// Label [-a-zA-Z$._0-9]+: 723 /// NInteger -[0-9]+ 724 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? 725 /// PInteger [0-9]+ 726 /// HexFPConstant 0x[0-9A-Fa-f]+ 727 /// HexFP80Constant 0xK[0-9A-Fa-f]+ 728 /// HexFP128Constant 0xL[0-9A-Fa-f]+ 729 /// HexPPC128Constant 0xM[0-9A-Fa-f]+ 730 lltok::Kind LLLexer::LexDigitOrNegative() { 731 // If the letter after the negative is a number, this is probably a label. 732 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) { 733 // Okay, this is not a number after the -, it's probably a label. 734 if (const char *End = isLabelTail(CurPtr)) { 735 StrVal.assign(TokStart, End-1); 736 CurPtr = End; 737 return lltok::LabelStr; 738 } 739 740 return lltok::Error; 741 } 742 743 // At this point, it is either a label, int or fp constant. 744 745 // Skip digits, we have at least one. 746 for (; isdigit(CurPtr[0]); ++CurPtr) 747 /*empty*/; 748 749 // Check to see if this really is a label afterall, e.g. "-1:". 750 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') { 751 if (const char *End = isLabelTail(CurPtr)) { 752 StrVal.assign(TokStart, End-1); 753 CurPtr = End; 754 return lltok::LabelStr; 755 } 756 } 757 758 // If the next character is a '.', then it is a fp value, otherwise its 759 // integer. 760 if (CurPtr[0] != '.') { 761 if (TokStart[0] == '0' && TokStart[1] == 'x') 762 return Lex0x(); 763 unsigned Len = CurPtr-TokStart; 764 uint32_t numBits = ((Len * 64) / 19) + 2; 765 APInt Tmp(numBits, TokStart, Len, 10); 766 if (TokStart[0] == '-') { 767 uint32_t minBits = Tmp.getMinSignedBits(); 768 if (minBits > 0 && minBits < numBits) 769 Tmp.trunc(minBits); 770 APSIntVal = APSInt(Tmp, false); 771 } else { 772 uint32_t activeBits = Tmp.getActiveBits(); 773 if (activeBits > 0 && activeBits < numBits) 774 Tmp.trunc(activeBits); 775 APSIntVal = APSInt(Tmp, true); 776 } 777 return lltok::APSInt; 778 } 779 780 ++CurPtr; 781 782 // Skip over [0-9]*([eE][-+]?[0-9]+)? 783 while (isdigit(CurPtr[0])) ++CurPtr; 784 785 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') { 786 if (isdigit(CurPtr[1]) || 787 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) { 788 CurPtr += 2; 789 while (isdigit(CurPtr[0])) ++CurPtr; 790 } 791 } 792 793 APFloatVal = APFloat(atof(TokStart)); 794 return lltok::APFloat; 795 } 796 797 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? 798 lltok::Kind LLLexer::LexPositive() { 799 // If the letter after the negative is a number, this is probably not a 800 // label. 801 if (!isdigit(CurPtr[0])) 802 return lltok::Error; 803 804 // Skip digits. 805 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr) 806 /*empty*/; 807 808 // At this point, we need a '.'. 809 if (CurPtr[0] != '.') { 810 CurPtr = TokStart+1; 811 return lltok::Error; 812 } 813 814 ++CurPtr; 815 816 // Skip over [0-9]*([eE][-+]?[0-9]+)? 817 while (isdigit(CurPtr[0])) ++CurPtr; 818 819 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') { 820 if (isdigit(CurPtr[1]) || 821 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) { 822 CurPtr += 2; 823 while (isdigit(CurPtr[0])) ++CurPtr; 824 } 825 } 826 827 APFloatVal = APFloat(atof(TokStart)); 828 return lltok::APFloat; 829 } 830