LLVM 24.0.0git
LLLexer.cpp
Go to the documentation of this file.
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.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/Twine.h"
19#include "llvm/IR/Instruction.h"
22#include <cassert>
23#include <cctype>
24#include <cstdio>
25
26using namespace llvm;
27
28// Both the lexer and parser can issue error messages. If the lexer issues a
29// lexer error, since we do not terminate execution immediately, usually that
30// is followed by the parser issuing a parser error. However, the error issued
31// by the lexer is more relevant in that case as opposed to potentially more
32// generic parser error. So instead of always recording the last error message
33// use the `Priority` to establish a priority, with Lexer > Parser > None. We
34// record the issued message only if the message has same or higher priority
35// than the existing one. This prevents lexer errors from being overwritten by
36// parser errors.
37void LLLexer::Error(LocTy ErrorLoc, const Twine &Msg,
38 LLLexer::ErrorPriority Priority) {
39 if (Priority < ErrorInfo.Priority)
40 return;
41 ErrorInfo.Error = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
42 ErrorInfo.Priority = Priority;
43}
44
45void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const {
46 SM.PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
47}
48
49//===----------------------------------------------------------------------===//
50// Helper functions.
51//===----------------------------------------------------------------------===//
52
53// atoull - Convert an ascii string of decimal digits into the unsigned long
54// long representation... this does not have to do input error checking,
55// because we know that the input will be matched by a suitable regex...
56//
57uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
58 uint64_t Result = 0;
59 for (; Buffer != End; Buffer++) {
60 uint64_t OldRes = Result;
61 Result *= 10;
62 Result += *Buffer-'0';
63 if (Result < OldRes) { // overflow detected.
64 LexError("constant bigger than 64 bits detected");
65 return 0;
66 }
67 }
68 return Result;
69}
70
71uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
72 uint64_t Result = 0;
73 for (; Buffer != End; ++Buffer) {
74 uint64_t OldRes = Result;
75 Result *= 16;
76 Result += hexDigitValue(*Buffer);
77
78 if (Result < OldRes) { // overflow detected.
79 LexError("constant bigger than 64 bits detected");
80 return 0;
81 }
82 }
83 return Result;
84}
85
86void LLLexer::HexToIntPair(const char *Buffer, const char *End,
87 uint64_t Pair[2]) {
88 Pair[0] = 0;
89 if (End - Buffer >= 16) {
90 for (int i = 0; i < 16; i++, Buffer++) {
91 assert(Buffer != End);
92 Pair[0] *= 16;
93 Pair[0] += hexDigitValue(*Buffer);
94 }
95 }
96 Pair[1] = 0;
97 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
98 Pair[1] *= 16;
99 Pair[1] += hexDigitValue(*Buffer);
100 }
101 if (Buffer != End)
102 LexError("constant bigger than 128 bits detected");
103}
104
105/// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
106/// { low64, high16 } as usual for an APInt.
107void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
108 uint64_t Pair[2]) {
109 Pair[1] = 0;
110 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
111 assert(Buffer != End);
112 Pair[1] *= 16;
113 Pair[1] += hexDigitValue(*Buffer);
114 }
115 Pair[0] = 0;
116 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
117 Pair[0] *= 16;
118 Pair[0] += hexDigitValue(*Buffer);
119 }
120 if (Buffer != End)
121 LexError("constant bigger than 128 bits detected");
122}
123
124// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
125// appropriate character.
126static void UnEscapeLexed(std::string &Str) {
127 if (Str.empty()) return;
128
129 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
130 char *BOut = Buffer;
131 for (char *BIn = Buffer; BIn != EndBuffer; ) {
132 if (BIn[0] == '\\') {
133 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
134 *BOut++ = '\\'; // Two \ becomes one
135 BIn += 2;
136 } else if (BIn < EndBuffer-2 &&
137 isxdigit(static_cast<unsigned char>(BIn[1])) &&
138 isxdigit(static_cast<unsigned char>(BIn[2]))) {
139 *BOut = hexDigitValue(BIn[1]) * 16 + hexDigitValue(BIn[2]);
140 BIn += 3; // Skip over handled chars
141 ++BOut;
142 } else {
143 *BOut++ = *BIn++;
144 }
145 } else {
146 *BOut++ = *BIn++;
147 }
148 }
149 Str.resize(BOut-Buffer);
150}
151
152/// isLabelChar - Return true for [-a-zA-Z$._0-9].
153static bool isLabelChar(char C) {
154 return isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
155 C == '.' || C == '_';
156}
157
158/// isLabelTail - Return true if this pointer points to a valid end of a label.
159static const char *isLabelTail(const char *CurPtr) {
160 while (true) {
161 if (CurPtr[0] == ':') return CurPtr+1;
162 if (!isLabelChar(CurPtr[0])) return nullptr;
163 ++CurPtr;
164 }
165}
166
167//===----------------------------------------------------------------------===//
168// Lexer definition.
169//===----------------------------------------------------------------------===//
170
172 LLVMContext &C)
173 : CurBuf(StartBuf), ErrorInfo(Err), SM(SM), Context(C) {
174 CurPtr = CurBuf.begin();
175}
176
177int LLLexer::getNextChar() {
178 char CurChar = *CurPtr++;
179 switch (CurChar) {
180 default: return (unsigned char)CurChar;
181 case 0:
182 // A nul character in the stream is either the end of the current buffer or
183 // a random nul in the file. Disambiguate that here.
184 if (CurPtr-1 != CurBuf.end())
185 return 0; // Just whitespace.
186
187 // Otherwise, return end of file.
188 --CurPtr; // Another call to lex will return EOF again.
189 return EOF;
190 }
191}
192
193lltok::Kind LLLexer::LexToken() {
194 // Set token end to next location, since the end is exclusive.
195 PrevTokEnd = CurPtr;
196 while (true) {
197 TokStart = CurPtr;
198
199 int CurChar = getNextChar();
200 switch (CurChar) {
201 default:
202 // Handle letters: [a-zA-Z_]
203 if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
204 return LexIdentifier();
205 return lltok::Error;
206 case EOF: return lltok::Eof;
207 case 0:
208 case ' ':
209 case '\t':
210 case '\n':
211 case '\r':
212 // Ignore whitespace.
213 continue;
214 case '+': return LexPositive();
215 case '@': return LexAt();
216 case '$': return LexDollar();
217 case '%': return LexPercent();
218 case '"': return LexQuote();
219 case '.':
220 if (const char *Ptr = isLabelTail(CurPtr)) {
221 CurPtr = Ptr;
222 StrVal.assign(TokStart, CurPtr-1);
223 return lltok::LabelStr;
224 }
225 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
226 CurPtr += 2;
227 return lltok::dotdotdot;
228 }
229 return lltok::Error;
230 case ';':
231 SkipLineComment();
232 continue;
233 case '!': return LexExclaim();
234 case '^':
235 return LexCaret();
236 case ':':
237 return lltok::colon;
238 case '#': return LexHash();
239 case '0': case '1': case '2': case '3': case '4':
240 case '5': case '6': case '7': case '8': case '9':
241 case '-':
242 return LexDigitOrNegative();
243 case '=': return lltok::equal;
244 case '[': return lltok::lsquare;
245 case ']': return lltok::rsquare;
246 case '{': return lltok::lbrace;
247 case '}': return lltok::rbrace;
248 case '<': return lltok::less;
249 case '>': return lltok::greater;
250 case '(': return lltok::lparen;
251 case ')': return lltok::rparen;
252 case ',': return lltok::comma;
253 case '*': return lltok::star;
254 case '|': return lltok::bar;
255 case '/':
256 if (getNextChar() != '*')
257 return lltok::Error;
258 if (SkipCComment())
259 return lltok::Error;
260 continue;
261 }
262 }
263}
264
265void LLLexer::SkipLineComment() {
266 while (true) {
267 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
268 return;
269 }
270}
271
272/// This skips C-style /**/ comments. Returns true if there
273/// was an error.
274bool LLLexer::SkipCComment() {
275 while (true) {
276 int CurChar = getNextChar();
277 switch (CurChar) {
278 case EOF:
279 LexError("unterminated comment");
280 return true;
281 case '*':
282 // End of the comment?
283 CurChar = getNextChar();
284 if (CurChar == '/')
285 return false;
286 if (CurChar == EOF) {
287 LexError("unterminated comment");
288 return true;
289 }
290 }
291 }
292}
293
294/// Lex all tokens that start with an @ character.
295/// GlobalVar @\"[^\"]*\"
296/// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
297/// GlobalVarID @[0-9]+
298lltok::Kind LLLexer::LexAt() {
299 return LexVar(lltok::GlobalVar, lltok::GlobalID);
300}
301
302lltok::Kind LLLexer::LexDollar() {
303 if (const char *Ptr = isLabelTail(TokStart)) {
304 CurPtr = Ptr;
305 StrVal.assign(TokStart, CurPtr - 1);
306 return lltok::LabelStr;
307 }
308
309 // Handle DollarStringConstant: $\"[^\"]*\"
310 if (CurPtr[0] == '"') {
311 ++CurPtr;
312
313 while (true) {
314 int CurChar = getNextChar();
315
316 if (CurChar == EOF) {
317 LexError("end of file in COMDAT variable name");
318 return lltok::Error;
319 }
320 if (CurChar == '"') {
321 StrVal.assign(TokStart + 2, CurPtr - 1);
322 UnEscapeLexed(StrVal);
323 if (StringRef(StrVal).contains(0)) {
324 LexError("NUL character is not allowed in names");
325 return lltok::Error;
326 }
327 return lltok::ComdatVar;
328 }
329 }
330 }
331
332 // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
333 if (ReadVarName())
334 return lltok::ComdatVar;
335
336 return lltok::Error;
337}
338
339/// ReadString - Read a string until the closing quote.
340lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
341 const char *Start = CurPtr;
342 while (true) {
343 int CurChar = getNextChar();
344
345 if (CurChar == EOF) {
346 LexError("end of file in string constant");
347 return lltok::Error;
348 }
349 if (CurChar == '"') {
350 StrVal.assign(Start, CurPtr-1);
351 UnEscapeLexed(StrVal);
352 return kind;
353 }
354 }
355}
356
357/// ReadVarName - Read the rest of a token containing a variable name.
358bool LLLexer::ReadVarName() {
359 const char *NameStart = CurPtr;
360 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
361 CurPtr[0] == '-' || CurPtr[0] == '$' ||
362 CurPtr[0] == '.' || CurPtr[0] == '_') {
363 ++CurPtr;
364 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
365 CurPtr[0] == '-' || CurPtr[0] == '$' ||
366 CurPtr[0] == '.' || CurPtr[0] == '_')
367 ++CurPtr;
368
369 StrVal.assign(NameStart, CurPtr);
370 return true;
371 }
372 return false;
373}
374
375// Lex an ID: [0-9]+. On success, the ID is stored in UIntVal and Token is
376// returned, otherwise the Error token is returned.
377lltok::Kind LLLexer::LexUIntID(lltok::Kind Token) {
378 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
379 return lltok::Error;
380
381 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
382 /*empty*/;
383
384 uint64_t Val = atoull(TokStart + 1, CurPtr);
385 if ((unsigned)Val != Val)
386 LexError("invalid value number (too large)");
387 UIntVal = unsigned(Val);
388 return Token;
389}
390
391lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) {
392 // Handle StringConstant: \"[^\"]*\"
393 if (CurPtr[0] == '"') {
394 ++CurPtr;
395
396 while (true) {
397 int CurChar = getNextChar();
398
399 if (CurChar == EOF) {
400 LexError("end of file in global variable name");
401 return lltok::Error;
402 }
403 if (CurChar == '"') {
404 StrVal.assign(TokStart+2, CurPtr-1);
405 UnEscapeLexed(StrVal);
406 if (StringRef(StrVal).contains(0)) {
407 LexError("NUL character is not allowed in names");
408 return lltok::Error;
409 }
410 return Var;
411 }
412 }
413 }
414
415 // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]*
416 if (ReadVarName())
417 return Var;
418
419 // Handle VarID: [0-9]+
420 return LexUIntID(VarID);
421}
422
423/// Lex all tokens that start with a % character.
424/// LocalVar ::= %\"[^\"]*\"
425/// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
426/// LocalVarID ::= %[0-9]+
427lltok::Kind LLLexer::LexPercent() {
428 return LexVar(lltok::LocalVar, lltok::LocalVarID);
429}
430
431/// Lex all tokens that start with a " character.
432/// QuoteLabel "[^"]+":
433/// StringConstant "[^"]*"
434lltok::Kind LLLexer::LexQuote() {
435 lltok::Kind kind = ReadString(lltok::StringConstant);
436 if (kind == lltok::Error || kind == lltok::Eof)
437 return kind;
438
439 if (CurPtr[0] == ':') {
440 ++CurPtr;
441 if (StringRef(StrVal).contains(0)) {
442 LexError("NUL character is not allowed in names");
443 kind = lltok::Error;
444 } else {
445 kind = lltok::LabelStr;
446 }
447 }
448
449 return kind;
450}
451
452/// Lex all tokens that start with a ! character.
453/// !foo
454/// !
455lltok::Kind LLLexer::LexExclaim() {
456 // Lex a metadata name as a MetadataVar.
457 if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
458 CurPtr[0] == '-' || CurPtr[0] == '$' ||
459 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
460 ++CurPtr;
461 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
462 CurPtr[0] == '-' || CurPtr[0] == '$' ||
463 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
464 ++CurPtr;
465
466 StrVal.assign(TokStart+1, CurPtr); // Skip !
467 UnEscapeLexed(StrVal);
468 return lltok::MetadataVar;
469 }
470 return lltok::exclaim;
471}
472
473/// Lex all tokens that start with a ^ character.
474/// SummaryID ::= ^[0-9]+
475lltok::Kind LLLexer::LexCaret() {
476 // Handle SummaryID: ^[0-9]+
477 return LexUIntID(lltok::SummaryID);
478}
479
480/// Lex all tokens that start with a # character.
481/// AttrGrpID ::= #[0-9]+
482/// Hash ::= #
483lltok::Kind LLLexer::LexHash() {
484 // Handle AttrGrpID: #[0-9]+
485 if (isdigit(static_cast<unsigned char>(CurPtr[0])))
486 return LexUIntID(lltok::AttrGrpID);
487 return lltok::hash;
488}
489
490/// Lex a label, integer or byte types, keyword, or hexadecimal integer
491/// constant.
492/// Label [-a-zA-Z$._0-9]+:
493/// ByteType b[0-9]+
494/// IntegerType i[0-9]+
495/// Keyword sdiv, float, ...
496/// HexIntConstant [us]0x[0-9A-Fa-f]+
497/// HexFloatConstant f0x[0-9A-Fa-f]+
498lltok::Kind LLLexer::LexIdentifier() {
499 const char *StartChar = CurPtr;
500 const char IntOrByteIdentifier = CurPtr[-1];
501 const char *IntOrByteEnd =
502 (IntOrByteIdentifier == 'i' || IntOrByteIdentifier == 'b') ? nullptr
503 : StartChar;
504 const char *KeywordEnd = nullptr;
505
506 for (; isLabelChar(*CurPtr); ++CurPtr) {
507 // If we decide this is a byte or an integer, remember the end of the
508 // sequence.
509 if (!IntOrByteEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
510 IntOrByteEnd = CurPtr;
511 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
512 *CurPtr != '_')
513 KeywordEnd = CurPtr;
514 }
515
516 // If we stopped due to a colon, unless we were directed to ignore it,
517 // this really is a label.
518 if (!IgnoreColonInIdentifiers && *CurPtr == ':') {
519 StrVal.assign(StartChar-1, CurPtr++);
520 return lltok::LabelStr;
521 }
522
523 // Otherwise, this wasn't a label. If this was valid as a byte or an integer
524 // type, return it.
525 if (!IntOrByteEnd)
526 IntOrByteEnd = CurPtr;
527 if (IntOrByteEnd != StartChar) {
528 CurPtr = IntOrByteEnd;
529 uint64_t NumBits = atoull(StartChar, CurPtr);
530 if (NumBits < IntegerType::MIN_INT_BITS ||
531 NumBits > IntegerType::MAX_INT_BITS) {
532 LexError("bitwidth for integer or byte type out of range");
533 return lltok::Error;
534 }
535 if (IntOrByteIdentifier == 'i')
536 TyVal = IntegerType::get(Context, NumBits);
537 else
538 TyVal = ByteType::get(Context, NumBits);
539
540 return lltok::Type;
541 }
542
543 // Otherwise, this was a letter sequence. See which keyword this is.
544 if (!KeywordEnd) KeywordEnd = CurPtr;
545 CurPtr = KeywordEnd;
546 --StartChar;
547 StringRef Keyword(StartChar, CurPtr - StartChar);
548
549#define KEYWORD(STR) \
550 do { \
551 if (Keyword == #STR) \
552 return lltok::kw_##STR; \
553 } while (false)
554
555 KEYWORD(true); KEYWORD(false);
556 KEYWORD(declare); KEYWORD(define);
557 KEYWORD(global); KEYWORD(constant);
558 KEYWORD(br);
559
560 KEYWORD(dso_local);
561 KEYWORD(dso_preemptable);
562
563 KEYWORD(private);
564 KEYWORD(internal);
565 KEYWORD(available_externally);
566 KEYWORD(linkonce);
567 KEYWORD(linkonce_odr);
568 KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
569 KEYWORD(weak_odr);
570 KEYWORD(appending);
571 KEYWORD(dllimport);
572 KEYWORD(dllexport);
573 KEYWORD(common);
574 KEYWORD(default);
575 KEYWORD(hidden);
576 KEYWORD(protected);
577 KEYWORD(unnamed_addr);
578 KEYWORD(local_unnamed_addr);
579 KEYWORD(externally_initialized);
580 KEYWORD(extern_weak);
581 KEYWORD(external);
582 KEYWORD(thread_local);
583 KEYWORD(localdynamic);
584 KEYWORD(initialexec);
585 KEYWORD(localexec);
586 KEYWORD(zeroinitializer);
587 KEYWORD(undef);
588 KEYWORD(null);
589 KEYWORD(none);
590 KEYWORD(poison);
591 KEYWORD(to);
592 KEYWORD(caller);
593 KEYWORD(within);
594 KEYWORD(from);
595 KEYWORD(tail);
596 KEYWORD(musttail);
597 KEYWORD(notail);
598 KEYWORD(target);
599 KEYWORD(triple);
600 KEYWORD(source_filename);
601 KEYWORD(unwind);
602 KEYWORD(datalayout);
603 KEYWORD(volatile);
604 KEYWORD(elementwise);
605 KEYWORD(atomic);
606 KEYWORD(unordered);
607 KEYWORD(monotonic);
612 KEYWORD(syncscope);
613
614 KEYWORD(nnan);
615 KEYWORD(ninf);
616 KEYWORD(nsz);
617 KEYWORD(arcp);
619 KEYWORD(reassoc);
620 KEYWORD(afn);
621 KEYWORD(fast);
622 KEYWORD(nuw);
623 KEYWORD(nsw);
624 KEYWORD(nusw);
625 KEYWORD(exact);
626 KEYWORD(disjoint);
627 KEYWORD(inbounds);
628 KEYWORD(nneg);
629 KEYWORD(samesign);
630 KEYWORD(inrange);
631 KEYWORD(addrspace);
632 KEYWORD(section);
634 KEYWORD(code_model);
635 KEYWORD(alias);
636 KEYWORD(ifunc);
637 KEYWORD(module);
638 KEYWORD(asm);
639 KEYWORD(sideeffect);
640 KEYWORD(inteldialect);
641 KEYWORD(gc);
642 KEYWORD(prefix);
643 KEYWORD(prologue);
644 KEYWORD(prefalign);
645
646 KEYWORD(no_sanitize_address);
647 KEYWORD(no_sanitize_hwaddress);
648 KEYWORD(sanitize_address_dyninit);
649
650 KEYWORD(ccc);
651 KEYWORD(fastcc);
652 KEYWORD(coldcc);
653 KEYWORD(cfguard_checkcc);
654 KEYWORD(x86_stdcallcc);
655 KEYWORD(x86_fastcallcc);
656 KEYWORD(x86_thiscallcc);
657 KEYWORD(x86_vectorcallcc);
658 KEYWORD(arm_apcscc);
659 KEYWORD(arm_aapcscc);
660 KEYWORD(arm_aapcs_vfpcc);
661 KEYWORD(aarch64_vector_pcs);
662 KEYWORD(aarch64_sve_vector_pcs);
663 KEYWORD(aarch64_sme_preservemost_from_x0);
664 KEYWORD(aarch64_sme_preservemost_from_x1);
665 KEYWORD(aarch64_sme_preservemost_from_x2);
666 KEYWORD(msp430_intrcc);
667 KEYWORD(avr_intrcc);
668 KEYWORD(avr_signalcc);
669 KEYWORD(ptx_kernel);
670 KEYWORD(ptx_device);
671 KEYWORD(spir_kernel);
672 KEYWORD(spir_func);
673 KEYWORD(intel_ocl_bicc);
674 KEYWORD(x86_64_sysvcc);
675 KEYWORD(win64cc);
676 KEYWORD(x86_regcallcc);
677 KEYWORD(swiftcc);
678 KEYWORD(swifttailcc);
679 KEYWORD(anyregcc);
680 KEYWORD(preserve_mostcc);
681 KEYWORD(preserve_allcc);
682 KEYWORD(preserve_nonecc);
683 KEYWORD(ghccc);
684 KEYWORD(x86_intrcc);
685 KEYWORD(hhvmcc);
686 KEYWORD(hhvm_ccc);
687 KEYWORD(cxx_fast_tlscc);
688 KEYWORD(amdgpu_vs);
689 KEYWORD(amdgpu_ls);
690 KEYWORD(amdgpu_hs);
691 KEYWORD(amdgpu_es);
692 KEYWORD(amdgpu_gs);
693 KEYWORD(amdgpu_ps);
694 KEYWORD(amdgpu_cs);
695 KEYWORD(amdgpu_cs_chain);
696 KEYWORD(amdgpu_cs_chain_preserve);
697 KEYWORD(amdgpu_kernel);
698 KEYWORD(amdgpu_gfx);
699 KEYWORD(amdgpu_gfx_whole_wave);
700 KEYWORD(tailcc);
701 KEYWORD(m68k_rtdcc);
702 KEYWORD(wasm_emscripten_invokecc);
703 KEYWORD(graalcc);
704 KEYWORD(riscv_vector_cc);
705 KEYWORD(riscv_vls_cc);
706 KEYWORD(cheriot_compartmentcallcc);
707 KEYWORD(cheriot_compartmentcalleecc);
708 KEYWORD(cheriot_librarycallcc);
709
710 KEYWORD(cc);
711 KEYWORD(c);
712
713 KEYWORD(attributes);
714 KEYWORD(sync);
715 KEYWORD(async);
716
717#define GET_ATTR_NAMES
718#define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
719 KEYWORD(DISPLAY_NAME);
720#include "llvm/IR/Attributes.inc"
721
722 KEYWORD(read);
723 KEYWORD(write);
724 KEYWORD(readwrite);
725 KEYWORD(argmem);
726 KEYWORD(target_mem0);
727 KEYWORD(target_mem1);
728 KEYWORD(target_mem);
729 KEYWORD(inaccessiblemem);
730 KEYWORD(errnomem);
731 KEYWORD(argmemonly);
732 KEYWORD(inaccessiblememonly);
733 KEYWORD(inaccessiblemem_or_argmemonly);
734 KEYWORD(nocapture);
735 KEYWORD(address_is_null);
736 KEYWORD(address);
737 KEYWORD(provenance);
738 KEYWORD(read_provenance);
739
740 // denormal_fpenv attribute
741 KEYWORD(ieee);
742 KEYWORD(preservesign);
743 KEYWORD(positivezero);
744 KEYWORD(dynamic);
745
746 // nofpclass attribute
747 KEYWORD(all);
748 KEYWORD(nan);
749 KEYWORD(snan);
750 KEYWORD(qnan);
751 KEYWORD(inf);
752 // ninf already a keyword
753 KEYWORD(pinf);
754 KEYWORD(norm);
755 KEYWORD(nnorm);
756 KEYWORD(pnorm);
757 // sub already a keyword
758 KEYWORD(nsub);
759 KEYWORD(psub);
760 KEYWORD(zero);
761 KEYWORD(nzero);
762 KEYWORD(pzero);
763
764 KEYWORD(type);
765 KEYWORD(opaque);
766
767 KEYWORD(comdat);
768
769 // Comdat types
770 KEYWORD(any);
771 KEYWORD(exactmatch);
772 KEYWORD(largest);
773 KEYWORD(nodeduplicate);
774 KEYWORD(samesize);
775
776 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
777 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
778 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
779 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
780
781 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
782 KEYWORD(umin); KEYWORD(fmax); KEYWORD(fmin);
783 KEYWORD(fmaximum);
784 KEYWORD(fminimum);
785 KEYWORD(fmaximumnum);
786 KEYWORD(fminimumnum);
787 KEYWORD(uinc_wrap);
788 KEYWORD(udec_wrap);
789 KEYWORD(usub_cond);
790 KEYWORD(usub_sat);
791
792 KEYWORD(splat);
793 KEYWORD(vscale);
794 KEYWORD(x);
795 KEYWORD(blockaddress);
796 KEYWORD(dso_local_equivalent);
797 KEYWORD(no_cfi);
798 KEYWORD(ptrauth);
799
800 // Metadata types.
801 KEYWORD(distinct);
802
803 // Use-list order directives.
804 KEYWORD(uselistorder);
805
806 KEYWORD(personality);
808 KEYWORD(catch);
809 KEYWORD(filter);
810
811 // Summary index keywords.
812 KEYWORD(path);
813 KEYWORD(hash);
814 KEYWORD(gv);
815 KEYWORD(guid);
816 KEYWORD(name);
817 KEYWORD(summaries);
818 KEYWORD(flags);
819 KEYWORD(blockcount);
820 KEYWORD(linkage);
821 KEYWORD(visibility);
822 KEYWORD(notEligibleToImport);
823 KEYWORD(live);
824 KEYWORD(dsoLocal);
825 KEYWORD(canAutoHide);
826 KEYWORD(importType);
827 KEYWORD(definition);
828 KEYWORD(declaration);
829 KEYWORD(noRenameOnPromotion);
831 KEYWORD(insts);
832 KEYWORD(funcFlags);
833 KEYWORD(readNone);
834 KEYWORD(readOnly);
835 KEYWORD(noRecurse);
836 KEYWORD(returnDoesNotAlias);
837 KEYWORD(noInline);
838 KEYWORD(alwaysInline);
839 KEYWORD(noUnwind);
840 KEYWORD(mayThrow);
841 KEYWORD(hasUnknownCall);
842 KEYWORD(mustBeUnreachable);
843 KEYWORD(calls);
844 KEYWORD(callee);
845 KEYWORD(params);
846 KEYWORD(param);
847 KEYWORD(hotness);
848 KEYWORD(unknown);
849 KEYWORD(critical);
850 // Deprecated, keep in order to support old files.
851 KEYWORD(relbf);
852 KEYWORD(indirect);
853 KEYWORD(variable);
854 KEYWORD(vTableFuncs);
855 KEYWORD(virtFunc);
856 KEYWORD(aliasee);
857 KEYWORD(refs);
858 KEYWORD(typeIdInfo);
859 KEYWORD(typeTests);
860 KEYWORD(typeTestAssumeVCalls);
861 KEYWORD(typeCheckedLoadVCalls);
862 KEYWORD(typeTestAssumeConstVCalls);
863 KEYWORD(typeCheckedLoadConstVCalls);
864 KEYWORD(vFuncId);
865 KEYWORD(offset);
866 KEYWORD(args);
867 KEYWORD(typeid);
868 KEYWORD(typeidCompatibleVTable);
869 KEYWORD(summary);
870 KEYWORD(typeTestRes);
871 KEYWORD(kind);
872 KEYWORD(unsat);
873 KEYWORD(byteArray);
874 KEYWORD(inline);
875 KEYWORD(single);
877 KEYWORD(sizeM1BitWidth);
878 KEYWORD(alignLog2);
879 KEYWORD(sizeM1);
880 KEYWORD(bitMask);
881 KEYWORD(inlineBits);
882 KEYWORD(vcall_visibility);
883 KEYWORD(wpdResolutions);
884 KEYWORD(wpdRes);
885 KEYWORD(indir);
886 KEYWORD(singleImpl);
887 KEYWORD(branchFunnel);
888 KEYWORD(singleImplName);
889 KEYWORD(resByArg);
890 KEYWORD(byArg);
891 KEYWORD(uniformRetVal);
892 KEYWORD(uniqueRetVal);
893 KEYWORD(virtualConstProp);
894 KEYWORD(info);
895 KEYWORD(byte);
896 KEYWORD(bit);
897 KEYWORD(varFlags);
898 KEYWORD(callsites);
899 KEYWORD(clones);
900 KEYWORD(stackIds);
901 KEYWORD(allocs);
902 KEYWORD(versions);
903 KEYWORD(memProf);
904 KEYWORD(notcold);
905
906#undef KEYWORD
907
908 // Keywords for types.
909#define TYPEKEYWORD(STR, LLVMTY) \
910 do { \
911 if (Keyword == STR) { \
912 TyVal = LLVMTY; \
913 return lltok::Type; \
914 } \
915 } while (false)
916
917 TYPEKEYWORD("void", Type::getVoidTy(Context));
918 TYPEKEYWORD("half", Type::getHalfTy(Context));
919 TYPEKEYWORD("bfloat", Type::getBFloatTy(Context));
920 TYPEKEYWORD("float", Type::getFloatTy(Context));
921 TYPEKEYWORD("double", Type::getDoubleTy(Context));
922 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
923 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
924 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
925 TYPEKEYWORD("label", Type::getLabelTy(Context));
926 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
927 TYPEKEYWORD("x86_amx", Type::getX86_AMXTy(Context));
928 TYPEKEYWORD("token", Type::getTokenTy(Context));
929 TYPEKEYWORD("ptr", PointerType::getUnqual(Context));
930
931#undef TYPEKEYWORD
932
933 // Keywords for instructions.
934#define INSTKEYWORD(STR, Enum) \
935 do { \
936 if (Keyword == #STR) { \
937 UIntVal = Instruction::Enum; \
938 return lltok::kw_##STR; \
939 } \
940 } while (false)
941
942 INSTKEYWORD(fneg, FNeg);
943
944 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
945 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
946 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
947 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
948 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
949 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
950 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
951 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
952
953 INSTKEYWORD(phi, PHI);
954 INSTKEYWORD(call, Call);
955 INSTKEYWORD(trunc, Trunc);
956 INSTKEYWORD(zext, ZExt);
957 INSTKEYWORD(sext, SExt);
958 INSTKEYWORD(fptrunc, FPTrunc);
959 INSTKEYWORD(fpext, FPExt);
960 INSTKEYWORD(uitofp, UIToFP);
961 INSTKEYWORD(sitofp, SIToFP);
962 INSTKEYWORD(fptoui, FPToUI);
963 INSTKEYWORD(fptosi, FPToSI);
964 INSTKEYWORD(inttoptr, IntToPtr);
965 INSTKEYWORD(ptrtoaddr, PtrToAddr);
966 INSTKEYWORD(ptrtoint, PtrToInt);
967 INSTKEYWORD(bitcast, BitCast);
968 INSTKEYWORD(addrspacecast, AddrSpaceCast);
969 INSTKEYWORD(select, Select);
970 INSTKEYWORD(va_arg, VAArg);
971 INSTKEYWORD(ret, Ret);
972 INSTKEYWORD(switch, Switch);
973 INSTKEYWORD(indirectbr, IndirectBr);
974 INSTKEYWORD(invoke, Invoke);
975 INSTKEYWORD(resume, Resume);
976 INSTKEYWORD(unreachable, Unreachable);
977 INSTKEYWORD(callbr, CallBr);
978
979 INSTKEYWORD(alloca, Alloca);
981 INSTKEYWORD(store, Store);
982 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
983 INSTKEYWORD(atomicrmw, AtomicRMW);
984 INSTKEYWORD(fence, Fence);
985 INSTKEYWORD(getelementptr, GetElementPtr);
986
987 INSTKEYWORD(extractelement, ExtractElement);
988 INSTKEYWORD(insertelement, InsertElement);
989 INSTKEYWORD(shufflevector, ShuffleVector);
990 INSTKEYWORD(extractvalue, ExtractValue);
991 INSTKEYWORD(insertvalue, InsertValue);
992 INSTKEYWORD(landingpad, LandingPad);
993 INSTKEYWORD(cleanupret, CleanupRet);
994 INSTKEYWORD(catchret, CatchRet);
995 INSTKEYWORD(catchswitch, CatchSwitch);
996 INSTKEYWORD(catchpad, CatchPad);
997 INSTKEYWORD(cleanuppad, CleanupPad);
998
999 INSTKEYWORD(freeze, Freeze);
1000 INSTKEYWORD(bitinsert, BitInsert);
1001 INSTKEYWORD(bitextract, BitExtract);
1002
1003#undef INSTKEYWORD
1004
1005#define DWKEYWORD(TYPE, TOKEN) \
1006 do { \
1007 if (Keyword.starts_with("DW_" #TYPE "_")) { \
1008 StrVal.assign(Keyword.begin(), Keyword.end()); \
1009 return lltok::TOKEN; \
1010 } \
1011 } while (false)
1012
1013 DWKEYWORD(TAG, DwarfTag);
1014 DWKEYWORD(ATE, DwarfAttEncoding);
1015 DWKEYWORD(VIRTUALITY, DwarfVirtuality);
1016 DWKEYWORD(LLVM_LANG_DIALECT, DwarfLangDialect);
1017 DWKEYWORD(LANG, DwarfLang);
1018 DWKEYWORD(LNAME, DwarfSourceLangName);
1019 DWKEYWORD(CC, DwarfCC);
1020 DWKEYWORD(OP, DwarfOp);
1021 DWKEYWORD(MACINFO, DwarfMacinfo);
1022 DWKEYWORD(APPLE_ENUM_KIND, DwarfEnumKind);
1023
1024#undef DWKEYWORD
1025
1026// Keywords for debug record types.
1027#define DBGRECORDTYPEKEYWORD(STR) \
1028 do { \
1029 if (Keyword == "dbg_" #STR) { \
1030 StrVal = #STR; \
1031 return lltok::DbgRecordType; \
1032 } \
1033 } while (false)
1034
1035 DBGRECORDTYPEKEYWORD(value);
1036 DBGRECORDTYPEKEYWORD(declare);
1038 DBGRECORDTYPEKEYWORD(label);
1039 DBGRECORDTYPEKEYWORD(declare_value);
1040#undef DBGRECORDTYPEKEYWORD
1041
1042 if (Keyword.starts_with("DIFlag")) {
1043 StrVal.assign(Keyword.begin(), Keyword.end());
1044 return lltok::DIFlag;
1045 }
1046
1047 if (Keyword.starts_with("DISPFlag")) {
1048 StrVal.assign(Keyword.begin(), Keyword.end());
1049 return lltok::DISPFlag;
1050 }
1051
1052 if (Keyword.starts_with("CSK_")) {
1053 StrVal.assign(Keyword.begin(), Keyword.end());
1054 return lltok::ChecksumKind;
1055 }
1056
1057 if (Keyword == "NoDebug" || Keyword == "FullDebug" ||
1058 Keyword == "LineTablesOnly" || Keyword == "DebugDirectivesOnly") {
1059 StrVal.assign(Keyword.begin(), Keyword.end());
1060 return lltok::EmissionKind;
1061 }
1062
1063 if (Keyword == "GNU" || Keyword == "Apple" || Keyword == "None" ||
1064 Keyword == "Default") {
1065 StrVal.assign(Keyword.begin(), Keyword.end());
1066 return lltok::NameTableKind;
1067 }
1068
1069 if (Keyword == "Binary" || Keyword == "Decimal" || Keyword == "Rational") {
1070 StrVal.assign(Keyword.begin(), Keyword.end());
1071 return lltok::FixedPointKind;
1072 }
1073
1074 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
1075 // the CFE to avoid forcing it to deal with 64-bit numbers. Also check for
1076 // f0x[0-9A-Fa-f]+, which is the floating-point hexadecimal literal constant.
1077 if ((TokStart[0] == 'u' || TokStart[0] == 's' || TokStart[0] == 'f') &&
1078 TokStart[1] == '0' && TokStart[2] == 'x' &&
1079 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
1080 bool IsFloatConst = TokStart[0] == 'f';
1081 size_t Len = CurPtr - TokStart - 3;
1082 uint32_t Bits = Len * 4;
1083 StringRef HexStr(TokStart + 3, Len);
1084 if (!all_of(HexStr, isxdigit)) {
1085 // Bad token, return it as an error.
1086 CurPtr = TokStart + 3;
1087 return lltok::Error;
1088 }
1089 APInt Tmp(Bits, HexStr, 16);
1090 uint32_t ActiveBits = Tmp.getActiveBits();
1091 if (!IsFloatConst && ActiveBits > 0 && ActiveBits < Bits)
1092 Tmp = Tmp.trunc(ActiveBits);
1093 APSIntVal = APSInt(Tmp, TokStart[0] != 's');
1094 return IsFloatConst ? lltok::FloatHexLiteral : lltok::APSInt;
1095 }
1096
1097 // If this is "cc1234", return this as just "cc".
1098 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
1099 CurPtr = TokStart+2;
1100 return lltok::kw_cc;
1101 }
1102
1103 // Finally, if this isn't known, return an error.
1104 CurPtr = TokStart+1;
1105 return lltok::Error;
1106}
1107
1108/// Lex all tokens that start with a 0x prefix, knowing they match and are not
1109/// labels.
1110/// HexFPLiteral [-+]?0x[0-9A-Fa-f]+.[0-9A-Fa-f]*[pP][-+]?[0-9]+
1111/// HexFPConstant 0x[0-9A-Fa-f]+
1112/// HexFP80Constant 0xK[0-9A-Fa-f]+
1113/// HexFP128Constant 0xL[0-9A-Fa-f]+
1114/// HexPPC128Constant 0xM[0-9A-Fa-f]+
1115/// HexHalfConstant 0xH[0-9A-Fa-f]+
1116/// HexBFloatConstant 0xR[0-9A-Fa-f]+
1117lltok::Kind LLLexer::Lex0x() {
1118 CurPtr = TokStart + 2;
1119
1120 char Kind;
1121 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H' ||
1122 CurPtr[0] == 'R') {
1123 Kind = *CurPtr++;
1124 } else {
1125 Kind = 'J';
1126 }
1127
1128 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
1129 // Bad token, return it as an error.
1130 CurPtr = TokStart+1;
1131 return lltok::Error;
1132 }
1133
1134 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
1135 ++CurPtr;
1136
1137 if (*CurPtr == '.') {
1138 // HexFPLiteral, following C's %a syntax
1139 return LexFloatStr();
1140 }
1141
1142 if (Kind == 'J') {
1143 // HexFPConstant - Floating point constant represented in IEEE format as a
1144 // hexadecimal number for when exponential notation is not precise enough.
1145 // Half, BFloat, Float, and double only.
1146 APFloatVal = APFloat(APFloat::IEEEdouble(),
1147 APInt(64, HexIntToVal(TokStart + 2, CurPtr)));
1148 return lltok::APFloat;
1149 }
1150
1151 uint64_t Pair[2];
1152 switch (Kind) {
1153 default:
1154 llvm_unreachable("Unknown kind!");
1155 case 'K':
1156 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
1157 FP80HexToIntPair(TokStart + 3, CurPtr, Pair);
1158 APSIntVal = APInt(80, Pair);
1160 case 'L':
1161 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
1162 HexToIntPair(TokStart + 3, CurPtr, Pair);
1163 APSIntVal = APInt(128, Pair);
1165 case 'M':
1166 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
1167 HexToIntPair(TokStart + 3, CurPtr, Pair);
1168 APSIntVal = APInt(128, Pair);
1170 case 'H': {
1171 uint64_t Val = HexIntToVal(TokStart + 3, CurPtr);
1172 if (!llvm::isUInt<16>(Val)) {
1173 LexError("hexadecimal constant too large for half (16-bit)");
1174 return lltok::Error;
1175 }
1176 APSIntVal = APInt(16, Val);
1178 }
1179 case 'R': {
1180 // Brain floating point
1181 uint64_t Val = HexIntToVal(TokStart + 3, CurPtr);
1182 if (!llvm::isUInt<16>(Val)) {
1183 LexError("hexadecimal constant too large for bfloat (16-bit)");
1184 return lltok::Error;
1185 }
1186 APSIntVal = APInt(16, Val);
1188 }
1189 }
1190}
1191
1192/// Lex tokens for a label or a numeric constant, possibly starting with -.
1193/// Label [-a-zA-Z$._0-9]+:
1194/// NInteger -[0-9]+
1195/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1196/// PInteger [0-9]+
1197/// HexFPLiteral [-+]?0x[0-9A-Fa-f]+.[0-9A-Fa-f]*[pP][-+]?[0-9]+
1198/// HexFPConstant 0x[0-9A-Fa-f]+
1199/// HexFP80Constant 0xK[0-9A-Fa-f]+
1200/// HexFP128Constant 0xL[0-9A-Fa-f]+
1201/// HexPPC128Constant 0xM[0-9A-Fa-f]+
1202lltok::Kind LLLexer::LexDigitOrNegative() {
1203 // If the letter after the negative is not a number, this is probably a label.
1204 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
1205 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
1206 // Okay, this is not a number after the -, it's probably a label.
1207 if (const char *End = isLabelTail(CurPtr)) {
1208 StrVal.assign(TokStart, End-1);
1209 CurPtr = End;
1210 return lltok::LabelStr;
1211 }
1212
1213 // It might be a -inf, -nan, etc. Check if it's a float string (which will
1214 // also handle error conditions there).
1215 return LexFloatStr();
1216 }
1217
1218 // At this point, it is either a label, int or fp constant.
1219
1220 // Skip digits, we have at least one.
1221 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1222 /*empty*/;
1223
1224 // Check if this is a fully-numeric label:
1225 if (isdigit(TokStart[0]) && CurPtr[0] == ':') {
1226 uint64_t Val = atoull(TokStart, CurPtr);
1227 ++CurPtr; // Skip the colon.
1228 if ((unsigned)Val != Val)
1229 LexError("invalid value number (too large)");
1230 UIntVal = unsigned(Val);
1231 return lltok::LabelID;
1232 }
1233
1234 // Check to see if this really is a string label, e.g. "-1:".
1235 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
1236 if (const char *End = isLabelTail(CurPtr)) {
1237 StrVal.assign(TokStart, End-1);
1238 CurPtr = End;
1239 return lltok::LabelStr;
1240 }
1241 }
1242
1243 // If the next character is a '.', then it is a fp value, otherwise its
1244 // integer.
1245 if (CurPtr[0] != '.') {
1246 if (TokStart[0] == '0' && TokStart[1] == 'x')
1247 return Lex0x();
1248 if (TokStart[0] == '-' && TokStart[1] == '0' && TokStart[2] == 'x')
1249 return LexFloatStr();
1250
1251 APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart));
1252 return lltok::APSInt;
1253 }
1254
1255 ++CurPtr;
1256
1257 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1258 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1259
1260 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1261 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1262 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1263 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1264 CurPtr += 2;
1265 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1266 }
1267 }
1268
1269 StrVal.assign(TokStart, CurPtr - TokStart);
1270 return lltok::FloatLiteral;
1271}
1272
1273/// Lex a floating point constant starting with +.
1274/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1275/// HexFPLiteral [-+]?0x[0-9A-Fa-f]+.[0-9A-Fa-f]*[pP][-+]?[0-9]+
1276/// HexFPSpecial [-+](inf|qnan|s?nan\‍(0x[0-9A-Fa-f]+\‍))
1277lltok::Kind LLLexer::LexPositive() {
1278 // If it's not numeric, check for special floating-point values.
1279 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
1280 return LexFloatStr();
1281
1282 // Skip digits.
1283 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1284 /*empty*/;
1285
1286 // If the first non-digit is an x, check if it's a hex FP literal. LexFloatStr
1287 // will reanalyze TokStr..CurPtr to make sure that it's 0x and not 413x.
1288 if (CurPtr[0] == 'x')
1289 return LexFloatStr();
1290
1291 // At this point, we need a '.'.
1292 if (CurPtr[0] != '.') {
1293 CurPtr = TokStart + 1;
1294 return lltok::Error;
1295 }
1296
1297 ++CurPtr;
1298
1299 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1300 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1301
1302 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1303 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1304 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1305 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1306 CurPtr += 2;
1307 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1308 }
1309 }
1310
1311 StrVal.assign(TokStart, CurPtr - TokStart);
1312 return lltok::FloatLiteral;
1313}
1314
1315/// Lex all tokens that start with a + or - that could be a float literal.
1316/// HexFPLiteral [-+]?0x[0-9A-Fa-f]+.[0-9A-Fa-f]*[pP][-+]?[0-9]+
1317/// HexFPSpecial [-+](inf|qnan|s?nan\‍(0x[0-9A-Fa-f]+\‍))
1318lltok::Kind LLLexer::LexFloatStr() {
1319 // At the point we enter this function, we may have seen a few characters
1320 // already, but how many differs based on the entry point. Rewind to the
1321 // beginning just in case.
1322 CurPtr = TokStart;
1323
1324 // Check for optional sign.
1325 if (*CurPtr == '-' || *CurPtr == '+')
1326 ++CurPtr;
1327
1328 if (*CurPtr != '0') {
1329 // Check for keywords.
1330 const char *LabelStart = CurPtr;
1331 while (isLabelChar(*CurPtr))
1332 ++CurPtr;
1333 StringRef Label(LabelStart, CurPtr - LabelStart);
1334
1335 // Basic special values.
1336 if (Label == "inf") {
1337 // Copy from the beginning, to include the sign.
1338 StrVal.assign(TokStart, CurPtr - TokStart);
1339 return lltok::FloatLiteral;
1340 }
1341
1342 // APFloat::convertFromString doesn't support qnan, so translate it to a
1343 // nan payload string it does support.
1344 if (Label == "qnan") {
1345 StrVal = *TokStart == '-' ? "-nan(0)" : "nan(0)";
1346 return lltok::FloatLiteral;
1347 }
1348
1349 // NaN with payload.
1350 if ((Label == "nan" || Label == "snan") && *CurPtr == '(') {
1351 const char *Payload = ++CurPtr;
1352 while (*CurPtr && *CurPtr != ')')
1353 ++CurPtr;
1354
1355 // If no close parenthesis, it's a bad token, return it as an error.
1356 if (*CurPtr++ != ')') {
1357 CurPtr = TokStart + 1;
1358 LexError("unclosed nan literal");
1359 return lltok::Error;
1360 }
1361
1362 StringRef PayloadStr(Payload, CurPtr - Payload);
1363 APInt Val;
1364 if (PayloadStr.consume_front("0x") && PayloadStr.getAsInteger(16, Val)) {
1365 StrVal.assign(TokStart, CurPtr - TokStart);
1366 // Drop the leading + from the string, as APFloat::convertFromString
1367 // doesn't support leading + sign.
1368 if (StrVal[0] == '+')
1369 StrVal.erase(0, 1);
1370 return lltok::FloatLiteral;
1371 }
1372 }
1373
1374 // Bad token, return it as an error.
1375 LexError("bad payload format for nan literal");
1376 CurPtr = TokStart + 1;
1377 return lltok::Error;
1378 }
1379 ++CurPtr;
1380
1381 if (*CurPtr++ != 'x') {
1382 // Bad token, return it as an error.
1383 CurPtr = TokStart + 1;
1384 return lltok::Error;
1385 }
1386
1387 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
1388 // Bad token, return it as an error.
1389 CurPtr = TokStart + 1;
1390 return lltok::Error;
1391 }
1392
1393 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
1394 ++CurPtr;
1395
1396 if (*CurPtr != '.') {
1397 // Bad token, return it as an error.
1398 CurPtr = TokStart + 1;
1399 return lltok::Error;
1400 }
1401
1402 ++CurPtr; // Eat the .
1403 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
1404 ++CurPtr;
1405
1406 if (*CurPtr != 'p' && *CurPtr != 'P') {
1407 // Bad token, return it as an error.
1408 CurPtr = TokStart + 1;
1409 return lltok::Error;
1410 }
1411
1412 ++CurPtr;
1413 if (*CurPtr == '+' || *CurPtr == '-')
1414 ++CurPtr;
1415 while (isdigit(static_cast<unsigned char>(CurPtr[0])))
1416 ++CurPtr;
1417
1418 StrVal.assign(TokStart, CurPtr - TokStart);
1419 return lltok::FloatLiteral;
1420}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
AMDGPU Mark last scratch load
AMDGPU Register Bank Select
Rewrite undef for PHI
This file implements a class to represent arbitrary precision integral constant values and operations...
static void cleanup(BlockFrequencyInfoImplBase &BFI)
Clear all memory not needed downstream.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static void zero(T &Obj)
expand ir insts
static void UnEscapeLexed(std::string &Str)
Definition LLLexer.cpp:126
static const char * isLabelTail(const char *CurPtr)
isLabelTail - Return true if this pointer points to a valid end of a label.
Definition LLLexer.cpp:159
#define DBGRECORDTYPEKEYWORD(STR)
static bool isLabelChar(char C)
isLabelChar - Return true for [-a-zA-Z$._0-9].
Definition LLLexer.cpp:153
#define TYPEKEYWORD(STR, LLVMTY)
#define DWKEYWORD(TYPE, TOKEN)
#define INSTKEYWORD(STR, Enum)
#define KEYWORD(STR)
lazy value info
nvptx lower args
objc arc contract
static constexpr auto TAG
RISCVCC::CondCode CC
dot regions Print regions of function to dot true view regions View regions of function(with no function bodies)"
const char * Msg
This file contains some templates that are useful if you are working with the STL at all.
static const char * name
#define OP(OPC)
Definition Instruction.h:46
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:480
This file contains some functions that are useful when dealing with strings.
static uint64_t allOnes(unsigned int Count)
static const fltSemantics & IEEEdouble()
Definition APFloat.h:305
static LLVM_ABI ByteType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing a ByteType.
Definition Type.cpp:368
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:338
@ MIN_INT_BITS
Minimum number of bits that can be specified.
@ MAX_INT_BITS
Maximum number of bits that can be specified.
LLVM_ABI void Warning(LocTy WarningLoc, const Twine &Msg) const
Definition LLLexer.cpp:45
LLVM_ABI LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &, LLVMContext &C)
Definition LLLexer.cpp:171
SMLoc LocTy
Definition LLLexer.h:70
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
static PointerType * getUnqual(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition SourceMgr.h:307
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:34
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
iterator end() const
Definition StringRef.h:116
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static LLVM_ABI Type * getX86_AMXTy(LLVMContext &C)
Definition Type.cpp:283
static LLVM_ABI Type * getMetadataTy(LLVMContext &C)
Definition Type.cpp:278
static LLVM_ABI Type * getTokenTy(LLVMContext &C)
Definition Type.cpp:279
static LLVM_ABI Type * getPPC_FP128Ty(LLVMContext &C)
Definition Type.cpp:282
static LLVM_ABI Type * getFP128Ty(LLVMContext &C)
Definition Type.cpp:281
static LLVM_ABI Type * getLabelTy(LLVMContext &C)
Definition Type.cpp:273
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
Definition Type.cpp:272
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:277
static LLVM_ABI Type * getX86_FP80Ty(LLVMContext &C)
Definition Type.cpp:280
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:276
static LLVM_ABI Type * getBFloatTy(LLVMContext &C)
Definition Type.cpp:275
static LLVM_ABI Type * getHalfTy(LLVMContext &C)
Definition Type.cpp:274
CallInst * Call
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ FloatHexLiteral
Definition LLToken.h:536
@ StringConstant
Definition LLToken.h:513
@ NameTableKind
Definition LLToken.h:522
@ FixedPointKind
Definition LLToken.h:523
This is an optimization pass for GlobalISel generic memory operations.
std::tuple< const DIScope *, const DIScope *, const DILocalVariable * > VarID
A unique key that represents a debug variable.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
unsigned hexDigitValue(char C)
Interpret the given character C as a hexadecimal digit and return its value.
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
constexpr NextUseDistance min(NextUseDistance A, NextUseDistance B)
constexpr std::invoke_result_t< FnT, ArgsT... > invoke(FnT &&Fn, ArgsT &&...Args)
C++20 constexpr invoke.
static void assign(DXContainerYAML::SourceInfo::SectionHeader &Dst, const dxbc::SourceInfo::SectionHeader &Src)
LLVM_ABI FPClassTest fneg(FPClassTest Mask)
Return the test mask which returns true if the value's sign bit is flipped.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
constexpr NextUseDistance max(NextUseDistance A, NextUseDistance B)
@ Or
Bitwise or logical OR of integers.
@ Mul
Product of integers.
@ FSub
Subtraction of floats.
@ Xor
Bitwise or logical XOR of integers.
@ FMul
Product of floats.
@ And
Bitwise or logical AND of integers.
@ Sub
Subtraction of integers.
@ Add
Sum of integers.
@ FAdd
Sum of floats.
auto partition(R &&Range, UnaryPredicate P)
Provide wrappers to std::partition which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:2049
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:267
LLVM_ABI Error write(DWPWriter &Out, ArrayRef< std::string > Inputs, OnCuIndexOverflow OverflowOptValue, Dwarf64StrOffsetsPromotion StrOffsetsOptValue, raw_pwrite_stream *OS=nullptr)
Definition DWP.cpp:746