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/AsmParser/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 void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const {
38 SM.PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
41 //===----------------------------------------------------------------------===//
43 //===----------------------------------------------------------------------===//
45 // atoull - Convert an ascii string of decimal digits into the unsigned long
46 // long representation... this does not have to do input error checking,
47 // because we know that the input will be matched by a suitable regex...
49 uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
51 for (; Buffer != End; Buffer++) {
52 uint64_t OldRes = Result;
54 Result += *Buffer-'0';
55 if (Result < OldRes) { // Uh, oh, overflow detected!!!
56 Error("constant bigger than 64 bits detected!");
63 uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
65 for (; Buffer != End; ++Buffer) {
66 uint64_t OldRes = Result;
68 Result += hexDigitValue(*Buffer);
70 if (Result < OldRes) { // Uh, oh, overflow detected!!!
71 Error("constant bigger than 64 bits detected!");
78 void LLLexer::HexToIntPair(const char *Buffer, const char *End,
81 for (int i=0; i<16; i++, Buffer++) {
82 assert(Buffer != End);
84 Pair[0] += hexDigitValue(*Buffer);
87 for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
89 Pair[1] += hexDigitValue(*Buffer);
92 Error("constant bigger than 128 bits detected!");
95 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
96 /// { low64, high16 } as usual for an APInt.
97 void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
100 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
101 assert(Buffer != End);
103 Pair[1] += hexDigitValue(*Buffer);
106 for (int i=0; i<16; i++, Buffer++) {
108 Pair[0] += hexDigitValue(*Buffer);
111 Error("constant bigger than 128 bits detected!");
114 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
115 // appropriate character.
116 static void UnEscapeLexed(std::string &Str) {
117 if (Str.empty()) return;
119 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
121 for (char *BIn = Buffer; BIn != EndBuffer; ) {
122 if (BIn[0] == '\\') {
123 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
124 *BOut++ = '\\'; // Two \ becomes one
126 } else if (BIn < EndBuffer-2 &&
127 isxdigit(static_cast<unsigned char>(BIn[1])) &&
128 isxdigit(static_cast<unsigned char>(BIn[2]))) {
129 *BOut = hexDigitValue(BIn[1]) * 16 + hexDigitValue(BIn[2]);
130 BIn += 3; // Skip over handled chars
139 Str.resize(BOut-Buffer);
142 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
143 static bool isLabelChar(char C) {
144 return isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
145 C == '.' || C == '_';
149 /// isLabelTail - Return true if this pointer points to a valid end of a label.
150 static const char *isLabelTail(const char *CurPtr) {
152 if (CurPtr[0] == ':') return CurPtr+1;
153 if (!isLabelChar(CurPtr[0])) return nullptr;
160 //===----------------------------------------------------------------------===//
162 //===----------------------------------------------------------------------===//
164 LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err,
166 : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
167 CurPtr = CurBuf->getBufferStart();
170 std::string LLLexer::getFilename() const {
171 return CurBuf->getBufferIdentifier();
174 int LLLexer::getNextChar() {
175 char CurChar = *CurPtr++;
177 default: return (unsigned char)CurChar;
179 // A nul character in the stream is either the end of the current buffer or
180 // a random nul in the file. Disambiguate that here.
181 if (CurPtr-1 != CurBuf->getBufferEnd())
182 return 0; // Just whitespace.
184 // Otherwise, return end of file.
185 --CurPtr; // Another call to lex will return EOF again.
191 lltok::Kind LLLexer::LexToken() {
194 int CurChar = getNextChar();
197 // Handle letters: [a-zA-Z_]
198 if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
199 return LexIdentifier();
202 case EOF: return lltok::Eof;
208 // Ignore whitespace.
210 case '+': return LexPositive();
211 case '@': return LexAt();
212 case '%': return LexPercent();
213 case '"': return LexQuote();
215 if (const char *Ptr = isLabelTail(CurPtr)) {
217 StrVal.assign(TokStart, CurPtr-1);
218 return lltok::LabelStr;
220 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
222 return lltok::dotdotdot;
226 if (const char *Ptr = isLabelTail(CurPtr)) {
228 StrVal.assign(TokStart, CurPtr-1);
229 return lltok::LabelStr;
235 case '!': return LexExclaim();
236 case '#': return LexHash();
237 case '0': case '1': case '2': case '3': case '4':
238 case '5': case '6': case '7': case '8': case '9':
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;
256 void LLLexer::SkipLineComment() {
258 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
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]+
267 lltok::Kind LLLexer::LexAt() {
268 // Handle AtStringConstant: @\"[^\"]*\"
269 if (CurPtr[0] == '"') {
273 int CurChar = getNextChar();
275 if (CurChar == EOF) {
276 Error("end of file in global variable name");
279 if (CurChar == '"') {
280 StrVal.assign(TokStart+2, CurPtr-1);
281 UnEscapeLexed(StrVal);
282 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
283 Error("Null bytes are not allowed in names");
286 return lltok::GlobalVar;
291 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
293 return lltok::GlobalVar;
295 // Handle GlobalVarID: @[0-9]+
296 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
297 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
300 uint64_t Val = atoull(TokStart+1, CurPtr);
301 if ((unsigned)Val != Val)
302 Error("invalid value number (too large)!");
303 UIntVal = unsigned(Val);
304 return lltok::GlobalID;
310 /// ReadString - Read a string until the closing quote.
311 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
312 const char *Start = CurPtr;
314 int CurChar = getNextChar();
316 if (CurChar == EOF) {
317 Error("end of file in string constant");
320 if (CurChar == '"') {
321 StrVal.assign(Start, CurPtr-1);
322 UnEscapeLexed(StrVal);
328 /// ReadVarName - Read the rest of a token containing a variable name.
329 bool LLLexer::ReadVarName() {
330 const char *NameStart = CurPtr;
331 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
332 CurPtr[0] == '-' || CurPtr[0] == '$' ||
333 CurPtr[0] == '.' || CurPtr[0] == '_') {
335 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
336 CurPtr[0] == '-' || CurPtr[0] == '$' ||
337 CurPtr[0] == '.' || CurPtr[0] == '_')
340 StrVal.assign(NameStart, CurPtr);
346 /// LexPercent - Lex all tokens that start with a % character:
347 /// LocalVar ::= %\"[^\"]*\"
348 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
349 /// LocalVarID ::= %[0-9]+
350 lltok::Kind LLLexer::LexPercent() {
351 // Handle LocalVarName: %\"[^\"]*\"
352 if (CurPtr[0] == '"') {
354 return ReadString(lltok::LocalVar);
357 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
359 return lltok::LocalVar;
361 // Handle LocalVarID: %[0-9]+
362 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
363 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
366 uint64_t Val = atoull(TokStart+1, CurPtr);
367 if ((unsigned)Val != Val)
368 Error("invalid value number (too large)!");
369 UIntVal = unsigned(Val);
370 return lltok::LocalVarID;
376 /// LexQuote - Lex all tokens that start with a " character:
377 /// QuoteLabel "[^"]+":
378 /// StringConstant "[^"]*"
379 lltok::Kind LLLexer::LexQuote() {
380 lltok::Kind kind = ReadString(lltok::StringConstant);
381 if (kind == lltok::Error || kind == lltok::Eof)
384 if (CurPtr[0] == ':') {
386 kind = lltok::LabelStr;
395 lltok::Kind LLLexer::LexExclaim() {
396 // Lex a metadata name as a MetadataVar.
397 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
398 CurPtr[0] == '-' || CurPtr[0] == '$' ||
399 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
401 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
402 CurPtr[0] == '-' || CurPtr[0] == '$' ||
403 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
406 StrVal.assign(TokStart+1, CurPtr); // Skip !
407 UnEscapeLexed(StrVal);
408 return lltok::MetadataVar;
410 return lltok::exclaim;
413 /// LexHash - Lex all tokens that start with a # character:
414 /// AttrGrpID ::= #[0-9]+
415 lltok::Kind LLLexer::LexHash() {
416 // Handle AttrGrpID: #[0-9]+
417 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
418 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
421 uint64_t Val = atoull(TokStart+1, CurPtr);
422 if ((unsigned)Val != Val)
423 Error("invalid value number (too large)!");
424 UIntVal = unsigned(Val);
425 return lltok::AttrGrpID;
431 /// LexIdentifier: Handle several related productions:
432 /// Label [-a-zA-Z$._0-9]+:
433 /// IntegerType i[0-9]+
434 /// Keyword sdiv, float, ...
435 /// HexIntConstant [us]0x[0-9A-Fa-f]+
436 lltok::Kind LLLexer::LexIdentifier() {
437 const char *StartChar = CurPtr;
438 const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar;
439 const char *KeywordEnd = nullptr;
441 for (; isLabelChar(*CurPtr); ++CurPtr) {
442 // If we decide this is an integer, remember the end of the sequence.
443 if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
445 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
450 // If we stopped due to a colon, this really is a label.
451 if (*CurPtr == ':') {
452 StrVal.assign(StartChar-1, CurPtr++);
453 return lltok::LabelStr;
456 // Otherwise, this wasn't a label. If this was valid as an integer type,
458 if (!IntEnd) IntEnd = CurPtr;
459 if (IntEnd != StartChar) {
461 uint64_t NumBits = atoull(StartChar, CurPtr);
462 if (NumBits < IntegerType::MIN_INT_BITS ||
463 NumBits > IntegerType::MAX_INT_BITS) {
464 Error("bitwidth for integer type out of range!");
467 TyVal = IntegerType::get(Context, NumBits);
471 // Otherwise, this was a letter sequence. See which keyword this is.
472 if (!KeywordEnd) KeywordEnd = CurPtr;
475 unsigned Len = CurPtr-StartChar;
476 #define KEYWORD(STR) \
478 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
479 return lltok::kw_##STR; \
482 KEYWORD(true); KEYWORD(false);
483 KEYWORD(declare); KEYWORD(define);
484 KEYWORD(global); KEYWORD(constant);
488 KEYWORD(linker_private); // NOTE: deprecated, for parser compatibility
489 KEYWORD(linker_private_weak); // NOTE: deprecated, for parser compatibility
490 KEYWORD(available_externally);
492 KEYWORD(linkonce_odr);
502 KEYWORD(unnamed_addr);
503 KEYWORD(externally_initialized);
504 KEYWORD(extern_weak);
506 KEYWORD(thread_local);
507 KEYWORD(localdynamic);
508 KEYWORD(initialexec);
510 KEYWORD(zeroinitializer);
519 KEYWORD(deplibs); // FIXME: Remove in 4.0.
529 KEYWORD(singlethread);
548 KEYWORD(inteldialect);
555 KEYWORD(x86_stdcallcc);
556 KEYWORD(x86_fastcallcc);
557 KEYWORD(x86_thiscallcc);
559 KEYWORD(arm_aapcscc);
560 KEYWORD(arm_aapcs_vfpcc);
561 KEYWORD(msp430_intrcc);
564 KEYWORD(spir_kernel);
566 KEYWORD(intel_ocl_bicc);
567 KEYWORD(x86_64_sysvcc);
568 KEYWORD(x86_64_win64cc);
569 KEYWORD(webkit_jscc);
571 KEYWORD(preserve_mostcc);
572 KEYWORD(preserve_allcc);
579 KEYWORD(alwaysinline);
593 KEYWORD(noduplicate);
594 KEYWORD(noimplicitfloat);
596 KEYWORD(nonlazybind);
606 KEYWORD(returns_twice);
612 KEYWORD(sanitize_address);
613 KEYWORD(sanitize_thread);
614 KEYWORD(sanitize_memory);
621 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
622 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
623 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
624 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
626 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
630 KEYWORD(blockaddress);
632 KEYWORD(personality);
638 // Keywords for types.
639 #define TYPEKEYWORD(STR, LLVMTY) \
640 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
641 TyVal = LLVMTY; return lltok::Type; }
642 TYPEKEYWORD("void", Type::getVoidTy(Context));
643 TYPEKEYWORD("half", Type::getHalfTy(Context));
644 TYPEKEYWORD("float", Type::getFloatTy(Context));
645 TYPEKEYWORD("double", Type::getDoubleTy(Context));
646 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
647 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
648 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
649 TYPEKEYWORD("label", Type::getLabelTy(Context));
650 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
651 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
654 // Keywords for instructions.
655 #define INSTKEYWORD(STR, Enum) \
656 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
657 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
659 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
660 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
661 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
662 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
663 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
664 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
665 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
666 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
668 INSTKEYWORD(phi, PHI);
669 INSTKEYWORD(call, Call);
670 INSTKEYWORD(trunc, Trunc);
671 INSTKEYWORD(zext, ZExt);
672 INSTKEYWORD(sext, SExt);
673 INSTKEYWORD(fptrunc, FPTrunc);
674 INSTKEYWORD(fpext, FPExt);
675 INSTKEYWORD(uitofp, UIToFP);
676 INSTKEYWORD(sitofp, SIToFP);
677 INSTKEYWORD(fptoui, FPToUI);
678 INSTKEYWORD(fptosi, FPToSI);
679 INSTKEYWORD(inttoptr, IntToPtr);
680 INSTKEYWORD(ptrtoint, PtrToInt);
681 INSTKEYWORD(bitcast, BitCast);
682 INSTKEYWORD(addrspacecast, AddrSpaceCast);
683 INSTKEYWORD(select, Select);
684 INSTKEYWORD(va_arg, VAArg);
685 INSTKEYWORD(ret, Ret);
687 INSTKEYWORD(switch, Switch);
688 INSTKEYWORD(indirectbr, IndirectBr);
689 INSTKEYWORD(invoke, Invoke);
690 INSTKEYWORD(resume, Resume);
691 INSTKEYWORD(unreachable, Unreachable);
693 INSTKEYWORD(alloca, Alloca);
694 INSTKEYWORD(load, Load);
695 INSTKEYWORD(store, Store);
696 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
697 INSTKEYWORD(atomicrmw, AtomicRMW);
698 INSTKEYWORD(fence, Fence);
699 INSTKEYWORD(getelementptr, GetElementPtr);
701 INSTKEYWORD(extractelement, ExtractElement);
702 INSTKEYWORD(insertelement, InsertElement);
703 INSTKEYWORD(shufflevector, ShuffleVector);
704 INSTKEYWORD(extractvalue, ExtractValue);
705 INSTKEYWORD(insertvalue, InsertValue);
706 INSTKEYWORD(landingpad, LandingPad);
709 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
710 // the CFE to avoid forcing it to deal with 64-bit numbers.
711 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
712 TokStart[1] == '0' && TokStart[2] == 'x' &&
713 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
714 int len = CurPtr-TokStart-3;
715 uint32_t bits = len * 4;
716 APInt Tmp(bits, StringRef(TokStart+3, len), 16);
717 uint32_t activeBits = Tmp.getActiveBits();
718 if (activeBits > 0 && activeBits < bits)
719 Tmp = Tmp.trunc(activeBits);
720 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
721 return lltok::APSInt;
724 // If this is "cc1234", return this as just "cc".
725 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
730 // Finally, if this isn't known, return an error.
736 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
737 /// that this is not a label:
738 /// HexFPConstant 0x[0-9A-Fa-f]+
739 /// HexFP80Constant 0xK[0-9A-Fa-f]+
740 /// HexFP128Constant 0xL[0-9A-Fa-f]+
741 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
742 /// HexHalfConstant 0xH[0-9A-Fa-f]+
743 lltok::Kind LLLexer::Lex0x() {
744 CurPtr = TokStart + 2;
747 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
753 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
754 // Bad token, return it as an error.
759 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
763 // HexFPConstant - Floating point constant represented in IEEE format as a
764 // hexadecimal number for when exponential notation is not precise enough.
765 // Half, Float, and double only.
766 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
767 return lltok::APFloat;
772 default: llvm_unreachable("Unknown kind!");
774 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
775 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
776 APFloatVal = APFloat(APFloat::x87DoubleExtended, APInt(80, Pair));
777 return lltok::APFloat;
779 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
780 HexToIntPair(TokStart+3, CurPtr, Pair);
781 APFloatVal = APFloat(APFloat::IEEEquad, APInt(128, Pair));
782 return lltok::APFloat;
784 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
785 HexToIntPair(TokStart+3, CurPtr, Pair);
786 APFloatVal = APFloat(APFloat::PPCDoubleDouble, APInt(128, Pair));
787 return lltok::APFloat;
789 APFloatVal = APFloat(APFloat::IEEEhalf,
790 APInt(16,HexIntToVal(TokStart+3, CurPtr)));
791 return lltok::APFloat;
795 /// LexIdentifier: Handle several related productions:
796 /// Label [-a-zA-Z$._0-9]+:
798 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
800 /// HexFPConstant 0x[0-9A-Fa-f]+
801 /// HexFP80Constant 0xK[0-9A-Fa-f]+
802 /// HexFP128Constant 0xL[0-9A-Fa-f]+
803 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
804 lltok::Kind LLLexer::LexDigitOrNegative() {
805 // If the letter after the negative is not a number, this is probably a label.
806 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
807 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
808 // Okay, this is not a number after the -, it's probably a label.
809 if (const char *End = isLabelTail(CurPtr)) {
810 StrVal.assign(TokStart, End-1);
812 return lltok::LabelStr;
818 // At this point, it is either a label, int or fp constant.
820 // Skip digits, we have at least one.
821 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
824 // Check to see if this really is a label afterall, e.g. "-1:".
825 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
826 if (const char *End = isLabelTail(CurPtr)) {
827 StrVal.assign(TokStart, End-1);
829 return lltok::LabelStr;
833 // If the next character is a '.', then it is a fp value, otherwise its
835 if (CurPtr[0] != '.') {
836 if (TokStart[0] == '0' && TokStart[1] == 'x')
838 unsigned Len = CurPtr-TokStart;
839 uint32_t numBits = ((Len * 64) / 19) + 2;
840 APInt Tmp(numBits, StringRef(TokStart, Len), 10);
841 if (TokStart[0] == '-') {
842 uint32_t minBits = Tmp.getMinSignedBits();
843 if (minBits > 0 && minBits < numBits)
844 Tmp = Tmp.trunc(minBits);
845 APSIntVal = APSInt(Tmp, false);
847 uint32_t activeBits = Tmp.getActiveBits();
848 if (activeBits > 0 && activeBits < numBits)
849 Tmp = Tmp.trunc(activeBits);
850 APSIntVal = APSInt(Tmp, true);
852 return lltok::APSInt;
857 // Skip over [0-9]*([eE][-+]?[0-9]+)?
858 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
860 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
861 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
862 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
863 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
865 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
869 APFloatVal = APFloat(std::atof(TokStart));
870 return lltok::APFloat;
873 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
874 lltok::Kind LLLexer::LexPositive() {
875 // If the letter after the negative is a number, this is probably not a
877 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
881 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
884 // At this point, we need a '.'.
885 if (CurPtr[0] != '.') {
892 // Skip over [0-9]*([eE][-+]?[0-9]+)?
893 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
895 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
896 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
897 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
898 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
900 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
904 APFloatVal = APFloat(std::atof(TokStart));
905 return lltok::APFloat;