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);
558 KEYWORD(x86_cdeclmethodcc);
560 KEYWORD(arm_aapcscc);
561 KEYWORD(arm_aapcs_vfpcc);
562 KEYWORD(msp430_intrcc);
565 KEYWORD(spir_kernel);
567 KEYWORD(intel_ocl_bicc);
568 KEYWORD(x86_64_sysvcc);
569 KEYWORD(x86_64_win64cc);
570 KEYWORD(webkit_jscc);
572 KEYWORD(preserve_mostcc);
573 KEYWORD(preserve_allcc);
580 KEYWORD(alwaysinline);
593 KEYWORD(noduplicate);
594 KEYWORD(noimplicitfloat);
596 KEYWORD(nonlazybind);
605 KEYWORD(returns_twice);
611 KEYWORD(sanitize_address);
612 KEYWORD(sanitize_thread);
613 KEYWORD(sanitize_memory);
620 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
621 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
622 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
623 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
625 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
629 KEYWORD(blockaddress);
631 KEYWORD(personality);
637 // Keywords for types.
638 #define TYPEKEYWORD(STR, LLVMTY) \
639 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
640 TyVal = LLVMTY; return lltok::Type; }
641 TYPEKEYWORD("void", Type::getVoidTy(Context));
642 TYPEKEYWORD("half", Type::getHalfTy(Context));
643 TYPEKEYWORD("float", Type::getFloatTy(Context));
644 TYPEKEYWORD("double", Type::getDoubleTy(Context));
645 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
646 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
647 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
648 TYPEKEYWORD("label", Type::getLabelTy(Context));
649 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
650 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
653 // Keywords for instructions.
654 #define INSTKEYWORD(STR, Enum) \
655 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
656 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
658 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
659 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
660 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
661 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
662 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
663 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
664 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
665 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
667 INSTKEYWORD(phi, PHI);
668 INSTKEYWORD(call, Call);
669 INSTKEYWORD(trunc, Trunc);
670 INSTKEYWORD(zext, ZExt);
671 INSTKEYWORD(sext, SExt);
672 INSTKEYWORD(fptrunc, FPTrunc);
673 INSTKEYWORD(fpext, FPExt);
674 INSTKEYWORD(uitofp, UIToFP);
675 INSTKEYWORD(sitofp, SIToFP);
676 INSTKEYWORD(fptoui, FPToUI);
677 INSTKEYWORD(fptosi, FPToSI);
678 INSTKEYWORD(inttoptr, IntToPtr);
679 INSTKEYWORD(ptrtoint, PtrToInt);
680 INSTKEYWORD(bitcast, BitCast);
681 INSTKEYWORD(addrspacecast, AddrSpaceCast);
682 INSTKEYWORD(select, Select);
683 INSTKEYWORD(va_arg, VAArg);
684 INSTKEYWORD(ret, Ret);
686 INSTKEYWORD(switch, Switch);
687 INSTKEYWORD(indirectbr, IndirectBr);
688 INSTKEYWORD(invoke, Invoke);
689 INSTKEYWORD(resume, Resume);
690 INSTKEYWORD(unreachable, Unreachable);
692 INSTKEYWORD(alloca, Alloca);
693 INSTKEYWORD(load, Load);
694 INSTKEYWORD(store, Store);
695 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
696 INSTKEYWORD(atomicrmw, AtomicRMW);
697 INSTKEYWORD(fence, Fence);
698 INSTKEYWORD(getelementptr, GetElementPtr);
700 INSTKEYWORD(extractelement, ExtractElement);
701 INSTKEYWORD(insertelement, InsertElement);
702 INSTKEYWORD(shufflevector, ShuffleVector);
703 INSTKEYWORD(extractvalue, ExtractValue);
704 INSTKEYWORD(insertvalue, InsertValue);
705 INSTKEYWORD(landingpad, LandingPad);
708 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
709 // the CFE to avoid forcing it to deal with 64-bit numbers.
710 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
711 TokStart[1] == '0' && TokStart[2] == 'x' &&
712 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
713 int len = CurPtr-TokStart-3;
714 uint32_t bits = len * 4;
715 APInt Tmp(bits, StringRef(TokStart+3, len), 16);
716 uint32_t activeBits = Tmp.getActiveBits();
717 if (activeBits > 0 && activeBits < bits)
718 Tmp = Tmp.trunc(activeBits);
719 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
720 return lltok::APSInt;
723 // If this is "cc1234", return this as just "cc".
724 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
729 // Finally, if this isn't known, return an error.
735 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
736 /// that this is not a label:
737 /// HexFPConstant 0x[0-9A-Fa-f]+
738 /// HexFP80Constant 0xK[0-9A-Fa-f]+
739 /// HexFP128Constant 0xL[0-9A-Fa-f]+
740 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
741 /// HexHalfConstant 0xH[0-9A-Fa-f]+
742 lltok::Kind LLLexer::Lex0x() {
743 CurPtr = TokStart + 2;
746 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
752 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
753 // Bad token, return it as an error.
758 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
762 // HexFPConstant - Floating point constant represented in IEEE format as a
763 // hexadecimal number for when exponential notation is not precise enough.
764 // Half, Float, and double only.
765 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
766 return lltok::APFloat;
771 default: llvm_unreachable("Unknown kind!");
773 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
774 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
775 APFloatVal = APFloat(APFloat::x87DoubleExtended, APInt(80, Pair));
776 return lltok::APFloat;
778 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
779 HexToIntPair(TokStart+3, CurPtr, Pair);
780 APFloatVal = APFloat(APFloat::IEEEquad, APInt(128, Pair));
781 return lltok::APFloat;
783 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
784 HexToIntPair(TokStart+3, CurPtr, Pair);
785 APFloatVal = APFloat(APFloat::PPCDoubleDouble, APInt(128, Pair));
786 return lltok::APFloat;
788 APFloatVal = APFloat(APFloat::IEEEhalf,
789 APInt(16,HexIntToVal(TokStart+3, CurPtr)));
790 return lltok::APFloat;
794 /// LexIdentifier: Handle several related productions:
795 /// Label [-a-zA-Z$._0-9]+:
797 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
799 /// HexFPConstant 0x[0-9A-Fa-f]+
800 /// HexFP80Constant 0xK[0-9A-Fa-f]+
801 /// HexFP128Constant 0xL[0-9A-Fa-f]+
802 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
803 lltok::Kind LLLexer::LexDigitOrNegative() {
804 // If the letter after the negative is not a number, this is probably a label.
805 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
806 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
807 // Okay, this is not a number after the -, it's probably a label.
808 if (const char *End = isLabelTail(CurPtr)) {
809 StrVal.assign(TokStart, End-1);
811 return lltok::LabelStr;
817 // At this point, it is either a label, int or fp constant.
819 // Skip digits, we have at least one.
820 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
823 // Check to see if this really is a label afterall, e.g. "-1:".
824 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
825 if (const char *End = isLabelTail(CurPtr)) {
826 StrVal.assign(TokStart, End-1);
828 return lltok::LabelStr;
832 // If the next character is a '.', then it is a fp value, otherwise its
834 if (CurPtr[0] != '.') {
835 if (TokStart[0] == '0' && TokStart[1] == 'x')
837 unsigned Len = CurPtr-TokStart;
838 uint32_t numBits = ((Len * 64) / 19) + 2;
839 APInt Tmp(numBits, StringRef(TokStart, Len), 10);
840 if (TokStart[0] == '-') {
841 uint32_t minBits = Tmp.getMinSignedBits();
842 if (minBits > 0 && minBits < numBits)
843 Tmp = Tmp.trunc(minBits);
844 APSIntVal = APSInt(Tmp, false);
846 uint32_t activeBits = Tmp.getActiveBits();
847 if (activeBits > 0 && activeBits < numBits)
848 Tmp = Tmp.trunc(activeBits);
849 APSIntVal = APSInt(Tmp, true);
851 return lltok::APSInt;
856 // Skip over [0-9]*([eE][-+]?[0-9]+)?
857 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
859 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
860 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
861 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
862 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
864 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
868 APFloatVal = APFloat(std::atof(TokStart));
869 return lltok::APFloat;
872 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
873 lltok::Kind LLLexer::LexPositive() {
874 // If the letter after the negative is a number, this is probably not a
876 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
880 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
883 // At this point, we need a '.'.
884 if (CurPtr[0] != '.') {
891 // Skip over [0-9]*([eE][-+]?[0-9]+)?
892 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
894 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
895 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
896 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
897 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
899 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
903 APFloatVal = APFloat(std::atof(TokStart));
904 return lltok::APFloat;