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/APInt.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/Instruction.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/SourceMgr.h"
29 bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
30 ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
34 void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const {
35 SM.PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
38 //===----------------------------------------------------------------------===//
40 //===----------------------------------------------------------------------===//
42 // atoull - Convert an ascii string of decimal digits into the unsigned long
43 // long representation... this does not have to do input error checking,
44 // because we know that the input will be matched by a suitable regex...
46 uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
48 for (; Buffer != End; Buffer++) {
49 uint64_t OldRes = Result;
51 Result += *Buffer-'0';
52 if (Result < OldRes) { // Uh, oh, overflow detected!!!
53 Error("constant bigger than 64 bits detected!");
60 uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
62 for (; Buffer != End; ++Buffer) {
63 uint64_t OldRes = Result;
65 Result += hexDigitValue(*Buffer);
67 if (Result < OldRes) { // Uh, oh, overflow detected!!!
68 Error("constant bigger than 64 bits detected!");
75 void LLLexer::HexToIntPair(const char *Buffer, const char *End,
78 if (End - Buffer >= 16) {
79 for (int i = 0; i < 16; i++, Buffer++) {
80 assert(Buffer != End);
82 Pair[0] += hexDigitValue(*Buffer);
86 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
88 Pair[1] += hexDigitValue(*Buffer);
91 Error("constant bigger than 128 bits detected!");
94 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
95 /// { low64, high16 } as usual for an APInt.
96 void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
99 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
100 assert(Buffer != End);
102 Pair[1] += hexDigitValue(*Buffer);
105 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
107 Pair[0] += hexDigitValue(*Buffer);
110 Error("constant bigger than 128 bits detected!");
113 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
114 // appropriate character.
115 static void UnEscapeLexed(std::string &Str) {
116 if (Str.empty()) return;
118 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
120 for (char *BIn = Buffer; BIn != EndBuffer; ) {
121 if (BIn[0] == '\\') {
122 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
123 *BOut++ = '\\'; // Two \ becomes one
125 } else if (BIn < EndBuffer-2 &&
126 isxdigit(static_cast<unsigned char>(BIn[1])) &&
127 isxdigit(static_cast<unsigned char>(BIn[2]))) {
128 *BOut = hexDigitValue(BIn[1]) * 16 + hexDigitValue(BIn[2]);
129 BIn += 3; // Skip over handled chars
138 Str.resize(BOut-Buffer);
141 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
142 static bool isLabelChar(char C) {
143 return isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
144 C == '.' || C == '_';
147 /// isLabelTail - Return true if this pointer points to a valid end of a label.
148 static const char *isLabelTail(const char *CurPtr) {
150 if (CurPtr[0] == ':') return CurPtr+1;
151 if (!isLabelChar(CurPtr[0])) return nullptr;
156 //===----------------------------------------------------------------------===//
158 //===----------------------------------------------------------------------===//
160 LLLexer::LLLexer(StringRef StartBuf, SourceMgr &sm, SMDiagnostic &Err,
162 : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
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();
222 case '#': return LexHash();
223 case '0': case '1': case '2': case '3': case '4':
224 case '5': case '6': case '7': case '8': case '9':
226 return LexDigitOrNegative();
227 case '=': return lltok::equal;
228 case '[': return lltok::lsquare;
229 case ']': return lltok::rsquare;
230 case '{': return lltok::lbrace;
231 case '}': return lltok::rbrace;
232 case '<': return lltok::less;
233 case '>': return lltok::greater;
234 case '(': return lltok::lparen;
235 case ')': return lltok::rparen;
236 case ',': return lltok::comma;
237 case '*': return lltok::star;
238 case '|': return lltok::bar;
243 void LLLexer::SkipLineComment() {
245 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
250 /// Lex all tokens that start with an @ character.
251 /// GlobalVar @\"[^\"]*\"
252 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
253 /// GlobalVarID @[0-9]+
254 lltok::Kind LLLexer::LexAt() {
255 return LexVar(lltok::GlobalVar, lltok::GlobalID);
258 lltok::Kind LLLexer::LexDollar() {
259 if (const char *Ptr = isLabelTail(TokStart)) {
261 StrVal.assign(TokStart, CurPtr - 1);
262 return lltok::LabelStr;
265 // Handle DollarStringConstant: $\"[^\"]*\"
266 if (CurPtr[0] == '"') {
270 int CurChar = getNextChar();
272 if (CurChar == EOF) {
273 Error("end of file in COMDAT variable name");
276 if (CurChar == '"') {
277 StrVal.assign(TokStart + 2, CurPtr - 1);
278 UnEscapeLexed(StrVal);
279 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
280 Error("Null bytes are not allowed in names");
283 return lltok::ComdatVar;
288 // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
290 return lltok::ComdatVar;
295 /// ReadString - Read a string until the closing quote.
296 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
297 const char *Start = CurPtr;
299 int CurChar = getNextChar();
301 if (CurChar == EOF) {
302 Error("end of file in string constant");
305 if (CurChar == '"') {
306 StrVal.assign(Start, CurPtr-1);
307 UnEscapeLexed(StrVal);
313 /// ReadVarName - Read the rest of a token containing a variable name.
314 bool LLLexer::ReadVarName() {
315 const char *NameStart = CurPtr;
316 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
317 CurPtr[0] == '-' || CurPtr[0] == '$' ||
318 CurPtr[0] == '.' || CurPtr[0] == '_') {
320 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
321 CurPtr[0] == '-' || CurPtr[0] == '$' ||
322 CurPtr[0] == '.' || CurPtr[0] == '_')
325 StrVal.assign(NameStart, CurPtr);
331 lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) {
332 // Handle StringConstant: \"[^\"]*\"
333 if (CurPtr[0] == '"') {
337 int CurChar = getNextChar();
339 if (CurChar == EOF) {
340 Error("end of file in global variable name");
343 if (CurChar == '"') {
344 StrVal.assign(TokStart+2, CurPtr-1);
345 UnEscapeLexed(StrVal);
346 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
347 Error("Null bytes are not allowed in names");
355 // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]*
359 // Handle VarID: [0-9]+
360 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
361 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
364 uint64_t Val = atoull(TokStart+1, CurPtr);
365 if ((unsigned)Val != Val)
366 Error("invalid value number (too large)!");
367 UIntVal = unsigned(Val);
373 /// Lex all tokens that start with a % character.
374 /// LocalVar ::= %\"[^\"]*\"
375 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
376 /// LocalVarID ::= %[0-9]+
377 lltok::Kind LLLexer::LexPercent() {
378 return LexVar(lltok::LocalVar, lltok::LocalVarID);
381 /// Lex all tokens that start with a " character.
382 /// QuoteLabel "[^"]+":
383 /// StringConstant "[^"]*"
384 lltok::Kind LLLexer::LexQuote() {
385 lltok::Kind kind = ReadString(lltok::StringConstant);
386 if (kind == lltok::Error || kind == lltok::Eof)
389 if (CurPtr[0] == ':') {
391 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
392 Error("Null bytes are not allowed in names");
395 kind = lltok::LabelStr;
402 /// Lex all tokens that start with a ! character.
405 lltok::Kind LLLexer::LexExclaim() {
406 // Lex a metadata name as a MetadataVar.
407 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
408 CurPtr[0] == '-' || CurPtr[0] == '$' ||
409 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
411 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
412 CurPtr[0] == '-' || CurPtr[0] == '$' ||
413 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
416 StrVal.assign(TokStart+1, CurPtr); // Skip !
417 UnEscapeLexed(StrVal);
418 return lltok::MetadataVar;
420 return lltok::exclaim;
423 /// Lex all tokens that start with a # character.
424 /// AttrGrpID ::= #[0-9]+
425 lltok::Kind LLLexer::LexHash() {
426 // Handle AttrGrpID: #[0-9]+
427 if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
428 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
431 uint64_t Val = atoull(TokStart+1, CurPtr);
432 if ((unsigned)Val != Val)
433 Error("invalid value number (too large)!");
434 UIntVal = unsigned(Val);
435 return lltok::AttrGrpID;
441 /// Lex a label, integer type, keyword, or hexadecimal integer constant.
442 /// Label [-a-zA-Z$._0-9]+:
443 /// IntegerType i[0-9]+
444 /// Keyword sdiv, float, ...
445 /// HexIntConstant [us]0x[0-9A-Fa-f]+
446 lltok::Kind LLLexer::LexIdentifier() {
447 const char *StartChar = CurPtr;
448 const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar;
449 const char *KeywordEnd = nullptr;
451 for (; isLabelChar(*CurPtr); ++CurPtr) {
452 // If we decide this is an integer, remember the end of the sequence.
453 if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
455 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
460 // If we stopped due to a colon, this really is a label.
461 if (*CurPtr == ':') {
462 StrVal.assign(StartChar-1, CurPtr++);
463 return lltok::LabelStr;
466 // Otherwise, this wasn't a label. If this was valid as an integer type,
468 if (!IntEnd) IntEnd = CurPtr;
469 if (IntEnd != StartChar) {
471 uint64_t NumBits = atoull(StartChar, CurPtr);
472 if (NumBits < IntegerType::MIN_INT_BITS ||
473 NumBits > IntegerType::MAX_INT_BITS) {
474 Error("bitwidth for integer type out of range!");
477 TyVal = IntegerType::get(Context, NumBits);
481 // Otherwise, this was a letter sequence. See which keyword this is.
482 if (!KeywordEnd) KeywordEnd = CurPtr;
485 StringRef Keyword(StartChar, CurPtr - StartChar);
487 #define KEYWORD(STR) \
489 if (Keyword == #STR) \
490 return lltok::kw_##STR; \
493 KEYWORD(true); KEYWORD(false);
494 KEYWORD(declare); KEYWORD(define);
495 KEYWORD(global); KEYWORD(constant);
499 KEYWORD(available_externally);
501 KEYWORD(linkonce_odr);
502 KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
511 KEYWORD(unnamed_addr);
512 KEYWORD(local_unnamed_addr);
513 KEYWORD(externally_initialized);
514 KEYWORD(extern_weak);
516 KEYWORD(thread_local);
517 KEYWORD(localdynamic);
518 KEYWORD(initialexec);
520 KEYWORD(zeroinitializer);
533 KEYWORD(source_filename);
535 KEYWORD(deplibs); // FIXME: Remove in 4.0.
545 KEYWORD(singlethread);
567 KEYWORD(inteldialect);
575 KEYWORD(x86_stdcallcc);
576 KEYWORD(x86_fastcallcc);
577 KEYWORD(x86_thiscallcc);
578 KEYWORD(x86_vectorcallcc);
580 KEYWORD(arm_aapcscc);
581 KEYWORD(arm_aapcs_vfpcc);
582 KEYWORD(msp430_intrcc);
584 KEYWORD(avr_signalcc);
587 KEYWORD(spir_kernel);
589 KEYWORD(intel_ocl_bicc);
590 KEYWORD(x86_64_sysvcc);
591 KEYWORD(x86_64_win64cc);
592 KEYWORD(x86_regcallcc);
593 KEYWORD(webkit_jscc);
596 KEYWORD(preserve_mostcc);
597 KEYWORD(preserve_allcc);
602 KEYWORD(cxx_fast_tlscc);
608 KEYWORD(amdgpu_kernel);
615 KEYWORD(alwaysinline);
623 KEYWORD(dereferenceable);
624 KEYWORD(dereferenceable_or_null);
625 KEYWORD(inaccessiblememonly);
626 KEYWORD(inaccessiblemem_or_argmemonly);
636 KEYWORD(noduplicate);
637 KEYWORD(noimplicitfloat);
640 KEYWORD(nonlazybind);
650 KEYWORD(returns_twice);
652 KEYWORD(speculatable);
658 KEYWORD(sanitize_address);
659 KEYWORD(sanitize_thread);
660 KEYWORD(sanitize_memory);
676 KEYWORD(noduplicates);
679 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
680 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
681 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
682 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
684 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
688 KEYWORD(blockaddress);
693 // Use-list order directives.
694 KEYWORD(uselistorder);
695 KEYWORD(uselistorder_bb);
697 KEYWORD(personality);
704 // Keywords for types.
705 #define TYPEKEYWORD(STR, LLVMTY) \
707 if (Keyword == STR) { \
709 return lltok::Type; \
713 TYPEKEYWORD("void", Type::getVoidTy(Context));
714 TYPEKEYWORD("half", Type::getHalfTy(Context));
715 TYPEKEYWORD("float", Type::getFloatTy(Context));
716 TYPEKEYWORD("double", Type::getDoubleTy(Context));
717 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
718 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
719 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
720 TYPEKEYWORD("label", Type::getLabelTy(Context));
721 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
722 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
723 TYPEKEYWORD("token", Type::getTokenTy(Context));
727 // Keywords for instructions.
728 #define INSTKEYWORD(STR, Enum) \
730 if (Keyword == #STR) { \
731 UIntVal = Instruction::Enum; \
732 return lltok::kw_##STR; \
736 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
737 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
738 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
739 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
740 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
741 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
742 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
743 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
745 INSTKEYWORD(phi, PHI);
746 INSTKEYWORD(call, Call);
747 INSTKEYWORD(trunc, Trunc);
748 INSTKEYWORD(zext, ZExt);
749 INSTKEYWORD(sext, SExt);
750 INSTKEYWORD(fptrunc, FPTrunc);
751 INSTKEYWORD(fpext, FPExt);
752 INSTKEYWORD(uitofp, UIToFP);
753 INSTKEYWORD(sitofp, SIToFP);
754 INSTKEYWORD(fptoui, FPToUI);
755 INSTKEYWORD(fptosi, FPToSI);
756 INSTKEYWORD(inttoptr, IntToPtr);
757 INSTKEYWORD(ptrtoint, PtrToInt);
758 INSTKEYWORD(bitcast, BitCast);
759 INSTKEYWORD(addrspacecast, AddrSpaceCast);
760 INSTKEYWORD(select, Select);
761 INSTKEYWORD(va_arg, VAArg);
762 INSTKEYWORD(ret, Ret);
764 INSTKEYWORD(switch, Switch);
765 INSTKEYWORD(indirectbr, IndirectBr);
766 INSTKEYWORD(invoke, Invoke);
767 INSTKEYWORD(resume, Resume);
768 INSTKEYWORD(unreachable, Unreachable);
770 INSTKEYWORD(alloca, Alloca);
771 INSTKEYWORD(load, Load);
772 INSTKEYWORD(store, Store);
773 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
774 INSTKEYWORD(atomicrmw, AtomicRMW);
775 INSTKEYWORD(fence, Fence);
776 INSTKEYWORD(getelementptr, GetElementPtr);
778 INSTKEYWORD(extractelement, ExtractElement);
779 INSTKEYWORD(insertelement, InsertElement);
780 INSTKEYWORD(shufflevector, ShuffleVector);
781 INSTKEYWORD(extractvalue, ExtractValue);
782 INSTKEYWORD(insertvalue, InsertValue);
783 INSTKEYWORD(landingpad, LandingPad);
784 INSTKEYWORD(cleanupret, CleanupRet);
785 INSTKEYWORD(catchret, CatchRet);
786 INSTKEYWORD(catchswitch, CatchSwitch);
787 INSTKEYWORD(catchpad, CatchPad);
788 INSTKEYWORD(cleanuppad, CleanupPad);
792 #define DWKEYWORD(TYPE, TOKEN) \
794 if (Keyword.startswith("DW_" #TYPE "_")) { \
795 StrVal.assign(Keyword.begin(), Keyword.end()); \
796 return lltok::TOKEN; \
800 DWKEYWORD(TAG, DwarfTag);
801 DWKEYWORD(ATE, DwarfAttEncoding);
802 DWKEYWORD(VIRTUALITY, DwarfVirtuality);
803 DWKEYWORD(LANG, DwarfLang);
804 DWKEYWORD(CC, DwarfCC);
805 DWKEYWORD(OP, DwarfOp);
806 DWKEYWORD(MACINFO, DwarfMacinfo);
810 if (Keyword.startswith("DIFlag")) {
811 StrVal.assign(Keyword.begin(), Keyword.end());
812 return lltok::DIFlag;
815 if (Keyword.startswith("CSK_")) {
816 StrVal.assign(Keyword.begin(), Keyword.end());
817 return lltok::ChecksumKind;
820 if (Keyword == "NoDebug" || Keyword == "FullDebug" ||
821 Keyword == "LineTablesOnly") {
822 StrVal.assign(Keyword.begin(), Keyword.end());
823 return lltok::EmissionKind;
826 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
827 // the CFE to avoid forcing it to deal with 64-bit numbers.
828 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
829 TokStart[1] == '0' && TokStart[2] == 'x' &&
830 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
831 int len = CurPtr-TokStart-3;
832 uint32_t bits = len * 4;
833 StringRef HexStr(TokStart + 3, len);
834 if (!all_of(HexStr, isxdigit)) {
835 // Bad token, return it as an error.
839 APInt Tmp(bits, HexStr, 16);
840 uint32_t activeBits = Tmp.getActiveBits();
841 if (activeBits > 0 && activeBits < bits)
842 Tmp = Tmp.trunc(activeBits);
843 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
844 return lltok::APSInt;
847 // If this is "cc1234", return this as just "cc".
848 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
853 // Finally, if this isn't known, return an error.
858 /// Lex all tokens that start with a 0x prefix, knowing they match and are not
860 /// HexFPConstant 0x[0-9A-Fa-f]+
861 /// HexFP80Constant 0xK[0-9A-Fa-f]+
862 /// HexFP128Constant 0xL[0-9A-Fa-f]+
863 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
864 /// HexHalfConstant 0xH[0-9A-Fa-f]+
865 lltok::Kind LLLexer::Lex0x() {
866 CurPtr = TokStart + 2;
869 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
875 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
876 // Bad token, return it as an error.
881 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
885 // HexFPConstant - Floating point constant represented in IEEE format as a
886 // hexadecimal number for when exponential notation is not precise enough.
887 // Half, Float, and double only.
888 APFloatVal = APFloat(APFloat::IEEEdouble(),
889 APInt(64, HexIntToVal(TokStart + 2, CurPtr)));
890 return lltok::APFloat;
895 default: llvm_unreachable("Unknown kind!");
897 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
898 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
899 APFloatVal = APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair));
900 return lltok::APFloat;
902 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
903 HexToIntPair(TokStart+3, CurPtr, Pair);
904 APFloatVal = APFloat(APFloat::IEEEquad(), APInt(128, Pair));
905 return lltok::APFloat;
907 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
908 HexToIntPair(TokStart+3, CurPtr, Pair);
909 APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair));
910 return lltok::APFloat;
912 APFloatVal = APFloat(APFloat::IEEEhalf(),
913 APInt(16,HexIntToVal(TokStart+3, CurPtr)));
914 return lltok::APFloat;
918 /// Lex tokens for a label or a numeric constant, possibly starting with -.
919 /// Label [-a-zA-Z$._0-9]+:
921 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
923 /// HexFPConstant 0x[0-9A-Fa-f]+
924 /// HexFP80Constant 0xK[0-9A-Fa-f]+
925 /// HexFP128Constant 0xL[0-9A-Fa-f]+
926 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
927 lltok::Kind LLLexer::LexDigitOrNegative() {
928 // If the letter after the negative is not a number, this is probably a label.
929 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
930 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
931 // Okay, this is not a number after the -, it's probably a label.
932 if (const char *End = isLabelTail(CurPtr)) {
933 StrVal.assign(TokStart, End-1);
935 return lltok::LabelStr;
941 // At this point, it is either a label, int or fp constant.
943 // Skip digits, we have at least one.
944 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
947 // Check to see if this really is a label afterall, e.g. "-1:".
948 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
949 if (const char *End = isLabelTail(CurPtr)) {
950 StrVal.assign(TokStart, End-1);
952 return lltok::LabelStr;
956 // If the next character is a '.', then it is a fp value, otherwise its
958 if (CurPtr[0] != '.') {
959 if (TokStart[0] == '0' && TokStart[1] == 'x')
961 APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart));
962 return lltok::APSInt;
967 // Skip over [0-9]*([eE][-+]?[0-9]+)?
968 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
970 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
971 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
972 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
973 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
975 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
979 APFloatVal = APFloat(APFloat::IEEEdouble(),
980 StringRef(TokStart, CurPtr - TokStart));
981 return lltok::APFloat;
984 /// Lex a floating point constant starting with +.
985 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
986 lltok::Kind LLLexer::LexPositive() {
987 // If the letter after the negative is a number, this is probably not a
989 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
993 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
996 // At this point, we need a '.'.
997 if (CurPtr[0] != '.') {
1004 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1005 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1007 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1008 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1009 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1010 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1012 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1016 APFloatVal = APFloat(APFloat::IEEEdouble(),
1017 StringRef(TokStart, CurPtr - TokStart));
1018 return lltok::APFloat;