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 //===----------------------------------------------------------------------===//
13 #include "llvm/AsmParser/LLLexer.h"
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
) {
162 CurPtr
= CurBuf
.begin();
165 int LLLexer::getNextChar() {
166 char CurChar
= *CurPtr
++;
168 default: return (unsigned char)CurChar
;
170 // A nul character in the stream is either the end of the current buffer or
171 // a random nul in the file. Disambiguate that here.
172 if (CurPtr
-1 != CurBuf
.end())
173 return 0; // Just whitespace.
175 // Otherwise, return end of file.
176 --CurPtr
; // Another call to lex will return EOF again.
181 lltok::Kind
LLLexer::LexToken() {
185 int CurChar
= getNextChar();
188 // Handle letters: [a-zA-Z_]
189 if (isalpha(static_cast<unsigned char>(CurChar
)) || CurChar
== '_')
190 return LexIdentifier();
193 case EOF
: return lltok::Eof
;
199 // Ignore whitespace.
201 case '+': return LexPositive();
202 case '@': return LexAt();
203 case '$': return LexDollar();
204 case '%': return LexPercent();
205 case '"': return LexQuote();
207 if (const char *Ptr
= isLabelTail(CurPtr
)) {
209 StrVal
.assign(TokStart
, CurPtr
-1);
210 return lltok::LabelStr
;
212 if (CurPtr
[0] == '.' && CurPtr
[1] == '.') {
214 return lltok::dotdotdot
;
220 case '!': return LexExclaim();
225 case '#': return LexHash();
226 case '0': case '1': case '2': case '3': case '4':
227 case '5': case '6': case '7': case '8': case '9':
229 return LexDigitOrNegative();
230 case '=': return lltok::equal
;
231 case '[': return lltok::lsquare
;
232 case ']': return lltok::rsquare
;
233 case '{': return lltok::lbrace
;
234 case '}': return lltok::rbrace
;
235 case '<': return lltok::less
;
236 case '>': return lltok::greater
;
237 case '(': return lltok::lparen
;
238 case ')': return lltok::rparen
;
239 case ',': return lltok::comma
;
240 case '*': return lltok::star
;
241 case '|': return lltok::bar
;
246 void LLLexer::SkipLineComment() {
248 if (CurPtr
[0] == '\n' || CurPtr
[0] == '\r' || getNextChar() == EOF
)
253 /// Lex all tokens that start with an @ character.
254 /// GlobalVar @\"[^\"]*\"
255 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
256 /// GlobalVarID @[0-9]+
257 lltok::Kind
LLLexer::LexAt() {
258 return LexVar(lltok::GlobalVar
, lltok::GlobalID
);
261 lltok::Kind
LLLexer::LexDollar() {
262 if (const char *Ptr
= isLabelTail(TokStart
)) {
264 StrVal
.assign(TokStart
, CurPtr
- 1);
265 return lltok::LabelStr
;
268 // Handle DollarStringConstant: $\"[^\"]*\"
269 if (CurPtr
[0] == '"') {
273 int CurChar
= getNextChar();
275 if (CurChar
== EOF
) {
276 Error("end of file in COMDAT variable name");
279 if (CurChar
== '"') {
280 StrVal
.assign(TokStart
+ 2, CurPtr
- 1);
281 UnEscapeLexed(StrVal
);
282 if (StringRef(StrVal
).contains(0)) {
283 Error("Null bytes are not allowed in names");
286 return lltok::ComdatVar
;
291 // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
293 return lltok::ComdatVar
;
298 /// ReadString - Read a string until the closing quote.
299 lltok::Kind
LLLexer::ReadString(lltok::Kind kind
) {
300 const char *Start
= CurPtr
;
302 int CurChar
= getNextChar();
304 if (CurChar
== EOF
) {
305 Error("end of file in string constant");
308 if (CurChar
== '"') {
309 StrVal
.assign(Start
, CurPtr
-1);
310 UnEscapeLexed(StrVal
);
316 /// ReadVarName - Read the rest of a token containing a variable name.
317 bool LLLexer::ReadVarName() {
318 const char *NameStart
= CurPtr
;
319 if (isalpha(static_cast<unsigned char>(CurPtr
[0])) ||
320 CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
321 CurPtr
[0] == '.' || CurPtr
[0] == '_') {
323 while (isalnum(static_cast<unsigned char>(CurPtr
[0])) ||
324 CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
325 CurPtr
[0] == '.' || CurPtr
[0] == '_')
328 StrVal
.assign(NameStart
, CurPtr
);
334 // Lex an ID: [0-9]+. On success, the ID is stored in UIntVal and Token is
335 // returned, otherwise the Error token is returned.
336 lltok::Kind
LLLexer::LexUIntID(lltok::Kind Token
) {
337 if (!isdigit(static_cast<unsigned char>(CurPtr
[0])))
340 for (++CurPtr
; isdigit(static_cast<unsigned char>(CurPtr
[0])); ++CurPtr
)
343 uint64_t Val
= atoull(TokStart
+ 1, CurPtr
);
344 if ((unsigned)Val
!= Val
)
345 Error("invalid value number (too large)!");
346 UIntVal
= unsigned(Val
);
350 lltok::Kind
LLLexer::LexVar(lltok::Kind Var
, lltok::Kind VarID
) {
351 // Handle StringConstant: \"[^\"]*\"
352 if (CurPtr
[0] == '"') {
356 int CurChar
= getNextChar();
358 if (CurChar
== EOF
) {
359 Error("end of file in global variable name");
362 if (CurChar
== '"') {
363 StrVal
.assign(TokStart
+2, CurPtr
-1);
364 UnEscapeLexed(StrVal
);
365 if (StringRef(StrVal
).contains(0)) {
366 Error("Null bytes are not allowed in names");
374 // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]*
378 // Handle VarID: [0-9]+
379 return LexUIntID(VarID
);
382 /// Lex all tokens that start with a % character.
383 /// LocalVar ::= %\"[^\"]*\"
384 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
385 /// LocalVarID ::= %[0-9]+
386 lltok::Kind
LLLexer::LexPercent() {
387 return LexVar(lltok::LocalVar
, lltok::LocalVarID
);
390 /// Lex all tokens that start with a " character.
391 /// QuoteLabel "[^"]+":
392 /// StringConstant "[^"]*"
393 lltok::Kind
LLLexer::LexQuote() {
394 lltok::Kind kind
= ReadString(lltok::StringConstant
);
395 if (kind
== lltok::Error
|| kind
== lltok::Eof
)
398 if (CurPtr
[0] == ':') {
400 if (StringRef(StrVal
).contains(0)) {
401 Error("Null bytes are not allowed in names");
404 kind
= lltok::LabelStr
;
411 /// Lex all tokens that start with a ! character.
414 lltok::Kind
LLLexer::LexExclaim() {
415 // Lex a metadata name as a MetadataVar.
416 if (isalpha(static_cast<unsigned char>(CurPtr
[0])) ||
417 CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
418 CurPtr
[0] == '.' || CurPtr
[0] == '_' || CurPtr
[0] == '\\') {
420 while (isalnum(static_cast<unsigned char>(CurPtr
[0])) ||
421 CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
422 CurPtr
[0] == '.' || CurPtr
[0] == '_' || CurPtr
[0] == '\\')
425 StrVal
.assign(TokStart
+1, CurPtr
); // Skip !
426 UnEscapeLexed(StrVal
);
427 return lltok::MetadataVar
;
429 return lltok::exclaim
;
432 /// Lex all tokens that start with a ^ character.
433 /// SummaryID ::= ^[0-9]+
434 lltok::Kind
LLLexer::LexCaret() {
435 // Handle SummaryID: ^[0-9]+
436 return LexUIntID(lltok::SummaryID
);
439 /// Lex all tokens that start with a # character.
440 /// AttrGrpID ::= #[0-9]+
441 lltok::Kind
LLLexer::LexHash() {
442 // Handle AttrGrpID: #[0-9]+
443 return LexUIntID(lltok::AttrGrpID
);
446 /// Lex a label, integer type, keyword, or hexadecimal integer constant.
447 /// Label [-a-zA-Z$._0-9]+:
448 /// IntegerType i[0-9]+
449 /// Keyword sdiv, float, ...
450 /// HexIntConstant [us]0x[0-9A-Fa-f]+
451 lltok::Kind
LLLexer::LexIdentifier() {
452 const char *StartChar
= CurPtr
;
453 const char *IntEnd
= CurPtr
[-1] == 'i' ? nullptr : StartChar
;
454 const char *KeywordEnd
= nullptr;
456 for (; isLabelChar(*CurPtr
); ++CurPtr
) {
457 // If we decide this is an integer, remember the end of the sequence.
458 if (!IntEnd
&& !isdigit(static_cast<unsigned char>(*CurPtr
)))
460 if (!KeywordEnd
&& !isalnum(static_cast<unsigned char>(*CurPtr
)) &&
465 // If we stopped due to a colon, unless we were directed to ignore it,
466 // this really is a label.
467 if (!IgnoreColonInIdentifiers
&& *CurPtr
== ':') {
468 StrVal
.assign(StartChar
-1, CurPtr
++);
469 return lltok::LabelStr
;
472 // Otherwise, this wasn't a label. If this was valid as an integer type,
474 if (!IntEnd
) IntEnd
= CurPtr
;
475 if (IntEnd
!= StartChar
) {
477 uint64_t NumBits
= atoull(StartChar
, CurPtr
);
478 if (NumBits
< IntegerType::MIN_INT_BITS
||
479 NumBits
> IntegerType::MAX_INT_BITS
) {
480 Error("bitwidth for integer type out of range!");
483 TyVal
= IntegerType::get(Context
, NumBits
);
487 // Otherwise, this was a letter sequence. See which keyword this is.
488 if (!KeywordEnd
) KeywordEnd
= CurPtr
;
491 StringRef
Keyword(StartChar
, CurPtr
- StartChar
);
493 #define KEYWORD(STR) \
495 if (Keyword == #STR) \
496 return lltok::kw_##STR; \
499 KEYWORD(true); KEYWORD(false);
500 KEYWORD(declare
); KEYWORD(define
);
501 KEYWORD(global
); KEYWORD(constant
);
504 KEYWORD(dso_preemptable
);
508 KEYWORD(available_externally
);
510 KEYWORD(linkonce_odr
);
511 KEYWORD(weak
); // Use as a linkage, and a modifier for "cmpxchg".
520 KEYWORD(unnamed_addr
);
521 KEYWORD(local_unnamed_addr
);
522 KEYWORD(externally_initialized
);
523 KEYWORD(extern_weak
);
525 KEYWORD(thread_local
);
526 KEYWORD(localdynamic
);
527 KEYWORD(initialexec
);
529 KEYWORD(zeroinitializer
);
543 KEYWORD(source_filename
);
580 KEYWORD(inteldialect
);
585 KEYWORD(no_sanitize_address
);
586 KEYWORD(no_sanitize_hwaddress
);
587 KEYWORD(sanitize_address_dyninit
);
592 KEYWORD(cfguard_checkcc
);
593 KEYWORD(x86_stdcallcc
);
594 KEYWORD(x86_fastcallcc
);
595 KEYWORD(x86_thiscallcc
);
596 KEYWORD(x86_vectorcallcc
);
598 KEYWORD(arm_aapcscc
);
599 KEYWORD(arm_aapcs_vfpcc
);
600 KEYWORD(aarch64_vector_pcs
);
601 KEYWORD(aarch64_sve_vector_pcs
);
602 KEYWORD(aarch64_sme_preservemost_from_x0
);
603 KEYWORD(aarch64_sme_preservemost_from_x2
);
604 KEYWORD(msp430_intrcc
);
606 KEYWORD(avr_signalcc
);
609 KEYWORD(spir_kernel
);
611 KEYWORD(intel_ocl_bicc
);
612 KEYWORD(x86_64_sysvcc
);
614 KEYWORD(x86_regcallcc
);
616 KEYWORD(swifttailcc
);
618 KEYWORD(preserve_mostcc
);
619 KEYWORD(preserve_allcc
);
624 KEYWORD(cxx_fast_tlscc
);
632 KEYWORD(amdgpu_cs_chain
);
633 KEYWORD(amdgpu_cs_chain_preserve
);
634 KEYWORD(amdgpu_kernel
);
647 #define GET_ATTR_NAMES
648 #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
649 KEYWORD(DISPLAY_NAME);
650 #include "llvm/IR/Attributes.inc"
656 KEYWORD(inaccessiblemem
);
658 KEYWORD(inaccessiblememonly
);
659 KEYWORD(inaccessiblemem_or_argmemonly
);
661 // nofpclass attribute
667 // ninf already a keyword
672 // sub already a keyword
688 KEYWORD(nodeduplicate
);
691 KEYWORD(eq
); KEYWORD(ne
); KEYWORD(slt
); KEYWORD(sgt
); KEYWORD(sle
);
692 KEYWORD(sge
); KEYWORD(ult
); KEYWORD(ugt
); KEYWORD(ule
); KEYWORD(uge
);
693 KEYWORD(oeq
); KEYWORD(one
); KEYWORD(olt
); KEYWORD(ogt
); KEYWORD(ole
);
694 KEYWORD(oge
); KEYWORD(ord
); KEYWORD(uno
); KEYWORD(ueq
); KEYWORD(une
);
696 KEYWORD(xchg
); KEYWORD(nand
); KEYWORD(max
); KEYWORD(min
); KEYWORD(umax
);
697 KEYWORD(umin
); KEYWORD(fmax
); KEYWORD(fmin
);
704 KEYWORD(blockaddress
);
705 KEYWORD(dso_local_equivalent
);
711 // Use-list order directives.
712 KEYWORD(uselistorder
);
713 KEYWORD(uselistorder_bb
);
715 KEYWORD(personality
);
720 // Summary index keywords.
731 KEYWORD(notEligibleToImport
);
734 KEYWORD(canAutoHide
);
741 KEYWORD(returnDoesNotAlias
);
743 KEYWORD(alwaysInline
);
746 KEYWORD(hasUnknownCall
);
747 KEYWORD(mustBeUnreachable
);
757 KEYWORD(vTableFuncs
);
763 KEYWORD(typeTestAssumeVCalls
);
764 KEYWORD(typeCheckedLoadVCalls
);
765 KEYWORD(typeTestAssumeConstVCalls
);
766 KEYWORD(typeCheckedLoadConstVCalls
);
771 KEYWORD(typeidCompatibleVTable
);
773 KEYWORD(typeTestRes
);
780 KEYWORD(sizeM1BitWidth
);
785 KEYWORD(vcall_visibility
);
786 KEYWORD(wpdResolutions
);
790 KEYWORD(branchFunnel
);
791 KEYWORD(singleImplName
);
794 KEYWORD(uniformRetVal
);
795 KEYWORD(uniqueRetVal
);
796 KEYWORD(virtualConstProp
);
811 // Keywords for types.
812 #define TYPEKEYWORD(STR, LLVMTY) \
814 if (Keyword == STR) { \
816 return lltok::Type; \
820 TYPEKEYWORD("void", Type::getVoidTy(Context
));
821 TYPEKEYWORD("half", Type::getHalfTy(Context
));
822 TYPEKEYWORD("bfloat", Type::getBFloatTy(Context
));
823 TYPEKEYWORD("float", Type::getFloatTy(Context
));
824 TYPEKEYWORD("double", Type::getDoubleTy(Context
));
825 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context
));
826 TYPEKEYWORD("fp128", Type::getFP128Ty(Context
));
827 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context
));
828 TYPEKEYWORD("label", Type::getLabelTy(Context
));
829 TYPEKEYWORD("metadata", Type::getMetadataTy(Context
));
830 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context
));
831 TYPEKEYWORD("x86_amx", Type::getX86_AMXTy(Context
));
832 TYPEKEYWORD("token", Type::getTokenTy(Context
));
833 TYPEKEYWORD("ptr", PointerType::getUnqual(Context
));
837 // Keywords for instructions.
838 #define INSTKEYWORD(STR, Enum) \
840 if (Keyword == #STR) { \
841 UIntVal = Instruction::Enum; \
842 return lltok::kw_##STR; \
846 INSTKEYWORD(fneg
, FNeg
);
848 INSTKEYWORD(add
, Add
); INSTKEYWORD(fadd
, FAdd
);
849 INSTKEYWORD(sub
, Sub
); INSTKEYWORD(fsub
, FSub
);
850 INSTKEYWORD(mul
, Mul
); INSTKEYWORD(fmul
, FMul
);
851 INSTKEYWORD(udiv
, UDiv
); INSTKEYWORD(sdiv
, SDiv
); INSTKEYWORD(fdiv
, FDiv
);
852 INSTKEYWORD(urem
, URem
); INSTKEYWORD(srem
, SRem
); INSTKEYWORD(frem
, FRem
);
853 INSTKEYWORD(shl
, Shl
); INSTKEYWORD(lshr
, LShr
); INSTKEYWORD(ashr
, AShr
);
854 INSTKEYWORD(and, And
); INSTKEYWORD(or, Or
); INSTKEYWORD(xor, Xor
);
855 INSTKEYWORD(icmp
, ICmp
); INSTKEYWORD(fcmp
, FCmp
);
857 INSTKEYWORD(phi
, PHI
);
858 INSTKEYWORD(call
, Call
);
859 INSTKEYWORD(trunc
, Trunc
);
860 INSTKEYWORD(zext
, ZExt
);
861 INSTKEYWORD(sext
, SExt
);
862 INSTKEYWORD(fptrunc
, FPTrunc
);
863 INSTKEYWORD(fpext
, FPExt
);
864 INSTKEYWORD(uitofp
, UIToFP
);
865 INSTKEYWORD(sitofp
, SIToFP
);
866 INSTKEYWORD(fptoui
, FPToUI
);
867 INSTKEYWORD(fptosi
, FPToSI
);
868 INSTKEYWORD(inttoptr
, IntToPtr
);
869 INSTKEYWORD(ptrtoint
, PtrToInt
);
870 INSTKEYWORD(bitcast
, BitCast
);
871 INSTKEYWORD(addrspacecast
, AddrSpaceCast
);
872 INSTKEYWORD(select
, Select
);
873 INSTKEYWORD(va_arg
, VAArg
);
874 INSTKEYWORD(ret
, Ret
);
876 INSTKEYWORD(switch, Switch
);
877 INSTKEYWORD(indirectbr
, IndirectBr
);
878 INSTKEYWORD(invoke
, Invoke
);
879 INSTKEYWORD(resume
, Resume
);
880 INSTKEYWORD(unreachable
, Unreachable
);
881 INSTKEYWORD(callbr
, CallBr
);
883 INSTKEYWORD(alloca
, Alloca
);
884 INSTKEYWORD(load
, Load
);
885 INSTKEYWORD(store
, Store
);
886 INSTKEYWORD(cmpxchg
, AtomicCmpXchg
);
887 INSTKEYWORD(atomicrmw
, AtomicRMW
);
888 INSTKEYWORD(fence
, Fence
);
889 INSTKEYWORD(getelementptr
, GetElementPtr
);
891 INSTKEYWORD(extractelement
, ExtractElement
);
892 INSTKEYWORD(insertelement
, InsertElement
);
893 INSTKEYWORD(shufflevector
, ShuffleVector
);
894 INSTKEYWORD(extractvalue
, ExtractValue
);
895 INSTKEYWORD(insertvalue
, InsertValue
);
896 INSTKEYWORD(landingpad
, LandingPad
);
897 INSTKEYWORD(cleanupret
, CleanupRet
);
898 INSTKEYWORD(catchret
, CatchRet
);
899 INSTKEYWORD(catchswitch
, CatchSwitch
);
900 INSTKEYWORD(catchpad
, CatchPad
);
901 INSTKEYWORD(cleanuppad
, CleanupPad
);
903 INSTKEYWORD(freeze
, Freeze
);
907 #define DWKEYWORD(TYPE, TOKEN) \
909 if (Keyword.starts_with("DW_" #TYPE "_")) { \
910 StrVal.assign(Keyword.begin(), Keyword.end()); \
911 return lltok::TOKEN; \
915 DWKEYWORD(TAG
, DwarfTag
);
916 DWKEYWORD(ATE
, DwarfAttEncoding
);
917 DWKEYWORD(VIRTUALITY
, DwarfVirtuality
);
918 DWKEYWORD(LANG
, DwarfLang
);
919 DWKEYWORD(CC
, DwarfCC
);
920 DWKEYWORD(OP
, DwarfOp
);
921 DWKEYWORD(MACINFO
, DwarfMacinfo
);
925 if (Keyword
.starts_with("DIFlag")) {
926 StrVal
.assign(Keyword
.begin(), Keyword
.end());
927 return lltok::DIFlag
;
930 if (Keyword
.starts_with("DISPFlag")) {
931 StrVal
.assign(Keyword
.begin(), Keyword
.end());
932 return lltok::DISPFlag
;
935 if (Keyword
.starts_with("CSK_")) {
936 StrVal
.assign(Keyword
.begin(), Keyword
.end());
937 return lltok::ChecksumKind
;
940 if (Keyword
== "NoDebug" || Keyword
== "FullDebug" ||
941 Keyword
== "LineTablesOnly" || Keyword
== "DebugDirectivesOnly") {
942 StrVal
.assign(Keyword
.begin(), Keyword
.end());
943 return lltok::EmissionKind
;
946 if (Keyword
== "GNU" || Keyword
== "Apple" || Keyword
== "None" ||
947 Keyword
== "Default") {
948 StrVal
.assign(Keyword
.begin(), Keyword
.end());
949 return lltok::NameTableKind
;
952 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
953 // the CFE to avoid forcing it to deal with 64-bit numbers.
954 if ((TokStart
[0] == 'u' || TokStart
[0] == 's') &&
955 TokStart
[1] == '0' && TokStart
[2] == 'x' &&
956 isxdigit(static_cast<unsigned char>(TokStart
[3]))) {
957 int len
= CurPtr
-TokStart
-3;
958 uint32_t bits
= len
* 4;
959 StringRef
HexStr(TokStart
+ 3, len
);
960 if (!all_of(HexStr
, isxdigit
)) {
961 // Bad token, return it as an error.
965 APInt
Tmp(bits
, HexStr
, 16);
966 uint32_t activeBits
= Tmp
.getActiveBits();
967 if (activeBits
> 0 && activeBits
< bits
)
968 Tmp
= Tmp
.trunc(activeBits
);
969 APSIntVal
= APSInt(Tmp
, TokStart
[0] == 'u');
970 return lltok::APSInt
;
973 // If this is "cc1234", return this as just "cc".
974 if (TokStart
[0] == 'c' && TokStart
[1] == 'c') {
979 // Finally, if this isn't known, return an error.
984 /// Lex all tokens that start with a 0x prefix, knowing they match and are not
986 /// HexFPConstant 0x[0-9A-Fa-f]+
987 /// HexFP80Constant 0xK[0-9A-Fa-f]+
988 /// HexFP128Constant 0xL[0-9A-Fa-f]+
989 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
990 /// HexHalfConstant 0xH[0-9A-Fa-f]+
991 /// HexBFloatConstant 0xR[0-9A-Fa-f]+
992 lltok::Kind
LLLexer::Lex0x() {
993 CurPtr
= TokStart
+ 2;
996 if ((CurPtr
[0] >= 'K' && CurPtr
[0] <= 'M') || CurPtr
[0] == 'H' ||
1003 if (!isxdigit(static_cast<unsigned char>(CurPtr
[0]))) {
1004 // Bad token, return it as an error.
1005 CurPtr
= TokStart
+1;
1006 return lltok::Error
;
1009 while (isxdigit(static_cast<unsigned char>(CurPtr
[0])))
1013 // HexFPConstant - Floating point constant represented in IEEE format as a
1014 // hexadecimal number for when exponential notation is not precise enough.
1015 // Half, BFloat, Float, and double only.
1016 APFloatVal
= APFloat(APFloat::IEEEdouble(),
1017 APInt(64, HexIntToVal(TokStart
+ 2, CurPtr
)));
1018 return lltok::APFloat
;
1023 default: llvm_unreachable("Unknown kind!");
1025 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
1026 FP80HexToIntPair(TokStart
+3, CurPtr
, Pair
);
1027 APFloatVal
= APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair
));
1028 return lltok::APFloat
;
1030 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
1031 HexToIntPair(TokStart
+3, CurPtr
, Pair
);
1032 APFloatVal
= APFloat(APFloat::IEEEquad(), APInt(128, Pair
));
1033 return lltok::APFloat
;
1035 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
1036 HexToIntPair(TokStart
+3, CurPtr
, Pair
);
1037 APFloatVal
= APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair
));
1038 return lltok::APFloat
;
1040 APFloatVal
= APFloat(APFloat::IEEEhalf(),
1041 APInt(16,HexIntToVal(TokStart
+3, CurPtr
)));
1042 return lltok::APFloat
;
1044 // Brain floating point
1045 APFloatVal
= APFloat(APFloat::BFloat(),
1046 APInt(16, HexIntToVal(TokStart
+ 3, CurPtr
)));
1047 return lltok::APFloat
;
1051 /// Lex tokens for a label or a numeric constant, possibly starting with -.
1052 /// Label [-a-zA-Z$._0-9]+:
1053 /// NInteger -[0-9]+
1054 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1056 /// HexFPConstant 0x[0-9A-Fa-f]+
1057 /// HexFP80Constant 0xK[0-9A-Fa-f]+
1058 /// HexFP128Constant 0xL[0-9A-Fa-f]+
1059 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
1060 lltok::Kind
LLLexer::LexDigitOrNegative() {
1061 // If the letter after the negative is not a number, this is probably a label.
1062 if (!isdigit(static_cast<unsigned char>(TokStart
[0])) &&
1063 !isdigit(static_cast<unsigned char>(CurPtr
[0]))) {
1064 // Okay, this is not a number after the -, it's probably a label.
1065 if (const char *End
= isLabelTail(CurPtr
)) {
1066 StrVal
.assign(TokStart
, End
-1);
1068 return lltok::LabelStr
;
1071 return lltok::Error
;
1074 // At this point, it is either a label, int or fp constant.
1076 // Skip digits, we have at least one.
1077 for (; isdigit(static_cast<unsigned char>(CurPtr
[0])); ++CurPtr
)
1080 // Check if this is a fully-numeric label:
1081 if (isdigit(TokStart
[0]) && CurPtr
[0] == ':') {
1082 uint64_t Val
= atoull(TokStart
, CurPtr
);
1083 ++CurPtr
; // Skip the colon.
1084 if ((unsigned)Val
!= Val
)
1085 Error("invalid value number (too large)!");
1086 UIntVal
= unsigned(Val
);
1087 return lltok::LabelID
;
1090 // Check to see if this really is a string label, e.g. "-1:".
1091 if (isLabelChar(CurPtr
[0]) || CurPtr
[0] == ':') {
1092 if (const char *End
= isLabelTail(CurPtr
)) {
1093 StrVal
.assign(TokStart
, End
-1);
1095 return lltok::LabelStr
;
1099 // If the next character is a '.', then it is a fp value, otherwise its
1101 if (CurPtr
[0] != '.') {
1102 if (TokStart
[0] == '0' && TokStart
[1] == 'x')
1104 APSIntVal
= APSInt(StringRef(TokStart
, CurPtr
- TokStart
));
1105 return lltok::APSInt
;
1110 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1111 while (isdigit(static_cast<unsigned char>(CurPtr
[0]))) ++CurPtr
;
1113 if (CurPtr
[0] == 'e' || CurPtr
[0] == 'E') {
1114 if (isdigit(static_cast<unsigned char>(CurPtr
[1])) ||
1115 ((CurPtr
[1] == '-' || CurPtr
[1] == '+') &&
1116 isdigit(static_cast<unsigned char>(CurPtr
[2])))) {
1118 while (isdigit(static_cast<unsigned char>(CurPtr
[0]))) ++CurPtr
;
1122 APFloatVal
= APFloat(APFloat::IEEEdouble(),
1123 StringRef(TokStart
, CurPtr
- TokStart
));
1124 return lltok::APFloat
;
1127 /// Lex a floating point constant starting with +.
1128 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1129 lltok::Kind
LLLexer::LexPositive() {
1130 // If the letter after the negative is a number, this is probably not a
1132 if (!isdigit(static_cast<unsigned char>(CurPtr
[0])))
1133 return lltok::Error
;
1136 for (++CurPtr
; isdigit(static_cast<unsigned char>(CurPtr
[0])); ++CurPtr
)
1139 // At this point, we need a '.'.
1140 if (CurPtr
[0] != '.') {
1141 CurPtr
= TokStart
+1;
1142 return lltok::Error
;
1147 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1148 while (isdigit(static_cast<unsigned char>(CurPtr
[0]))) ++CurPtr
;
1150 if (CurPtr
[0] == 'e' || CurPtr
[0] == 'E') {
1151 if (isdigit(static_cast<unsigned char>(CurPtr
[1])) ||
1152 ((CurPtr
[1] == '-' || CurPtr
[1] == '+') &&
1153 isdigit(static_cast<unsigned char>(CurPtr
[2])))) {
1155 while (isdigit(static_cast<unsigned char>(CurPtr
[0]))) ++CurPtr
;
1159 APFloatVal
= APFloat(APFloat::IEEEdouble(),
1160 StringRef(TokStart
, CurPtr
- TokStart
));
1161 return lltok::APFloat
;