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(StringRef StartBuf, SourceMgr &sm, SMDiagnostic &Err,
166 : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
167 CurPtr = CurBuf.begin();
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.end())
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 LexDollar();
209 case '%': return LexPercent();
210 case '"': return LexQuote();
212 if (const char *Ptr = isLabelTail(CurPtr)) {
214 StrVal.assign(TokStart, CurPtr-1);
215 return lltok::LabelStr;
217 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
219 return lltok::dotdotdot;
225 case '!': return LexExclaim();
226 case '#': return LexHash();
227 case '0': case '1': case '2': case '3': case '4':
228 case '5': case '6': case '7': case '8': case '9':
230 return LexDigitOrNegative();
231 case '=': return lltok::equal;
232 case '[': return lltok::lsquare;
233 case ']': return lltok::rsquare;
234 case '{': return lltok::lbrace;
235 case '}': return lltok::rbrace;
236 case '<': return lltok::less;
237 case '>': return lltok::greater;
238 case '(': return lltok::lparen;
239 case ')': return lltok::rparen;
240 case ',': return lltok::comma;
241 case '*': return lltok::star;
242 case '\\': return lltok::backslash;
246 void LLLexer::SkipLineComment() {
248 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
253 /// LexAt - Lex all tokens that start with an @ character:
254 /// GlobalVar @\"[^\"]*\"
255 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
256 /// GlobalVarID @[0-9]+
257 lltok::Kind LLLexer::LexAt() {
258 // Handle AtStringConstant: @\"[^\"]*\"
259 if (CurPtr[0] == '"') {
263 int CurChar = getNextChar();
265 if (CurChar == EOF) {
266 Error("end of file in global variable name");
269 if (CurChar == '"') {
270 StrVal.assign(TokStart+2, CurPtr-1);
271 UnEscapeLexed(StrVal);
272 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
273 Error("Null bytes are not allowed in names");
276 return lltok::GlobalVar;
281 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
283 return lltok::GlobalVar;
285 // Handle GlobalVarID: @[0-9]+
286 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
287 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
290 uint64_t Val = atoull(TokStart+1, CurPtr);
291 if ((unsigned)Val != Val)
292 Error("invalid value number (too large)!");
293 UIntVal = unsigned(Val);
294 return lltok::GlobalID;
300 lltok::Kind LLLexer::LexDollar() {
301 if (const char *Ptr = isLabelTail(TokStart)) {
303 StrVal.assign(TokStart, CurPtr - 1);
304 return lltok::LabelStr;
307 // Handle DollarStringConstant: $\"[^\"]*\"
308 if (CurPtr[0] == '"') {
312 int CurChar = getNextChar();
314 if (CurChar == EOF) {
315 Error("end of file in COMDAT variable name");
318 if (CurChar == '"') {
319 StrVal.assign(TokStart + 2, CurPtr - 1);
320 UnEscapeLexed(StrVal);
321 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
322 Error("Null bytes are not allowed in names");
325 return lltok::ComdatVar;
330 // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
332 return lltok::ComdatVar;
337 /// ReadString - Read a string until the closing quote.
338 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
339 const char *Start = CurPtr;
341 int CurChar = getNextChar();
343 if (CurChar == EOF) {
344 Error("end of file in string constant");
347 if (CurChar == '"') {
348 StrVal.assign(Start, CurPtr-1);
349 UnEscapeLexed(StrVal);
355 /// ReadVarName - Read the rest of a token containing a variable name.
356 bool LLLexer::ReadVarName() {
357 const char *NameStart = CurPtr;
358 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
359 CurPtr[0] == '-' || CurPtr[0] == '$' ||
360 CurPtr[0] == '.' || CurPtr[0] == '_') {
362 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
363 CurPtr[0] == '-' || CurPtr[0] == '$' ||
364 CurPtr[0] == '.' || CurPtr[0] == '_')
367 StrVal.assign(NameStart, CurPtr);
373 /// LexPercent - Lex all tokens that start with a % character:
374 /// LocalVar ::= %\"[^\"]*\"
375 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
376 /// LocalVarID ::= %[0-9]+
377 lltok::Kind LLLexer::LexPercent() {
378 // Handle LocalVarName: %\"[^\"]*\"
379 if (CurPtr[0] == '"') {
381 return ReadString(lltok::LocalVar);
384 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
386 return lltok::LocalVar;
388 // Handle LocalVarID: %[0-9]+
389 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
390 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
393 uint64_t Val = atoull(TokStart+1, CurPtr);
394 if ((unsigned)Val != Val)
395 Error("invalid value number (too large)!");
396 UIntVal = unsigned(Val);
397 return lltok::LocalVarID;
403 /// LexQuote - Lex all tokens that start with a " character:
404 /// QuoteLabel "[^"]+":
405 /// StringConstant "[^"]*"
406 lltok::Kind LLLexer::LexQuote() {
407 lltok::Kind kind = ReadString(lltok::StringConstant);
408 if (kind == lltok::Error || kind == lltok::Eof)
411 if (CurPtr[0] == ':') {
413 kind = lltok::LabelStr;
422 lltok::Kind LLLexer::LexExclaim() {
423 // Lex a metadata name as a MetadataVar.
424 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
425 CurPtr[0] == '-' || CurPtr[0] == '$' ||
426 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
428 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
429 CurPtr[0] == '-' || CurPtr[0] == '$' ||
430 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
433 StrVal.assign(TokStart+1, CurPtr); // Skip !
434 UnEscapeLexed(StrVal);
435 return lltok::MetadataVar;
437 return lltok::exclaim;
440 /// LexHash - Lex all tokens that start with a # character:
441 /// AttrGrpID ::= #[0-9]+
442 lltok::Kind LLLexer::LexHash() {
443 // Handle AttrGrpID: #[0-9]+
444 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
445 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
448 uint64_t Val = atoull(TokStart+1, CurPtr);
449 if ((unsigned)Val != Val)
450 Error("invalid value number (too large)!");
451 UIntVal = unsigned(Val);
452 return lltok::AttrGrpID;
458 /// LexIdentifier: Handle several related productions:
459 /// Label [-a-zA-Z$._0-9]+:
460 /// IntegerType i[0-9]+
461 /// Keyword sdiv, float, ...
462 /// HexIntConstant [us]0x[0-9A-Fa-f]+
463 lltok::Kind LLLexer::LexIdentifier() {
464 const char *StartChar = CurPtr;
465 const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar;
466 const char *KeywordEnd = nullptr;
468 for (; isLabelChar(*CurPtr); ++CurPtr) {
469 // If we decide this is an integer, remember the end of the sequence.
470 if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
472 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
477 // If we stopped due to a colon, this really is a label.
478 if (*CurPtr == ':') {
479 StrVal.assign(StartChar-1, CurPtr++);
480 return lltok::LabelStr;
483 // Otherwise, this wasn't a label. If this was valid as an integer type,
485 if (!IntEnd) IntEnd = CurPtr;
486 if (IntEnd != StartChar) {
488 uint64_t NumBits = atoull(StartChar, CurPtr);
489 if (NumBits < IntegerType::MIN_INT_BITS ||
490 NumBits > IntegerType::MAX_INT_BITS) {
491 Error("bitwidth for integer type out of range!");
494 TyVal = IntegerType::get(Context, NumBits);
498 // Otherwise, this was a letter sequence. See which keyword this is.
499 if (!KeywordEnd) KeywordEnd = CurPtr;
502 unsigned Len = CurPtr-StartChar;
503 #define KEYWORD(STR) \
505 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
506 return lltok::kw_##STR; \
509 KEYWORD(true); KEYWORD(false);
510 KEYWORD(declare); KEYWORD(define);
511 KEYWORD(global); KEYWORD(constant);
515 KEYWORD(available_externally);
517 KEYWORD(linkonce_odr);
518 KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
527 KEYWORD(unnamed_addr);
528 KEYWORD(externally_initialized);
529 KEYWORD(extern_weak);
531 KEYWORD(thread_local);
532 KEYWORD(localdynamic);
533 KEYWORD(initialexec);
535 KEYWORD(zeroinitializer);
544 KEYWORD(deplibs); // FIXME: Remove in 4.0.
554 KEYWORD(singlethread);
573 KEYWORD(inteldialect);
580 KEYWORD(x86_stdcallcc);
581 KEYWORD(x86_fastcallcc);
582 KEYWORD(x86_thiscallcc);
584 KEYWORD(arm_aapcscc);
585 KEYWORD(arm_aapcs_vfpcc);
586 KEYWORD(msp430_intrcc);
589 KEYWORD(spir_kernel);
591 KEYWORD(intel_ocl_bicc);
592 KEYWORD(x86_64_sysvcc);
593 KEYWORD(x86_64_win64cc);
594 KEYWORD(webkit_jscc);
596 KEYWORD(preserve_mostcc);
597 KEYWORD(preserve_allcc);
604 KEYWORD(alwaysinline);
609 KEYWORD(dereferenceable);
619 KEYWORD(noduplicate);
620 KEYWORD(noimplicitfloat);
622 KEYWORD(nonlazybind);
632 KEYWORD(returns_twice);
638 KEYWORD(sanitize_address);
639 KEYWORD(sanitize_thread);
640 KEYWORD(sanitize_memory);
653 KEYWORD(noduplicates);
656 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
657 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
658 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
659 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
661 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
665 KEYWORD(blockaddress);
667 KEYWORD(personality);
673 // Keywords for types.
674 #define TYPEKEYWORD(STR, LLVMTY) \
675 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
676 TyVal = LLVMTY; return lltok::Type; }
677 TYPEKEYWORD("void", Type::getVoidTy(Context));
678 TYPEKEYWORD("half", Type::getHalfTy(Context));
679 TYPEKEYWORD("float", Type::getFloatTy(Context));
680 TYPEKEYWORD("double", Type::getDoubleTy(Context));
681 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
682 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
683 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
684 TYPEKEYWORD("label", Type::getLabelTy(Context));
685 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
686 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
689 // Keywords for instructions.
690 #define INSTKEYWORD(STR, Enum) \
691 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
692 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
694 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
695 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
696 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
697 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
698 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
699 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
700 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
701 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
703 INSTKEYWORD(phi, PHI);
704 INSTKEYWORD(call, Call);
705 INSTKEYWORD(trunc, Trunc);
706 INSTKEYWORD(zext, ZExt);
707 INSTKEYWORD(sext, SExt);
708 INSTKEYWORD(fptrunc, FPTrunc);
709 INSTKEYWORD(fpext, FPExt);
710 INSTKEYWORD(uitofp, UIToFP);
711 INSTKEYWORD(sitofp, SIToFP);
712 INSTKEYWORD(fptoui, FPToUI);
713 INSTKEYWORD(fptosi, FPToSI);
714 INSTKEYWORD(inttoptr, IntToPtr);
715 INSTKEYWORD(ptrtoint, PtrToInt);
716 INSTKEYWORD(bitcast, BitCast);
717 INSTKEYWORD(addrspacecast, AddrSpaceCast);
718 INSTKEYWORD(select, Select);
719 INSTKEYWORD(va_arg, VAArg);
720 INSTKEYWORD(ret, Ret);
722 INSTKEYWORD(switch, Switch);
723 INSTKEYWORD(indirectbr, IndirectBr);
724 INSTKEYWORD(invoke, Invoke);
725 INSTKEYWORD(resume, Resume);
726 INSTKEYWORD(unreachable, Unreachable);
728 INSTKEYWORD(alloca, Alloca);
729 INSTKEYWORD(load, Load);
730 INSTKEYWORD(store, Store);
731 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
732 INSTKEYWORD(atomicrmw, AtomicRMW);
733 INSTKEYWORD(fence, Fence);
734 INSTKEYWORD(getelementptr, GetElementPtr);
736 INSTKEYWORD(extractelement, ExtractElement);
737 INSTKEYWORD(insertelement, InsertElement);
738 INSTKEYWORD(shufflevector, ShuffleVector);
739 INSTKEYWORD(extractvalue, ExtractValue);
740 INSTKEYWORD(insertvalue, InsertValue);
741 INSTKEYWORD(landingpad, LandingPad);
744 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
745 // the CFE to avoid forcing it to deal with 64-bit numbers.
746 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
747 TokStart[1] == '0' && TokStart[2] == 'x' &&
748 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
749 int len = CurPtr-TokStart-3;
750 uint32_t bits = len * 4;
751 APInt Tmp(bits, StringRef(TokStart+3, len), 16);
752 uint32_t activeBits = Tmp.getActiveBits();
753 if (activeBits > 0 && activeBits < bits)
754 Tmp = Tmp.trunc(activeBits);
755 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
756 return lltok::APSInt;
759 // If this is "cc1234", return this as just "cc".
760 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
765 // Finally, if this isn't known, return an error.
771 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
772 /// that this is not a label:
773 /// HexFPConstant 0x[0-9A-Fa-f]+
774 /// HexFP80Constant 0xK[0-9A-Fa-f]+
775 /// HexFP128Constant 0xL[0-9A-Fa-f]+
776 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
777 /// HexHalfConstant 0xH[0-9A-Fa-f]+
778 lltok::Kind LLLexer::Lex0x() {
779 CurPtr = TokStart + 2;
782 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
788 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
789 // Bad token, return it as an error.
794 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
798 // HexFPConstant - Floating point constant represented in IEEE format as a
799 // hexadecimal number for when exponential notation is not precise enough.
800 // Half, Float, and double only.
801 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
802 return lltok::APFloat;
807 default: llvm_unreachable("Unknown kind!");
809 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
810 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
811 APFloatVal = APFloat(APFloat::x87DoubleExtended, APInt(80, Pair));
812 return lltok::APFloat;
814 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
815 HexToIntPair(TokStart+3, CurPtr, Pair);
816 APFloatVal = APFloat(APFloat::IEEEquad, APInt(128, Pair));
817 return lltok::APFloat;
819 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
820 HexToIntPair(TokStart+3, CurPtr, Pair);
821 APFloatVal = APFloat(APFloat::PPCDoubleDouble, APInt(128, Pair));
822 return lltok::APFloat;
824 APFloatVal = APFloat(APFloat::IEEEhalf,
825 APInt(16,HexIntToVal(TokStart+3, CurPtr)));
826 return lltok::APFloat;
830 /// LexIdentifier: Handle several related productions:
831 /// Label [-a-zA-Z$._0-9]+:
833 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
835 /// HexFPConstant 0x[0-9A-Fa-f]+
836 /// HexFP80Constant 0xK[0-9A-Fa-f]+
837 /// HexFP128Constant 0xL[0-9A-Fa-f]+
838 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
839 lltok::Kind LLLexer::LexDigitOrNegative() {
840 // If the letter after the negative is not a number, this is probably a label.
841 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
842 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
843 // Okay, this is not a number after the -, it's probably a label.
844 if (const char *End = isLabelTail(CurPtr)) {
845 StrVal.assign(TokStart, End-1);
847 return lltok::LabelStr;
853 // At this point, it is either a label, int or fp constant.
855 // Skip digits, we have at least one.
856 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
859 // Check to see if this really is a label afterall, e.g. "-1:".
860 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
861 if (const char *End = isLabelTail(CurPtr)) {
862 StrVal.assign(TokStart, End-1);
864 return lltok::LabelStr;
868 // If the next character is a '.', then it is a fp value, otherwise its
870 if (CurPtr[0] != '.') {
871 if (TokStart[0] == '0' && TokStart[1] == 'x')
873 unsigned Len = CurPtr-TokStart;
874 uint32_t numBits = ((Len * 64) / 19) + 2;
875 APInt Tmp(numBits, StringRef(TokStart, Len), 10);
876 if (TokStart[0] == '-') {
877 uint32_t minBits = Tmp.getMinSignedBits();
878 if (minBits > 0 && minBits < numBits)
879 Tmp = Tmp.trunc(minBits);
880 APSIntVal = APSInt(Tmp, false);
882 uint32_t activeBits = Tmp.getActiveBits();
883 if (activeBits > 0 && activeBits < numBits)
884 Tmp = Tmp.trunc(activeBits);
885 APSIntVal = APSInt(Tmp, true);
887 return lltok::APSInt;
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;
908 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
909 lltok::Kind LLLexer::LexPositive() {
910 // If the letter after the negative is a number, this is probably not a
912 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
916 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
919 // At this point, we need a '.'.
920 if (CurPtr[0] != '.') {
927 // Skip over [0-9]*([eE][-+]?[0-9]+)?
928 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
930 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
931 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
932 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
933 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
935 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
939 APFloatVal = APFloat(std::atof(TokStart));
940 return lltok::APFloat;