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 return lltok::GlobalVar;
283 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
285 return lltok::GlobalVar;
287 // Handle GlobalVarID: @[0-9]+
288 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
289 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
292 uint64_t Val = atoull(TokStart+1, CurPtr);
293 if ((unsigned)Val != Val)
294 Error("invalid value number (too large)!");
295 UIntVal = unsigned(Val);
296 return lltok::GlobalID;
302 /// ReadString - Read a string until the closing quote.
303 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
304 const char *Start = CurPtr;
306 int CurChar = getNextChar();
308 if (CurChar == EOF) {
309 Error("end of file in string constant");
312 if (CurChar == '"') {
313 StrVal.assign(Start, CurPtr-1);
314 UnEscapeLexed(StrVal);
320 /// ReadVarName - Read the rest of a token containing a variable name.
321 bool LLLexer::ReadVarName() {
322 const char *NameStart = CurPtr;
323 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
324 CurPtr[0] == '-' || CurPtr[0] == '$' ||
325 CurPtr[0] == '.' || CurPtr[0] == '_') {
327 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
328 CurPtr[0] == '-' || CurPtr[0] == '$' ||
329 CurPtr[0] == '.' || CurPtr[0] == '_')
332 StrVal.assign(NameStart, CurPtr);
338 /// LexPercent - Lex all tokens that start with a % character:
339 /// LocalVar ::= %\"[^\"]*\"
340 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
341 /// LocalVarID ::= %[0-9]+
342 lltok::Kind LLLexer::LexPercent() {
343 // Handle LocalVarName: %\"[^\"]*\"
344 if (CurPtr[0] == '"') {
346 return ReadString(lltok::LocalVar);
349 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
351 return lltok::LocalVar;
353 // Handle LocalVarID: %[0-9]+
354 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
355 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
358 uint64_t Val = atoull(TokStart+1, CurPtr);
359 if ((unsigned)Val != Val)
360 Error("invalid value number (too large)!");
361 UIntVal = unsigned(Val);
362 return lltok::LocalVarID;
368 /// LexQuote - Lex all tokens that start with a " character:
369 /// QuoteLabel "[^"]+":
370 /// StringConstant "[^"]*"
371 lltok::Kind LLLexer::LexQuote() {
372 lltok::Kind kind = ReadString(lltok::StringConstant);
373 if (kind == lltok::Error || kind == lltok::Eof)
376 if (CurPtr[0] == ':') {
378 kind = lltok::LabelStr;
387 lltok::Kind LLLexer::LexExclaim() {
388 // Lex a metadata name as a MetadataVar.
389 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
390 CurPtr[0] == '-' || CurPtr[0] == '$' ||
391 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
393 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
394 CurPtr[0] == '-' || CurPtr[0] == '$' ||
395 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
398 StrVal.assign(TokStart+1, CurPtr); // Skip !
399 UnEscapeLexed(StrVal);
400 return lltok::MetadataVar;
402 return lltok::exclaim;
405 /// LexHash - Lex all tokens that start with a # character:
406 /// AttrGrpID ::= #[0-9]+
407 lltok::Kind LLLexer::LexHash() {
408 // Handle AttrGrpID: #[0-9]+
409 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
410 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
413 uint64_t Val = atoull(TokStart+1, CurPtr);
414 if ((unsigned)Val != Val)
415 Error("invalid value number (too large)!");
416 UIntVal = unsigned(Val);
417 return lltok::AttrGrpID;
423 /// LexIdentifier: Handle several related productions:
424 /// Label [-a-zA-Z$._0-9]+:
425 /// IntegerType i[0-9]+
426 /// Keyword sdiv, float, ...
427 /// HexIntConstant [us]0x[0-9A-Fa-f]+
428 lltok::Kind LLLexer::LexIdentifier() {
429 const char *StartChar = CurPtr;
430 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
431 const char *KeywordEnd = 0;
433 for (; isLabelChar(*CurPtr); ++CurPtr) {
434 // If we decide this is an integer, remember the end of the sequence.
435 if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
437 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
442 // If we stopped due to a colon, this really is a label.
443 if (*CurPtr == ':') {
444 StrVal.assign(StartChar-1, CurPtr++);
445 return lltok::LabelStr;
448 // Otherwise, this wasn't a label. If this was valid as an integer type,
450 if (IntEnd == 0) IntEnd = CurPtr;
451 if (IntEnd != StartChar) {
453 uint64_t NumBits = atoull(StartChar, CurPtr);
454 if (NumBits < IntegerType::MIN_INT_BITS ||
455 NumBits > IntegerType::MAX_INT_BITS) {
456 Error("bitwidth for integer type out of range!");
459 TyVal = IntegerType::get(Context, NumBits);
463 // Otherwise, this was a letter sequence. See which keyword this is.
464 if (KeywordEnd == 0) KeywordEnd = CurPtr;
467 unsigned Len = CurPtr-StartChar;
468 #define KEYWORD(STR) \
470 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
471 return lltok::kw_##STR; \
474 KEYWORD(true); KEYWORD(false);
475 KEYWORD(declare); KEYWORD(define);
476 KEYWORD(global); KEYWORD(constant);
479 KEYWORD(linker_private);
480 KEYWORD(linker_private_weak);
481 KEYWORD(linker_private_weak_def_auto); // FIXME: For backwards compatibility.
483 KEYWORD(available_externally);
485 KEYWORD(linkonce_odr);
486 KEYWORD(linkonce_odr_auto_hide);
496 KEYWORD(unnamed_addr);
497 KEYWORD(externally_initialized);
498 KEYWORD(extern_weak);
500 KEYWORD(thread_local);
501 KEYWORD(localdynamic);
502 KEYWORD(initialexec);
504 KEYWORD(zeroinitializer);
512 KEYWORD(deplibs); // FIXME: Remove in 4.0.
522 KEYWORD(singlethread);
541 KEYWORD(inteldialect);
548 KEYWORD(x86_stdcallcc);
549 KEYWORD(x86_fastcallcc);
550 KEYWORD(x86_thiscallcc);
552 KEYWORD(arm_aapcscc);
553 KEYWORD(arm_aapcs_vfpcc);
554 KEYWORD(msp430_intrcc);
557 KEYWORD(spir_kernel);
559 KEYWORD(intel_ocl_bicc);
560 KEYWORD(x86_64_sysvcc);
561 KEYWORD(x86_64_win64cc);
562 KEYWORD(webkit_jscc);
569 KEYWORD(alwaysinline);
581 KEYWORD(noduplicate);
582 KEYWORD(noimplicitfloat);
584 KEYWORD(nonlazybind);
593 KEYWORD(returns_twice);
599 KEYWORD(sanitize_address);
600 KEYWORD(sanitize_thread);
601 KEYWORD(sanitize_memory);
608 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
609 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
610 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
611 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
613 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
617 KEYWORD(blockaddress);
619 KEYWORD(personality);
625 // Keywords for types.
626 #define TYPEKEYWORD(STR, LLVMTY) \
627 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
628 TyVal = LLVMTY; return lltok::Type; }
629 TYPEKEYWORD("void", Type::getVoidTy(Context));
630 TYPEKEYWORD("half", Type::getHalfTy(Context));
631 TYPEKEYWORD("float", Type::getFloatTy(Context));
632 TYPEKEYWORD("double", Type::getDoubleTy(Context));
633 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
634 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
635 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
636 TYPEKEYWORD("label", Type::getLabelTy(Context));
637 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
638 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
641 // Keywords for instructions.
642 #define INSTKEYWORD(STR, Enum) \
643 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
644 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
646 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
647 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
648 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
649 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
650 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
651 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
652 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
653 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
655 INSTKEYWORD(phi, PHI);
656 INSTKEYWORD(call, Call);
657 INSTKEYWORD(trunc, Trunc);
658 INSTKEYWORD(zext, ZExt);
659 INSTKEYWORD(sext, SExt);
660 INSTKEYWORD(fptrunc, FPTrunc);
661 INSTKEYWORD(fpext, FPExt);
662 INSTKEYWORD(uitofp, UIToFP);
663 INSTKEYWORD(sitofp, SIToFP);
664 INSTKEYWORD(fptoui, FPToUI);
665 INSTKEYWORD(fptosi, FPToSI);
666 INSTKEYWORD(inttoptr, IntToPtr);
667 INSTKEYWORD(ptrtoint, PtrToInt);
668 INSTKEYWORD(bitcast, BitCast);
669 INSTKEYWORD(select, Select);
670 INSTKEYWORD(va_arg, VAArg);
671 INSTKEYWORD(ret, Ret);
673 INSTKEYWORD(switch, Switch);
674 INSTKEYWORD(indirectbr, IndirectBr);
675 INSTKEYWORD(invoke, Invoke);
676 INSTKEYWORD(resume, Resume);
677 INSTKEYWORD(unreachable, Unreachable);
679 INSTKEYWORD(alloca, Alloca);
680 INSTKEYWORD(load, Load);
681 INSTKEYWORD(store, Store);
682 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
683 INSTKEYWORD(atomicrmw, AtomicRMW);
684 INSTKEYWORD(fence, Fence);
685 INSTKEYWORD(getelementptr, GetElementPtr);
687 INSTKEYWORD(extractelement, ExtractElement);
688 INSTKEYWORD(insertelement, InsertElement);
689 INSTKEYWORD(shufflevector, ShuffleVector);
690 INSTKEYWORD(extractvalue, ExtractValue);
691 INSTKEYWORD(insertvalue, InsertValue);
692 INSTKEYWORD(landingpad, LandingPad);
695 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
696 // the CFE to avoid forcing it to deal with 64-bit numbers.
697 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
698 TokStart[1] == '0' && TokStart[2] == 'x' &&
699 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
700 int len = CurPtr-TokStart-3;
701 uint32_t bits = len * 4;
702 APInt Tmp(bits, StringRef(TokStart+3, len), 16);
703 uint32_t activeBits = Tmp.getActiveBits();
704 if (activeBits > 0 && activeBits < bits)
705 Tmp = Tmp.trunc(activeBits);
706 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
707 return lltok::APSInt;
710 // If this is "cc1234", return this as just "cc".
711 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
716 // Finally, if this isn't known, return an error.
722 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
723 /// that this is not a label:
724 /// HexFPConstant 0x[0-9A-Fa-f]+
725 /// HexFP80Constant 0xK[0-9A-Fa-f]+
726 /// HexFP128Constant 0xL[0-9A-Fa-f]+
727 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
728 /// HexHalfConstant 0xH[0-9A-Fa-f]+
729 lltok::Kind LLLexer::Lex0x() {
730 CurPtr = TokStart + 2;
733 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
739 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
740 // Bad token, return it as an error.
745 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
749 // HexFPConstant - Floating point constant represented in IEEE format as a
750 // hexadecimal number for when exponential notation is not precise enough.
751 // Half, Float, and double only.
752 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
753 return lltok::APFloat;
758 default: llvm_unreachable("Unknown kind!");
760 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
761 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
762 APFloatVal = APFloat(APFloat::x87DoubleExtended, APInt(80, Pair));
763 return lltok::APFloat;
765 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
766 HexToIntPair(TokStart+3, CurPtr, Pair);
767 APFloatVal = APFloat(APFloat::IEEEquad, APInt(128, Pair));
768 return lltok::APFloat;
770 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
771 HexToIntPair(TokStart+3, CurPtr, Pair);
772 APFloatVal = APFloat(APFloat::PPCDoubleDouble, APInt(128, Pair));
773 return lltok::APFloat;
775 APFloatVal = APFloat(APFloat::IEEEhalf,
776 APInt(16,HexIntToVal(TokStart+3, CurPtr)));
777 return lltok::APFloat;
781 /// LexIdentifier: Handle several related productions:
782 /// Label [-a-zA-Z$._0-9]+:
784 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
786 /// HexFPConstant 0x[0-9A-Fa-f]+
787 /// HexFP80Constant 0xK[0-9A-Fa-f]+
788 /// HexFP128Constant 0xL[0-9A-Fa-f]+
789 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
790 lltok::Kind LLLexer::LexDigitOrNegative() {
791 // If the letter after the negative is not a number, this is probably a label.
792 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
793 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
794 // Okay, this is not a number after the -, it's probably a label.
795 if (const char *End = isLabelTail(CurPtr)) {
796 StrVal.assign(TokStart, End-1);
798 return lltok::LabelStr;
804 // At this point, it is either a label, int or fp constant.
806 // Skip digits, we have at least one.
807 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
810 // Check to see if this really is a label afterall, e.g. "-1:".
811 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
812 if (const char *End = isLabelTail(CurPtr)) {
813 StrVal.assign(TokStart, End-1);
815 return lltok::LabelStr;
819 // If the next character is a '.', then it is a fp value, otherwise its
821 if (CurPtr[0] != '.') {
822 if (TokStart[0] == '0' && TokStart[1] == 'x')
824 unsigned Len = CurPtr-TokStart;
825 uint32_t numBits = ((Len * 64) / 19) + 2;
826 APInt Tmp(numBits, StringRef(TokStart, Len), 10);
827 if (TokStart[0] == '-') {
828 uint32_t minBits = Tmp.getMinSignedBits();
829 if (minBits > 0 && minBits < numBits)
830 Tmp = Tmp.trunc(minBits);
831 APSIntVal = APSInt(Tmp, false);
833 uint32_t activeBits = Tmp.getActiveBits();
834 if (activeBits > 0 && activeBits < numBits)
835 Tmp = Tmp.trunc(activeBits);
836 APSIntVal = APSInt(Tmp, true);
838 return lltok::APSInt;
843 // Skip over [0-9]*([eE][-+]?[0-9]+)?
844 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
846 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
847 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
848 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
849 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
851 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
855 APFloatVal = APFloat(std::atof(TokStart));
856 return lltok::APFloat;
859 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
860 lltok::Kind LLLexer::LexPositive() {
861 // If the letter after the negative is a number, this is probably not a
863 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
867 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
870 // At this point, we need a '.'.
871 if (CurPtr[0] != '.') {
878 // Skip over [0-9]*([eE][-+]?[0-9]+)?
879 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
881 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
882 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
883 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
884 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
886 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
890 APFloatVal = APFloat(std::atof(TokStart));
891 return lltok::APFloat;