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::bar;
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 return LexVar(lltok::GlobalVar, lltok::GlobalID);
263 lltok::Kind LLLexer::LexDollar() {
264 if (const char *Ptr = isLabelTail(TokStart)) {
266 StrVal.assign(TokStart, CurPtr - 1);
267 return lltok::LabelStr;
270 // Handle DollarStringConstant: $\"[^\"]*\"
271 if (CurPtr[0] == '"') {
275 int CurChar = getNextChar();
277 if (CurChar == EOF) {
278 Error("end of file in COMDAT variable name");
281 if (CurChar == '"') {
282 StrVal.assign(TokStart + 2, CurPtr - 1);
283 UnEscapeLexed(StrVal);
284 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
285 Error("Null bytes are not allowed in names");
288 return lltok::ComdatVar;
293 // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
295 return lltok::ComdatVar;
300 /// ReadString - Read a string until the closing quote.
301 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
302 const char *Start = CurPtr;
304 int CurChar = getNextChar();
306 if (CurChar == EOF) {
307 Error("end of file in string constant");
310 if (CurChar == '"') {
311 StrVal.assign(Start, CurPtr-1);
312 UnEscapeLexed(StrVal);
318 /// ReadVarName - Read the rest of a token containing a variable name.
319 bool LLLexer::ReadVarName() {
320 const char *NameStart = CurPtr;
321 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
322 CurPtr[0] == '-' || CurPtr[0] == '$' ||
323 CurPtr[0] == '.' || CurPtr[0] == '_') {
325 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
326 CurPtr[0] == '-' || CurPtr[0] == '$' ||
327 CurPtr[0] == '.' || CurPtr[0] == '_')
330 StrVal.assign(NameStart, CurPtr);
336 lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) {
337 // Handle StringConstant: \"[^\"]*\"
338 if (CurPtr[0] == '"') {
342 int CurChar = getNextChar();
344 if (CurChar == EOF) {
345 Error("end of file in global variable name");
348 if (CurChar == '"') {
349 StrVal.assign(TokStart+2, CurPtr-1);
350 UnEscapeLexed(StrVal);
351 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
352 Error("Null bytes are not allowed in names");
360 // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]*
364 // Handle VarID: [0-9]+
365 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
366 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
369 uint64_t Val = atoull(TokStart+1, CurPtr);
370 if ((unsigned)Val != Val)
371 Error("invalid value number (too large)!");
372 UIntVal = unsigned(Val);
378 /// LexPercent - Lex all tokens that start with a % character:
379 /// LocalVar ::= %\"[^\"]*\"
380 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
381 /// LocalVarID ::= %[0-9]+
382 lltok::Kind LLLexer::LexPercent() {
383 return LexVar(lltok::LocalVar, lltok::LocalVarID);
386 /// LexQuote - Lex all tokens that start with a " character:
387 /// QuoteLabel "[^"]+":
388 /// StringConstant "[^"]*"
389 lltok::Kind LLLexer::LexQuote() {
390 lltok::Kind kind = ReadString(lltok::StringConstant);
391 if (kind == lltok::Error || kind == lltok::Eof)
394 if (CurPtr[0] == ':') {
396 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
397 Error("Null bytes are not allowed in names");
400 kind = lltok::LabelStr;
410 lltok::Kind LLLexer::LexExclaim() {
411 // Lex a metadata name as a MetadataVar.
412 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
413 CurPtr[0] == '-' || CurPtr[0] == '$' ||
414 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
416 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
417 CurPtr[0] == '-' || CurPtr[0] == '$' ||
418 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
421 StrVal.assign(TokStart+1, CurPtr); // Skip !
422 UnEscapeLexed(StrVal);
423 return lltok::MetadataVar;
425 return lltok::exclaim;
428 /// LexHash - Lex all tokens that start with a # character:
429 /// AttrGrpID ::= #[0-9]+
430 lltok::Kind LLLexer::LexHash() {
431 // Handle AttrGrpID: #[0-9]+
432 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
433 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
436 uint64_t Val = atoull(TokStart+1, CurPtr);
437 if ((unsigned)Val != Val)
438 Error("invalid value number (too large)!");
439 UIntVal = unsigned(Val);
440 return lltok::AttrGrpID;
446 /// LexIdentifier: Handle several related productions:
447 /// Label [-a-zA-Z$._0-9]+:
448 /// IntegerType i[0-9]+
449 /// Keyword sdiv, float, ...
450 /// HexIntConstant [us]0x[0-9A-Fa-f]+
451 lltok::Kind LLLexer::LexIdentifier() {
452 const char *StartChar = CurPtr;
453 const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar;
454 const char *KeywordEnd = nullptr;
456 for (; isLabelChar(*CurPtr); ++CurPtr) {
457 // If we decide this is an integer, remember the end of the sequence.
458 if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
460 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
465 // If we stopped due to a colon, this really is a label.
466 if (*CurPtr == ':') {
467 StrVal.assign(StartChar-1, CurPtr++);
468 return lltok::LabelStr;
471 // Otherwise, this wasn't a label. If this was valid as an integer type,
473 if (!IntEnd) IntEnd = CurPtr;
474 if (IntEnd != StartChar) {
476 uint64_t NumBits = atoull(StartChar, CurPtr);
477 if (NumBits < IntegerType::MIN_INT_BITS ||
478 NumBits > IntegerType::MAX_INT_BITS) {
479 Error("bitwidth for integer type out of range!");
482 TyVal = IntegerType::get(Context, NumBits);
486 // Otherwise, this was a letter sequence. See which keyword this is.
487 if (!KeywordEnd) KeywordEnd = CurPtr;
490 StringRef Keyword(StartChar, CurPtr - StartChar);
491 #define KEYWORD(STR) \
493 if (Keyword == #STR) \
494 return lltok::kw_##STR; \
497 KEYWORD(true); KEYWORD(false);
498 KEYWORD(declare); KEYWORD(define);
499 KEYWORD(global); KEYWORD(constant);
503 KEYWORD(available_externally);
505 KEYWORD(linkonce_odr);
506 KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
515 KEYWORD(unnamed_addr);
516 KEYWORD(externally_initialized);
517 KEYWORD(extern_weak);
519 KEYWORD(thread_local);
520 KEYWORD(localdynamic);
521 KEYWORD(initialexec);
523 KEYWORD(zeroinitializer);
532 KEYWORD(deplibs); // FIXME: Remove in 4.0.
542 KEYWORD(singlethread);
561 KEYWORD(inteldialect);
569 KEYWORD(x86_stdcallcc);
570 KEYWORD(x86_fastcallcc);
571 KEYWORD(x86_thiscallcc);
572 KEYWORD(x86_vectorcallcc);
574 KEYWORD(arm_aapcscc);
575 KEYWORD(arm_aapcs_vfpcc);
576 KEYWORD(msp430_intrcc);
579 KEYWORD(spir_kernel);
581 KEYWORD(intel_ocl_bicc);
582 KEYWORD(x86_64_sysvcc);
583 KEYWORD(x86_64_win64cc);
584 KEYWORD(webkit_jscc);
586 KEYWORD(preserve_mostcc);
587 KEYWORD(preserve_allcc);
595 KEYWORD(alwaysinline);
600 KEYWORD(dereferenceable);
610 KEYWORD(noduplicate);
611 KEYWORD(noimplicitfloat);
613 KEYWORD(nonlazybind);
623 KEYWORD(returns_twice);
629 KEYWORD(sanitize_address);
630 KEYWORD(sanitize_thread);
631 KEYWORD(sanitize_memory);
644 KEYWORD(noduplicates);
647 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
648 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
649 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
650 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
652 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
656 KEYWORD(blockaddress);
661 // Use-list order directives.
662 KEYWORD(uselistorder);
663 KEYWORD(uselistorder_bb);
665 KEYWORD(personality);
671 // Keywords for types.
672 #define TYPEKEYWORD(STR, LLVMTY) \
674 if (Keyword == STR) { \
676 return lltok::Type; \
679 TYPEKEYWORD("void", Type::getVoidTy(Context));
680 TYPEKEYWORD("half", Type::getHalfTy(Context));
681 TYPEKEYWORD("float", Type::getFloatTy(Context));
682 TYPEKEYWORD("double", Type::getDoubleTy(Context));
683 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
684 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
685 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
686 TYPEKEYWORD("label", Type::getLabelTy(Context));
687 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
688 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
691 // Keywords for instructions.
692 #define INSTKEYWORD(STR, Enum) \
694 if (Keyword == #STR) { \
695 UIntVal = Instruction::Enum; \
696 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 #define DWKEYWORD(TYPE, TOKEN) \
752 if (Keyword.startswith("DW_" #TYPE "_")) { \
753 StrVal.assign(Keyword.begin(), Keyword.end()); \
754 return lltok::TOKEN; \
757 DWKEYWORD(TAG, DwarfTag);
758 DWKEYWORD(ATE, DwarfAttEncoding);
759 DWKEYWORD(VIRTUALITY, DwarfVirtuality);
760 DWKEYWORD(LANG, DwarfLang);
761 DWKEYWORD(OP, DwarfOp);
764 if (Keyword.startswith("DIFlag")) {
765 StrVal.assign(Keyword.begin(), Keyword.end());
766 return lltok::DIFlag;
769 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
770 // the CFE to avoid forcing it to deal with 64-bit numbers.
771 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
772 TokStart[1] == '0' && TokStart[2] == 'x' &&
773 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
774 int len = CurPtr-TokStart-3;
775 uint32_t bits = len * 4;
776 StringRef HexStr(TokStart + 3, len);
777 if (!std::all_of(HexStr.begin(), HexStr.end(), isxdigit)) {
778 // Bad token, return it as an error.
782 APInt Tmp(bits, HexStr, 16);
783 uint32_t activeBits = Tmp.getActiveBits();
784 if (activeBits > 0 && activeBits < bits)
785 Tmp = Tmp.trunc(activeBits);
786 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
787 return lltok::APSInt;
790 // If this is "cc1234", return this as just "cc".
791 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
796 // Finally, if this isn't known, return an error.
802 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
803 /// that this is not a label:
804 /// HexFPConstant 0x[0-9A-Fa-f]+
805 /// HexFP80Constant 0xK[0-9A-Fa-f]+
806 /// HexFP128Constant 0xL[0-9A-Fa-f]+
807 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
808 /// HexHalfConstant 0xH[0-9A-Fa-f]+
809 lltok::Kind LLLexer::Lex0x() {
810 CurPtr = TokStart + 2;
813 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
819 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
820 // Bad token, return it as an error.
825 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
829 // HexFPConstant - Floating point constant represented in IEEE format as a
830 // hexadecimal number for when exponential notation is not precise enough.
831 // Half, Float, and double only.
832 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
833 return lltok::APFloat;
838 default: llvm_unreachable("Unknown kind!");
840 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
841 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
842 APFloatVal = APFloat(APFloat::x87DoubleExtended, APInt(80, Pair));
843 return lltok::APFloat;
845 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
846 HexToIntPair(TokStart+3, CurPtr, Pair);
847 APFloatVal = APFloat(APFloat::IEEEquad, APInt(128, Pair));
848 return lltok::APFloat;
850 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
851 HexToIntPair(TokStart+3, CurPtr, Pair);
852 APFloatVal = APFloat(APFloat::PPCDoubleDouble, APInt(128, Pair));
853 return lltok::APFloat;
855 APFloatVal = APFloat(APFloat::IEEEhalf,
856 APInt(16,HexIntToVal(TokStart+3, CurPtr)));
857 return lltok::APFloat;
861 /// LexIdentifier: Handle several related productions:
862 /// Label [-a-zA-Z$._0-9]+:
864 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
866 /// HexFPConstant 0x[0-9A-Fa-f]+
867 /// HexFP80Constant 0xK[0-9A-Fa-f]+
868 /// HexFP128Constant 0xL[0-9A-Fa-f]+
869 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
870 lltok::Kind LLLexer::LexDigitOrNegative() {
871 // If the letter after the negative is not a number, this is probably a label.
872 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
873 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
874 // Okay, this is not a number after the -, it's probably a label.
875 if (const char *End = isLabelTail(CurPtr)) {
876 StrVal.assign(TokStart, End-1);
878 return lltok::LabelStr;
884 // At this point, it is either a label, int or fp constant.
886 // Skip digits, we have at least one.
887 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
890 // Check to see if this really is a label afterall, e.g. "-1:".
891 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
892 if (const char *End = isLabelTail(CurPtr)) {
893 StrVal.assign(TokStart, End-1);
895 return lltok::LabelStr;
899 // If the next character is a '.', then it is a fp value, otherwise its
901 if (CurPtr[0] != '.') {
902 if (TokStart[0] == '0' && TokStart[1] == 'x')
904 unsigned Len = CurPtr-TokStart;
905 uint32_t numBits = ((Len * 64) / 19) + 2;
906 APInt Tmp(numBits, StringRef(TokStart, Len), 10);
907 if (TokStart[0] == '-') {
908 uint32_t minBits = Tmp.getMinSignedBits();
909 if (minBits > 0 && minBits < numBits)
910 Tmp = Tmp.trunc(minBits);
911 APSIntVal = APSInt(Tmp, false);
913 uint32_t activeBits = Tmp.getActiveBits();
914 if (activeBits > 0 && activeBits < numBits)
915 Tmp = Tmp.trunc(activeBits);
916 APSIntVal = APSInt(Tmp, true);
918 return lltok::APSInt;
923 // Skip over [0-9]*([eE][-+]?[0-9]+)?
924 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
926 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
927 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
928 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
929 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
931 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
935 APFloatVal = APFloat(std::atof(TokStart));
936 return lltok::APFloat;
939 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
940 lltok::Kind LLLexer::LexPositive() {
941 // If the letter after the negative is a number, this is probably not a
943 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
947 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
950 // At this point, we need a '.'.
951 if (CurPtr[0] != '.') {
958 // Skip over [0-9]*([eE][-+]?[0-9]+)?
959 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
961 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
962 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
963 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
964 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
966 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
970 APFloatVal = APFloat(std::atof(TokStart));
971 return lltok::APFloat;