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 IgnoreColonInIdentifiers(false) {
164 CurPtr
= CurBuf
.begin();
167 int LLLexer::getNextChar() {
168 char CurChar
= *CurPtr
++;
170 default: return (unsigned char)CurChar
;
172 // A nul character in the stream is either the end of the current buffer or
173 // a random nul in the file. Disambiguate that here.
174 if (CurPtr
-1 != CurBuf
.end())
175 return 0; // Just whitespace.
177 // Otherwise, return end of file.
178 --CurPtr
; // Another call to lex will return EOF again.
183 lltok::Kind
LLLexer::LexToken() {
187 int CurChar
= getNextChar();
190 // Handle letters: [a-zA-Z_]
191 if (isalpha(static_cast<unsigned char>(CurChar
)) || CurChar
== '_')
192 return LexIdentifier();
195 case EOF
: return lltok::Eof
;
201 // Ignore whitespace.
203 case '+': return LexPositive();
204 case '@': return LexAt();
205 case '$': return LexDollar();
206 case '%': return LexPercent();
207 case '"': return LexQuote();
209 if (const char *Ptr
= isLabelTail(CurPtr
)) {
211 StrVal
.assign(TokStart
, CurPtr
-1);
212 return lltok::LabelStr
;
214 if (CurPtr
[0] == '.' && CurPtr
[1] == '.') {
216 return lltok::dotdotdot
;
222 case '!': return LexExclaim();
227 case '#': return LexHash();
228 case '0': case '1': case '2': case '3': case '4':
229 case '5': case '6': case '7': case '8': case '9':
231 return LexDigitOrNegative();
232 case '=': return lltok::equal
;
233 case '[': return lltok::lsquare
;
234 case ']': return lltok::rsquare
;
235 case '{': return lltok::lbrace
;
236 case '}': return lltok::rbrace
;
237 case '<': return lltok::less
;
238 case '>': return lltok::greater
;
239 case '(': return lltok::lparen
;
240 case ')': return lltok::rparen
;
241 case ',': return lltok::comma
;
242 case '*': return lltok::star
;
243 case '|': return lltok::bar
;
248 void LLLexer::SkipLineComment() {
250 if (CurPtr
[0] == '\n' || CurPtr
[0] == '\r' || getNextChar() == EOF
)
255 /// 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 // Lex an ID: [0-9]+. On success, the ID is stored in UIntVal and Token is
337 // returned, otherwise the Error token is returned.
338 lltok::Kind
LLLexer::LexUIntID(lltok::Kind Token
) {
339 if (!isdigit(static_cast<unsigned char>(CurPtr
[0])))
342 for (++CurPtr
; isdigit(static_cast<unsigned char>(CurPtr
[0])); ++CurPtr
)
345 uint64_t Val
= atoull(TokStart
+ 1, CurPtr
);
346 if ((unsigned)Val
!= Val
)
347 Error("invalid value number (too large)!");
348 UIntVal
= unsigned(Val
);
352 lltok::Kind
LLLexer::LexVar(lltok::Kind Var
, lltok::Kind VarID
) {
353 // Handle StringConstant: \"[^\"]*\"
354 if (CurPtr
[0] == '"') {
358 int CurChar
= getNextChar();
360 if (CurChar
== EOF
) {
361 Error("end of file in global variable name");
364 if (CurChar
== '"') {
365 StrVal
.assign(TokStart
+2, CurPtr
-1);
366 UnEscapeLexed(StrVal
);
367 if (StringRef(StrVal
).find_first_of(0) != StringRef::npos
) {
368 Error("Null bytes are not allowed in names");
376 // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]*
380 // Handle VarID: [0-9]+
381 return LexUIntID(VarID
);
384 /// Lex all tokens that start with a % character.
385 /// LocalVar ::= %\"[^\"]*\"
386 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
387 /// LocalVarID ::= %[0-9]+
388 lltok::Kind
LLLexer::LexPercent() {
389 return LexVar(lltok::LocalVar
, lltok::LocalVarID
);
392 /// Lex all tokens that start with a " character.
393 /// QuoteLabel "[^"]+":
394 /// StringConstant "[^"]*"
395 lltok::Kind
LLLexer::LexQuote() {
396 lltok::Kind kind
= ReadString(lltok::StringConstant
);
397 if (kind
== lltok::Error
|| kind
== lltok::Eof
)
400 if (CurPtr
[0] == ':') {
402 if (StringRef(StrVal
).find_first_of(0) != StringRef::npos
) {
403 Error("Null bytes are not allowed in names");
406 kind
= lltok::LabelStr
;
413 /// Lex all tokens that start with a ! character.
416 lltok::Kind
LLLexer::LexExclaim() {
417 // Lex a metadata name as a MetadataVar.
418 if (isalpha(static_cast<unsigned char>(CurPtr
[0])) ||
419 CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
420 CurPtr
[0] == '.' || CurPtr
[0] == '_' || CurPtr
[0] == '\\') {
422 while (isalnum(static_cast<unsigned char>(CurPtr
[0])) ||
423 CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
424 CurPtr
[0] == '.' || CurPtr
[0] == '_' || CurPtr
[0] == '\\')
427 StrVal
.assign(TokStart
+1, CurPtr
); // Skip !
428 UnEscapeLexed(StrVal
);
429 return lltok::MetadataVar
;
431 return lltok::exclaim
;
434 /// Lex all tokens that start with a ^ character.
435 /// SummaryID ::= ^[0-9]+
436 lltok::Kind
LLLexer::LexCaret() {
437 // Handle SummaryID: ^[0-9]+
438 return LexUIntID(lltok::SummaryID
);
441 /// Lex all tokens that start with a # character.
442 /// AttrGrpID ::= #[0-9]+
443 lltok::Kind
LLLexer::LexHash() {
444 // Handle AttrGrpID: #[0-9]+
445 return LexUIntID(lltok::AttrGrpID
);
448 /// Lex a label, integer type, keyword, or hexadecimal integer constant.
449 /// Label [-a-zA-Z$._0-9]+:
450 /// IntegerType i[0-9]+
451 /// Keyword sdiv, float, ...
452 /// HexIntConstant [us]0x[0-9A-Fa-f]+
453 lltok::Kind
LLLexer::LexIdentifier() {
454 const char *StartChar
= CurPtr
;
455 const char *IntEnd
= CurPtr
[-1] == 'i' ? nullptr : StartChar
;
456 const char *KeywordEnd
= nullptr;
458 for (; isLabelChar(*CurPtr
); ++CurPtr
) {
459 // If we decide this is an integer, remember the end of the sequence.
460 if (!IntEnd
&& !isdigit(static_cast<unsigned char>(*CurPtr
)))
462 if (!KeywordEnd
&& !isalnum(static_cast<unsigned char>(*CurPtr
)) &&
467 // If we stopped due to a colon, unless we were directed to ignore it,
468 // this really is a label.
469 if (!IgnoreColonInIdentifiers
&& *CurPtr
== ':') {
470 StrVal
.assign(StartChar
-1, CurPtr
++);
471 return lltok::LabelStr
;
474 // Otherwise, this wasn't a label. If this was valid as an integer type,
476 if (!IntEnd
) IntEnd
= CurPtr
;
477 if (IntEnd
!= StartChar
) {
479 uint64_t NumBits
= atoull(StartChar
, CurPtr
);
480 if (NumBits
< IntegerType::MIN_INT_BITS
||
481 NumBits
> IntegerType::MAX_INT_BITS
) {
482 Error("bitwidth for integer type out of range!");
485 TyVal
= IntegerType::get(Context
, NumBits
);
489 // Otherwise, this was a letter sequence. See which keyword this is.
490 if (!KeywordEnd
) KeywordEnd
= CurPtr
;
493 StringRef
Keyword(StartChar
, CurPtr
- StartChar
);
495 #define KEYWORD(STR) \
497 if (Keyword == #STR) \
498 return lltok::kw_##STR; \
501 KEYWORD(true); KEYWORD(false);
502 KEYWORD(declare
); KEYWORD(define
);
503 KEYWORD(global
); KEYWORD(constant
);
506 KEYWORD(dso_preemptable
);
510 KEYWORD(available_externally
);
512 KEYWORD(linkonce_odr
);
513 KEYWORD(weak
); // Use as a linkage, and a modifier for "cmpxchg".
522 KEYWORD(unnamed_addr
);
523 KEYWORD(local_unnamed_addr
);
524 KEYWORD(externally_initialized
);
525 KEYWORD(extern_weak
);
527 KEYWORD(thread_local
);
528 KEYWORD(localdynamic
);
529 KEYWORD(initialexec
);
531 KEYWORD(zeroinitializer
);
544 KEYWORD(source_filename
);
546 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(msp430_intrcc
);
597 KEYWORD(avr_signalcc
);
600 KEYWORD(spir_kernel
);
602 KEYWORD(intel_ocl_bicc
);
603 KEYWORD(x86_64_sysvcc
);
605 KEYWORD(x86_regcallcc
);
606 KEYWORD(webkit_jscc
);
609 KEYWORD(preserve_mostcc
);
610 KEYWORD(preserve_allcc
);
615 KEYWORD(cxx_fast_tlscc
);
623 KEYWORD(amdgpu_kernel
);
630 KEYWORD(alwaysinline
);
638 KEYWORD(dereferenceable
);
639 KEYWORD(dereferenceable_or_null
);
640 KEYWORD(inaccessiblememonly
);
641 KEYWORD(inaccessiblemem_or_argmemonly
);
651 KEYWORD(noduplicate
);
652 KEYWORD(noimplicitfloat
);
655 KEYWORD(nonlazybind
);
661 KEYWORD(optforfuzzing
);
667 KEYWORD(returns_twice
);
669 KEYWORD(speculatable
);
676 KEYWORD(shadowcallstack
);
677 KEYWORD(sanitize_address
);
678 KEYWORD(sanitize_hwaddress
);
679 KEYWORD(sanitize_thread
);
680 KEYWORD(sanitize_memory
);
681 KEYWORD(speculative_load_hardening
);
697 KEYWORD(noduplicates
);
700 KEYWORD(eq
); KEYWORD(ne
); KEYWORD(slt
); KEYWORD(sgt
); KEYWORD(sle
);
701 KEYWORD(sge
); KEYWORD(ult
); KEYWORD(ugt
); KEYWORD(ule
); KEYWORD(uge
);
702 KEYWORD(oeq
); KEYWORD(one
); KEYWORD(olt
); KEYWORD(ogt
); KEYWORD(ole
);
703 KEYWORD(oge
); KEYWORD(ord
); KEYWORD(uno
); KEYWORD(ueq
); KEYWORD(une
);
705 KEYWORD(xchg
); KEYWORD(nand
); KEYWORD(max
); KEYWORD(min
); KEYWORD(umax
);
709 KEYWORD(blockaddress
);
714 // Use-list order directives.
715 KEYWORD(uselistorder
);
716 KEYWORD(uselistorder_bb
);
718 KEYWORD(personality
);
723 // Summary index keywords.
732 KEYWORD(notEligibleToImport
);
741 KEYWORD(returnDoesNotAlias
);
754 KEYWORD(typeTestAssumeVCalls
);
755 KEYWORD(typeCheckedLoadVCalls
);
756 KEYWORD(typeTestAssumeConstVCalls
);
757 KEYWORD(typeCheckedLoadConstVCalls
);
763 KEYWORD(typeTestRes
);
770 KEYWORD(sizeM1BitWidth
);
775 KEYWORD(wpdResolutions
);
779 KEYWORD(branchFunnel
);
780 KEYWORD(singleImplName
);
783 KEYWORD(uniformRetVal
);
784 KEYWORD(uniqueRetVal
);
785 KEYWORD(virtualConstProp
);
792 // Keywords for types.
793 #define TYPEKEYWORD(STR, LLVMTY) \
795 if (Keyword == STR) { \
797 return lltok::Type; \
801 TYPEKEYWORD("void", Type::getVoidTy(Context
));
802 TYPEKEYWORD("half", Type::getHalfTy(Context
));
803 TYPEKEYWORD("float", Type::getFloatTy(Context
));
804 TYPEKEYWORD("double", Type::getDoubleTy(Context
));
805 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context
));
806 TYPEKEYWORD("fp128", Type::getFP128Ty(Context
));
807 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context
));
808 TYPEKEYWORD("label", Type::getLabelTy(Context
));
809 TYPEKEYWORD("metadata", Type::getMetadataTy(Context
));
810 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context
));
811 TYPEKEYWORD("token", Type::getTokenTy(Context
));
815 // Keywords for instructions.
816 #define INSTKEYWORD(STR, Enum) \
818 if (Keyword == #STR) { \
819 UIntVal = Instruction::Enum; \
820 return lltok::kw_##STR; \
824 INSTKEYWORD(add
, Add
); INSTKEYWORD(fadd
, FAdd
);
825 INSTKEYWORD(sub
, Sub
); INSTKEYWORD(fsub
, FSub
);
826 INSTKEYWORD(mul
, Mul
); INSTKEYWORD(fmul
, FMul
);
827 INSTKEYWORD(udiv
, UDiv
); INSTKEYWORD(sdiv
, SDiv
); INSTKEYWORD(fdiv
, FDiv
);
828 INSTKEYWORD(urem
, URem
); INSTKEYWORD(srem
, SRem
); INSTKEYWORD(frem
, FRem
);
829 INSTKEYWORD(shl
, Shl
); INSTKEYWORD(lshr
, LShr
); INSTKEYWORD(ashr
, AShr
);
830 INSTKEYWORD(and, And
); INSTKEYWORD(or, Or
); INSTKEYWORD(xor, Xor
);
831 INSTKEYWORD(icmp
, ICmp
); INSTKEYWORD(fcmp
, FCmp
);
833 INSTKEYWORD(phi
, PHI
);
834 INSTKEYWORD(call
, Call
);
835 INSTKEYWORD(trunc
, Trunc
);
836 INSTKEYWORD(zext
, ZExt
);
837 INSTKEYWORD(sext
, SExt
);
838 INSTKEYWORD(fptrunc
, FPTrunc
);
839 INSTKEYWORD(fpext
, FPExt
);
840 INSTKEYWORD(uitofp
, UIToFP
);
841 INSTKEYWORD(sitofp
, SIToFP
);
842 INSTKEYWORD(fptoui
, FPToUI
);
843 INSTKEYWORD(fptosi
, FPToSI
);
844 INSTKEYWORD(inttoptr
, IntToPtr
);
845 INSTKEYWORD(ptrtoint
, PtrToInt
);
846 INSTKEYWORD(bitcast
, BitCast
);
847 INSTKEYWORD(addrspacecast
, AddrSpaceCast
);
848 INSTKEYWORD(select
, Select
);
849 INSTKEYWORD(va_arg
, VAArg
);
850 INSTKEYWORD(ret
, Ret
);
852 INSTKEYWORD(switch, Switch
);
853 INSTKEYWORD(indirectbr
, IndirectBr
);
854 INSTKEYWORD(invoke
, Invoke
);
855 INSTKEYWORD(resume
, Resume
);
856 INSTKEYWORD(unreachable
, Unreachable
);
858 INSTKEYWORD(alloca
, Alloca
);
859 INSTKEYWORD(load
, Load
);
860 INSTKEYWORD(store
, Store
);
861 INSTKEYWORD(cmpxchg
, AtomicCmpXchg
);
862 INSTKEYWORD(atomicrmw
, AtomicRMW
);
863 INSTKEYWORD(fence
, Fence
);
864 INSTKEYWORD(getelementptr
, GetElementPtr
);
866 INSTKEYWORD(extractelement
, ExtractElement
);
867 INSTKEYWORD(insertelement
, InsertElement
);
868 INSTKEYWORD(shufflevector
, ShuffleVector
);
869 INSTKEYWORD(extractvalue
, ExtractValue
);
870 INSTKEYWORD(insertvalue
, InsertValue
);
871 INSTKEYWORD(landingpad
, LandingPad
);
872 INSTKEYWORD(cleanupret
, CleanupRet
);
873 INSTKEYWORD(catchret
, CatchRet
);
874 INSTKEYWORD(catchswitch
, CatchSwitch
);
875 INSTKEYWORD(catchpad
, CatchPad
);
876 INSTKEYWORD(cleanuppad
, CleanupPad
);
880 #define DWKEYWORD(TYPE, TOKEN) \
882 if (Keyword.startswith("DW_" #TYPE "_")) { \
883 StrVal.assign(Keyword.begin(), Keyword.end()); \
884 return lltok::TOKEN; \
888 DWKEYWORD(TAG
, DwarfTag
);
889 DWKEYWORD(ATE
, DwarfAttEncoding
);
890 DWKEYWORD(VIRTUALITY
, DwarfVirtuality
);
891 DWKEYWORD(LANG
, DwarfLang
);
892 DWKEYWORD(CC
, DwarfCC
);
893 DWKEYWORD(OP
, DwarfOp
);
894 DWKEYWORD(MACINFO
, DwarfMacinfo
);
898 if (Keyword
.startswith("DIFlag")) {
899 StrVal
.assign(Keyword
.begin(), Keyword
.end());
900 return lltok::DIFlag
;
903 if (Keyword
.startswith("CSK_")) {
904 StrVal
.assign(Keyword
.begin(), Keyword
.end());
905 return lltok::ChecksumKind
;
908 if (Keyword
== "NoDebug" || Keyword
== "FullDebug" ||
909 Keyword
== "LineTablesOnly" || Keyword
== "DebugDirectivesOnly") {
910 StrVal
.assign(Keyword
.begin(), Keyword
.end());
911 return lltok::EmissionKind
;
914 if (Keyword
== "GNU" || Keyword
== "None" || Keyword
== "Default") {
915 StrVal
.assign(Keyword
.begin(), Keyword
.end());
916 return lltok::NameTableKind
;
919 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
920 // the CFE to avoid forcing it to deal with 64-bit numbers.
921 if ((TokStart
[0] == 'u' || TokStart
[0] == 's') &&
922 TokStart
[1] == '0' && TokStart
[2] == 'x' &&
923 isxdigit(static_cast<unsigned char>(TokStart
[3]))) {
924 int len
= CurPtr
-TokStart
-3;
925 uint32_t bits
= len
* 4;
926 StringRef
HexStr(TokStart
+ 3, len
);
927 if (!all_of(HexStr
, isxdigit
)) {
928 // Bad token, return it as an error.
932 APInt
Tmp(bits
, HexStr
, 16);
933 uint32_t activeBits
= Tmp
.getActiveBits();
934 if (activeBits
> 0 && activeBits
< bits
)
935 Tmp
= Tmp
.trunc(activeBits
);
936 APSIntVal
= APSInt(Tmp
, TokStart
[0] == 'u');
937 return lltok::APSInt
;
940 // If this is "cc1234", return this as just "cc".
941 if (TokStart
[0] == 'c' && TokStart
[1] == 'c') {
946 // Finally, if this isn't known, return an error.
951 /// Lex all tokens that start with a 0x prefix, knowing they match and are not
953 /// HexFPConstant 0x[0-9A-Fa-f]+
954 /// HexFP80Constant 0xK[0-9A-Fa-f]+
955 /// HexFP128Constant 0xL[0-9A-Fa-f]+
956 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
957 /// HexHalfConstant 0xH[0-9A-Fa-f]+
958 lltok::Kind
LLLexer::Lex0x() {
959 CurPtr
= TokStart
+ 2;
962 if ((CurPtr
[0] >= 'K' && CurPtr
[0] <= 'M') || CurPtr
[0] == 'H') {
968 if (!isxdigit(static_cast<unsigned char>(CurPtr
[0]))) {
969 // Bad token, return it as an error.
974 while (isxdigit(static_cast<unsigned char>(CurPtr
[0])))
978 // HexFPConstant - Floating point constant represented in IEEE format as a
979 // hexadecimal number for when exponential notation is not precise enough.
980 // Half, Float, and double only.
981 APFloatVal
= APFloat(APFloat::IEEEdouble(),
982 APInt(64, HexIntToVal(TokStart
+ 2, CurPtr
)));
983 return lltok::APFloat
;
988 default: llvm_unreachable("Unknown kind!");
990 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
991 FP80HexToIntPair(TokStart
+3, CurPtr
, Pair
);
992 APFloatVal
= APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair
));
993 return lltok::APFloat
;
995 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
996 HexToIntPair(TokStart
+3, CurPtr
, Pair
);
997 APFloatVal
= APFloat(APFloat::IEEEquad(), APInt(128, Pair
));
998 return lltok::APFloat
;
1000 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
1001 HexToIntPair(TokStart
+3, CurPtr
, Pair
);
1002 APFloatVal
= APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair
));
1003 return lltok::APFloat
;
1005 APFloatVal
= APFloat(APFloat::IEEEhalf(),
1006 APInt(16,HexIntToVal(TokStart
+3, CurPtr
)));
1007 return lltok::APFloat
;
1011 /// Lex tokens for a label or a numeric constant, possibly starting with -.
1012 /// Label [-a-zA-Z$._0-9]+:
1013 /// NInteger -[0-9]+
1014 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1016 /// HexFPConstant 0x[0-9A-Fa-f]+
1017 /// HexFP80Constant 0xK[0-9A-Fa-f]+
1018 /// HexFP128Constant 0xL[0-9A-Fa-f]+
1019 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
1020 lltok::Kind
LLLexer::LexDigitOrNegative() {
1021 // If the letter after the negative is not a number, this is probably a label.
1022 if (!isdigit(static_cast<unsigned char>(TokStart
[0])) &&
1023 !isdigit(static_cast<unsigned char>(CurPtr
[0]))) {
1024 // Okay, this is not a number after the -, it's probably a label.
1025 if (const char *End
= isLabelTail(CurPtr
)) {
1026 StrVal
.assign(TokStart
, End
-1);
1028 return lltok::LabelStr
;
1031 return lltok::Error
;
1034 // At this point, it is either a label, int or fp constant.
1036 // Skip digits, we have at least one.
1037 for (; isdigit(static_cast<unsigned char>(CurPtr
[0])); ++CurPtr
)
1040 // Check to see if this really is a label afterall, e.g. "-1:".
1041 if (isLabelChar(CurPtr
[0]) || CurPtr
[0] == ':') {
1042 if (const char *End
= isLabelTail(CurPtr
)) {
1043 StrVal
.assign(TokStart
, End
-1);
1045 return lltok::LabelStr
;
1049 // If the next character is a '.', then it is a fp value, otherwise its
1051 if (CurPtr
[0] != '.') {
1052 if (TokStart
[0] == '0' && TokStart
[1] == 'x')
1054 APSIntVal
= APSInt(StringRef(TokStart
, CurPtr
- TokStart
));
1055 return lltok::APSInt
;
1060 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1061 while (isdigit(static_cast<unsigned char>(CurPtr
[0]))) ++CurPtr
;
1063 if (CurPtr
[0] == 'e' || CurPtr
[0] == 'E') {
1064 if (isdigit(static_cast<unsigned char>(CurPtr
[1])) ||
1065 ((CurPtr
[1] == '-' || CurPtr
[1] == '+') &&
1066 isdigit(static_cast<unsigned char>(CurPtr
[2])))) {
1068 while (isdigit(static_cast<unsigned char>(CurPtr
[0]))) ++CurPtr
;
1072 APFloatVal
= APFloat(APFloat::IEEEdouble(),
1073 StringRef(TokStart
, CurPtr
- TokStart
));
1074 return lltok::APFloat
;
1077 /// Lex a floating point constant starting with +.
1078 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1079 lltok::Kind
LLLexer::LexPositive() {
1080 // If the letter after the negative is a number, this is probably not a
1082 if (!isdigit(static_cast<unsigned char>(CurPtr
[0])))
1083 return lltok::Error
;
1086 for (++CurPtr
; isdigit(static_cast<unsigned char>(CurPtr
[0])); ++CurPtr
)
1089 // At this point, we need a '.'.
1090 if (CurPtr
[0] != '.') {
1091 CurPtr
= TokStart
+1;
1092 return lltok::Error
;
1097 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1098 while (isdigit(static_cast<unsigned char>(CurPtr
[0]))) ++CurPtr
;
1100 if (CurPtr
[0] == 'e' || CurPtr
[0] == 'E') {
1101 if (isdigit(static_cast<unsigned char>(CurPtr
[1])) ||
1102 ((CurPtr
[1] == '-' || CurPtr
[1] == '+') &&
1103 isdigit(static_cast<unsigned char>(CurPtr
[2])))) {
1105 while (isdigit(static_cast<unsigned char>(CurPtr
[0]))) ++CurPtr
;
1109 APFloatVal
= APFloat(APFloat::IEEEdouble(),
1110 StringRef(TokStart
, CurPtr
- TokStart
));
1111 return lltok::APFloat
;