[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / AsmParser / LLLexer.cpp
blobbee3d947de930699d71f46f56ae8a6010e99517c
1 //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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"
22 #include <cassert>
23 #include <cctype>
24 #include <cstdio>
26 using namespace llvm;
28 bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
29 ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
30 return true;
33 void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const {
34 SM.PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
37 //===----------------------------------------------------------------------===//
38 // Helper functions.
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) {
46 uint64_t Result = 0;
47 for (; Buffer != End; Buffer++) {
48 uint64_t OldRes = Result;
49 Result *= 10;
50 Result += *Buffer-'0';
51 if (Result < OldRes) { // Uh, oh, overflow detected!!!
52 Error("constant bigger than 64 bits detected!");
53 return 0;
56 return Result;
59 uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
60 uint64_t Result = 0;
61 for (; Buffer != End; ++Buffer) {
62 uint64_t OldRes = Result;
63 Result *= 16;
64 Result += hexDigitValue(*Buffer);
66 if (Result < OldRes) { // Uh, oh, overflow detected!!!
67 Error("constant bigger than 64 bits detected!");
68 return 0;
71 return Result;
74 void LLLexer::HexToIntPair(const char *Buffer, const char *End,
75 uint64_t Pair[2]) {
76 Pair[0] = 0;
77 if (End - Buffer >= 16) {
78 for (int i = 0; i < 16; i++, Buffer++) {
79 assert(Buffer != End);
80 Pair[0] *= 16;
81 Pair[0] += hexDigitValue(*Buffer);
84 Pair[1] = 0;
85 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
86 Pair[1] *= 16;
87 Pair[1] += hexDigitValue(*Buffer);
89 if (Buffer != End)
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,
96 uint64_t Pair[2]) {
97 Pair[1] = 0;
98 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
99 assert(Buffer != End);
100 Pair[1] *= 16;
101 Pair[1] += hexDigitValue(*Buffer);
103 Pair[0] = 0;
104 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
105 Pair[0] *= 16;
106 Pair[0] += hexDigitValue(*Buffer);
108 if (Buffer != End)
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();
118 char *BOut = Buffer;
119 for (char *BIn = Buffer; BIn != EndBuffer; ) {
120 if (BIn[0] == '\\') {
121 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
122 *BOut++ = '\\'; // Two \ becomes one
123 BIn += 2;
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
129 ++BOut;
130 } else {
131 *BOut++ = *BIn++;
133 } else {
134 *BOut++ = *BIn++;
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) {
148 while (true) {
149 if (CurPtr[0] == ':') return CurPtr+1;
150 if (!isLabelChar(CurPtr[0])) return nullptr;
151 ++CurPtr;
155 //===----------------------------------------------------------------------===//
156 // Lexer definition.
157 //===----------------------------------------------------------------------===//
159 LLLexer::LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &Err,
160 LLVMContext &C)
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++;
168 switch (CurChar) {
169 default: return (unsigned char)CurChar;
170 case 0:
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.
178 return EOF;
182 lltok::Kind LLLexer::LexToken() {
183 while (true) {
184 TokStart = CurPtr;
186 int CurChar = getNextChar();
187 switch (CurChar) {
188 default:
189 // Handle letters: [a-zA-Z_]
190 if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
191 return LexIdentifier();
193 return lltok::Error;
194 case EOF: return lltok::Eof;
195 case 0:
196 case ' ':
197 case '\t':
198 case '\n':
199 case '\r':
200 // Ignore whitespace.
201 continue;
202 case '+': return LexPositive();
203 case '@': return LexAt();
204 case '$': return LexDollar();
205 case '%': return LexPercent();
206 case '"': return LexQuote();
207 case '.':
208 if (const char *Ptr = isLabelTail(CurPtr)) {
209 CurPtr = Ptr;
210 StrVal.assign(TokStart, CurPtr-1);
211 return lltok::LabelStr;
213 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
214 CurPtr += 2;
215 return lltok::dotdotdot;
217 return lltok::Error;
218 case ';':
219 SkipLineComment();
220 continue;
221 case '!': return LexExclaim();
222 case '^':
223 return LexCaret();
224 case ':':
225 return lltok::colon;
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':
229 case '-':
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() {
248 while (true) {
249 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
250 return;
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)) {
264 CurPtr = Ptr;
265 StrVal.assign(TokStart, CurPtr - 1);
266 return lltok::LabelStr;
269 // Handle DollarStringConstant: $\"[^\"]*\"
270 if (CurPtr[0] == '"') {
271 ++CurPtr;
273 while (true) {
274 int CurChar = getNextChar();
276 if (CurChar == EOF) {
277 Error("end of file in COMDAT variable name");
278 return lltok::Error;
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");
285 return lltok::Error;
287 return lltok::ComdatVar;
292 // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
293 if (ReadVarName())
294 return lltok::ComdatVar;
296 return lltok::Error;
299 /// ReadString - Read a string until the closing quote.
300 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
301 const char *Start = CurPtr;
302 while (true) {
303 int CurChar = getNextChar();
305 if (CurChar == EOF) {
306 Error("end of file in string constant");
307 return lltok::Error;
309 if (CurChar == '"') {
310 StrVal.assign(Start, CurPtr-1);
311 UnEscapeLexed(StrVal);
312 return kind;
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] == '_') {
323 ++CurPtr;
324 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
325 CurPtr[0] == '-' || CurPtr[0] == '$' ||
326 CurPtr[0] == '.' || CurPtr[0] == '_')
327 ++CurPtr;
329 StrVal.assign(NameStart, CurPtr);
330 return true;
332 return false;
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])))
339 return lltok::Error;
341 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
342 /*empty*/;
344 uint64_t Val = atoull(TokStart + 1, CurPtr);
345 if ((unsigned)Val != Val)
346 Error("invalid value number (too large)!");
347 UIntVal = unsigned(Val);
348 return Token;
351 lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) {
352 // Handle StringConstant: \"[^\"]*\"
353 if (CurPtr[0] == '"') {
354 ++CurPtr;
356 while (true) {
357 int CurChar = getNextChar();
359 if (CurChar == EOF) {
360 Error("end of file in global variable name");
361 return lltok::Error;
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");
368 return lltok::Error;
370 return Var;
375 // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]*
376 if (ReadVarName())
377 return Var;
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)
397 return kind;
399 if (CurPtr[0] == ':') {
400 ++CurPtr;
401 if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
402 Error("Null bytes are not allowed in names");
403 kind = lltok::Error;
404 } else {
405 kind = lltok::LabelStr;
409 return kind;
412 /// Lex all tokens that start with a ! character.
413 /// !foo
414 /// !
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] == '\\') {
420 ++CurPtr;
421 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
422 CurPtr[0] == '-' || CurPtr[0] == '$' ||
423 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
424 ++CurPtr;
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)))
460 IntEnd = CurPtr;
461 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
462 *CurPtr != '_')
463 KeywordEnd = 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,
474 // return it.
475 if (!IntEnd) IntEnd = CurPtr;
476 if (IntEnd != StartChar) {
477 CurPtr = IntEnd;
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!");
482 return lltok::Error;
484 TyVal = IntegerType::get(Context, NumBits);
485 return lltok::Type;
488 // Otherwise, this was a letter sequence. See which keyword this is.
489 if (!KeywordEnd) KeywordEnd = CurPtr;
490 CurPtr = KeywordEnd;
491 --StartChar;
492 StringRef Keyword(StartChar, CurPtr - StartChar);
494 #define KEYWORD(STR) \
495 do { \
496 if (Keyword == #STR) \
497 return lltok::kw_##STR; \
498 } while (false)
500 KEYWORD(true); KEYWORD(false);
501 KEYWORD(declare); KEYWORD(define);
502 KEYWORD(global); KEYWORD(constant);
504 KEYWORD(dso_local);
505 KEYWORD(dso_preemptable);
507 KEYWORD(private);
508 KEYWORD(internal);
509 KEYWORD(available_externally);
510 KEYWORD(linkonce);
511 KEYWORD(linkonce_odr);
512 KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
513 KEYWORD(weak_odr);
514 KEYWORD(appending);
515 KEYWORD(dllimport);
516 KEYWORD(dllexport);
517 KEYWORD(common);
518 KEYWORD(default);
519 KEYWORD(hidden);
520 KEYWORD(protected);
521 KEYWORD(unnamed_addr);
522 KEYWORD(local_unnamed_addr);
523 KEYWORD(externally_initialized);
524 KEYWORD(extern_weak);
525 KEYWORD(external);
526 KEYWORD(thread_local);
527 KEYWORD(localdynamic);
528 KEYWORD(initialexec);
529 KEYWORD(localexec);
530 KEYWORD(zeroinitializer);
531 KEYWORD(undef);
532 KEYWORD(null);
533 KEYWORD(none);
534 KEYWORD(poison);
535 KEYWORD(to);
536 KEYWORD(caller);
537 KEYWORD(within);
538 KEYWORD(from);
539 KEYWORD(tail);
540 KEYWORD(musttail);
541 KEYWORD(notail);
542 KEYWORD(target);
543 KEYWORD(triple);
544 KEYWORD(source_filename);
545 KEYWORD(unwind);
546 KEYWORD(datalayout);
547 KEYWORD(volatile);
548 KEYWORD(atomic);
549 KEYWORD(unordered);
550 KEYWORD(monotonic);
551 KEYWORD(acquire);
552 KEYWORD(release);
553 KEYWORD(acq_rel);
554 KEYWORD(seq_cst);
555 KEYWORD(syncscope);
557 KEYWORD(nnan);
558 KEYWORD(ninf);
559 KEYWORD(nsz);
560 KEYWORD(arcp);
561 KEYWORD(contract);
562 KEYWORD(reassoc);
563 KEYWORD(afn);
564 KEYWORD(fast);
565 KEYWORD(nuw);
566 KEYWORD(nsw);
567 KEYWORD(exact);
568 KEYWORD(inbounds);
569 KEYWORD(inrange);
570 KEYWORD(align);
571 KEYWORD(addrspace);
572 KEYWORD(section);
573 KEYWORD(partition);
574 KEYWORD(alias);
575 KEYWORD(ifunc);
576 KEYWORD(module);
577 KEYWORD(asm);
578 KEYWORD(sideeffect);
579 KEYWORD(alignstack);
580 KEYWORD(inteldialect);
581 KEYWORD(gc);
582 KEYWORD(prefix);
583 KEYWORD(prologue);
585 KEYWORD(ccc);
586 KEYWORD(fastcc);
587 KEYWORD(coldcc);
588 KEYWORD(cfguard_checkcc);
589 KEYWORD(x86_stdcallcc);
590 KEYWORD(x86_fastcallcc);
591 KEYWORD(x86_thiscallcc);
592 KEYWORD(x86_vectorcallcc);
593 KEYWORD(arm_apcscc);
594 KEYWORD(arm_aapcscc);
595 KEYWORD(arm_aapcs_vfpcc);
596 KEYWORD(aarch64_vector_pcs);
597 KEYWORD(aarch64_sve_vector_pcs);
598 KEYWORD(msp430_intrcc);
599 KEYWORD(avr_intrcc);
600 KEYWORD(avr_signalcc);
601 KEYWORD(ptx_kernel);
602 KEYWORD(ptx_device);
603 KEYWORD(spir_kernel);
604 KEYWORD(spir_func);
605 KEYWORD(intel_ocl_bicc);
606 KEYWORD(x86_64_sysvcc);
607 KEYWORD(win64cc);
608 KEYWORD(x86_regcallcc);
609 KEYWORD(webkit_jscc);
610 KEYWORD(swiftcc);
611 KEYWORD(swifttailcc);
612 KEYWORD(anyregcc);
613 KEYWORD(preserve_mostcc);
614 KEYWORD(preserve_allcc);
615 KEYWORD(ghccc);
616 KEYWORD(x86_intrcc);
617 KEYWORD(hhvmcc);
618 KEYWORD(hhvm_ccc);
619 KEYWORD(cxx_fast_tlscc);
620 KEYWORD(amdgpu_vs);
621 KEYWORD(amdgpu_ls);
622 KEYWORD(amdgpu_hs);
623 KEYWORD(amdgpu_es);
624 KEYWORD(amdgpu_gs);
625 KEYWORD(amdgpu_ps);
626 KEYWORD(amdgpu_cs);
627 KEYWORD(amdgpu_kernel);
628 KEYWORD(amdgpu_gfx);
629 KEYWORD(tailcc);
631 KEYWORD(cc);
632 KEYWORD(c);
634 KEYWORD(attributes);
636 KEYWORD(alwaysinline);
637 KEYWORD(allocsize);
638 KEYWORD(argmemonly);
639 KEYWORD(builtin);
640 KEYWORD(byval);
641 KEYWORD(inalloca);
642 KEYWORD(cold);
643 KEYWORD(convergent);
644 KEYWORD(dereferenceable);
645 KEYWORD(dereferenceable_or_null);
646 KEYWORD(disable_sanitizer_instrumentation);
647 KEYWORD(elementtype);
648 KEYWORD(inaccessiblememonly);
649 KEYWORD(inaccessiblemem_or_argmemonly);
650 KEYWORD(inlinehint);
651 KEYWORD(inreg);
652 KEYWORD(jumptable);
653 KEYWORD(minsize);
654 KEYWORD(naked);
655 KEYWORD(nest);
656 KEYWORD(noalias);
657 KEYWORD(nobuiltin);
658 KEYWORD(nocallback);
659 KEYWORD(nocapture);
660 KEYWORD(noduplicate);
661 KEYWORD(nofree);
662 KEYWORD(noimplicitfloat);
663 KEYWORD(noinline);
664 KEYWORD(norecurse);
665 KEYWORD(nonlazybind);
666 KEYWORD(nomerge);
667 KEYWORD(nonnull);
668 KEYWORD(noprofile);
669 KEYWORD(noredzone);
670 KEYWORD(noreturn);
671 KEYWORD(nosync);
672 KEYWORD(nocf_check);
673 KEYWORD(noundef);
674 KEYWORD(nounwind);
675 KEYWORD(nosanitize_coverage);
676 KEYWORD(null_pointer_is_valid);
677 KEYWORD(optforfuzzing);
678 KEYWORD(optnone);
679 KEYWORD(optsize);
680 KEYWORD(preallocated);
681 KEYWORD(readnone);
682 KEYWORD(readonly);
683 KEYWORD(returned);
684 KEYWORD(returns_twice);
685 KEYWORD(signext);
686 KEYWORD(speculatable);
687 KEYWORD(sret);
688 KEYWORD(ssp);
689 KEYWORD(sspreq);
690 KEYWORD(sspstrong);
691 KEYWORD(strictfp);
692 KEYWORD(safestack);
693 KEYWORD(shadowcallstack);
694 KEYWORD(sanitize_address);
695 KEYWORD(sanitize_hwaddress);
696 KEYWORD(sanitize_memtag);
697 KEYWORD(sanitize_thread);
698 KEYWORD(sanitize_memory);
699 KEYWORD(speculative_load_hardening);
700 KEYWORD(swifterror);
701 KEYWORD(swiftself);
702 KEYWORD(swiftasync);
703 KEYWORD(uwtable);
704 KEYWORD(vscale_range);
705 KEYWORD(willreturn);
706 KEYWORD(writeonly);
707 KEYWORD(zeroext);
708 KEYWORD(immarg);
709 KEYWORD(byref);
710 KEYWORD(mustprogress);
712 KEYWORD(type);
713 KEYWORD(opaque);
715 KEYWORD(comdat);
717 // Comdat types
718 KEYWORD(any);
719 KEYWORD(exactmatch);
720 KEYWORD(largest);
721 KEYWORD(nodeduplicate);
722 KEYWORD(samesize);
724 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
725 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
726 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
727 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
729 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
730 KEYWORD(umin);
732 KEYWORD(vscale);
733 KEYWORD(x);
734 KEYWORD(blockaddress);
735 KEYWORD(dso_local_equivalent);
737 // Metadata types.
738 KEYWORD(distinct);
740 // Use-list order directives.
741 KEYWORD(uselistorder);
742 KEYWORD(uselistorder_bb);
744 KEYWORD(personality);
745 KEYWORD(cleanup);
746 KEYWORD(catch);
747 KEYWORD(filter);
749 // Summary index keywords.
750 KEYWORD(path);
751 KEYWORD(hash);
752 KEYWORD(gv);
753 KEYWORD(guid);
754 KEYWORD(name);
755 KEYWORD(summaries);
756 KEYWORD(flags);
757 KEYWORD(blockcount);
758 KEYWORD(linkage);
759 KEYWORD(visibility);
760 KEYWORD(notEligibleToImport);
761 KEYWORD(live);
762 KEYWORD(dsoLocal);
763 KEYWORD(canAutoHide);
764 KEYWORD(function);
765 KEYWORD(insts);
766 KEYWORD(funcFlags);
767 KEYWORD(readNone);
768 KEYWORD(readOnly);
769 KEYWORD(noRecurse);
770 KEYWORD(returnDoesNotAlias);
771 KEYWORD(noInline);
772 KEYWORD(alwaysInline);
773 KEYWORD(calls);
774 KEYWORD(callee);
775 KEYWORD(params);
776 KEYWORD(param);
777 KEYWORD(hotness);
778 KEYWORD(unknown);
779 KEYWORD(hot);
780 KEYWORD(critical);
781 KEYWORD(relbf);
782 KEYWORD(variable);
783 KEYWORD(vTableFuncs);
784 KEYWORD(virtFunc);
785 KEYWORD(aliasee);
786 KEYWORD(refs);
787 KEYWORD(typeIdInfo);
788 KEYWORD(typeTests);
789 KEYWORD(typeTestAssumeVCalls);
790 KEYWORD(typeCheckedLoadVCalls);
791 KEYWORD(typeTestAssumeConstVCalls);
792 KEYWORD(typeCheckedLoadConstVCalls);
793 KEYWORD(vFuncId);
794 KEYWORD(offset);
795 KEYWORD(args);
796 KEYWORD(typeid);
797 KEYWORD(typeidCompatibleVTable);
798 KEYWORD(summary);
799 KEYWORD(typeTestRes);
800 KEYWORD(kind);
801 KEYWORD(unsat);
802 KEYWORD(byteArray);
803 KEYWORD(inline);
804 KEYWORD(single);
805 KEYWORD(allOnes);
806 KEYWORD(sizeM1BitWidth);
807 KEYWORD(alignLog2);
808 KEYWORD(sizeM1);
809 KEYWORD(bitMask);
810 KEYWORD(inlineBits);
811 KEYWORD(vcall_visibility);
812 KEYWORD(wpdResolutions);
813 KEYWORD(wpdRes);
814 KEYWORD(indir);
815 KEYWORD(singleImpl);
816 KEYWORD(branchFunnel);
817 KEYWORD(singleImplName);
818 KEYWORD(resByArg);
819 KEYWORD(byArg);
820 KEYWORD(uniformRetVal);
821 KEYWORD(uniqueRetVal);
822 KEYWORD(virtualConstProp);
823 KEYWORD(info);
824 KEYWORD(byte);
825 KEYWORD(bit);
826 KEYWORD(varFlags);
828 #undef KEYWORD
830 // Keywords for types.
831 #define TYPEKEYWORD(STR, LLVMTY) \
832 do { \
833 if (Keyword == STR) { \
834 TyVal = LLVMTY; \
835 return lltok::Type; \
837 } while (false)
839 TYPEKEYWORD("void", Type::getVoidTy(Context));
840 TYPEKEYWORD("half", Type::getHalfTy(Context));
841 TYPEKEYWORD("bfloat", Type::getBFloatTy(Context));
842 TYPEKEYWORD("float", Type::getFloatTy(Context));
843 TYPEKEYWORD("double", Type::getDoubleTy(Context));
844 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
845 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
846 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
847 TYPEKEYWORD("label", Type::getLabelTy(Context));
848 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
849 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
850 TYPEKEYWORD("x86_amx", Type::getX86_AMXTy(Context));
851 TYPEKEYWORD("token", Type::getTokenTy(Context));
852 TYPEKEYWORD("ptr", PointerType::getUnqual(Context));
854 #undef TYPEKEYWORD
856 // Keywords for instructions.
857 #define INSTKEYWORD(STR, Enum) \
858 do { \
859 if (Keyword == #STR) { \
860 UIntVal = Instruction::Enum; \
861 return lltok::kw_##STR; \
863 } while (false)
865 INSTKEYWORD(fneg, FNeg);
867 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
868 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
869 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
870 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
871 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
872 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
873 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
874 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
876 INSTKEYWORD(phi, PHI);
877 INSTKEYWORD(call, Call);
878 INSTKEYWORD(trunc, Trunc);
879 INSTKEYWORD(zext, ZExt);
880 INSTKEYWORD(sext, SExt);
881 INSTKEYWORD(fptrunc, FPTrunc);
882 INSTKEYWORD(fpext, FPExt);
883 INSTKEYWORD(uitofp, UIToFP);
884 INSTKEYWORD(sitofp, SIToFP);
885 INSTKEYWORD(fptoui, FPToUI);
886 INSTKEYWORD(fptosi, FPToSI);
887 INSTKEYWORD(inttoptr, IntToPtr);
888 INSTKEYWORD(ptrtoint, PtrToInt);
889 INSTKEYWORD(bitcast, BitCast);
890 INSTKEYWORD(addrspacecast, AddrSpaceCast);
891 INSTKEYWORD(select, Select);
892 INSTKEYWORD(va_arg, VAArg);
893 INSTKEYWORD(ret, Ret);
894 INSTKEYWORD(br, Br);
895 INSTKEYWORD(switch, Switch);
896 INSTKEYWORD(indirectbr, IndirectBr);
897 INSTKEYWORD(invoke, Invoke);
898 INSTKEYWORD(resume, Resume);
899 INSTKEYWORD(unreachable, Unreachable);
900 INSTKEYWORD(callbr, CallBr);
902 INSTKEYWORD(alloca, Alloca);
903 INSTKEYWORD(load, Load);
904 INSTKEYWORD(store, Store);
905 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
906 INSTKEYWORD(atomicrmw, AtomicRMW);
907 INSTKEYWORD(fence, Fence);
908 INSTKEYWORD(getelementptr, GetElementPtr);
910 INSTKEYWORD(extractelement, ExtractElement);
911 INSTKEYWORD(insertelement, InsertElement);
912 INSTKEYWORD(shufflevector, ShuffleVector);
913 INSTKEYWORD(extractvalue, ExtractValue);
914 INSTKEYWORD(insertvalue, InsertValue);
915 INSTKEYWORD(landingpad, LandingPad);
916 INSTKEYWORD(cleanupret, CleanupRet);
917 INSTKEYWORD(catchret, CatchRet);
918 INSTKEYWORD(catchswitch, CatchSwitch);
919 INSTKEYWORD(catchpad, CatchPad);
920 INSTKEYWORD(cleanuppad, CleanupPad);
922 INSTKEYWORD(freeze, Freeze);
924 #undef INSTKEYWORD
926 #define DWKEYWORD(TYPE, TOKEN) \
927 do { \
928 if (Keyword.startswith("DW_" #TYPE "_")) { \
929 StrVal.assign(Keyword.begin(), Keyword.end()); \
930 return lltok::TOKEN; \
932 } while (false)
934 DWKEYWORD(TAG, DwarfTag);
935 DWKEYWORD(ATE, DwarfAttEncoding);
936 DWKEYWORD(VIRTUALITY, DwarfVirtuality);
937 DWKEYWORD(LANG, DwarfLang);
938 DWKEYWORD(CC, DwarfCC);
939 DWKEYWORD(OP, DwarfOp);
940 DWKEYWORD(MACINFO, DwarfMacinfo);
942 #undef DWKEYWORD
944 if (Keyword.startswith("DIFlag")) {
945 StrVal.assign(Keyword.begin(), Keyword.end());
946 return lltok::DIFlag;
949 if (Keyword.startswith("DISPFlag")) {
950 StrVal.assign(Keyword.begin(), Keyword.end());
951 return lltok::DISPFlag;
954 if (Keyword.startswith("CSK_")) {
955 StrVal.assign(Keyword.begin(), Keyword.end());
956 return lltok::ChecksumKind;
959 if (Keyword == "NoDebug" || Keyword == "FullDebug" ||
960 Keyword == "LineTablesOnly" || Keyword == "DebugDirectivesOnly") {
961 StrVal.assign(Keyword.begin(), Keyword.end());
962 return lltok::EmissionKind;
965 if (Keyword == "GNU" || Keyword == "None" || Keyword == "Default") {
966 StrVal.assign(Keyword.begin(), Keyword.end());
967 return lltok::NameTableKind;
970 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
971 // the CFE to avoid forcing it to deal with 64-bit numbers.
972 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
973 TokStart[1] == '0' && TokStart[2] == 'x' &&
974 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
975 int len = CurPtr-TokStart-3;
976 uint32_t bits = len * 4;
977 StringRef HexStr(TokStart + 3, len);
978 if (!all_of(HexStr, isxdigit)) {
979 // Bad token, return it as an error.
980 CurPtr = TokStart+3;
981 return lltok::Error;
983 APInt Tmp(bits, HexStr, 16);
984 uint32_t activeBits = Tmp.getActiveBits();
985 if (activeBits > 0 && activeBits < bits)
986 Tmp = Tmp.trunc(activeBits);
987 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
988 return lltok::APSInt;
991 // If this is "cc1234", return this as just "cc".
992 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
993 CurPtr = TokStart+2;
994 return lltok::kw_cc;
997 // Finally, if this isn't known, return an error.
998 CurPtr = TokStart+1;
999 return lltok::Error;
1002 /// Lex all tokens that start with a 0x prefix, knowing they match and are not
1003 /// labels.
1004 /// HexFPConstant 0x[0-9A-Fa-f]+
1005 /// HexFP80Constant 0xK[0-9A-Fa-f]+
1006 /// HexFP128Constant 0xL[0-9A-Fa-f]+
1007 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
1008 /// HexHalfConstant 0xH[0-9A-Fa-f]+
1009 /// HexBFloatConstant 0xR[0-9A-Fa-f]+
1010 lltok::Kind LLLexer::Lex0x() {
1011 CurPtr = TokStart + 2;
1013 char Kind;
1014 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H' ||
1015 CurPtr[0] == 'R') {
1016 Kind = *CurPtr++;
1017 } else {
1018 Kind = 'J';
1021 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
1022 // Bad token, return it as an error.
1023 CurPtr = TokStart+1;
1024 return lltok::Error;
1027 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
1028 ++CurPtr;
1030 if (Kind == 'J') {
1031 // HexFPConstant - Floating point constant represented in IEEE format as a
1032 // hexadecimal number for when exponential notation is not precise enough.
1033 // Half, BFloat, Float, and double only.
1034 APFloatVal = APFloat(APFloat::IEEEdouble(),
1035 APInt(64, HexIntToVal(TokStart + 2, CurPtr)));
1036 return lltok::APFloat;
1039 uint64_t Pair[2];
1040 switch (Kind) {
1041 default: llvm_unreachable("Unknown kind!");
1042 case 'K':
1043 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
1044 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
1045 APFloatVal = APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair));
1046 return lltok::APFloat;
1047 case 'L':
1048 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
1049 HexToIntPair(TokStart+3, CurPtr, Pair);
1050 APFloatVal = APFloat(APFloat::IEEEquad(), APInt(128, Pair));
1051 return lltok::APFloat;
1052 case 'M':
1053 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
1054 HexToIntPair(TokStart+3, CurPtr, Pair);
1055 APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair));
1056 return lltok::APFloat;
1057 case 'H':
1058 APFloatVal = APFloat(APFloat::IEEEhalf(),
1059 APInt(16,HexIntToVal(TokStart+3, CurPtr)));
1060 return lltok::APFloat;
1061 case 'R':
1062 // Brain floating point
1063 APFloatVal = APFloat(APFloat::BFloat(),
1064 APInt(16, HexIntToVal(TokStart + 3, CurPtr)));
1065 return lltok::APFloat;
1069 /// Lex tokens for a label or a numeric constant, possibly starting with -.
1070 /// Label [-a-zA-Z$._0-9]+:
1071 /// NInteger -[0-9]+
1072 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1073 /// PInteger [0-9]+
1074 /// HexFPConstant 0x[0-9A-Fa-f]+
1075 /// HexFP80Constant 0xK[0-9A-Fa-f]+
1076 /// HexFP128Constant 0xL[0-9A-Fa-f]+
1077 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
1078 lltok::Kind LLLexer::LexDigitOrNegative() {
1079 // If the letter after the negative is not a number, this is probably a label.
1080 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
1081 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
1082 // Okay, this is not a number after the -, it's probably a label.
1083 if (const char *End = isLabelTail(CurPtr)) {
1084 StrVal.assign(TokStart, End-1);
1085 CurPtr = End;
1086 return lltok::LabelStr;
1089 return lltok::Error;
1092 // At this point, it is either a label, int or fp constant.
1094 // Skip digits, we have at least one.
1095 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1096 /*empty*/;
1098 // Check if this is a fully-numeric label:
1099 if (isdigit(TokStart[0]) && CurPtr[0] == ':') {
1100 uint64_t Val = atoull(TokStart, CurPtr);
1101 ++CurPtr; // Skip the colon.
1102 if ((unsigned)Val != Val)
1103 Error("invalid value number (too large)!");
1104 UIntVal = unsigned(Val);
1105 return lltok::LabelID;
1108 // Check to see if this really is a string label, e.g. "-1:".
1109 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
1110 if (const char *End = isLabelTail(CurPtr)) {
1111 StrVal.assign(TokStart, End-1);
1112 CurPtr = End;
1113 return lltok::LabelStr;
1117 // If the next character is a '.', then it is a fp value, otherwise its
1118 // integer.
1119 if (CurPtr[0] != '.') {
1120 if (TokStart[0] == '0' && TokStart[1] == 'x')
1121 return Lex0x();
1122 APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart));
1123 return lltok::APSInt;
1126 ++CurPtr;
1128 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1129 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1131 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1132 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1133 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1134 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1135 CurPtr += 2;
1136 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1140 APFloatVal = APFloat(APFloat::IEEEdouble(),
1141 StringRef(TokStart, CurPtr - TokStart));
1142 return lltok::APFloat;
1145 /// Lex a floating point constant starting with +.
1146 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1147 lltok::Kind LLLexer::LexPositive() {
1148 // If the letter after the negative is a number, this is probably not a
1149 // label.
1150 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
1151 return lltok::Error;
1153 // Skip digits.
1154 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1155 /*empty*/;
1157 // At this point, we need a '.'.
1158 if (CurPtr[0] != '.') {
1159 CurPtr = TokStart+1;
1160 return lltok::Error;
1163 ++CurPtr;
1165 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1166 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1168 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1169 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1170 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1171 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1172 CurPtr += 2;
1173 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1177 APFloatVal = APFloat(APFloat::IEEEdouble(),
1178 StringRef(TokStart, CurPtr - TokStart));
1179 return lltok::APFloat;