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 if (End - Buffer >= 16) {
82 for (int i = 0; i < 16; i++, Buffer++) {
83 assert(Buffer != End);
85 Pair[0] += hexDigitValue(*Buffer);
89 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
91 Pair[1] += hexDigitValue(*Buffer);
94 Error("constant bigger than 128 bits detected!");
97 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
98 /// { low64, high16 } as usual for an APInt.
99 void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
102 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
103 assert(Buffer != End);
105 Pair[1] += hexDigitValue(*Buffer);
108 for (int i=0; i<16; i++, Buffer++) {
110 Pair[0] += hexDigitValue(*Buffer);
113 Error("constant bigger than 128 bits detected!");
116 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
117 // appropriate character.
118 static void UnEscapeLexed(std::string &Str) {
119 if (Str.empty()) return;
121 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
123 for (char *BIn = Buffer; BIn != EndBuffer; ) {
124 if (BIn[0] == '\\') {
125 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
126 *BOut++ = '\\'; // Two \ becomes one
128 } else if (BIn < EndBuffer-2 &&
129 isxdigit(static_cast<unsigned char>(BIn[1])) &&
130 isxdigit(static_cast<unsigned char>(BIn[2]))) {
131 *BOut = hexDigitValue(BIn[1]) * 16 + hexDigitValue(BIn[2]);
132 BIn += 3; // Skip over handled chars
141 Str.resize(BOut-Buffer);
144 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
145 static bool isLabelChar(char C) {
146 return isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
147 C == '.' || C == '_';
151 /// isLabelTail - Return true if this pointer points to a valid end of a label.
152 static const char *isLabelTail(const char *CurPtr) {
154 if (CurPtr[0] == ':') return CurPtr+1;
155 if (!isLabelChar(CurPtr[0])) return nullptr;
162 //===----------------------------------------------------------------------===//
164 //===----------------------------------------------------------------------===//
166 LLLexer::LLLexer(StringRef StartBuf, SourceMgr &sm, SMDiagnostic &Err,
168 : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
169 CurPtr = CurBuf.begin();
172 int LLLexer::getNextChar() {
173 char CurChar = *CurPtr++;
175 default: return (unsigned char)CurChar;
177 // A nul character in the stream is either the end of the current buffer or
178 // a random nul in the file. Disambiguate that here.
179 if (CurPtr-1 != CurBuf.end())
180 return 0; // Just whitespace.
182 // Otherwise, return end of file.
183 --CurPtr; // Another call to lex will return EOF again.
189 lltok::Kind LLLexer::LexToken() {
192 int CurChar = getNextChar();
195 // Handle letters: [a-zA-Z_]
196 if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
197 return LexIdentifier();
200 case EOF: return lltok::Eof;
206 // Ignore whitespace.
208 case '+': return LexPositive();
209 case '@': return LexAt();
210 case '$': return LexDollar();
211 case '%': return LexPercent();
212 case '"': return LexQuote();
214 if (const char *Ptr = isLabelTail(CurPtr)) {
216 StrVal.assign(TokStart, CurPtr-1);
217 return lltok::LabelStr;
219 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
221 return lltok::dotdotdot;
227 case '!': return LexExclaim();
228 case '#': return LexHash();
229 case '0': case '1': case '2': case '3': case '4':
230 case '5': case '6': case '7': case '8': case '9':
232 return LexDigitOrNegative();
233 case '=': return lltok::equal;
234 case '[': return lltok::lsquare;
235 case ']': return lltok::rsquare;
236 case '{': return lltok::lbrace;
237 case '}': return lltok::rbrace;
238 case '<': return lltok::less;
239 case '>': return lltok::greater;
240 case '(': return lltok::lparen;
241 case ')': return lltok::rparen;
242 case ',': return lltok::comma;
243 case '*': return lltok::star;
244 case '\\': return lltok::backslash;
248 void LLLexer::SkipLineComment() {
250 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
255 /// LexAt - Lex all tokens that start with an @ character:
256 /// GlobalVar @\"[^\"]*\"
257 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
258 /// GlobalVarID @[0-9]+
259 lltok::Kind LLLexer::LexAt() {
260 // Handle AtStringConstant: @\"[^\"]*\"
261 if (CurPtr[0] == '"') {
265 int CurChar = getNextChar();
267 if (CurChar == EOF) {
268 Error("end of file in global variable name");
271 if (CurChar == '"') {
272 StrVal.assign(TokStart+2, CurPtr-1);
273 UnEscapeLexed(StrVal);
274 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
275 Error("Null bytes are not allowed in names");
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 lltok::Kind LLLexer::LexDollar() {
303 if (const char *Ptr = isLabelTail(TokStart)) {
305 StrVal.assign(TokStart, CurPtr - 1);
306 return lltok::LabelStr;
309 // Handle DollarStringConstant: $\"[^\"]*\"
310 if (CurPtr[0] == '"') {
314 int CurChar = getNextChar();
316 if (CurChar == EOF) {
317 Error("end of file in COMDAT variable name");
320 if (CurChar == '"') {
321 StrVal.assign(TokStart + 2, CurPtr - 1);
322 UnEscapeLexed(StrVal);
323 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
324 Error("Null bytes are not allowed in names");
327 return lltok::ComdatVar;
332 // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
334 return lltok::ComdatVar;
339 /// ReadString - Read a string until the closing quote.
340 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
341 const char *Start = CurPtr;
343 int CurChar = getNextChar();
345 if (CurChar == EOF) {
346 Error("end of file in string constant");
349 if (CurChar == '"') {
350 StrVal.assign(Start, CurPtr-1);
351 UnEscapeLexed(StrVal);
357 /// ReadVarName - Read the rest of a token containing a variable name.
358 bool LLLexer::ReadVarName() {
359 const char *NameStart = CurPtr;
360 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
361 CurPtr[0] == '-' || CurPtr[0] == '$' ||
362 CurPtr[0] == '.' || CurPtr[0] == '_') {
364 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
365 CurPtr[0] == '-' || CurPtr[0] == '$' ||
366 CurPtr[0] == '.' || CurPtr[0] == '_')
369 StrVal.assign(NameStart, CurPtr);
375 /// LexPercent - Lex all tokens that start with a % character:
376 /// LocalVar ::= %\"[^\"]*\"
377 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
378 /// LocalVarID ::= %[0-9]+
379 lltok::Kind LLLexer::LexPercent() {
380 // Handle LocalVarName: %\"[^\"]*\"
381 if (CurPtr[0] == '"') {
383 return ReadString(lltok::LocalVar);
386 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
388 return lltok::LocalVar;
390 // Handle LocalVarID: %[0-9]+
391 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
392 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
395 uint64_t Val = atoull(TokStart+1, CurPtr);
396 if ((unsigned)Val != Val)
397 Error("invalid value number (too large)!");
398 UIntVal = unsigned(Val);
399 return lltok::LocalVarID;
405 /// LexQuote - Lex all tokens that start with a " character:
406 /// QuoteLabel "[^"]+":
407 /// StringConstant "[^"]*"
408 lltok::Kind LLLexer::LexQuote() {
409 lltok::Kind kind = ReadString(lltok::StringConstant);
410 if (kind == lltok::Error || kind == lltok::Eof)
413 if (CurPtr[0] == ':') {
415 kind = lltok::LabelStr;
424 lltok::Kind LLLexer::LexExclaim() {
425 // Lex a metadata name as a MetadataVar.
426 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
427 CurPtr[0] == '-' || CurPtr[0] == '$' ||
428 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
430 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
431 CurPtr[0] == '-' || CurPtr[0] == '$' ||
432 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
435 StrVal.assign(TokStart+1, CurPtr); // Skip !
436 UnEscapeLexed(StrVal);
437 return lltok::MetadataVar;
439 return lltok::exclaim;
442 /// LexHash - Lex all tokens that start with a # character:
443 /// AttrGrpID ::= #[0-9]+
444 lltok::Kind LLLexer::LexHash() {
445 // Handle AttrGrpID: #[0-9]+
446 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
447 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
450 uint64_t Val = atoull(TokStart+1, CurPtr);
451 if ((unsigned)Val != Val)
452 Error("invalid value number (too large)!");
453 UIntVal = unsigned(Val);
454 return lltok::AttrGrpID;
460 /// LexIdentifier: Handle several related productions:
461 /// Label [-a-zA-Z$._0-9]+:
462 /// IntegerType i[0-9]+
463 /// Keyword sdiv, float, ...
464 /// HexIntConstant [us]0x[0-9A-Fa-f]+
465 lltok::Kind LLLexer::LexIdentifier() {
466 const char *StartChar = CurPtr;
467 const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar;
468 const char *KeywordEnd = nullptr;
470 for (; isLabelChar(*CurPtr); ++CurPtr) {
471 // If we decide this is an integer, remember the end of the sequence.
472 if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
474 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
479 // If we stopped due to a colon, this really is a label.
480 if (*CurPtr == ':') {
481 StrVal.assign(StartChar-1, CurPtr++);
482 return lltok::LabelStr;
485 // Otherwise, this wasn't a label. If this was valid as an integer type,
487 if (!IntEnd) IntEnd = CurPtr;
488 if (IntEnd != StartChar) {
490 uint64_t NumBits = atoull(StartChar, CurPtr);
491 if (NumBits < IntegerType::MIN_INT_BITS ||
492 NumBits > IntegerType::MAX_INT_BITS) {
493 Error("bitwidth for integer type out of range!");
496 TyVal = IntegerType::get(Context, NumBits);
500 // Otherwise, this was a letter sequence. See which keyword this is.
501 if (!KeywordEnd) KeywordEnd = CurPtr;
504 unsigned Len = CurPtr-StartChar;
505 #define KEYWORD(STR) \
507 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
508 return lltok::kw_##STR; \
511 KEYWORD(true); KEYWORD(false);
512 KEYWORD(declare); KEYWORD(define);
513 KEYWORD(global); KEYWORD(constant);
517 KEYWORD(available_externally);
519 KEYWORD(linkonce_odr);
520 KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
529 KEYWORD(unnamed_addr);
530 KEYWORD(externally_initialized);
531 KEYWORD(extern_weak);
533 KEYWORD(thread_local);
534 KEYWORD(localdynamic);
535 KEYWORD(initialexec);
537 KEYWORD(zeroinitializer);
546 KEYWORD(deplibs); // FIXME: Remove in 4.0.
556 KEYWORD(singlethread);
575 KEYWORD(inteldialect);
583 KEYWORD(x86_stdcallcc);
584 KEYWORD(x86_fastcallcc);
585 KEYWORD(x86_thiscallcc);
586 KEYWORD(x86_vectorcallcc);
588 KEYWORD(arm_aapcscc);
589 KEYWORD(arm_aapcs_vfpcc);
590 KEYWORD(msp430_intrcc);
593 KEYWORD(spir_kernel);
595 KEYWORD(intel_ocl_bicc);
596 KEYWORD(x86_64_sysvcc);
597 KEYWORD(x86_64_win64cc);
598 KEYWORD(webkit_jscc);
600 KEYWORD(preserve_mostcc);
601 KEYWORD(preserve_allcc);
609 KEYWORD(alwaysinline);
614 KEYWORD(dereferenceable);
624 KEYWORD(noduplicate);
625 KEYWORD(noimplicitfloat);
627 KEYWORD(nonlazybind);
637 KEYWORD(returns_twice);
643 KEYWORD(sanitize_address);
644 KEYWORD(sanitize_thread);
645 KEYWORD(sanitize_memory);
658 KEYWORD(noduplicates);
661 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
662 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
663 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
664 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
666 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
670 KEYWORD(blockaddress);
672 // Use-list order directives.
673 KEYWORD(uselistorder);
674 KEYWORD(uselistorder_bb);
676 KEYWORD(personality);
682 // Keywords for types.
683 #define TYPEKEYWORD(STR, LLVMTY) \
684 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
685 TyVal = LLVMTY; return lltok::Type; }
686 TYPEKEYWORD("void", Type::getVoidTy(Context));
687 TYPEKEYWORD("half", Type::getHalfTy(Context));
688 TYPEKEYWORD("float", Type::getFloatTy(Context));
689 TYPEKEYWORD("double", Type::getDoubleTy(Context));
690 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
691 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
692 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
693 TYPEKEYWORD("label", Type::getLabelTy(Context));
694 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
695 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
698 // Keywords for instructions.
699 #define INSTKEYWORD(STR, Enum) \
700 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
701 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
703 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
704 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
705 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
706 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
707 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
708 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
709 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
710 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
712 INSTKEYWORD(phi, PHI);
713 INSTKEYWORD(call, Call);
714 INSTKEYWORD(trunc, Trunc);
715 INSTKEYWORD(zext, ZExt);
716 INSTKEYWORD(sext, SExt);
717 INSTKEYWORD(fptrunc, FPTrunc);
718 INSTKEYWORD(fpext, FPExt);
719 INSTKEYWORD(uitofp, UIToFP);
720 INSTKEYWORD(sitofp, SIToFP);
721 INSTKEYWORD(fptoui, FPToUI);
722 INSTKEYWORD(fptosi, FPToSI);
723 INSTKEYWORD(inttoptr, IntToPtr);
724 INSTKEYWORD(ptrtoint, PtrToInt);
725 INSTKEYWORD(bitcast, BitCast);
726 INSTKEYWORD(addrspacecast, AddrSpaceCast);
727 INSTKEYWORD(select, Select);
728 INSTKEYWORD(va_arg, VAArg);
729 INSTKEYWORD(ret, Ret);
731 INSTKEYWORD(switch, Switch);
732 INSTKEYWORD(indirectbr, IndirectBr);
733 INSTKEYWORD(invoke, Invoke);
734 INSTKEYWORD(resume, Resume);
735 INSTKEYWORD(unreachable, Unreachable);
737 INSTKEYWORD(alloca, Alloca);
738 INSTKEYWORD(load, Load);
739 INSTKEYWORD(store, Store);
740 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
741 INSTKEYWORD(atomicrmw, AtomicRMW);
742 INSTKEYWORD(fence, Fence);
743 INSTKEYWORD(getelementptr, GetElementPtr);
745 INSTKEYWORD(extractelement, ExtractElement);
746 INSTKEYWORD(insertelement, InsertElement);
747 INSTKEYWORD(shufflevector, ShuffleVector);
748 INSTKEYWORD(extractvalue, ExtractValue);
749 INSTKEYWORD(insertvalue, InsertValue);
750 INSTKEYWORD(landingpad, LandingPad);
753 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
754 // the CFE to avoid forcing it to deal with 64-bit numbers.
755 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
756 TokStart[1] == '0' && TokStart[2] == 'x' &&
757 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
758 int len = CurPtr-TokStart-3;
759 uint32_t bits = len * 4;
760 StringRef HexStr(TokStart + 3, len);
761 if (!std::all_of(HexStr.begin(), HexStr.end(), isxdigit)) {
762 // Bad token, return it as an error.
766 APInt Tmp(bits, HexStr, 16);
767 uint32_t activeBits = Tmp.getActiveBits();
768 if (activeBits > 0 && activeBits < bits)
769 Tmp = Tmp.trunc(activeBits);
770 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
771 return lltok::APSInt;
774 // If this is "cc1234", return this as just "cc".
775 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
780 // Finally, if this isn't known, return an error.
786 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
787 /// that this is not a label:
788 /// HexFPConstant 0x[0-9A-Fa-f]+
789 /// HexFP80Constant 0xK[0-9A-Fa-f]+
790 /// HexFP128Constant 0xL[0-9A-Fa-f]+
791 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
792 /// HexHalfConstant 0xH[0-9A-Fa-f]+
793 lltok::Kind LLLexer::Lex0x() {
794 CurPtr = TokStart + 2;
797 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
803 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
804 // Bad token, return it as an error.
809 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
813 // HexFPConstant - Floating point constant represented in IEEE format as a
814 // hexadecimal number for when exponential notation is not precise enough.
815 // Half, Float, and double only.
816 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
817 return lltok::APFloat;
822 default: llvm_unreachable("Unknown kind!");
824 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
825 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
826 APFloatVal = APFloat(APFloat::x87DoubleExtended, APInt(80, Pair));
827 return lltok::APFloat;
829 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
830 HexToIntPair(TokStart+3, CurPtr, Pair);
831 APFloatVal = APFloat(APFloat::IEEEquad, APInt(128, Pair));
832 return lltok::APFloat;
834 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
835 HexToIntPair(TokStart+3, CurPtr, Pair);
836 APFloatVal = APFloat(APFloat::PPCDoubleDouble, APInt(128, Pair));
837 return lltok::APFloat;
839 APFloatVal = APFloat(APFloat::IEEEhalf,
840 APInt(16,HexIntToVal(TokStart+3, CurPtr)));
841 return lltok::APFloat;
845 /// LexIdentifier: Handle several related productions:
846 /// Label [-a-zA-Z$._0-9]+:
848 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
850 /// HexFPConstant 0x[0-9A-Fa-f]+
851 /// HexFP80Constant 0xK[0-9A-Fa-f]+
852 /// HexFP128Constant 0xL[0-9A-Fa-f]+
853 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
854 lltok::Kind LLLexer::LexDigitOrNegative() {
855 // If the letter after the negative is not a number, this is probably a label.
856 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
857 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
858 // Okay, this is not a number after the -, it's probably a label.
859 if (const char *End = isLabelTail(CurPtr)) {
860 StrVal.assign(TokStart, End-1);
862 return lltok::LabelStr;
868 // At this point, it is either a label, int or fp constant.
870 // Skip digits, we have at least one.
871 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
874 // Check to see if this really is a label afterall, e.g. "-1:".
875 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
876 if (const char *End = isLabelTail(CurPtr)) {
877 StrVal.assign(TokStart, End-1);
879 return lltok::LabelStr;
883 // If the next character is a '.', then it is a fp value, otherwise its
885 if (CurPtr[0] != '.') {
886 if (TokStart[0] == '0' && TokStart[1] == 'x')
888 unsigned Len = CurPtr-TokStart;
889 uint32_t numBits = ((Len * 64) / 19) + 2;
890 APInt Tmp(numBits, StringRef(TokStart, Len), 10);
891 if (TokStart[0] == '-') {
892 uint32_t minBits = Tmp.getMinSignedBits();
893 if (minBits > 0 && minBits < numBits)
894 Tmp = Tmp.trunc(minBits);
895 APSIntVal = APSInt(Tmp, false);
897 uint32_t activeBits = Tmp.getActiveBits();
898 if (activeBits > 0 && activeBits < numBits)
899 Tmp = Tmp.trunc(activeBits);
900 APSIntVal = APSInt(Tmp, true);
902 return lltok::APSInt;
907 // Skip over [0-9]*([eE][-+]?[0-9]+)?
908 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
910 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
911 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
912 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
913 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
915 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
919 APFloatVal = APFloat(std::atof(TokStart));
920 return lltok::APFloat;
923 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
924 lltok::Kind LLLexer::LexPositive() {
925 // If the letter after the negative is a number, this is probably not a
927 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
931 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
934 // At this point, we need a '.'.
935 if (CurPtr[0] != '.') {
942 // Skip over [0-9]*([eE][-+]?[0-9]+)?
943 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
945 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
946 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
947 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
948 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
950 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
954 APFloatVal = APFloat(std::atof(TokStart));
955 return lltok::APFloat;