1 //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implement the Lexer for .ll files.
12 //===----------------------------------------------------------------------===//
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"
32 bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
33 ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
37 //===----------------------------------------------------------------------===//
39 //===----------------------------------------------------------------------===//
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...
45 uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
47 for (; Buffer != End; Buffer++) {
48 uint64_t OldRes = Result;
50 Result += *Buffer-'0';
51 if (Result < OldRes) { // Uh, oh, overflow detected!!!
52 Error("constant bigger than 64 bits detected!");
59 uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
61 for (; Buffer != End; ++Buffer) {
62 uint64_t OldRes = Result;
64 Result += hexDigitValue(*Buffer);
66 if (Result < OldRes) { // Uh, oh, overflow detected!!!
67 Error("constant bigger than 64 bits detected!");
74 void LLLexer::HexToIntPair(const char *Buffer, const char *End,
77 for (int i=0; i<16; i++, Buffer++) {
78 assert(Buffer != End);
80 Pair[0] += hexDigitValue(*Buffer);
83 for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
85 Pair[1] += hexDigitValue(*Buffer);
88 Error("constant bigger than 128 bits detected!");
91 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
92 /// { low64, high16 } as usual for an APInt.
93 void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
96 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
97 assert(Buffer != End);
99 Pair[1] += hexDigitValue(*Buffer);
102 for (int i=0; i<16; i++, Buffer++) {
104 Pair[0] += hexDigitValue(*Buffer);
107 Error("constant bigger than 128 bits detected!");
110 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
111 // appropriate character.
112 static void UnEscapeLexed(std::string &Str) {
113 if (Str.empty()) return;
115 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
117 for (char *BIn = Buffer; BIn != EndBuffer; ) {
118 if (BIn[0] == '\\') {
119 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
120 *BOut++ = '\\'; // Two \ becomes one
122 } else if (BIn < EndBuffer-2 &&
123 isxdigit(static_cast<unsigned char>(BIn[1])) &&
124 isxdigit(static_cast<unsigned char>(BIn[2]))) {
125 *BOut = hexDigitValue(BIn[1]) * 16 + hexDigitValue(BIn[2]);
126 BIn += 3; // Skip over handled chars
135 Str.resize(BOut-Buffer);
138 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
139 static bool isLabelChar(char C) {
140 return isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
141 C == '.' || C == '_';
145 /// isLabelTail - Return true if this pointer points to a valid end of a label.
146 static const char *isLabelTail(const char *CurPtr) {
148 if (CurPtr[0] == ':') return CurPtr+1;
149 if (!isLabelChar(CurPtr[0])) return 0;
156 //===----------------------------------------------------------------------===//
158 //===----------------------------------------------------------------------===//
160 LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err,
162 : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
163 CurPtr = CurBuf->getBufferStart();
166 std::string LLLexer::getFilename() const {
167 return CurBuf->getBufferIdentifier();
170 int LLLexer::getNextChar() {
171 char CurChar = *CurPtr++;
173 default: return (unsigned char)CurChar;
175 // A nul character in the stream is either the end of the current buffer or
176 // a random nul in the file. Disambiguate that here.
177 if (CurPtr-1 != CurBuf->getBufferEnd())
178 return 0; // Just whitespace.
180 // Otherwise, return end of file.
181 --CurPtr; // Another call to lex will return EOF again.
187 lltok::Kind LLLexer::LexToken() {
190 int CurChar = getNextChar();
193 // Handle letters: [a-zA-Z_]
194 if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
195 return LexIdentifier();
198 case EOF: return lltok::Eof;
204 // Ignore whitespace.
206 case '+': return LexPositive();
207 case '@': return LexAt();
208 case '%': return LexPercent();
209 case '"': return LexQuote();
211 if (const char *Ptr = isLabelTail(CurPtr)) {
213 StrVal.assign(TokStart, CurPtr-1);
214 return lltok::LabelStr;
216 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
218 return lltok::dotdotdot;
222 if (const char *Ptr = isLabelTail(CurPtr)) {
224 StrVal.assign(TokStart, CurPtr-1);
225 return lltok::LabelStr;
231 case '!': return LexExclaim();
232 case '#': return LexHash();
233 case '0': case '1': case '2': case '3': case '4':
234 case '5': case '6': case '7': case '8': case '9':
236 return LexDigitOrNegative();
237 case '=': return lltok::equal;
238 case '[': return lltok::lsquare;
239 case ']': return lltok::rsquare;
240 case '{': return lltok::lbrace;
241 case '}': return lltok::rbrace;
242 case '<': return lltok::less;
243 case '>': return lltok::greater;
244 case '(': return lltok::lparen;
245 case ')': return lltok::rparen;
246 case ',': return lltok::comma;
247 case '*': return lltok::star;
248 case '\\': return lltok::backslash;
252 void LLLexer::SkipLineComment() {
254 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
259 /// LexAt - Lex all tokens that start with an @ character:
260 /// GlobalVar @\"[^\"]*\"
261 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
262 /// GlobalVarID @[0-9]+
263 lltok::Kind LLLexer::LexAt() {
264 // Handle AtStringConstant: @\"[^\"]*\"
265 if (CurPtr[0] == '"') {
269 int CurChar = getNextChar();
271 if (CurChar == EOF) {
272 Error("end of file in global variable name");
275 if (CurChar == '"') {
276 StrVal.assign(TokStart+2, CurPtr-1);
277 UnEscapeLexed(StrVal);
278 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
279 Error("Null bytes are not allowed in names");
282 return lltok::GlobalVar;
287 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
289 return lltok::GlobalVar;
291 // Handle GlobalVarID: @[0-9]+
292 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
293 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
296 uint64_t Val = atoull(TokStart+1, CurPtr);
297 if ((unsigned)Val != Val)
298 Error("invalid value number (too large)!");
299 UIntVal = unsigned(Val);
300 return lltok::GlobalID;
306 /// ReadString - Read a string until the closing quote.
307 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
308 const char *Start = CurPtr;
310 int CurChar = getNextChar();
312 if (CurChar == EOF) {
313 Error("end of file in string constant");
316 if (CurChar == '"') {
317 StrVal.assign(Start, CurPtr-1);
318 UnEscapeLexed(StrVal);
324 /// ReadVarName - Read the rest of a token containing a variable name.
325 bool LLLexer::ReadVarName() {
326 const char *NameStart = CurPtr;
327 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
328 CurPtr[0] == '-' || CurPtr[0] == '$' ||
329 CurPtr[0] == '.' || CurPtr[0] == '_') {
331 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
332 CurPtr[0] == '-' || CurPtr[0] == '$' ||
333 CurPtr[0] == '.' || CurPtr[0] == '_')
336 StrVal.assign(NameStart, CurPtr);
342 /// LexPercent - Lex all tokens that start with a % character:
343 /// LocalVar ::= %\"[^\"]*\"
344 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
345 /// LocalVarID ::= %[0-9]+
346 lltok::Kind LLLexer::LexPercent() {
347 // Handle LocalVarName: %\"[^\"]*\"
348 if (CurPtr[0] == '"') {
350 return ReadString(lltok::LocalVar);
353 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
355 return lltok::LocalVar;
357 // Handle LocalVarID: %[0-9]+
358 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
359 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
362 uint64_t Val = atoull(TokStart+1, CurPtr);
363 if ((unsigned)Val != Val)
364 Error("invalid value number (too large)!");
365 UIntVal = unsigned(Val);
366 return lltok::LocalVarID;
372 /// LexQuote - Lex all tokens that start with a " character:
373 /// QuoteLabel "[^"]+":
374 /// StringConstant "[^"]*"
375 lltok::Kind LLLexer::LexQuote() {
376 lltok::Kind kind = ReadString(lltok::StringConstant);
377 if (kind == lltok::Error || kind == lltok::Eof)
380 if (CurPtr[0] == ':') {
382 kind = lltok::LabelStr;
391 lltok::Kind LLLexer::LexExclaim() {
392 // Lex a metadata name as a MetadataVar.
393 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
394 CurPtr[0] == '-' || CurPtr[0] == '$' ||
395 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
397 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
398 CurPtr[0] == '-' || CurPtr[0] == '$' ||
399 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
402 StrVal.assign(TokStart+1, CurPtr); // Skip !
403 UnEscapeLexed(StrVal);
404 return lltok::MetadataVar;
406 return lltok::exclaim;
409 /// LexHash - Lex all tokens that start with a # character:
410 /// AttrGrpID ::= #[0-9]+
411 lltok::Kind LLLexer::LexHash() {
412 // Handle AttrGrpID: #[0-9]+
413 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
414 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
417 uint64_t Val = atoull(TokStart+1, CurPtr);
418 if ((unsigned)Val != Val)
419 Error("invalid value number (too large)!");
420 UIntVal = unsigned(Val);
421 return lltok::AttrGrpID;
427 /// LexIdentifier: Handle several related productions:
428 /// Label [-a-zA-Z$._0-9]+:
429 /// IntegerType i[0-9]+
430 /// Keyword sdiv, float, ...
431 /// HexIntConstant [us]0x[0-9A-Fa-f]+
432 lltok::Kind LLLexer::LexIdentifier() {
433 const char *StartChar = CurPtr;
434 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
435 const char *KeywordEnd = 0;
437 for (; isLabelChar(*CurPtr); ++CurPtr) {
438 // If we decide this is an integer, remember the end of the sequence.
439 if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
441 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
446 // If we stopped due to a colon, this really is a label.
447 if (*CurPtr == ':') {
448 StrVal.assign(StartChar-1, CurPtr++);
449 return lltok::LabelStr;
452 // Otherwise, this wasn't a label. If this was valid as an integer type,
454 if (IntEnd == 0) IntEnd = CurPtr;
455 if (IntEnd != StartChar) {
457 uint64_t NumBits = atoull(StartChar, CurPtr);
458 if (NumBits < IntegerType::MIN_INT_BITS ||
459 NumBits > IntegerType::MAX_INT_BITS) {
460 Error("bitwidth for integer type out of range!");
463 TyVal = IntegerType::get(Context, NumBits);
467 // Otherwise, this was a letter sequence. See which keyword this is.
468 if (KeywordEnd == 0) KeywordEnd = CurPtr;
471 unsigned Len = CurPtr-StartChar;
472 #define KEYWORD(STR) \
474 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
475 return lltok::kw_##STR; \
478 KEYWORD(true); KEYWORD(false);
479 KEYWORD(declare); KEYWORD(define);
480 KEYWORD(global); KEYWORD(constant);
483 KEYWORD(linker_private);
484 KEYWORD(linker_private_weak);
486 KEYWORD(available_externally);
488 KEYWORD(linkonce_odr);
498 KEYWORD(unnamed_addr);
499 KEYWORD(externally_initialized);
500 KEYWORD(extern_weak);
502 KEYWORD(thread_local);
503 KEYWORD(localdynamic);
504 KEYWORD(initialexec);
506 KEYWORD(zeroinitializer);
514 KEYWORD(deplibs); // FIXME: Remove in 4.0.
524 KEYWORD(singlethread);
543 KEYWORD(inteldialect);
550 KEYWORD(x86_stdcallcc);
551 KEYWORD(x86_fastcallcc);
552 KEYWORD(x86_thiscallcc);
554 KEYWORD(arm_aapcscc);
555 KEYWORD(arm_aapcs_vfpcc);
556 KEYWORD(msp430_intrcc);
559 KEYWORD(spir_kernel);
561 KEYWORD(intel_ocl_bicc);
562 KEYWORD(x86_64_sysvcc);
563 KEYWORD(x86_64_win64cc);
564 KEYWORD(webkit_jscc);
572 KEYWORD(alwaysinline);
584 KEYWORD(noduplicate);
585 KEYWORD(noimplicitfloat);
587 KEYWORD(nonlazybind);
596 KEYWORD(returns_twice);
602 KEYWORD(sanitize_address);
603 KEYWORD(sanitize_thread);
604 KEYWORD(sanitize_memory);
611 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
612 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
613 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
614 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
616 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
620 KEYWORD(blockaddress);
622 KEYWORD(personality);
628 // Keywords for types.
629 #define TYPEKEYWORD(STR, LLVMTY) \
630 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
631 TyVal = LLVMTY; return lltok::Type; }
632 TYPEKEYWORD("void", Type::getVoidTy(Context));
633 TYPEKEYWORD("half", Type::getHalfTy(Context));
634 TYPEKEYWORD("float", Type::getFloatTy(Context));
635 TYPEKEYWORD("double", Type::getDoubleTy(Context));
636 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
637 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
638 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
639 TYPEKEYWORD("label", Type::getLabelTy(Context));
640 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
641 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
644 // Keywords for instructions.
645 #define INSTKEYWORD(STR, Enum) \
646 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
647 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
649 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
650 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
651 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
652 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
653 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
654 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
655 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
656 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
658 INSTKEYWORD(phi, PHI);
659 INSTKEYWORD(call, Call);
660 INSTKEYWORD(trunc, Trunc);
661 INSTKEYWORD(zext, ZExt);
662 INSTKEYWORD(sext, SExt);
663 INSTKEYWORD(fptrunc, FPTrunc);
664 INSTKEYWORD(fpext, FPExt);
665 INSTKEYWORD(uitofp, UIToFP);
666 INSTKEYWORD(sitofp, SIToFP);
667 INSTKEYWORD(fptoui, FPToUI);
668 INSTKEYWORD(fptosi, FPToSI);
669 INSTKEYWORD(inttoptr, IntToPtr);
670 INSTKEYWORD(ptrtoint, PtrToInt);
671 INSTKEYWORD(bitcast, BitCast);
672 INSTKEYWORD(addrspacecast, AddrSpaceCast);
673 INSTKEYWORD(select, Select);
674 INSTKEYWORD(va_arg, VAArg);
675 INSTKEYWORD(ret, Ret);
677 INSTKEYWORD(switch, Switch);
678 INSTKEYWORD(indirectbr, IndirectBr);
679 INSTKEYWORD(invoke, Invoke);
680 INSTKEYWORD(resume, Resume);
681 INSTKEYWORD(unreachable, Unreachable);
683 INSTKEYWORD(alloca, Alloca);
684 INSTKEYWORD(load, Load);
685 INSTKEYWORD(store, Store);
686 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
687 INSTKEYWORD(atomicrmw, AtomicRMW);
688 INSTKEYWORD(fence, Fence);
689 INSTKEYWORD(getelementptr, GetElementPtr);
691 INSTKEYWORD(extractelement, ExtractElement);
692 INSTKEYWORD(insertelement, InsertElement);
693 INSTKEYWORD(shufflevector, ShuffleVector);
694 INSTKEYWORD(extractvalue, ExtractValue);
695 INSTKEYWORD(insertvalue, InsertValue);
696 INSTKEYWORD(landingpad, LandingPad);
699 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
700 // the CFE to avoid forcing it to deal with 64-bit numbers.
701 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
702 TokStart[1] == '0' && TokStart[2] == 'x' &&
703 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
704 int len = CurPtr-TokStart-3;
705 uint32_t bits = len * 4;
706 APInt Tmp(bits, StringRef(TokStart+3, len), 16);
707 uint32_t activeBits = Tmp.getActiveBits();
708 if (activeBits > 0 && activeBits < bits)
709 Tmp = Tmp.trunc(activeBits);
710 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
711 return lltok::APSInt;
714 // If this is "cc1234", return this as just "cc".
715 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
720 // Finally, if this isn't known, return an error.
726 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
727 /// that this is not a label:
728 /// HexFPConstant 0x[0-9A-Fa-f]+
729 /// HexFP80Constant 0xK[0-9A-Fa-f]+
730 /// HexFP128Constant 0xL[0-9A-Fa-f]+
731 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
732 /// HexHalfConstant 0xH[0-9A-Fa-f]+
733 lltok::Kind LLLexer::Lex0x() {
734 CurPtr = TokStart + 2;
737 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
743 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
744 // Bad token, return it as an error.
749 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
753 // HexFPConstant - Floating point constant represented in IEEE format as a
754 // hexadecimal number for when exponential notation is not precise enough.
755 // Half, Float, and double only.
756 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
757 return lltok::APFloat;
762 default: llvm_unreachable("Unknown kind!");
764 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
765 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
766 APFloatVal = APFloat(APFloat::x87DoubleExtended, APInt(80, Pair));
767 return lltok::APFloat;
769 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
770 HexToIntPair(TokStart+3, CurPtr, Pair);
771 APFloatVal = APFloat(APFloat::IEEEquad, APInt(128, Pair));
772 return lltok::APFloat;
774 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
775 HexToIntPair(TokStart+3, CurPtr, Pair);
776 APFloatVal = APFloat(APFloat::PPCDoubleDouble, APInt(128, Pair));
777 return lltok::APFloat;
779 APFloatVal = APFloat(APFloat::IEEEhalf,
780 APInt(16,HexIntToVal(TokStart+3, CurPtr)));
781 return lltok::APFloat;
785 /// LexIdentifier: Handle several related productions:
786 /// Label [-a-zA-Z$._0-9]+:
788 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
790 /// HexFPConstant 0x[0-9A-Fa-f]+
791 /// HexFP80Constant 0xK[0-9A-Fa-f]+
792 /// HexFP128Constant 0xL[0-9A-Fa-f]+
793 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
794 lltok::Kind LLLexer::LexDigitOrNegative() {
795 // If the letter after the negative is not a number, this is probably a label.
796 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
797 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
798 // Okay, this is not a number after the -, it's probably a label.
799 if (const char *End = isLabelTail(CurPtr)) {
800 StrVal.assign(TokStart, End-1);
802 return lltok::LabelStr;
808 // At this point, it is either a label, int or fp constant.
810 // Skip digits, we have at least one.
811 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
814 // Check to see if this really is a label afterall, e.g. "-1:".
815 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
816 if (const char *End = isLabelTail(CurPtr)) {
817 StrVal.assign(TokStart, End-1);
819 return lltok::LabelStr;
823 // If the next character is a '.', then it is a fp value, otherwise its
825 if (CurPtr[0] != '.') {
826 if (TokStart[0] == '0' && TokStart[1] == 'x')
828 unsigned Len = CurPtr-TokStart;
829 uint32_t numBits = ((Len * 64) / 19) + 2;
830 APInt Tmp(numBits, StringRef(TokStart, Len), 10);
831 if (TokStart[0] == '-') {
832 uint32_t minBits = Tmp.getMinSignedBits();
833 if (minBits > 0 && minBits < numBits)
834 Tmp = Tmp.trunc(minBits);
835 APSIntVal = APSInt(Tmp, false);
837 uint32_t activeBits = Tmp.getActiveBits();
838 if (activeBits > 0 && activeBits < numBits)
839 Tmp = Tmp.trunc(activeBits);
840 APSIntVal = APSInt(Tmp, true);
842 return lltok::APSInt;
847 // Skip over [0-9]*([eE][-+]?[0-9]+)?
848 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
850 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
851 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
852 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
853 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
855 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
859 APFloatVal = APFloat(std::atof(TokStart));
860 return lltok::APFloat;
863 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
864 lltok::Kind LLLexer::LexPositive() {
865 // If the letter after the negative is a number, this is probably not a
867 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
871 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
874 // At this point, we need a '.'.
875 if (CurPtr[0] != '.') {
882 // Skip over [0-9]*([eE][-+]?[0-9]+)?
883 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
885 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
886 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
887 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
888 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
890 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
894 APFloatVal = APFloat(std::atof(TokStart));
895 return lltok::APFloat;