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