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.
579 KEYWORD(inteldialect
);
587 KEYWORD(x86_stdcallcc
);
588 KEYWORD(x86_fastcallcc
);
589 KEYWORD(x86_thiscallcc
);
590 KEYWORD(x86_vectorcallcc
);
592 KEYWORD(arm_aapcscc
);
593 KEYWORD(arm_aapcs_vfpcc
);
594 KEYWORD(aarch64_vector_pcs
);
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
);
755 KEYWORD(typeTestAssumeVCalls
);
756 KEYWORD(typeCheckedLoadVCalls
);
757 KEYWORD(typeTestAssumeConstVCalls
);
758 KEYWORD(typeCheckedLoadConstVCalls
);
764 KEYWORD(typeTestRes
);
771 KEYWORD(sizeM1BitWidth
);
776 KEYWORD(wpdResolutions
);
780 KEYWORD(branchFunnel
);
781 KEYWORD(singleImplName
);
784 KEYWORD(uniformRetVal
);
785 KEYWORD(uniqueRetVal
);
786 KEYWORD(virtualConstProp
);
794 // Keywords for types.
795 #define TYPEKEYWORD(STR, LLVMTY) \
797 if (Keyword == STR) { \
799 return lltok::Type; \
803 TYPEKEYWORD("void", Type::getVoidTy(Context
));
804 TYPEKEYWORD("half", Type::getHalfTy(Context
));
805 TYPEKEYWORD("float", Type::getFloatTy(Context
));
806 TYPEKEYWORD("double", Type::getDoubleTy(Context
));
807 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context
));
808 TYPEKEYWORD("fp128", Type::getFP128Ty(Context
));
809 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context
));
810 TYPEKEYWORD("label", Type::getLabelTy(Context
));
811 TYPEKEYWORD("metadata", Type::getMetadataTy(Context
));
812 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context
));
813 TYPEKEYWORD("token", Type::getTokenTy(Context
));
817 // Keywords for instructions.
818 #define INSTKEYWORD(STR, Enum) \
820 if (Keyword == #STR) { \
821 UIntVal = Instruction::Enum; \
822 return lltok::kw_##STR; \
826 INSTKEYWORD(fneg
, FNeg
);
828 INSTKEYWORD(add
, Add
); INSTKEYWORD(fadd
, FAdd
);
829 INSTKEYWORD(sub
, Sub
); INSTKEYWORD(fsub
, FSub
);
830 INSTKEYWORD(mul
, Mul
); INSTKEYWORD(fmul
, FMul
);
831 INSTKEYWORD(udiv
, UDiv
); INSTKEYWORD(sdiv
, SDiv
); INSTKEYWORD(fdiv
, FDiv
);
832 INSTKEYWORD(urem
, URem
); INSTKEYWORD(srem
, SRem
); INSTKEYWORD(frem
, FRem
);
833 INSTKEYWORD(shl
, Shl
); INSTKEYWORD(lshr
, LShr
); INSTKEYWORD(ashr
, AShr
);
834 INSTKEYWORD(and, And
); INSTKEYWORD(or, Or
); INSTKEYWORD(xor, Xor
);
835 INSTKEYWORD(icmp
, ICmp
); INSTKEYWORD(fcmp
, FCmp
);
837 INSTKEYWORD(phi
, PHI
);
838 INSTKEYWORD(call
, Call
);
839 INSTKEYWORD(trunc
, Trunc
);
840 INSTKEYWORD(zext
, ZExt
);
841 INSTKEYWORD(sext
, SExt
);
842 INSTKEYWORD(fptrunc
, FPTrunc
);
843 INSTKEYWORD(fpext
, FPExt
);
844 INSTKEYWORD(uitofp
, UIToFP
);
845 INSTKEYWORD(sitofp
, SIToFP
);
846 INSTKEYWORD(fptoui
, FPToUI
);
847 INSTKEYWORD(fptosi
, FPToSI
);
848 INSTKEYWORD(inttoptr
, IntToPtr
);
849 INSTKEYWORD(ptrtoint
, PtrToInt
);
850 INSTKEYWORD(bitcast
, BitCast
);
851 INSTKEYWORD(addrspacecast
, AddrSpaceCast
);
852 INSTKEYWORD(select
, Select
);
853 INSTKEYWORD(va_arg
, VAArg
);
854 INSTKEYWORD(ret
, Ret
);
856 INSTKEYWORD(switch, Switch
);
857 INSTKEYWORD(indirectbr
, IndirectBr
);
858 INSTKEYWORD(invoke
, Invoke
);
859 INSTKEYWORD(resume
, Resume
);
860 INSTKEYWORD(unreachable
, Unreachable
);
861 INSTKEYWORD(callbr
, CallBr
);
863 INSTKEYWORD(alloca
, Alloca
);
864 INSTKEYWORD(load
, Load
);
865 INSTKEYWORD(store
, Store
);
866 INSTKEYWORD(cmpxchg
, AtomicCmpXchg
);
867 INSTKEYWORD(atomicrmw
, AtomicRMW
);
868 INSTKEYWORD(fence
, Fence
);
869 INSTKEYWORD(getelementptr
, GetElementPtr
);
871 INSTKEYWORD(extractelement
, ExtractElement
);
872 INSTKEYWORD(insertelement
, InsertElement
);
873 INSTKEYWORD(shufflevector
, ShuffleVector
);
874 INSTKEYWORD(extractvalue
, ExtractValue
);
875 INSTKEYWORD(insertvalue
, InsertValue
);
876 INSTKEYWORD(landingpad
, LandingPad
);
877 INSTKEYWORD(cleanupret
, CleanupRet
);
878 INSTKEYWORD(catchret
, CatchRet
);
879 INSTKEYWORD(catchswitch
, CatchSwitch
);
880 INSTKEYWORD(catchpad
, CatchPad
);
881 INSTKEYWORD(cleanuppad
, CleanupPad
);
885 #define DWKEYWORD(TYPE, TOKEN) \
887 if (Keyword.startswith("DW_" #TYPE "_")) { \
888 StrVal.assign(Keyword.begin(), Keyword.end()); \
889 return lltok::TOKEN; \
893 DWKEYWORD(TAG
, DwarfTag
);
894 DWKEYWORD(ATE
, DwarfAttEncoding
);
895 DWKEYWORD(VIRTUALITY
, DwarfVirtuality
);
896 DWKEYWORD(LANG
, DwarfLang
);
897 DWKEYWORD(CC
, DwarfCC
);
898 DWKEYWORD(OP
, DwarfOp
);
899 DWKEYWORD(MACINFO
, DwarfMacinfo
);
903 if (Keyword
.startswith("DIFlag")) {
904 StrVal
.assign(Keyword
.begin(), Keyword
.end());
905 return lltok::DIFlag
;
908 if (Keyword
.startswith("DISPFlag")) {
909 StrVal
.assign(Keyword
.begin(), Keyword
.end());
910 return lltok::DISPFlag
;
913 if (Keyword
.startswith("CSK_")) {
914 StrVal
.assign(Keyword
.begin(), Keyword
.end());
915 return lltok::ChecksumKind
;
918 if (Keyword
== "NoDebug" || Keyword
== "FullDebug" ||
919 Keyword
== "LineTablesOnly" || Keyword
== "DebugDirectivesOnly") {
920 StrVal
.assign(Keyword
.begin(), Keyword
.end());
921 return lltok::EmissionKind
;
924 if (Keyword
== "GNU" || Keyword
== "None" || Keyword
== "Default") {
925 StrVal
.assign(Keyword
.begin(), Keyword
.end());
926 return lltok::NameTableKind
;
929 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
930 // the CFE to avoid forcing it to deal with 64-bit numbers.
931 if ((TokStart
[0] == 'u' || TokStart
[0] == 's') &&
932 TokStart
[1] == '0' && TokStart
[2] == 'x' &&
933 isxdigit(static_cast<unsigned char>(TokStart
[3]))) {
934 int len
= CurPtr
-TokStart
-3;
935 uint32_t bits
= len
* 4;
936 StringRef
HexStr(TokStart
+ 3, len
);
937 if (!all_of(HexStr
, isxdigit
)) {
938 // Bad token, return it as an error.
942 APInt
Tmp(bits
, HexStr
, 16);
943 uint32_t activeBits
= Tmp
.getActiveBits();
944 if (activeBits
> 0 && activeBits
< bits
)
945 Tmp
= Tmp
.trunc(activeBits
);
946 APSIntVal
= APSInt(Tmp
, TokStart
[0] == 'u');
947 return lltok::APSInt
;
950 // If this is "cc1234", return this as just "cc".
951 if (TokStart
[0] == 'c' && TokStart
[1] == 'c') {
956 // Finally, if this isn't known, return an error.
961 /// Lex all tokens that start with a 0x prefix, knowing they match and are not
963 /// HexFPConstant 0x[0-9A-Fa-f]+
964 /// HexFP80Constant 0xK[0-9A-Fa-f]+
965 /// HexFP128Constant 0xL[0-9A-Fa-f]+
966 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
967 /// HexHalfConstant 0xH[0-9A-Fa-f]+
968 lltok::Kind
LLLexer::Lex0x() {
969 CurPtr
= TokStart
+ 2;
972 if ((CurPtr
[0] >= 'K' && CurPtr
[0] <= 'M') || CurPtr
[0] == 'H') {
978 if (!isxdigit(static_cast<unsigned char>(CurPtr
[0]))) {
979 // Bad token, return it as an error.
984 while (isxdigit(static_cast<unsigned char>(CurPtr
[0])))
988 // HexFPConstant - Floating point constant represented in IEEE format as a
989 // hexadecimal number for when exponential notation is not precise enough.
990 // Half, Float, and double only.
991 APFloatVal
= APFloat(APFloat::IEEEdouble(),
992 APInt(64, HexIntToVal(TokStart
+ 2, CurPtr
)));
993 return lltok::APFloat
;
998 default: llvm_unreachable("Unknown kind!");
1000 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
1001 FP80HexToIntPair(TokStart
+3, CurPtr
, Pair
);
1002 APFloatVal
= APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair
));
1003 return lltok::APFloat
;
1005 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
1006 HexToIntPair(TokStart
+3, CurPtr
, Pair
);
1007 APFloatVal
= APFloat(APFloat::IEEEquad(), APInt(128, Pair
));
1008 return lltok::APFloat
;
1010 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
1011 HexToIntPair(TokStart
+3, CurPtr
, Pair
);
1012 APFloatVal
= APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair
));
1013 return lltok::APFloat
;
1015 APFloatVal
= APFloat(APFloat::IEEEhalf(),
1016 APInt(16,HexIntToVal(TokStart
+3, CurPtr
)));
1017 return lltok::APFloat
;
1021 /// Lex tokens for a label or a numeric constant, possibly starting with -.
1022 /// Label [-a-zA-Z$._0-9]+:
1023 /// NInteger -[0-9]+
1024 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1026 /// HexFPConstant 0x[0-9A-Fa-f]+
1027 /// HexFP80Constant 0xK[0-9A-Fa-f]+
1028 /// HexFP128Constant 0xL[0-9A-Fa-f]+
1029 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
1030 lltok::Kind
LLLexer::LexDigitOrNegative() {
1031 // If the letter after the negative is not a number, this is probably a label.
1032 if (!isdigit(static_cast<unsigned char>(TokStart
[0])) &&
1033 !isdigit(static_cast<unsigned char>(CurPtr
[0]))) {
1034 // Okay, this is not a number after the -, it's probably a label.
1035 if (const char *End
= isLabelTail(CurPtr
)) {
1036 StrVal
.assign(TokStart
, End
-1);
1038 return lltok::LabelStr
;
1041 return lltok::Error
;
1044 // At this point, it is either a label, int or fp constant.
1046 // Skip digits, we have at least one.
1047 for (; isdigit(static_cast<unsigned char>(CurPtr
[0])); ++CurPtr
)
1050 // Check to see if this really is a label afterall, e.g. "-1:".
1051 if (isLabelChar(CurPtr
[0]) || CurPtr
[0] == ':') {
1052 if (const char *End
= isLabelTail(CurPtr
)) {
1053 StrVal
.assign(TokStart
, End
-1);
1055 return lltok::LabelStr
;
1059 // If the next character is a '.', then it is a fp value, otherwise its
1061 if (CurPtr
[0] != '.') {
1062 if (TokStart
[0] == '0' && TokStart
[1] == 'x')
1064 APSIntVal
= APSInt(StringRef(TokStart
, CurPtr
- TokStart
));
1065 return lltok::APSInt
;
1070 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1071 while (isdigit(static_cast<unsigned char>(CurPtr
[0]))) ++CurPtr
;
1073 if (CurPtr
[0] == 'e' || CurPtr
[0] == 'E') {
1074 if (isdigit(static_cast<unsigned char>(CurPtr
[1])) ||
1075 ((CurPtr
[1] == '-' || CurPtr
[1] == '+') &&
1076 isdigit(static_cast<unsigned char>(CurPtr
[2])))) {
1078 while (isdigit(static_cast<unsigned char>(CurPtr
[0]))) ++CurPtr
;
1082 APFloatVal
= APFloat(APFloat::IEEEdouble(),
1083 StringRef(TokStart
, CurPtr
- TokStart
));
1084 return lltok::APFloat
;
1087 /// Lex a floating point constant starting with +.
1088 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1089 lltok::Kind
LLLexer::LexPositive() {
1090 // If the letter after the negative is a number, this is probably not a
1092 if (!isdigit(static_cast<unsigned char>(CurPtr
[0])))
1093 return lltok::Error
;
1096 for (++CurPtr
; isdigit(static_cast<unsigned char>(CurPtr
[0])); ++CurPtr
)
1099 // At this point, we need a '.'.
1100 if (CurPtr
[0] != '.') {
1101 CurPtr
= TokStart
+1;
1102 return lltok::Error
;
1107 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1108 while (isdigit(static_cast<unsigned char>(CurPtr
[0]))) ++CurPtr
;
1110 if (CurPtr
[0] == 'e' || CurPtr
[0] == 'E') {
1111 if (isdigit(static_cast<unsigned char>(CurPtr
[1])) ||
1112 ((CurPtr
[1] == '-' || CurPtr
[1] == '+') &&
1113 isdigit(static_cast<unsigned char>(CurPtr
[2])))) {
1115 while (isdigit(static_cast<unsigned char>(CurPtr
[0]))) ++CurPtr
;
1119 APFloatVal
= APFloat(APFloat::IEEEdouble(),
1120 StringRef(TokStart
, CurPtr
- TokStart
));
1121 return lltok::APFloat
;