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 LexDollar();
213 case '%': return LexPercent();
214 case '"': return LexQuote();
216 if (const char *Ptr = isLabelTail(CurPtr)) {
218 StrVal.assign(TokStart, CurPtr-1);
219 return lltok::LabelStr;
221 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
223 return lltok::dotdotdot;
229 case '!': return LexExclaim();
230 case '#': return LexHash();
231 case '0': case '1': case '2': case '3': case '4':
232 case '5': case '6': case '7': case '8': case '9':
234 return LexDigitOrNegative();
235 case '=': return lltok::equal;
236 case '[': return lltok::lsquare;
237 case ']': return lltok::rsquare;
238 case '{': return lltok::lbrace;
239 case '}': return lltok::rbrace;
240 case '<': return lltok::less;
241 case '>': return lltok::greater;
242 case '(': return lltok::lparen;
243 case ')': return lltok::rparen;
244 case ',': return lltok::comma;
245 case '*': return lltok::star;
246 case '\\': return lltok::backslash;
250 void LLLexer::SkipLineComment() {
252 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
257 /// LexAt - Lex all tokens that start with an @ character:
258 /// GlobalVar @\"[^\"]*\"
259 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
260 /// GlobalVarID @[0-9]+
261 lltok::Kind LLLexer::LexAt() {
262 // Handle AtStringConstant: @\"[^\"]*\"
263 if (CurPtr[0] == '"') {
267 int CurChar = getNextChar();
269 if (CurChar == EOF) {
270 Error("end of file in global variable name");
273 if (CurChar == '"') {
274 StrVal.assign(TokStart+2, CurPtr-1);
275 UnEscapeLexed(StrVal);
276 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
277 Error("Null bytes are not allowed in names");
280 return lltok::GlobalVar;
285 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
287 return lltok::GlobalVar;
289 // Handle GlobalVarID: @[0-9]+
290 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
291 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
294 uint64_t Val = atoull(TokStart+1, CurPtr);
295 if ((unsigned)Val != Val)
296 Error("invalid value number (too large)!");
297 UIntVal = unsigned(Val);
298 return lltok::GlobalID;
304 lltok::Kind LLLexer::LexDollar() {
305 if (const char *Ptr = isLabelTail(TokStart)) {
307 StrVal.assign(TokStart, CurPtr - 1);
308 return lltok::LabelStr;
311 // Handle DollarStringConstant: $\"[^\"]*\"
312 if (CurPtr[0] == '"') {
316 int CurChar = getNextChar();
318 if (CurChar == EOF) {
319 Error("end of file in COMDAT variable name");
322 if (CurChar == '"') {
323 StrVal.assign(TokStart + 2, CurPtr - 1);
324 UnEscapeLexed(StrVal);
325 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
326 Error("Null bytes are not allowed in names");
329 return lltok::ComdatVar;
334 // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
336 return lltok::ComdatVar;
341 /// ReadString - Read a string until the closing quote.
342 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
343 const char *Start = CurPtr;
345 int CurChar = getNextChar();
347 if (CurChar == EOF) {
348 Error("end of file in string constant");
351 if (CurChar == '"') {
352 StrVal.assign(Start, CurPtr-1);
353 UnEscapeLexed(StrVal);
359 /// ReadVarName - Read the rest of a token containing a variable name.
360 bool LLLexer::ReadVarName() {
361 const char *NameStart = CurPtr;
362 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
363 CurPtr[0] == '-' || CurPtr[0] == '$' ||
364 CurPtr[0] == '.' || CurPtr[0] == '_') {
366 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
367 CurPtr[0] == '-' || CurPtr[0] == '$' ||
368 CurPtr[0] == '.' || CurPtr[0] == '_')
371 StrVal.assign(NameStart, CurPtr);
377 /// LexPercent - Lex all tokens that start with a % character:
378 /// LocalVar ::= %\"[^\"]*\"
379 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
380 /// LocalVarID ::= %[0-9]+
381 lltok::Kind LLLexer::LexPercent() {
382 // Handle LocalVarName: %\"[^\"]*\"
383 if (CurPtr[0] == '"') {
385 return ReadString(lltok::LocalVar);
388 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
390 return lltok::LocalVar;
392 // Handle LocalVarID: %[0-9]+
393 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
394 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
397 uint64_t Val = atoull(TokStart+1, CurPtr);
398 if ((unsigned)Val != Val)
399 Error("invalid value number (too large)!");
400 UIntVal = unsigned(Val);
401 return lltok::LocalVarID;
407 /// LexQuote - Lex all tokens that start with a " character:
408 /// QuoteLabel "[^"]+":
409 /// StringConstant "[^"]*"
410 lltok::Kind LLLexer::LexQuote() {
411 lltok::Kind kind = ReadString(lltok::StringConstant);
412 if (kind == lltok::Error || kind == lltok::Eof)
415 if (CurPtr[0] == ':') {
417 kind = lltok::LabelStr;
426 lltok::Kind LLLexer::LexExclaim() {
427 // Lex a metadata name as a MetadataVar.
428 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
429 CurPtr[0] == '-' || CurPtr[0] == '$' ||
430 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
432 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
433 CurPtr[0] == '-' || CurPtr[0] == '$' ||
434 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
437 StrVal.assign(TokStart+1, CurPtr); // Skip !
438 UnEscapeLexed(StrVal);
439 return lltok::MetadataVar;
441 return lltok::exclaim;
444 /// LexHash - Lex all tokens that start with a # character:
445 /// AttrGrpID ::= #[0-9]+
446 lltok::Kind LLLexer::LexHash() {
447 // Handle AttrGrpID: #[0-9]+
448 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
449 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
452 uint64_t Val = atoull(TokStart+1, CurPtr);
453 if ((unsigned)Val != Val)
454 Error("invalid value number (too large)!");
455 UIntVal = unsigned(Val);
456 return lltok::AttrGrpID;
462 /// LexIdentifier: Handle several related productions:
463 /// Label [-a-zA-Z$._0-9]+:
464 /// IntegerType i[0-9]+
465 /// Keyword sdiv, float, ...
466 /// HexIntConstant [us]0x[0-9A-Fa-f]+
467 lltok::Kind LLLexer::LexIdentifier() {
468 const char *StartChar = CurPtr;
469 const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar;
470 const char *KeywordEnd = nullptr;
472 for (; isLabelChar(*CurPtr); ++CurPtr) {
473 // If we decide this is an integer, remember the end of the sequence.
474 if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
476 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
481 // If we stopped due to a colon, this really is a label.
482 if (*CurPtr == ':') {
483 StrVal.assign(StartChar-1, CurPtr++);
484 return lltok::LabelStr;
487 // Otherwise, this wasn't a label. If this was valid as an integer type,
489 if (!IntEnd) IntEnd = CurPtr;
490 if (IntEnd != StartChar) {
492 uint64_t NumBits = atoull(StartChar, CurPtr);
493 if (NumBits < IntegerType::MIN_INT_BITS ||
494 NumBits > IntegerType::MAX_INT_BITS) {
495 Error("bitwidth for integer type out of range!");
498 TyVal = IntegerType::get(Context, NumBits);
502 // Otherwise, this was a letter sequence. See which keyword this is.
503 if (!KeywordEnd) KeywordEnd = CurPtr;
506 unsigned Len = CurPtr-StartChar;
507 #define KEYWORD(STR) \
509 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
510 return lltok::kw_##STR; \
513 KEYWORD(true); KEYWORD(false);
514 KEYWORD(declare); KEYWORD(define);
515 KEYWORD(global); KEYWORD(constant);
519 KEYWORD(linker_private); // NOTE: deprecated, for parser compatibility
520 KEYWORD(linker_private_weak); // NOTE: deprecated, for parser compatibility
521 KEYWORD(available_externally);
523 KEYWORD(linkonce_odr);
524 KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
533 KEYWORD(unnamed_addr);
534 KEYWORD(externally_initialized);
535 KEYWORD(extern_weak);
537 KEYWORD(thread_local);
538 KEYWORD(localdynamic);
539 KEYWORD(initialexec);
541 KEYWORD(zeroinitializer);
550 KEYWORD(deplibs); // FIXME: Remove in 4.0.
560 KEYWORD(singlethread);
579 KEYWORD(inteldialect);
586 KEYWORD(x86_stdcallcc);
587 KEYWORD(x86_fastcallcc);
588 KEYWORD(x86_thiscallcc);
590 KEYWORD(arm_aapcscc);
591 KEYWORD(arm_aapcs_vfpcc);
592 KEYWORD(msp430_intrcc);
595 KEYWORD(spir_kernel);
597 KEYWORD(intel_ocl_bicc);
598 KEYWORD(x86_64_sysvcc);
599 KEYWORD(x86_64_win64cc);
600 KEYWORD(webkit_jscc);
602 KEYWORD(preserve_mostcc);
603 KEYWORD(preserve_allcc);
610 KEYWORD(alwaysinline);
615 KEYWORD(dereferenceable);
625 KEYWORD(noduplicate);
626 KEYWORD(noimplicitfloat);
628 KEYWORD(nonlazybind);
638 KEYWORD(returns_twice);
644 KEYWORD(sanitize_address);
645 KEYWORD(sanitize_thread);
646 KEYWORD(sanitize_memory);
659 KEYWORD(noduplicates);
662 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
663 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
664 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
665 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
667 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
671 KEYWORD(blockaddress);
673 KEYWORD(personality);
679 // Keywords for types.
680 #define TYPEKEYWORD(STR, LLVMTY) \
681 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
682 TyVal = LLVMTY; return lltok::Type; }
683 TYPEKEYWORD("void", Type::getVoidTy(Context));
684 TYPEKEYWORD("half", Type::getHalfTy(Context));
685 TYPEKEYWORD("float", Type::getFloatTy(Context));
686 TYPEKEYWORD("double", Type::getDoubleTy(Context));
687 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
688 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
689 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
690 TYPEKEYWORD("label", Type::getLabelTy(Context));
691 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
692 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
695 // Keywords for instructions.
696 #define INSTKEYWORD(STR, Enum) \
697 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
698 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
700 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
701 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
702 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
703 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
704 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
705 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
706 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
707 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
709 INSTKEYWORD(phi, PHI);
710 INSTKEYWORD(call, Call);
711 INSTKEYWORD(trunc, Trunc);
712 INSTKEYWORD(zext, ZExt);
713 INSTKEYWORD(sext, SExt);
714 INSTKEYWORD(fptrunc, FPTrunc);
715 INSTKEYWORD(fpext, FPExt);
716 INSTKEYWORD(uitofp, UIToFP);
717 INSTKEYWORD(sitofp, SIToFP);
718 INSTKEYWORD(fptoui, FPToUI);
719 INSTKEYWORD(fptosi, FPToSI);
720 INSTKEYWORD(inttoptr, IntToPtr);
721 INSTKEYWORD(ptrtoint, PtrToInt);
722 INSTKEYWORD(bitcast, BitCast);
723 INSTKEYWORD(addrspacecast, AddrSpaceCast);
724 INSTKEYWORD(select, Select);
725 INSTKEYWORD(va_arg, VAArg);
726 INSTKEYWORD(ret, Ret);
728 INSTKEYWORD(switch, Switch);
729 INSTKEYWORD(indirectbr, IndirectBr);
730 INSTKEYWORD(invoke, Invoke);
731 INSTKEYWORD(resume, Resume);
732 INSTKEYWORD(unreachable, Unreachable);
734 INSTKEYWORD(alloca, Alloca);
735 INSTKEYWORD(load, Load);
736 INSTKEYWORD(store, Store);
737 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
738 INSTKEYWORD(atomicrmw, AtomicRMW);
739 INSTKEYWORD(fence, Fence);
740 INSTKEYWORD(getelementptr, GetElementPtr);
742 INSTKEYWORD(extractelement, ExtractElement);
743 INSTKEYWORD(insertelement, InsertElement);
744 INSTKEYWORD(shufflevector, ShuffleVector);
745 INSTKEYWORD(extractvalue, ExtractValue);
746 INSTKEYWORD(insertvalue, InsertValue);
747 INSTKEYWORD(landingpad, LandingPad);
750 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
751 // the CFE to avoid forcing it to deal with 64-bit numbers.
752 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
753 TokStart[1] == '0' && TokStart[2] == 'x' &&
754 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
755 int len = CurPtr-TokStart-3;
756 uint32_t bits = len * 4;
757 APInt Tmp(bits, StringRef(TokStart+3, len), 16);
758 uint32_t activeBits = Tmp.getActiveBits();
759 if (activeBits > 0 && activeBits < bits)
760 Tmp = Tmp.trunc(activeBits);
761 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
762 return lltok::APSInt;
765 // If this is "cc1234", return this as just "cc".
766 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
771 // Finally, if this isn't known, return an error.
777 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
778 /// that this is not a label:
779 /// HexFPConstant 0x[0-9A-Fa-f]+
780 /// HexFP80Constant 0xK[0-9A-Fa-f]+
781 /// HexFP128Constant 0xL[0-9A-Fa-f]+
782 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
783 /// HexHalfConstant 0xH[0-9A-Fa-f]+
784 lltok::Kind LLLexer::Lex0x() {
785 CurPtr = TokStart + 2;
788 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
794 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
795 // Bad token, return it as an error.
800 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
804 // HexFPConstant - Floating point constant represented in IEEE format as a
805 // hexadecimal number for when exponential notation is not precise enough.
806 // Half, Float, and double only.
807 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
808 return lltok::APFloat;
813 default: llvm_unreachable("Unknown kind!");
815 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
816 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
817 APFloatVal = APFloat(APFloat::x87DoubleExtended, APInt(80, Pair));
818 return lltok::APFloat;
820 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
821 HexToIntPair(TokStart+3, CurPtr, Pair);
822 APFloatVal = APFloat(APFloat::IEEEquad, APInt(128, Pair));
823 return lltok::APFloat;
825 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
826 HexToIntPair(TokStart+3, CurPtr, Pair);
827 APFloatVal = APFloat(APFloat::PPCDoubleDouble, APInt(128, Pair));
828 return lltok::APFloat;
830 APFloatVal = APFloat(APFloat::IEEEhalf,
831 APInt(16,HexIntToVal(TokStart+3, CurPtr)));
832 return lltok::APFloat;
836 /// LexIdentifier: Handle several related productions:
837 /// Label [-a-zA-Z$._0-9]+:
839 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
841 /// HexFPConstant 0x[0-9A-Fa-f]+
842 /// HexFP80Constant 0xK[0-9A-Fa-f]+
843 /// HexFP128Constant 0xL[0-9A-Fa-f]+
844 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
845 lltok::Kind LLLexer::LexDigitOrNegative() {
846 // If the letter after the negative is not a number, this is probably a label.
847 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
848 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
849 // Okay, this is not a number after the -, it's probably a label.
850 if (const char *End = isLabelTail(CurPtr)) {
851 StrVal.assign(TokStart, End-1);
853 return lltok::LabelStr;
859 // At this point, it is either a label, int or fp constant.
861 // Skip digits, we have at least one.
862 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
865 // Check to see if this really is a label afterall, e.g. "-1:".
866 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
867 if (const char *End = isLabelTail(CurPtr)) {
868 StrVal.assign(TokStart, End-1);
870 return lltok::LabelStr;
874 // If the next character is a '.', then it is a fp value, otherwise its
876 if (CurPtr[0] != '.') {
877 if (TokStart[0] == '0' && TokStart[1] == 'x')
879 unsigned Len = CurPtr-TokStart;
880 uint32_t numBits = ((Len * 64) / 19) + 2;
881 APInt Tmp(numBits, StringRef(TokStart, Len), 10);
882 if (TokStart[0] == '-') {
883 uint32_t minBits = Tmp.getMinSignedBits();
884 if (minBits > 0 && minBits < numBits)
885 Tmp = Tmp.trunc(minBits);
886 APSIntVal = APSInt(Tmp, false);
888 uint32_t activeBits = Tmp.getActiveBits();
889 if (activeBits > 0 && activeBits < numBits)
890 Tmp = Tmp.trunc(activeBits);
891 APSIntVal = APSInt(Tmp, true);
893 return lltok::APSInt;
898 // Skip over [0-9]*([eE][-+]?[0-9]+)?
899 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
901 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
902 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
903 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
904 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
906 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
910 APFloatVal = APFloat(std::atof(TokStart));
911 return lltok::APFloat;
914 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
915 lltok::Kind LLLexer::LexPositive() {
916 // If the letter after the negative is a number, this is probably not a
918 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
922 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
925 // At this point, we need a '.'.
926 if (CurPtr[0] != '.') {
933 // Skip over [0-9]*([eE][-+]?[0-9]+)?
934 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
936 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
937 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
938 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
939 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
941 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
945 APFloatVal = APFloat(std::atof(TokStart));
946 return lltok::APFloat;