1 //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Implement the Lexer for .ll files.
11 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/Instruction.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/SourceMgr.h"
28 bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
29 ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
33 void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const {
34 SM.PrintMessage(WarningLoc, SourceMgr::DK_Warning, 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 if (End - Buffer >= 16) {
78 for (int i = 0; i < 16; i++, Buffer++) {
79 assert(Buffer != End);
81 Pair[0] += hexDigitValue(*Buffer);
85 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
87 Pair[1] += hexDigitValue(*Buffer);
90 Error("constant bigger than 128 bits detected!");
93 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
94 /// { low64, high16 } as usual for an APInt.
95 void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
98 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
99 assert(Buffer != End);
101 Pair[1] += hexDigitValue(*Buffer);
104 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
106 Pair[0] += hexDigitValue(*Buffer);
109 Error("constant bigger than 128 bits detected!");
112 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
113 // appropriate character.
114 static void UnEscapeLexed(std::string &Str) {
115 if (Str.empty()) return;
117 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
119 for (char *BIn = Buffer; BIn != EndBuffer; ) {
120 if (BIn[0] == '\\') {
121 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
122 *BOut++ = '\\'; // Two \ becomes one
124 } else if (BIn < EndBuffer-2 &&
125 isxdigit(static_cast<unsigned char>(BIn[1])) &&
126 isxdigit(static_cast<unsigned char>(BIn[2]))) {
127 *BOut = hexDigitValue(BIn[1]) * 16 + hexDigitValue(BIn[2]);
128 BIn += 3; // Skip over handled chars
137 Str.resize(BOut-Buffer);
140 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
141 static bool isLabelChar(char C) {
142 return isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
143 C == '.' || C == '_';
146 /// isLabelTail - Return true if this pointer points to a valid end of a label.
147 static const char *isLabelTail(const char *CurPtr) {
149 if (CurPtr[0] == ':') return CurPtr+1;
150 if (!isLabelChar(CurPtr[0])) return nullptr;
155 //===----------------------------------------------------------------------===//
157 //===----------------------------------------------------------------------===//
159 LLLexer::LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &Err,
161 : CurBuf(StartBuf), ErrorInfo(Err), SM(SM), Context(C), APFloatVal(0.0),
162 IgnoreColonInIdentifiers(false) {
163 CurPtr = CurBuf.begin();
166 int LLLexer::getNextChar() {
167 char CurChar = *CurPtr++;
169 default: return (unsigned char)CurChar;
171 // A nul character in the stream is either the end of the current buffer or
172 // a random nul in the file. Disambiguate that here.
173 if (CurPtr-1 != CurBuf.end())
174 return 0; // Just whitespace.
176 // Otherwise, return end of file.
177 --CurPtr; // Another call to lex will return EOF again.
182 lltok::Kind LLLexer::LexToken() {
186 int CurChar = getNextChar();
189 // Handle letters: [a-zA-Z_]
190 if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
191 return LexIdentifier();
194 case EOF: return lltok::Eof;
200 // Ignore whitespace.
202 case '+': return LexPositive();
203 case '@': return LexAt();
204 case '$': return LexDollar();
205 case '%': return LexPercent();
206 case '"': return LexQuote();
208 if (const char *Ptr = isLabelTail(CurPtr)) {
210 StrVal.assign(TokStart, CurPtr-1);
211 return lltok::LabelStr;
213 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
215 return lltok::dotdotdot;
221 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::bar;
247 void LLLexer::SkipLineComment() {
249 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
254 /// Lex all tokens that start with an @ character.
255 /// GlobalVar @\"[^\"]*\"
256 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
257 /// GlobalVarID @[0-9]+
258 lltok::Kind LLLexer::LexAt() {
259 return LexVar(lltok::GlobalVar, lltok::GlobalID);
262 lltok::Kind LLLexer::LexDollar() {
263 if (const char *Ptr = isLabelTail(TokStart)) {
265 StrVal.assign(TokStart, CurPtr - 1);
266 return lltok::LabelStr;
269 // Handle DollarStringConstant: $\"[^\"]*\"
270 if (CurPtr[0] == '"') {
274 int CurChar = getNextChar();
276 if (CurChar == EOF) {
277 Error("end of file in COMDAT variable name");
280 if (CurChar == '"') {
281 StrVal.assign(TokStart + 2, CurPtr - 1);
282 UnEscapeLexed(StrVal);
283 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
284 Error("Null bytes are not allowed in names");
287 return lltok::ComdatVar;
292 // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
294 return lltok::ComdatVar;
299 /// ReadString - Read a string until the closing quote.
300 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
301 const char *Start = CurPtr;
303 int CurChar = getNextChar();
305 if (CurChar == EOF) {
306 Error("end of file in string constant");
309 if (CurChar == '"') {
310 StrVal.assign(Start, CurPtr-1);
311 UnEscapeLexed(StrVal);
317 /// ReadVarName - Read the rest of a token containing a variable name.
318 bool LLLexer::ReadVarName() {
319 const char *NameStart = CurPtr;
320 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
321 CurPtr[0] == '-' || CurPtr[0] == '$' ||
322 CurPtr[0] == '.' || CurPtr[0] == '_') {
324 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
325 CurPtr[0] == '-' || CurPtr[0] == '$' ||
326 CurPtr[0] == '.' || CurPtr[0] == '_')
329 StrVal.assign(NameStart, CurPtr);
335 // Lex an ID: [0-9]+. On success, the ID is stored in UIntVal and Token is
336 // returned, otherwise the Error token is returned.
337 lltok::Kind LLLexer::LexUIntID(lltok::Kind Token) {
338 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
341 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
344 uint64_t Val = atoull(TokStart + 1, CurPtr);
345 if ((unsigned)Val != Val)
346 Error("invalid value number (too large)!");
347 UIntVal = unsigned(Val);
351 lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) {
352 // Handle StringConstant: \"[^\"]*\"
353 if (CurPtr[0] == '"') {
357 int CurChar = getNextChar();
359 if (CurChar == EOF) {
360 Error("end of file in global variable name");
363 if (CurChar == '"') {
364 StrVal.assign(TokStart+2, CurPtr-1);
365 UnEscapeLexed(StrVal);
366 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
367 Error("Null bytes are not allowed in names");
375 // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]*
379 // Handle VarID: [0-9]+
380 return LexUIntID(VarID);
383 /// Lex all tokens that start with a % character.
384 /// LocalVar ::= %\"[^\"]*\"
385 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
386 /// LocalVarID ::= %[0-9]+
387 lltok::Kind LLLexer::LexPercent() {
388 return LexVar(lltok::LocalVar, lltok::LocalVarID);
391 /// Lex all tokens that start with a " character.
392 /// QuoteLabel "[^"]+":
393 /// StringConstant "[^"]*"
394 lltok::Kind LLLexer::LexQuote() {
395 lltok::Kind kind = ReadString(lltok::StringConstant);
396 if (kind == lltok::Error || kind == lltok::Eof)
399 if (CurPtr[0] == ':') {
401 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
402 Error("Null bytes are not allowed in names");
405 kind = lltok::LabelStr;
412 /// Lex all tokens that start with a ! character.
415 lltok::Kind LLLexer::LexExclaim() {
416 // Lex a metadata name as a MetadataVar.
417 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
418 CurPtr[0] == '-' || CurPtr[0] == '$' ||
419 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
421 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
422 CurPtr[0] == '-' || CurPtr[0] == '$' ||
423 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
426 StrVal.assign(TokStart+1, CurPtr); // Skip !
427 UnEscapeLexed(StrVal);
428 return lltok::MetadataVar;
430 return lltok::exclaim;
433 /// Lex all tokens that start with a ^ character.
434 /// SummaryID ::= ^[0-9]+
435 lltok::Kind LLLexer::LexCaret() {
436 // Handle SummaryID: ^[0-9]+
437 return LexUIntID(lltok::SummaryID);
440 /// Lex all tokens that start with a # character.
441 /// AttrGrpID ::= #[0-9]+
442 lltok::Kind LLLexer::LexHash() {
443 // Handle AttrGrpID: #[0-9]+
444 return LexUIntID(lltok::AttrGrpID);
447 /// Lex a label, integer type, keyword, or hexadecimal integer constant.
448 /// Label [-a-zA-Z$._0-9]+:
449 /// IntegerType i[0-9]+
450 /// Keyword sdiv, float, ...
451 /// HexIntConstant [us]0x[0-9A-Fa-f]+
452 lltok::Kind LLLexer::LexIdentifier() {
453 const char *StartChar = CurPtr;
454 const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar;
455 const char *KeywordEnd = nullptr;
457 for (; isLabelChar(*CurPtr); ++CurPtr) {
458 // If we decide this is an integer, remember the end of the sequence.
459 if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
461 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
466 // If we stopped due to a colon, unless we were directed to ignore it,
467 // this really is a label.
468 if (!IgnoreColonInIdentifiers && *CurPtr == ':') {
469 StrVal.assign(StartChar-1, CurPtr++);
470 return lltok::LabelStr;
473 // Otherwise, this wasn't a label. If this was valid as an integer type,
475 if (!IntEnd) IntEnd = CurPtr;
476 if (IntEnd != StartChar) {
478 uint64_t NumBits = atoull(StartChar, CurPtr);
479 if (NumBits < IntegerType::MIN_INT_BITS ||
480 NumBits > IntegerType::MAX_INT_BITS) {
481 Error("bitwidth for integer type out of range!");
484 TyVal = IntegerType::get(Context, NumBits);
488 // Otherwise, this was a letter sequence. See which keyword this is.
489 if (!KeywordEnd) KeywordEnd = CurPtr;
492 StringRef Keyword(StartChar, CurPtr - StartChar);
494 #define KEYWORD(STR) \
496 if (Keyword == #STR) \
497 return lltok::kw_##STR; \
500 KEYWORD(true); KEYWORD(false);
501 KEYWORD(declare); KEYWORD(define);
502 KEYWORD(global); KEYWORD(constant);
505 KEYWORD(dso_preemptable);
509 KEYWORD(available_externally);
511 KEYWORD(linkonce_odr);
512 KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
521 KEYWORD(unnamed_addr);
522 KEYWORD(local_unnamed_addr);
523 KEYWORD(externally_initialized);
524 KEYWORD(extern_weak);
526 KEYWORD(thread_local);
527 KEYWORD(localdynamic);
528 KEYWORD(initialexec);
530 KEYWORD(zeroinitializer);
543 KEYWORD(source_filename);
545 KEYWORD(deplibs); // FIXME: Remove in 4.0.
580 KEYWORD(inteldialect);
588 KEYWORD(x86_stdcallcc);
589 KEYWORD(x86_fastcallcc);
590 KEYWORD(x86_thiscallcc);
591 KEYWORD(x86_vectorcallcc);
593 KEYWORD(arm_aapcscc);
594 KEYWORD(arm_aapcs_vfpcc);
595 KEYWORD(aarch64_vector_pcs);
596 KEYWORD(msp430_intrcc);
598 KEYWORD(avr_signalcc);
601 KEYWORD(spir_kernel);
603 KEYWORD(intel_ocl_bicc);
604 KEYWORD(x86_64_sysvcc);
606 KEYWORD(x86_regcallcc);
607 KEYWORD(webkit_jscc);
610 KEYWORD(preserve_mostcc);
611 KEYWORD(preserve_allcc);
616 KEYWORD(cxx_fast_tlscc);
624 KEYWORD(amdgpu_kernel);
631 KEYWORD(alwaysinline);
639 KEYWORD(dereferenceable);
640 KEYWORD(dereferenceable_or_null);
641 KEYWORD(inaccessiblememonly);
642 KEYWORD(inaccessiblemem_or_argmemonly);
652 KEYWORD(noduplicate);
654 KEYWORD(noimplicitfloat);
657 KEYWORD(nonlazybind);
664 KEYWORD(optforfuzzing);
670 KEYWORD(returns_twice);
672 KEYWORD(speculatable);
679 KEYWORD(shadowcallstack);
680 KEYWORD(sanitize_address);
681 KEYWORD(sanitize_hwaddress);
682 KEYWORD(sanitize_memtag);
683 KEYWORD(sanitize_thread);
684 KEYWORD(sanitize_memory);
685 KEYWORD(speculative_load_hardening);
703 KEYWORD(noduplicates);
706 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
707 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
708 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
709 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
711 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
716 KEYWORD(blockaddress);
721 // Use-list order directives.
722 KEYWORD(uselistorder);
723 KEYWORD(uselistorder_bb);
725 KEYWORD(personality);
730 // Summary index keywords.
739 KEYWORD(notEligibleToImport);
742 KEYWORD(canAutoHide);
749 KEYWORD(returnDoesNotAlias);
759 KEYWORD(vTableFuncs);
765 KEYWORD(typeTestAssumeVCalls);
766 KEYWORD(typeCheckedLoadVCalls);
767 KEYWORD(typeTestAssumeConstVCalls);
768 KEYWORD(typeCheckedLoadConstVCalls);
773 KEYWORD(typeidCompatibleVTable);
775 KEYWORD(typeTestRes);
782 KEYWORD(sizeM1BitWidth);
787 KEYWORD(wpdResolutions);
791 KEYWORD(branchFunnel);
792 KEYWORD(singleImplName);
795 KEYWORD(uniformRetVal);
796 KEYWORD(uniqueRetVal);
797 KEYWORD(virtualConstProp);
805 // Keywords for types.
806 #define TYPEKEYWORD(STR, LLVMTY) \
808 if (Keyword == STR) { \
810 return lltok::Type; \
814 TYPEKEYWORD("void", Type::getVoidTy(Context));
815 TYPEKEYWORD("half", Type::getHalfTy(Context));
816 TYPEKEYWORD("float", Type::getFloatTy(Context));
817 TYPEKEYWORD("double", Type::getDoubleTy(Context));
818 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
819 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
820 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
821 TYPEKEYWORD("label", Type::getLabelTy(Context));
822 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
823 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
824 TYPEKEYWORD("token", Type::getTokenTy(Context));
828 // Keywords for instructions.
829 #define INSTKEYWORD(STR, Enum) \
831 if (Keyword == #STR) { \
832 UIntVal = Instruction::Enum; \
833 return lltok::kw_##STR; \
837 INSTKEYWORD(fneg, FNeg);
839 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
840 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
841 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
842 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
843 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
844 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
845 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
846 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
848 INSTKEYWORD(phi, PHI);
849 INSTKEYWORD(call, Call);
850 INSTKEYWORD(trunc, Trunc);
851 INSTKEYWORD(zext, ZExt);
852 INSTKEYWORD(sext, SExt);
853 INSTKEYWORD(fptrunc, FPTrunc);
854 INSTKEYWORD(fpext, FPExt);
855 INSTKEYWORD(uitofp, UIToFP);
856 INSTKEYWORD(sitofp, SIToFP);
857 INSTKEYWORD(fptoui, FPToUI);
858 INSTKEYWORD(fptosi, FPToSI);
859 INSTKEYWORD(inttoptr, IntToPtr);
860 INSTKEYWORD(ptrtoint, PtrToInt);
861 INSTKEYWORD(bitcast, BitCast);
862 INSTKEYWORD(addrspacecast, AddrSpaceCast);
863 INSTKEYWORD(select, Select);
864 INSTKEYWORD(va_arg, VAArg);
865 INSTKEYWORD(ret, Ret);
867 INSTKEYWORD(switch, Switch);
868 INSTKEYWORD(indirectbr, IndirectBr);
869 INSTKEYWORD(invoke, Invoke);
870 INSTKEYWORD(resume, Resume);
871 INSTKEYWORD(unreachable, Unreachable);
872 INSTKEYWORD(callbr, CallBr);
874 INSTKEYWORD(alloca, Alloca);
875 INSTKEYWORD(load, Load);
876 INSTKEYWORD(store, Store);
877 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
878 INSTKEYWORD(atomicrmw, AtomicRMW);
879 INSTKEYWORD(fence, Fence);
880 INSTKEYWORD(getelementptr, GetElementPtr);
882 INSTKEYWORD(extractelement, ExtractElement);
883 INSTKEYWORD(insertelement, InsertElement);
884 INSTKEYWORD(shufflevector, ShuffleVector);
885 INSTKEYWORD(extractvalue, ExtractValue);
886 INSTKEYWORD(insertvalue, InsertValue);
887 INSTKEYWORD(landingpad, LandingPad);
888 INSTKEYWORD(cleanupret, CleanupRet);
889 INSTKEYWORD(catchret, CatchRet);
890 INSTKEYWORD(catchswitch, CatchSwitch);
891 INSTKEYWORD(catchpad, CatchPad);
892 INSTKEYWORD(cleanuppad, CleanupPad);
896 #define DWKEYWORD(TYPE, TOKEN) \
898 if (Keyword.startswith("DW_" #TYPE "_")) { \
899 StrVal.assign(Keyword.begin(), Keyword.end()); \
900 return lltok::TOKEN; \
904 DWKEYWORD(TAG, DwarfTag);
905 DWKEYWORD(ATE, DwarfAttEncoding);
906 DWKEYWORD(VIRTUALITY, DwarfVirtuality);
907 DWKEYWORD(LANG, DwarfLang);
908 DWKEYWORD(CC, DwarfCC);
909 DWKEYWORD(OP, DwarfOp);
910 DWKEYWORD(MACINFO, DwarfMacinfo);
914 if (Keyword.startswith("DIFlag")) {
915 StrVal.assign(Keyword.begin(), Keyword.end());
916 return lltok::DIFlag;
919 if (Keyword.startswith("DISPFlag")) {
920 StrVal.assign(Keyword.begin(), Keyword.end());
921 return lltok::DISPFlag;
924 if (Keyword.startswith("CSK_")) {
925 StrVal.assign(Keyword.begin(), Keyword.end());
926 return lltok::ChecksumKind;
929 if (Keyword == "NoDebug" || Keyword == "FullDebug" ||
930 Keyword == "LineTablesOnly" || Keyword == "DebugDirectivesOnly") {
931 StrVal.assign(Keyword.begin(), Keyword.end());
932 return lltok::EmissionKind;
935 if (Keyword == "GNU" || Keyword == "None" || Keyword == "Default") {
936 StrVal.assign(Keyword.begin(), Keyword.end());
937 return lltok::NameTableKind;
940 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
941 // the CFE to avoid forcing it to deal with 64-bit numbers.
942 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
943 TokStart[1] == '0' && TokStart[2] == 'x' &&
944 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
945 int len = CurPtr-TokStart-3;
946 uint32_t bits = len * 4;
947 StringRef HexStr(TokStart + 3, len);
948 if (!all_of(HexStr, isxdigit)) {
949 // Bad token, return it as an error.
953 APInt Tmp(bits, HexStr, 16);
954 uint32_t activeBits = Tmp.getActiveBits();
955 if (activeBits > 0 && activeBits < bits)
956 Tmp = Tmp.trunc(activeBits);
957 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
958 return lltok::APSInt;
961 // If this is "cc1234", return this as just "cc".
962 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
967 // Finally, if this isn't known, return an error.
972 /// Lex all tokens that start with a 0x prefix, knowing they match and are not
974 /// HexFPConstant 0x[0-9A-Fa-f]+
975 /// HexFP80Constant 0xK[0-9A-Fa-f]+
976 /// HexFP128Constant 0xL[0-9A-Fa-f]+
977 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
978 /// HexHalfConstant 0xH[0-9A-Fa-f]+
979 lltok::Kind LLLexer::Lex0x() {
980 CurPtr = TokStart + 2;
983 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
989 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
990 // Bad token, return it as an error.
995 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
999 // HexFPConstant - Floating point constant represented in IEEE format as a
1000 // hexadecimal number for when exponential notation is not precise enough.
1001 // Half, Float, and double only.
1002 APFloatVal = APFloat(APFloat::IEEEdouble(),
1003 APInt(64, HexIntToVal(TokStart + 2, CurPtr)));
1004 return lltok::APFloat;
1009 default: llvm_unreachable("Unknown kind!");
1011 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
1012 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
1013 APFloatVal = APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair));
1014 return lltok::APFloat;
1016 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
1017 HexToIntPair(TokStart+3, CurPtr, Pair);
1018 APFloatVal = APFloat(APFloat::IEEEquad(), APInt(128, Pair));
1019 return lltok::APFloat;
1021 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
1022 HexToIntPair(TokStart+3, CurPtr, Pair);
1023 APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair));
1024 return lltok::APFloat;
1026 APFloatVal = APFloat(APFloat::IEEEhalf(),
1027 APInt(16,HexIntToVal(TokStart+3, CurPtr)));
1028 return lltok::APFloat;
1032 /// Lex tokens for a label or a numeric constant, possibly starting with -.
1033 /// Label [-a-zA-Z$._0-9]+:
1034 /// NInteger -[0-9]+
1035 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1037 /// HexFPConstant 0x[0-9A-Fa-f]+
1038 /// HexFP80Constant 0xK[0-9A-Fa-f]+
1039 /// HexFP128Constant 0xL[0-9A-Fa-f]+
1040 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
1041 lltok::Kind LLLexer::LexDigitOrNegative() {
1042 // If the letter after the negative is not a number, this is probably a label.
1043 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
1044 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
1045 // Okay, this is not a number after the -, it's probably a label.
1046 if (const char *End = isLabelTail(CurPtr)) {
1047 StrVal.assign(TokStart, End-1);
1049 return lltok::LabelStr;
1052 return lltok::Error;
1055 // At this point, it is either a label, int or fp constant.
1057 // Skip digits, we have at least one.
1058 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1061 // Check if this is a fully-numeric label:
1062 if (isdigit(TokStart[0]) && CurPtr[0] == ':') {
1063 uint64_t Val = atoull(TokStart, CurPtr);
1064 ++CurPtr; // Skip the colon.
1065 if ((unsigned)Val != Val)
1066 Error("invalid value number (too large)!");
1067 UIntVal = unsigned(Val);
1068 return lltok::LabelID;
1071 // Check to see if this really is a string label, e.g. "-1:".
1072 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
1073 if (const char *End = isLabelTail(CurPtr)) {
1074 StrVal.assign(TokStart, End-1);
1076 return lltok::LabelStr;
1080 // If the next character is a '.', then it is a fp value, otherwise its
1082 if (CurPtr[0] != '.') {
1083 if (TokStart[0] == '0' && TokStart[1] == 'x')
1085 APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart));
1086 return lltok::APSInt;
1091 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1092 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1094 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1095 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1096 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1097 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1099 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1103 APFloatVal = APFloat(APFloat::IEEEdouble(),
1104 StringRef(TokStart, CurPtr - TokStart));
1105 return lltok::APFloat;
1108 /// Lex a floating point constant starting with +.
1109 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1110 lltok::Kind LLLexer::LexPositive() {
1111 // If the letter after the negative is a number, this is probably not a
1113 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
1114 return lltok::Error;
1117 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1120 // At this point, we need a '.'.
1121 if (CurPtr[0] != '.') {
1122 CurPtr = TokStart+1;
1123 return lltok::Error;
1128 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1129 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1131 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1132 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1133 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1134 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1136 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1140 APFloatVal = APFloat(APFloat::IEEEdouble(),
1141 StringRef(TokStart, CurPtr - TokStart));
1142 return lltok::APFloat;