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
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>
25
26using namespace llvm;
27
28bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
29 ErrorInfo = SM.GetMessage(Loc: ErrorLoc, Kind: SourceMgr::DK_Error, Msg);
30 return true;
31}
32
33void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const {
34 SM.PrintMessage(Loc: WarningLoc, Kind: SourceMgr::DK_Warning, Msg);
35}
36
37//===----------------------------------------------------------------------===//
38// Helper functions.
39//===----------------------------------------------------------------------===//
40
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...
44//
45uint64_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(Msg: "constant bigger than 64 bits detected!");
53 return 0;
54 }
55 }
56 return Result;
57}
58
59uint64_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(C: *Buffer);
65
66 if (Result < OldRes) { // Uh, oh, overflow detected!!!
67 Error(Msg: "constant bigger than 64 bits detected!");
68 return 0;
69 }
70 }
71 return Result;
72}
73
74void 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(C: *Buffer);
82 }
83 }
84 Pair[1] = 0;
85 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
86 Pair[1] *= 16;
87 Pair[1] += hexDigitValue(C: *Buffer);
88 }
89 if (Buffer != End)
90 Error(Msg: "constant bigger than 128 bits detected!");
91}
92
93/// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
94/// { low64, high16 } as usual for an APInt.
95void 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(C: *Buffer);
102 }
103 Pair[0] = 0;
104 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
105 Pair[0] *= 16;
106 Pair[0] += hexDigitValue(C: *Buffer);
107 }
108 if (Buffer != End)
109 Error(Msg: "constant bigger than 128 bits detected!");
110}
111
112// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
113// appropriate character.
114static void UnEscapeLexed(std::string &Str) {
115 if (Str.empty()) return;
116
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(C: BIn[1]) * 16 + hexDigitValue(C: BIn[2]);
128 BIn += 3; // Skip over handled chars
129 ++BOut;
130 } else {
131 *BOut++ = *BIn++;
132 }
133 } else {
134 *BOut++ = *BIn++;
135 }
136 }
137 Str.resize(n: BOut-Buffer);
138}
139
140/// isLabelChar - Return true for [-a-zA-Z$._0-9].
141static bool isLabelChar(char C) {
142 return isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
143 C == '.' || C == '_';
144}
145
146/// isLabelTail - Return true if this pointer points to a valid end of a label.
147static const char *isLabelTail(const char *CurPtr) {
148 while (true) {
149 if (CurPtr[0] == ':') return CurPtr+1;
150 if (!isLabelChar(C: CurPtr[0])) return nullptr;
151 ++CurPtr;
152 }
153}
154
155//===----------------------------------------------------------------------===//
156// Lexer definition.
157//===----------------------------------------------------------------------===//
158
159LLLexer::LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &Err,
160 LLVMContext &C)
161 : CurBuf(StartBuf), ErrorInfo(Err), SM(SM), Context(C) {
162 CurPtr = CurBuf.begin();
163}
164
165int LLLexer::getNextChar() {
166 char CurChar = *CurPtr++;
167 switch (CurChar) {
168 default: return (unsigned char)CurChar;
169 case 0:
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.
174
175 // Otherwise, return end of file.
176 --CurPtr; // Another call to lex will return EOF again.
177 return EOF;
178 }
179}
180
181lltok::Kind LLLexer::LexToken() {
182 while (true) {
183 TokStart = CurPtr;
184
185 int CurChar = getNextChar();
186 switch (CurChar) {
187 default:
188 // Handle letters: [a-zA-Z_]
189 if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
190 return LexIdentifier();
191
192 return lltok::Error;
193 case EOF: return lltok::Eof;
194 case 0:
195 case ' ':
196 case '\t':
197 case '\n':
198 case '\r':
199 // Ignore whitespace.
200 continue;
201 case '+': return LexPositive();
202 case '@': return LexAt();
203 case '$': return LexDollar();
204 case '%': return LexPercent();
205 case '"': return LexQuote();
206 case '.':
207 if (const char *Ptr = isLabelTail(CurPtr)) {
208 CurPtr = Ptr;
209 StrVal.assign(first: TokStart, last: CurPtr-1);
210 return lltok::LabelStr;
211 }
212 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
213 CurPtr += 2;
214 return lltok::dotdotdot;
215 }
216 return lltok::Error;
217 case ';':
218 SkipLineComment();
219 continue;
220 case '!': return LexExclaim();
221 case '^':
222 return LexCaret();
223 case ':':
224 return lltok::colon;
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':
228 case '-':
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;
242 }
243 }
244}
245
246void LLLexer::SkipLineComment() {
247 while (true) {
248 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
249 return;
250 }
251}
252
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]+
257lltok::Kind LLLexer::LexAt() {
258 return LexVar(Var: lltok::GlobalVar, VarID: lltok::GlobalID);
259}
260
261lltok::Kind LLLexer::LexDollar() {
262 if (const char *Ptr = isLabelTail(CurPtr: TokStart)) {
263 CurPtr = Ptr;
264 StrVal.assign(first: TokStart, last: CurPtr - 1);
265 return lltok::LabelStr;
266 }
267
268 // Handle DollarStringConstant: $\"[^\"]*\"
269 if (CurPtr[0] == '"') {
270 ++CurPtr;
271
272 while (true) {
273 int CurChar = getNextChar();
274
275 if (CurChar == EOF) {
276 Error(Msg: "end of file in COMDAT variable name");
277 return lltok::Error;
278 }
279 if (CurChar == '"') {
280 StrVal.assign(first: TokStart + 2, last: CurPtr - 1);
281 UnEscapeLexed(Str&: StrVal);
282 if (StringRef(StrVal).contains(C: 0)) {
283 Error(Msg: "Null bytes are not allowed in names");
284 return lltok::Error;
285 }
286 return lltok::ComdatVar;
287 }
288 }
289 }
290
291 // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
292 if (ReadVarName())
293 return lltok::ComdatVar;
294
295 return lltok::Error;
296}
297
298/// ReadString - Read a string until the closing quote.
299lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
300 const char *Start = CurPtr;
301 while (true) {
302 int CurChar = getNextChar();
303
304 if (CurChar == EOF) {
305 Error(Msg: "end of file in string constant");
306 return lltok::Error;
307 }
308 if (CurChar == '"') {
309 StrVal.assign(first: Start, last: CurPtr-1);
310 UnEscapeLexed(Str&: StrVal);
311 return kind;
312 }
313 }
314}
315
316/// ReadVarName - Read the rest of a token containing a variable name.
317bool 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] == '_') {
322 ++CurPtr;
323 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
324 CurPtr[0] == '-' || CurPtr[0] == '$' ||
325 CurPtr[0] == '.' || CurPtr[0] == '_')
326 ++CurPtr;
327
328 StrVal.assign(first: NameStart, last: CurPtr);
329 return true;
330 }
331 return false;
332}
333
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.
336lltok::Kind LLLexer::LexUIntID(lltok::Kind Token) {
337 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
338 return lltok::Error;
339
340 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
341 /*empty*/;
342
343 uint64_t Val = atoull(Buffer: TokStart + 1, End: CurPtr);
344 if ((unsigned)Val != Val)
345 Error(Msg: "invalid value number (too large)!");
346 UIntVal = unsigned(Val);
347 return Token;
348}
349
350lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) {
351 // Handle StringConstant: \"[^\"]*\"
352 if (CurPtr[0] == '"') {
353 ++CurPtr;
354
355 while (true) {
356 int CurChar = getNextChar();
357
358 if (CurChar == EOF) {
359 Error(Msg: "end of file in global variable name");
360 return lltok::Error;
361 }
362 if (CurChar == '"') {
363 StrVal.assign(first: TokStart+2, last: CurPtr-1);
364 UnEscapeLexed(Str&: StrVal);
365 if (StringRef(StrVal).contains(C: 0)) {
366 Error(Msg: "Null bytes are not allowed in names");
367 return lltok::Error;
368 }
369 return Var;
370 }
371 }
372 }
373
374 // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]*
375 if (ReadVarName())
376 return Var;
377
378 // Handle VarID: [0-9]+
379 return LexUIntID(Token: VarID);
380}
381
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]+
386lltok::Kind LLLexer::LexPercent() {
387 return LexVar(Var: lltok::LocalVar, VarID: lltok::LocalVarID);
388}
389
390/// Lex all tokens that start with a " character.
391/// QuoteLabel "[^"]+":
392/// StringConstant "[^"]*"
393lltok::Kind LLLexer::LexQuote() {
394 lltok::Kind kind = ReadString(kind: lltok::StringConstant);
395 if (kind == lltok::Error || kind == lltok::Eof)
396 return kind;
397
398 if (CurPtr[0] == ':') {
399 ++CurPtr;
400 if (StringRef(StrVal).contains(C: 0)) {
401 Error(Msg: "Null bytes are not allowed in names");
402 kind = lltok::Error;
403 } else {
404 kind = lltok::LabelStr;
405 }
406 }
407
408 return kind;
409}
410
411/// Lex all tokens that start with a ! character.
412/// !foo
413/// !
414lltok::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] == '\\') {
419 ++CurPtr;
420 while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
421 CurPtr[0] == '-' || CurPtr[0] == '$' ||
422 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
423 ++CurPtr;
424
425 StrVal.assign(first: TokStart+1, last: CurPtr); // Skip !
426 UnEscapeLexed(Str&: StrVal);
427 return lltok::MetadataVar;
428 }
429 return lltok::exclaim;
430}
431
432/// Lex all tokens that start with a ^ character.
433/// SummaryID ::= ^[0-9]+
434lltok::Kind LLLexer::LexCaret() {
435 // Handle SummaryID: ^[0-9]+
436 return LexUIntID(Token: lltok::SummaryID);
437}
438
439/// Lex all tokens that start with a # character.
440/// AttrGrpID ::= #[0-9]+
441/// Hash ::= #
442lltok::Kind LLLexer::LexHash() {
443 // Handle AttrGrpID: #[0-9]+
444 if (isdigit(static_cast<unsigned char>(CurPtr[0])))
445 return LexUIntID(Token: lltok::AttrGrpID);
446 return lltok::hash;
447}
448
449/// Lex a label, integer type, keyword, or hexadecimal integer constant.
450/// Label [-a-zA-Z$._0-9]+:
451/// IntegerType i[0-9]+
452/// Keyword sdiv, float, ...
453/// HexIntConstant [us]0x[0-9A-Fa-f]+
454lltok::Kind LLLexer::LexIdentifier() {
455 const char *StartChar = CurPtr;
456 const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar;
457 const char *KeywordEnd = nullptr;
458
459 for (; isLabelChar(C: *CurPtr); ++CurPtr) {
460 // If we decide this is an integer, remember the end of the sequence.
461 if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
462 IntEnd = CurPtr;
463 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
464 *CurPtr != '_')
465 KeywordEnd = CurPtr;
466 }
467
468 // If we stopped due to a colon, unless we were directed to ignore it,
469 // this really is a label.
470 if (!IgnoreColonInIdentifiers && *CurPtr == ':') {
471 StrVal.assign(first: StartChar-1, last: CurPtr++);
472 return lltok::LabelStr;
473 }
474
475 // Otherwise, this wasn't a label. If this was valid as an integer type,
476 // return it.
477 if (!IntEnd) IntEnd = CurPtr;
478 if (IntEnd != StartChar) {
479 CurPtr = IntEnd;
480 uint64_t NumBits = atoull(Buffer: StartChar, End: CurPtr);
481 if (NumBits < IntegerType::MIN_INT_BITS ||
482 NumBits > IntegerType::MAX_INT_BITS) {
483 Error(Msg: "bitwidth for integer type out of range!");
484 return lltok::Error;
485 }
486 TyVal = IntegerType::get(C&: Context, NumBits);
487 return lltok::Type;
488 }
489
490 // Otherwise, this was a letter sequence. See which keyword this is.
491 if (!KeywordEnd) KeywordEnd = CurPtr;
492 CurPtr = KeywordEnd;
493 --StartChar;
494 StringRef Keyword(StartChar, CurPtr - StartChar);
495
496#define KEYWORD(STR) \
497 do { \
498 if (Keyword == #STR) \
499 return lltok::kw_##STR; \
500 } while (false)
501
502 KEYWORD(true); KEYWORD(false);
503 KEYWORD(declare); KEYWORD(define);
504 KEYWORD(global); KEYWORD(constant);
505
506 KEYWORD(dso_local);
507 KEYWORD(dso_preemptable);
508
509 KEYWORD(private);
510 KEYWORD(internal);
511 KEYWORD(available_externally);
512 KEYWORD(linkonce);
513 KEYWORD(linkonce_odr);
514 KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
515 KEYWORD(weak_odr);
516 KEYWORD(appending);
517 KEYWORD(dllimport);
518 KEYWORD(dllexport);
519 KEYWORD(common);
520 KEYWORD(default);
521 KEYWORD(hidden);
522 KEYWORD(protected);
523 KEYWORD(unnamed_addr);
524 KEYWORD(local_unnamed_addr);
525 KEYWORD(externally_initialized);
526 KEYWORD(extern_weak);
527 KEYWORD(external);
528 KEYWORD(thread_local);
529 KEYWORD(localdynamic);
530 KEYWORD(initialexec);
531 KEYWORD(localexec);
532 KEYWORD(zeroinitializer);
533 KEYWORD(undef);
534 KEYWORD(null);
535 KEYWORD(none);
536 KEYWORD(poison);
537 KEYWORD(to);
538 KEYWORD(caller);
539 KEYWORD(within);
540 KEYWORD(from);
541 KEYWORD(tail);
542 KEYWORD(musttail);
543 KEYWORD(notail);
544 KEYWORD(target);
545 KEYWORD(triple);
546 KEYWORD(source_filename);
547 KEYWORD(unwind);
548 KEYWORD(datalayout);
549 KEYWORD(volatile);
550 KEYWORD(atomic);
551 KEYWORD(unordered);
552 KEYWORD(monotonic);
553 KEYWORD(acquire);
554 KEYWORD(release);
555 KEYWORD(acq_rel);
556 KEYWORD(seq_cst);
557 KEYWORD(syncscope);
558
559 KEYWORD(nnan);
560 KEYWORD(ninf);
561 KEYWORD(nsz);
562 KEYWORD(arcp);
563 KEYWORD(contract);
564 KEYWORD(reassoc);
565 KEYWORD(afn);
566 KEYWORD(fast);
567 KEYWORD(nuw);
568 KEYWORD(nsw);
569 KEYWORD(exact);
570 KEYWORD(disjoint);
571 KEYWORD(inbounds);
572 KEYWORD(nneg);
573 KEYWORD(inrange);
574 KEYWORD(addrspace);
575 KEYWORD(section);
576 KEYWORD(partition);
577 KEYWORD(code_model);
578 KEYWORD(alias);
579 KEYWORD(ifunc);
580 KEYWORD(module);
581 KEYWORD(asm);
582 KEYWORD(sideeffect);
583 KEYWORD(inteldialect);
584 KEYWORD(gc);
585 KEYWORD(prefix);
586 KEYWORD(prologue);
587
588 KEYWORD(no_sanitize_address);
589 KEYWORD(no_sanitize_hwaddress);
590 KEYWORD(sanitize_address_dyninit);
591
592 KEYWORD(ccc);
593 KEYWORD(fastcc);
594 KEYWORD(coldcc);
595 KEYWORD(cfguard_checkcc);
596 KEYWORD(x86_stdcallcc);
597 KEYWORD(x86_fastcallcc);
598 KEYWORD(x86_thiscallcc);
599 KEYWORD(x86_vectorcallcc);
600 KEYWORD(arm_apcscc);
601 KEYWORD(arm_aapcscc);
602 KEYWORD(arm_aapcs_vfpcc);
603 KEYWORD(aarch64_vector_pcs);
604 KEYWORD(aarch64_sve_vector_pcs);
605 KEYWORD(aarch64_sme_preservemost_from_x0);
606 KEYWORD(aarch64_sme_preservemost_from_x2);
607 KEYWORD(msp430_intrcc);
608 KEYWORD(avr_intrcc);
609 KEYWORD(avr_signalcc);
610 KEYWORD(ptx_kernel);
611 KEYWORD(ptx_device);
612 KEYWORD(spir_kernel);
613 KEYWORD(spir_func);
614 KEYWORD(intel_ocl_bicc);
615 KEYWORD(x86_64_sysvcc);
616 KEYWORD(win64cc);
617 KEYWORD(x86_regcallcc);
618 KEYWORD(swiftcc);
619 KEYWORD(swifttailcc);
620 KEYWORD(anyregcc);
621 KEYWORD(preserve_mostcc);
622 KEYWORD(preserve_allcc);
623 KEYWORD(preserve_nonecc);
624 KEYWORD(ghccc);
625 KEYWORD(x86_intrcc);
626 KEYWORD(hhvmcc);
627 KEYWORD(hhvm_ccc);
628 KEYWORD(cxx_fast_tlscc);
629 KEYWORD(amdgpu_vs);
630 KEYWORD(amdgpu_ls);
631 KEYWORD(amdgpu_hs);
632 KEYWORD(amdgpu_es);
633 KEYWORD(amdgpu_gs);
634 KEYWORD(amdgpu_ps);
635 KEYWORD(amdgpu_cs);
636 KEYWORD(amdgpu_cs_chain);
637 KEYWORD(amdgpu_cs_chain_preserve);
638 KEYWORD(amdgpu_kernel);
639 KEYWORD(amdgpu_gfx);
640 KEYWORD(tailcc);
641 KEYWORD(m68k_rtdcc);
642 KEYWORD(graalcc);
643 KEYWORD(riscv_vector_cc);
644
645 KEYWORD(cc);
646 KEYWORD(c);
647
648 KEYWORD(attributes);
649 KEYWORD(sync);
650 KEYWORD(async);
651
652#define GET_ATTR_NAMES
653#define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
654 KEYWORD(DISPLAY_NAME);
655#include "llvm/IR/Attributes.inc"
656
657 KEYWORD(read);
658 KEYWORD(write);
659 KEYWORD(readwrite);
660 KEYWORD(argmem);
661 KEYWORD(inaccessiblemem);
662 KEYWORD(argmemonly);
663 KEYWORD(inaccessiblememonly);
664 KEYWORD(inaccessiblemem_or_argmemonly);
665
666 // nofpclass attribute
667 KEYWORD(all);
668 KEYWORD(nan);
669 KEYWORD(snan);
670 KEYWORD(qnan);
671 KEYWORD(inf);
672 // ninf already a keyword
673 KEYWORD(pinf);
674 KEYWORD(norm);
675 KEYWORD(nnorm);
676 KEYWORD(pnorm);
677 // sub already a keyword
678 KEYWORD(nsub);
679 KEYWORD(psub);
680 KEYWORD(zero);
681 KEYWORD(nzero);
682 KEYWORD(pzero);
683
684 KEYWORD(type);
685 KEYWORD(opaque);
686
687 KEYWORD(comdat);
688
689 // Comdat types
690 KEYWORD(any);
691 KEYWORD(exactmatch);
692 KEYWORD(largest);
693 KEYWORD(nodeduplicate);
694 KEYWORD(samesize);
695
696 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
697 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
698 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
699 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
700
701 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
702 KEYWORD(umin); KEYWORD(fmax); KEYWORD(fmin);
703 KEYWORD(uinc_wrap);
704 KEYWORD(udec_wrap);
705
706 KEYWORD(splat);
707 KEYWORD(vscale);
708 KEYWORD(x);
709 KEYWORD(blockaddress);
710 KEYWORD(dso_local_equivalent);
711 KEYWORD(no_cfi);
712
713 // Metadata types.
714 KEYWORD(distinct);
715
716 // Use-list order directives.
717 KEYWORD(uselistorder);
718 KEYWORD(uselistorder_bb);
719
720 KEYWORD(personality);
721 KEYWORD(cleanup);
722 KEYWORD(catch);
723 KEYWORD(filter);
724
725 // Summary index keywords.
726 KEYWORD(path);
727 KEYWORD(hash);
728 KEYWORD(gv);
729 KEYWORD(guid);
730 KEYWORD(name);
731 KEYWORD(summaries);
732 KEYWORD(flags);
733 KEYWORD(blockcount);
734 KEYWORD(linkage);
735 KEYWORD(visibility);
736 KEYWORD(notEligibleToImport);
737 KEYWORD(live);
738 KEYWORD(dsoLocal);
739 KEYWORD(canAutoHide);
740 KEYWORD(importType);
741 KEYWORD(definition);
742 KEYWORD(declaration);
743 KEYWORD(function);
744 KEYWORD(insts);
745 KEYWORD(funcFlags);
746 KEYWORD(readNone);
747 KEYWORD(readOnly);
748 KEYWORD(noRecurse);
749 KEYWORD(returnDoesNotAlias);
750 KEYWORD(noInline);
751 KEYWORD(alwaysInline);
752 KEYWORD(noUnwind);
753 KEYWORD(mayThrow);
754 KEYWORD(hasUnknownCall);
755 KEYWORD(mustBeUnreachable);
756 KEYWORD(calls);
757 KEYWORD(callee);
758 KEYWORD(params);
759 KEYWORD(param);
760 KEYWORD(hotness);
761 KEYWORD(unknown);
762 KEYWORD(critical);
763 KEYWORD(relbf);
764 KEYWORD(variable);
765 KEYWORD(vTableFuncs);
766 KEYWORD(virtFunc);
767 KEYWORD(aliasee);
768 KEYWORD(refs);
769 KEYWORD(typeIdInfo);
770 KEYWORD(typeTests);
771 KEYWORD(typeTestAssumeVCalls);
772 KEYWORD(typeCheckedLoadVCalls);
773 KEYWORD(typeTestAssumeConstVCalls);
774 KEYWORD(typeCheckedLoadConstVCalls);
775 KEYWORD(vFuncId);
776 KEYWORD(offset);
777 KEYWORD(args);
778 KEYWORD(typeid);
779 KEYWORD(typeidCompatibleVTable);
780 KEYWORD(summary);
781 KEYWORD(typeTestRes);
782 KEYWORD(kind);
783 KEYWORD(unsat);
784 KEYWORD(byteArray);
785 KEYWORD(inline);
786 KEYWORD(single);
787 KEYWORD(allOnes);
788 KEYWORD(sizeM1BitWidth);
789 KEYWORD(alignLog2);
790 KEYWORD(sizeM1);
791 KEYWORD(bitMask);
792 KEYWORD(inlineBits);
793 KEYWORD(vcall_visibility);
794 KEYWORD(wpdResolutions);
795 KEYWORD(wpdRes);
796 KEYWORD(indir);
797 KEYWORD(singleImpl);
798 KEYWORD(branchFunnel);
799 KEYWORD(singleImplName);
800 KEYWORD(resByArg);
801 KEYWORD(byArg);
802 KEYWORD(uniformRetVal);
803 KEYWORD(uniqueRetVal);
804 KEYWORD(virtualConstProp);
805 KEYWORD(info);
806 KEYWORD(byte);
807 KEYWORD(bit);
808 KEYWORD(varFlags);
809 KEYWORD(callsites);
810 KEYWORD(clones);
811 KEYWORD(stackIds);
812 KEYWORD(allocs);
813 KEYWORD(versions);
814 KEYWORD(memProf);
815 KEYWORD(notcold);
816
817#undef KEYWORD
818
819 // Keywords for types.
820#define TYPEKEYWORD(STR, LLVMTY) \
821 do { \
822 if (Keyword == STR) { \
823 TyVal = LLVMTY; \
824 return lltok::Type; \
825 } \
826 } while (false)
827
828 TYPEKEYWORD("void", Type::getVoidTy(Context));
829 TYPEKEYWORD("half", Type::getHalfTy(Context));
830 TYPEKEYWORD("bfloat", Type::getBFloatTy(Context));
831 TYPEKEYWORD("float", Type::getFloatTy(Context));
832 TYPEKEYWORD("double", Type::getDoubleTy(Context));
833 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
834 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
835 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
836 TYPEKEYWORD("label", Type::getLabelTy(Context));
837 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
838 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
839 TYPEKEYWORD("x86_amx", Type::getX86_AMXTy(Context));
840 TYPEKEYWORD("token", Type::getTokenTy(Context));
841 TYPEKEYWORD("ptr", PointerType::getUnqual(Context));
842
843#undef TYPEKEYWORD
844
845 // Keywords for instructions.
846#define INSTKEYWORD(STR, Enum) \
847 do { \
848 if (Keyword == #STR) { \
849 UIntVal = Instruction::Enum; \
850 return lltok::kw_##STR; \
851 } \
852 } while (false)
853
854 INSTKEYWORD(fneg, FNeg);
855
856 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
857 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
858 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
859 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
860 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
861 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
862 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
863 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
864
865 INSTKEYWORD(phi, PHI);
866 INSTKEYWORD(call, Call);
867 INSTKEYWORD(trunc, Trunc);
868 INSTKEYWORD(zext, ZExt);
869 INSTKEYWORD(sext, SExt);
870 INSTKEYWORD(fptrunc, FPTrunc);
871 INSTKEYWORD(fpext, FPExt);
872 INSTKEYWORD(uitofp, UIToFP);
873 INSTKEYWORD(sitofp, SIToFP);
874 INSTKEYWORD(fptoui, FPToUI);
875 INSTKEYWORD(fptosi, FPToSI);
876 INSTKEYWORD(inttoptr, IntToPtr);
877 INSTKEYWORD(ptrtoint, PtrToInt);
878 INSTKEYWORD(bitcast, BitCast);
879 INSTKEYWORD(addrspacecast, AddrSpaceCast);
880 INSTKEYWORD(select, Select);
881 INSTKEYWORD(va_arg, VAArg);
882 INSTKEYWORD(ret, Ret);
883 INSTKEYWORD(br, Br);
884 INSTKEYWORD(switch, Switch);
885 INSTKEYWORD(indirectbr, IndirectBr);
886 INSTKEYWORD(invoke, Invoke);
887 INSTKEYWORD(resume, Resume);
888 INSTKEYWORD(unreachable, Unreachable);
889 INSTKEYWORD(callbr, CallBr);
890
891 INSTKEYWORD(alloca, Alloca);
892 INSTKEYWORD(load, Load);
893 INSTKEYWORD(store, Store);
894 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
895 INSTKEYWORD(atomicrmw, AtomicRMW);
896 INSTKEYWORD(fence, Fence);
897 INSTKEYWORD(getelementptr, GetElementPtr);
898
899 INSTKEYWORD(extractelement, ExtractElement);
900 INSTKEYWORD(insertelement, InsertElement);
901 INSTKEYWORD(shufflevector, ShuffleVector);
902 INSTKEYWORD(extractvalue, ExtractValue);
903 INSTKEYWORD(insertvalue, InsertValue);
904 INSTKEYWORD(landingpad, LandingPad);
905 INSTKEYWORD(cleanupret, CleanupRet);
906 INSTKEYWORD(catchret, CatchRet);
907 INSTKEYWORD(catchswitch, CatchSwitch);
908 INSTKEYWORD(catchpad, CatchPad);
909 INSTKEYWORD(cleanuppad, CleanupPad);
910
911 INSTKEYWORD(freeze, Freeze);
912
913#undef INSTKEYWORD
914
915#define DWKEYWORD(TYPE, TOKEN) \
916 do { \
917 if (Keyword.starts_with("DW_" #TYPE "_")) { \
918 StrVal.assign(Keyword.begin(), Keyword.end()); \
919 return lltok::TOKEN; \
920 } \
921 } while (false)
922
923 DWKEYWORD(TAG, DwarfTag);
924 DWKEYWORD(ATE, DwarfAttEncoding);
925 DWKEYWORD(VIRTUALITY, DwarfVirtuality);
926 DWKEYWORD(LANG, DwarfLang);
927 DWKEYWORD(CC, DwarfCC);
928 DWKEYWORD(OP, DwarfOp);
929 DWKEYWORD(MACINFO, DwarfMacinfo);
930
931#undef DWKEYWORD
932
933// Keywords for debug record types.
934#define DBGRECORDTYPEKEYWORD(STR) \
935 do { \
936 if (Keyword == "dbg_" #STR) { \
937 StrVal = #STR; \
938 return lltok::DbgRecordType; \
939 } \
940 } while (false)
941
942 DBGRECORDTYPEKEYWORD(value);
943 DBGRECORDTYPEKEYWORD(declare);
944 DBGRECORDTYPEKEYWORD(assign);
945 DBGRECORDTYPEKEYWORD(label);
946#undef DBGRECORDTYPEKEYWORD
947
948 if (Keyword.starts_with(Prefix: "DIFlag")) {
949 StrVal.assign(Keyword.begin(), Keyword.end());
950 return lltok::DIFlag;
951 }
952
953 if (Keyword.starts_with(Prefix: "DISPFlag")) {
954 StrVal.assign(Keyword.begin(), Keyword.end());
955 return lltok::DISPFlag;
956 }
957
958 if (Keyword.starts_with(Prefix: "CSK_")) {
959 StrVal.assign(Keyword.begin(), Keyword.end());
960 return lltok::ChecksumKind;
961 }
962
963 if (Keyword == "NoDebug" || Keyword == "FullDebug" ||
964 Keyword == "LineTablesOnly" || Keyword == "DebugDirectivesOnly") {
965 StrVal.assign(Keyword.begin(), Keyword.end());
966 return lltok::EmissionKind;
967 }
968
969 if (Keyword == "GNU" || Keyword == "Apple" || Keyword == "None" ||
970 Keyword == "Default") {
971 StrVal.assign(Keyword.begin(), Keyword.end());
972 return lltok::NameTableKind;
973 }
974
975 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
976 // the CFE to avoid forcing it to deal with 64-bit numbers.
977 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
978 TokStart[1] == '0' && TokStart[2] == 'x' &&
979 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
980 int len = CurPtr-TokStart-3;
981 uint32_t bits = len * 4;
982 StringRef HexStr(TokStart + 3, len);
983 if (!all_of(HexStr, isxdigit)) {
984 // Bad token, return it as an error.
985 CurPtr = TokStart+3;
986 return lltok::Error;
987 }
988 APInt Tmp(bits, HexStr, 16);
989 uint32_t activeBits = Tmp.getActiveBits();
990 if (activeBits > 0 && activeBits < bits)
991 Tmp = Tmp.trunc(width: activeBits);
992 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
993 return lltok::APSInt;
994 }
995
996 // If this is "cc1234", return this as just "cc".
997 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
998 CurPtr = TokStart+2;
999 return lltok::kw_cc;
1000 }
1001
1002 // Finally, if this isn't known, return an error.
1003 CurPtr = TokStart+1;
1004 return lltok::Error;
1005}
1006
1007/// Lex all tokens that start with a 0x prefix, knowing they match and are not
1008/// labels.
1009/// HexFPConstant 0x[0-9A-Fa-f]+
1010/// HexFP80Constant 0xK[0-9A-Fa-f]+
1011/// HexFP128Constant 0xL[0-9A-Fa-f]+
1012/// HexPPC128Constant 0xM[0-9A-Fa-f]+
1013/// HexHalfConstant 0xH[0-9A-Fa-f]+
1014/// HexBFloatConstant 0xR[0-9A-Fa-f]+
1015lltok::Kind LLLexer::Lex0x() {
1016 CurPtr = TokStart + 2;
1017
1018 char Kind;
1019 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H' ||
1020 CurPtr[0] == 'R') {
1021 Kind = *CurPtr++;
1022 } else {
1023 Kind = 'J';
1024 }
1025
1026 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
1027 // Bad token, return it as an error.
1028 CurPtr = TokStart+1;
1029 return lltok::Error;
1030 }
1031
1032 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
1033 ++CurPtr;
1034
1035 if (Kind == 'J') {
1036 // HexFPConstant - Floating point constant represented in IEEE format as a
1037 // hexadecimal number for when exponential notation is not precise enough.
1038 // Half, BFloat, Float, and double only.
1039 APFloatVal = APFloat(APFloat::IEEEdouble(),
1040 APInt(64, HexIntToVal(Buffer: TokStart + 2, End: CurPtr)));
1041 return lltok::APFloat;
1042 }
1043
1044 uint64_t Pair[2];
1045 switch (Kind) {
1046 default: llvm_unreachable("Unknown kind!");
1047 case 'K':
1048 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
1049 FP80HexToIntPair(Buffer: TokStart+3, End: CurPtr, Pair);
1050 APFloatVal = APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair));
1051 return lltok::APFloat;
1052 case 'L':
1053 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
1054 HexToIntPair(Buffer: TokStart+3, End: CurPtr, Pair);
1055 APFloatVal = APFloat(APFloat::IEEEquad(), APInt(128, Pair));
1056 return lltok::APFloat;
1057 case 'M':
1058 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
1059 HexToIntPair(Buffer: TokStart+3, End: CurPtr, Pair);
1060 APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair));
1061 return lltok::APFloat;
1062 case 'H':
1063 APFloatVal = APFloat(APFloat::IEEEhalf(),
1064 APInt(16,HexIntToVal(Buffer: TokStart+3, End: CurPtr)));
1065 return lltok::APFloat;
1066 case 'R':
1067 // Brain floating point
1068 APFloatVal = APFloat(APFloat::BFloat(),
1069 APInt(16, HexIntToVal(Buffer: TokStart + 3, End: CurPtr)));
1070 return lltok::APFloat;
1071 }
1072}
1073
1074/// Lex tokens for a label or a numeric constant, possibly starting with -.
1075/// Label [-a-zA-Z$._0-9]+:
1076/// NInteger -[0-9]+
1077/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1078/// PInteger [0-9]+
1079/// HexFPConstant 0x[0-9A-Fa-f]+
1080/// HexFP80Constant 0xK[0-9A-Fa-f]+
1081/// HexFP128Constant 0xL[0-9A-Fa-f]+
1082/// HexPPC128Constant 0xM[0-9A-Fa-f]+
1083lltok::Kind LLLexer::LexDigitOrNegative() {
1084 // If the letter after the negative is not a number, this is probably a label.
1085 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
1086 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
1087 // Okay, this is not a number after the -, it's probably a label.
1088 if (const char *End = isLabelTail(CurPtr)) {
1089 StrVal.assign(first: TokStart, last: End-1);
1090 CurPtr = End;
1091 return lltok::LabelStr;
1092 }
1093
1094 return lltok::Error;
1095 }
1096
1097 // At this point, it is either a label, int or fp constant.
1098
1099 // Skip digits, we have at least one.
1100 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1101 /*empty*/;
1102
1103 // Check if this is a fully-numeric label:
1104 if (isdigit(TokStart[0]) && CurPtr[0] == ':') {
1105 uint64_t Val = atoull(Buffer: TokStart, End: CurPtr);
1106 ++CurPtr; // Skip the colon.
1107 if ((unsigned)Val != Val)
1108 Error(Msg: "invalid value number (too large)!");
1109 UIntVal = unsigned(Val);
1110 return lltok::LabelID;
1111 }
1112
1113 // Check to see if this really is a string label, e.g. "-1:".
1114 if (isLabelChar(C: CurPtr[0]) || CurPtr[0] == ':') {
1115 if (const char *End = isLabelTail(CurPtr)) {
1116 StrVal.assign(first: TokStart, last: End-1);
1117 CurPtr = End;
1118 return lltok::LabelStr;
1119 }
1120 }
1121
1122 // If the next character is a '.', then it is a fp value, otherwise its
1123 // integer.
1124 if (CurPtr[0] != '.') {
1125 if (TokStart[0] == '0' && TokStart[1] == 'x')
1126 return Lex0x();
1127 APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart));
1128 return lltok::APSInt;
1129 }
1130
1131 ++CurPtr;
1132
1133 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1134 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1135
1136 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1137 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1138 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1139 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1140 CurPtr += 2;
1141 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1142 }
1143 }
1144
1145 APFloatVal = APFloat(APFloat::IEEEdouble(),
1146 StringRef(TokStart, CurPtr - TokStart));
1147 return lltok::APFloat;
1148}
1149
1150/// Lex a floating point constant starting with +.
1151/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1152lltok::Kind LLLexer::LexPositive() {
1153 // If the letter after the negative is a number, this is probably not a
1154 // label.
1155 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
1156 return lltok::Error;
1157
1158 // Skip digits.
1159 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1160 /*empty*/;
1161
1162 // At this point, we need a '.'.
1163 if (CurPtr[0] != '.') {
1164 CurPtr = TokStart+1;
1165 return lltok::Error;
1166 }
1167
1168 ++CurPtr;
1169
1170 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1171 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1172
1173 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1174 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1175 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1176 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1177 CurPtr += 2;
1178 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1179 }
1180 }
1181
1182 APFloatVal = APFloat(APFloat::IEEEdouble(),
1183 StringRef(TokStart, CurPtr - TokStart));
1184 return lltok::APFloat;
1185}
1186

source code of llvm/lib/AsmParser/LLLexer.cpp