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
26 using namespace llvm;
27
Error(LocTy ErrorLoc,const Twine & Msg) const28 bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
29 ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
30 return true;
31 }
32
Warning(LocTy WarningLoc,const Twine & Msg) const33 void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const {
34 SM.PrintMessage(WarningLoc, 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 //
atoull(const char * Buffer,const char * End)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;
54 }
55 }
56 return Result;
57 }
58
HexIntToVal(const char * Buffer,const char * End)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);
65
66 if (Result < OldRes) { // Uh, oh, overflow detected!!!
67 Error("constant bigger than 64 bits detected!");
68 return 0;
69 }
70 }
71 return Result;
72 }
73
HexToIntPair(const char * Buffer,const char * End,uint64_t Pair[2])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);
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(*Buffer);
88 }
89 if (Buffer != End)
90 Error("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.
FP80HexToIntPair(const char * Buffer,const char * End,uint64_t Pair[2])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);
102 }
103 Pair[0] = 0;
104 for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
105 Pair[0] *= 16;
106 Pair[0] += hexDigitValue(*Buffer);
107 }
108 if (Buffer != End)
109 Error("constant bigger than 128 bits detected!");
110 }
111
112 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
113 // appropriate character.
UnEscapeLexed(std::string & Str)114 static 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(BIn[1]) * 16 + hexDigitValue(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(BOut-Buffer);
138 }
139
140 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
isLabelChar(char C)141 static 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.
isLabelTail(const char * CurPtr)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;
152 }
153 }
154
155 //===----------------------------------------------------------------------===//
156 // Lexer definition.
157 //===----------------------------------------------------------------------===//
158
LLLexer(StringRef StartBuf,SourceMgr & SM,SMDiagnostic & Err,LLVMContext & C)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();
164 }
165
getNextChar()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.
175
176 // Otherwise, return end of file.
177 --CurPtr; // Another call to lex will return EOF again.
178 return EOF;
179 }
180 }
181
LexToken()182 lltok::Kind LLLexer::LexToken() {
183 while (true) {
184 TokStart = CurPtr;
185
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();
192
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;
212 }
213 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
214 CurPtr += 2;
215 return lltok::dotdotdot;
216 }
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;
243 }
244 }
245 }
246
SkipLineComment()247 void LLLexer::SkipLineComment() {
248 while (true) {
249 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
250 return;
251 }
252 }
253
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]+
LexAt()258 lltok::Kind LLLexer::LexAt() {
259 return LexVar(lltok::GlobalVar, lltok::GlobalID);
260 }
261
LexDollar()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;
267 }
268
269 // Handle DollarStringConstant: $\"[^\"]*\"
270 if (CurPtr[0] == '"') {
271 ++CurPtr;
272
273 while (true) {
274 int CurChar = getNextChar();
275
276 if (CurChar == EOF) {
277 Error("end of file in COMDAT variable name");
278 return lltok::Error;
279 }
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;
286 }
287 return lltok::ComdatVar;
288 }
289 }
290 }
291
292 // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
293 if (ReadVarName())
294 return lltok::ComdatVar;
295
296 return lltok::Error;
297 }
298
299 /// ReadString - Read a string until the closing quote.
ReadString(lltok::Kind kind)300 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
301 const char *Start = CurPtr;
302 while (true) {
303 int CurChar = getNextChar();
304
305 if (CurChar == EOF) {
306 Error("end of file in string constant");
307 return lltok::Error;
308 }
309 if (CurChar == '"') {
310 StrVal.assign(Start, CurPtr-1);
311 UnEscapeLexed(StrVal);
312 return kind;
313 }
314 }
315 }
316
317 /// ReadVarName - Read the rest of a token containing a variable name.
ReadVarName()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;
328
329 StrVal.assign(NameStart, CurPtr);
330 return true;
331 }
332 return false;
333 }
334
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.
LexUIntID(lltok::Kind Token)337 lltok::Kind LLLexer::LexUIntID(lltok::Kind Token) {
338 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
339 return lltok::Error;
340
341 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
342 /*empty*/;
343
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;
349 }
350
LexVar(lltok::Kind Var,lltok::Kind VarID)351 lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) {
352 // Handle StringConstant: \"[^\"]*\"
353 if (CurPtr[0] == '"') {
354 ++CurPtr;
355
356 while (true) {
357 int CurChar = getNextChar();
358
359 if (CurChar == EOF) {
360 Error("end of file in global variable name");
361 return lltok::Error;
362 }
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;
369 }
370 return Var;
371 }
372 }
373 }
374
375 // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]*
376 if (ReadVarName())
377 return Var;
378
379 // Handle VarID: [0-9]+
380 return LexUIntID(VarID);
381 }
382
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]+
LexPercent()387 lltok::Kind LLLexer::LexPercent() {
388 return LexVar(lltok::LocalVar, lltok::LocalVarID);
389 }
390
391 /// Lex all tokens that start with a " character.
392 /// QuoteLabel "[^"]+":
393 /// StringConstant "[^"]*"
LexQuote()394 lltok::Kind LLLexer::LexQuote() {
395 lltok::Kind kind = ReadString(lltok::StringConstant);
396 if (kind == lltok::Error || kind == lltok::Eof)
397 return kind;
398
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;
406 }
407 }
408
409 return kind;
410 }
411
412 /// Lex all tokens that start with a ! character.
413 /// !foo
414 /// !
LexExclaim()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;
425
426 StrVal.assign(TokStart+1, CurPtr); // Skip !
427 UnEscapeLexed(StrVal);
428 return lltok::MetadataVar;
429 }
430 return lltok::exclaim;
431 }
432
433 /// Lex all tokens that start with a ^ character.
434 /// SummaryID ::= ^[0-9]+
LexCaret()435 lltok::Kind LLLexer::LexCaret() {
436 // Handle SummaryID: ^[0-9]+
437 return LexUIntID(lltok::SummaryID);
438 }
439
440 /// Lex all tokens that start with a # character.
441 /// AttrGrpID ::= #[0-9]+
LexHash()442 lltok::Kind LLLexer::LexHash() {
443 // Handle AttrGrpID: #[0-9]+
444 return LexUIntID(lltok::AttrGrpID);
445 }
446
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]+
LexIdentifier()452 lltok::Kind LLLexer::LexIdentifier() {
453 const char *StartChar = CurPtr;
454 const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar;
455 const char *KeywordEnd = nullptr;
456
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;
464 }
465
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;
471 }
472
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;
483 }
484 TyVal = IntegerType::get(Context, NumBits);
485 return lltok::Type;
486 }
487
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);
493
494 #define KEYWORD(STR) \
495 do { \
496 if (Keyword == #STR) \
497 return lltok::kw_##STR; \
498 } while (false)
499
500 KEYWORD(true); KEYWORD(false);
501 KEYWORD(declare); KEYWORD(define);
502 KEYWORD(global); KEYWORD(constant);
503
504 KEYWORD(dso_local);
505 KEYWORD(dso_preemptable);
506
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(deplibs); // FIXME: Remove in 4.0.
547 KEYWORD(datalayout);
548 KEYWORD(volatile);
549 KEYWORD(atomic);
550 KEYWORD(unordered);
551 KEYWORD(monotonic);
552 KEYWORD(acquire);
553 KEYWORD(release);
554 KEYWORD(acq_rel);
555 KEYWORD(seq_cst);
556 KEYWORD(syncscope);
557
558 KEYWORD(nnan);
559 KEYWORD(ninf);
560 KEYWORD(nsz);
561 KEYWORD(arcp);
562 KEYWORD(contract);
563 KEYWORD(reassoc);
564 KEYWORD(afn);
565 KEYWORD(fast);
566 KEYWORD(nuw);
567 KEYWORD(nsw);
568 KEYWORD(exact);
569 KEYWORD(inbounds);
570 KEYWORD(inrange);
571 KEYWORD(align);
572 KEYWORD(addrspace);
573 KEYWORD(section);
574 KEYWORD(partition);
575 KEYWORD(alias);
576 KEYWORD(ifunc);
577 KEYWORD(module);
578 KEYWORD(asm);
579 KEYWORD(sideeffect);
580 KEYWORD(alignstack);
581 KEYWORD(inteldialect);
582 KEYWORD(gc);
583 KEYWORD(prefix);
584 KEYWORD(prologue);
585
586 KEYWORD(ccc);
587 KEYWORD(fastcc);
588 KEYWORD(coldcc);
589 KEYWORD(cfguard_checkcc);
590 KEYWORD(x86_stdcallcc);
591 KEYWORD(x86_fastcallcc);
592 KEYWORD(x86_thiscallcc);
593 KEYWORD(x86_vectorcallcc);
594 KEYWORD(arm_apcscc);
595 KEYWORD(arm_aapcscc);
596 KEYWORD(arm_aapcs_vfpcc);
597 KEYWORD(aarch64_vector_pcs);
598 KEYWORD(aarch64_sve_vector_pcs);
599 KEYWORD(msp430_intrcc);
600 KEYWORD(avr_intrcc);
601 KEYWORD(avr_signalcc);
602 KEYWORD(ptx_kernel);
603 KEYWORD(ptx_device);
604 KEYWORD(spir_kernel);
605 KEYWORD(spir_func);
606 KEYWORD(intel_ocl_bicc);
607 KEYWORD(x86_64_sysvcc);
608 KEYWORD(win64cc);
609 KEYWORD(x86_regcallcc);
610 KEYWORD(webkit_jscc);
611 KEYWORD(swiftcc);
612 KEYWORD(swifttailcc);
613 KEYWORD(anyregcc);
614 KEYWORD(preserve_mostcc);
615 KEYWORD(preserve_allcc);
616 KEYWORD(ghccc);
617 KEYWORD(x86_intrcc);
618 KEYWORD(hhvmcc);
619 KEYWORD(hhvm_ccc);
620 KEYWORD(cxx_fast_tlscc);
621 KEYWORD(amdgpu_vs);
622 KEYWORD(amdgpu_ls);
623 KEYWORD(amdgpu_hs);
624 KEYWORD(amdgpu_es);
625 KEYWORD(amdgpu_gs);
626 KEYWORD(amdgpu_ps);
627 KEYWORD(amdgpu_cs);
628 KEYWORD(amdgpu_kernel);
629 KEYWORD(amdgpu_gfx);
630 KEYWORD(tailcc);
631
632 KEYWORD(cc);
633 KEYWORD(c);
634
635 KEYWORD(attributes);
636
637 KEYWORD(alwaysinline);
638 KEYWORD(allocsize);
639 KEYWORD(argmemonly);
640 KEYWORD(builtin);
641 KEYWORD(byval);
642 KEYWORD(inalloca);
643 KEYWORD(cold);
644 KEYWORD(convergent);
645 KEYWORD(dereferenceable);
646 KEYWORD(dereferenceable_or_null);
647 KEYWORD(inaccessiblememonly);
648 KEYWORD(inaccessiblemem_or_argmemonly);
649 KEYWORD(inlinehint);
650 KEYWORD(inreg);
651 KEYWORD(jumptable);
652 KEYWORD(minsize);
653 KEYWORD(naked);
654 KEYWORD(nest);
655 KEYWORD(noalias);
656 KEYWORD(nobuiltin);
657 KEYWORD(nocallback);
658 KEYWORD(nocapture);
659 KEYWORD(noduplicate);
660 KEYWORD(nofree);
661 KEYWORD(noimplicitfloat);
662 KEYWORD(noinline);
663 KEYWORD(norecurse);
664 KEYWORD(nonlazybind);
665 KEYWORD(nomerge);
666 KEYWORD(nonnull);
667 KEYWORD(noprofile);
668 KEYWORD(noredzone);
669 KEYWORD(noreturn);
670 KEYWORD(nosync);
671 KEYWORD(nocf_check);
672 KEYWORD(noundef);
673 KEYWORD(nounwind);
674 KEYWORD(null_pointer_is_valid);
675 KEYWORD(optforfuzzing);
676 KEYWORD(optnone);
677 KEYWORD(optsize);
678 KEYWORD(preallocated);
679 KEYWORD(readnone);
680 KEYWORD(readonly);
681 KEYWORD(returned);
682 KEYWORD(returns_twice);
683 KEYWORD(signext);
684 KEYWORD(speculatable);
685 KEYWORD(sret);
686 KEYWORD(ssp);
687 KEYWORD(sspreq);
688 KEYWORD(sspstrong);
689 KEYWORD(strictfp);
690 KEYWORD(safestack);
691 KEYWORD(shadowcallstack);
692 KEYWORD(sanitize_address);
693 KEYWORD(sanitize_hwaddress);
694 KEYWORD(sanitize_memtag);
695 KEYWORD(sanitize_thread);
696 KEYWORD(sanitize_memory);
697 KEYWORD(speculative_load_hardening);
698 KEYWORD(swifterror);
699 KEYWORD(swiftself);
700 KEYWORD(swiftasync);
701 KEYWORD(uwtable);
702 KEYWORD(vscale_range);
703 KEYWORD(willreturn);
704 KEYWORD(writeonly);
705 KEYWORD(zeroext);
706 KEYWORD(immarg);
707 KEYWORD(byref);
708 KEYWORD(mustprogress);
709
710 KEYWORD(type);
711 KEYWORD(opaque);
712
713 KEYWORD(comdat);
714
715 // Comdat types
716 KEYWORD(any);
717 KEYWORD(exactmatch);
718 KEYWORD(largest);
719 KEYWORD(noduplicates);
720 KEYWORD(samesize);
721
722 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
723 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
724 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
725 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
726
727 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
728 KEYWORD(umin);
729
730 KEYWORD(vscale);
731 KEYWORD(x);
732 KEYWORD(blockaddress);
733 KEYWORD(dso_local_equivalent);
734
735 // Metadata types.
736 KEYWORD(distinct);
737
738 // Use-list order directives.
739 KEYWORD(uselistorder);
740 KEYWORD(uselistorder_bb);
741
742 KEYWORD(personality);
743 KEYWORD(cleanup);
744 KEYWORD(catch);
745 KEYWORD(filter);
746
747 // Summary index keywords.
748 KEYWORD(path);
749 KEYWORD(hash);
750 KEYWORD(gv);
751 KEYWORD(guid);
752 KEYWORD(name);
753 KEYWORD(summaries);
754 KEYWORD(flags);
755 KEYWORD(blockcount);
756 KEYWORD(linkage);
757 KEYWORD(visibility);
758 KEYWORD(notEligibleToImport);
759 KEYWORD(live);
760 KEYWORD(dsoLocal);
761 KEYWORD(canAutoHide);
762 KEYWORD(function);
763 KEYWORD(insts);
764 KEYWORD(funcFlags);
765 KEYWORD(readNone);
766 KEYWORD(readOnly);
767 KEYWORD(noRecurse);
768 KEYWORD(returnDoesNotAlias);
769 KEYWORD(noInline);
770 KEYWORD(alwaysInline);
771 KEYWORD(calls);
772 KEYWORD(callee);
773 KEYWORD(params);
774 KEYWORD(param);
775 KEYWORD(hotness);
776 KEYWORD(unknown);
777 KEYWORD(hot);
778 KEYWORD(critical);
779 KEYWORD(relbf);
780 KEYWORD(variable);
781 KEYWORD(vTableFuncs);
782 KEYWORD(virtFunc);
783 KEYWORD(aliasee);
784 KEYWORD(refs);
785 KEYWORD(typeIdInfo);
786 KEYWORD(typeTests);
787 KEYWORD(typeTestAssumeVCalls);
788 KEYWORD(typeCheckedLoadVCalls);
789 KEYWORD(typeTestAssumeConstVCalls);
790 KEYWORD(typeCheckedLoadConstVCalls);
791 KEYWORD(vFuncId);
792 KEYWORD(offset);
793 KEYWORD(args);
794 KEYWORD(typeid);
795 KEYWORD(typeidCompatibleVTable);
796 KEYWORD(summary);
797 KEYWORD(typeTestRes);
798 KEYWORD(kind);
799 KEYWORD(unsat);
800 KEYWORD(byteArray);
801 KEYWORD(inline);
802 KEYWORD(single);
803 KEYWORD(allOnes);
804 KEYWORD(sizeM1BitWidth);
805 KEYWORD(alignLog2);
806 KEYWORD(sizeM1);
807 KEYWORD(bitMask);
808 KEYWORD(inlineBits);
809 KEYWORD(vcall_visibility);
810 KEYWORD(wpdResolutions);
811 KEYWORD(wpdRes);
812 KEYWORD(indir);
813 KEYWORD(singleImpl);
814 KEYWORD(branchFunnel);
815 KEYWORD(singleImplName);
816 KEYWORD(resByArg);
817 KEYWORD(byArg);
818 KEYWORD(uniformRetVal);
819 KEYWORD(uniqueRetVal);
820 KEYWORD(virtualConstProp);
821 KEYWORD(info);
822 KEYWORD(byte);
823 KEYWORD(bit);
824 KEYWORD(varFlags);
825
826 #undef KEYWORD
827
828 // Keywords for types.
829 #define TYPEKEYWORD(STR, LLVMTY) \
830 do { \
831 if (Keyword == STR) { \
832 TyVal = LLVMTY; \
833 return lltok::Type; \
834 } \
835 } while (false)
836
837 TYPEKEYWORD("void", Type::getVoidTy(Context));
838 TYPEKEYWORD("half", Type::getHalfTy(Context));
839 TYPEKEYWORD("bfloat", Type::getBFloatTy(Context));
840 TYPEKEYWORD("float", Type::getFloatTy(Context));
841 TYPEKEYWORD("double", Type::getDoubleTy(Context));
842 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
843 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
844 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
845 TYPEKEYWORD("label", Type::getLabelTy(Context));
846 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
847 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
848 TYPEKEYWORD("x86_amx", Type::getX86_AMXTy(Context));
849 TYPEKEYWORD("token", Type::getTokenTy(Context));
850 TYPEKEYWORD("ptr", PointerType::getUnqual(Context));
851
852 #undef TYPEKEYWORD
853
854 // Keywords for instructions.
855 #define INSTKEYWORD(STR, Enum) \
856 do { \
857 if (Keyword == #STR) { \
858 UIntVal = Instruction::Enum; \
859 return lltok::kw_##STR; \
860 } \
861 } while (false)
862
863 INSTKEYWORD(fneg, FNeg);
864
865 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
866 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
867 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
868 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
869 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
870 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
871 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
872 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
873
874 INSTKEYWORD(phi, PHI);
875 INSTKEYWORD(call, Call);
876 INSTKEYWORD(trunc, Trunc);
877 INSTKEYWORD(zext, ZExt);
878 INSTKEYWORD(sext, SExt);
879 INSTKEYWORD(fptrunc, FPTrunc);
880 INSTKEYWORD(fpext, FPExt);
881 INSTKEYWORD(uitofp, UIToFP);
882 INSTKEYWORD(sitofp, SIToFP);
883 INSTKEYWORD(fptoui, FPToUI);
884 INSTKEYWORD(fptosi, FPToSI);
885 INSTKEYWORD(inttoptr, IntToPtr);
886 INSTKEYWORD(ptrtoint, PtrToInt);
887 INSTKEYWORD(bitcast, BitCast);
888 INSTKEYWORD(addrspacecast, AddrSpaceCast);
889 INSTKEYWORD(select, Select);
890 INSTKEYWORD(va_arg, VAArg);
891 INSTKEYWORD(ret, Ret);
892 INSTKEYWORD(br, Br);
893 INSTKEYWORD(switch, Switch);
894 INSTKEYWORD(indirectbr, IndirectBr);
895 INSTKEYWORD(invoke, Invoke);
896 INSTKEYWORD(resume, Resume);
897 INSTKEYWORD(unreachable, Unreachable);
898 INSTKEYWORD(callbr, CallBr);
899
900 INSTKEYWORD(alloca, Alloca);
901 INSTKEYWORD(load, Load);
902 INSTKEYWORD(store, Store);
903 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
904 INSTKEYWORD(atomicrmw, AtomicRMW);
905 INSTKEYWORD(fence, Fence);
906 INSTKEYWORD(getelementptr, GetElementPtr);
907
908 INSTKEYWORD(extractelement, ExtractElement);
909 INSTKEYWORD(insertelement, InsertElement);
910 INSTKEYWORD(shufflevector, ShuffleVector);
911 INSTKEYWORD(extractvalue, ExtractValue);
912 INSTKEYWORD(insertvalue, InsertValue);
913 INSTKEYWORD(landingpad, LandingPad);
914 INSTKEYWORD(cleanupret, CleanupRet);
915 INSTKEYWORD(catchret, CatchRet);
916 INSTKEYWORD(catchswitch, CatchSwitch);
917 INSTKEYWORD(catchpad, CatchPad);
918 INSTKEYWORD(cleanuppad, CleanupPad);
919
920 INSTKEYWORD(freeze, Freeze);
921
922 #undef INSTKEYWORD
923
924 #define DWKEYWORD(TYPE, TOKEN) \
925 do { \
926 if (Keyword.startswith("DW_" #TYPE "_")) { \
927 StrVal.assign(Keyword.begin(), Keyword.end()); \
928 return lltok::TOKEN; \
929 } \
930 } while (false)
931
932 DWKEYWORD(TAG, DwarfTag);
933 DWKEYWORD(ATE, DwarfAttEncoding);
934 DWKEYWORD(VIRTUALITY, DwarfVirtuality);
935 DWKEYWORD(LANG, DwarfLang);
936 DWKEYWORD(CC, DwarfCC);
937 DWKEYWORD(OP, DwarfOp);
938 DWKEYWORD(MACINFO, DwarfMacinfo);
939
940 #undef DWKEYWORD
941
942 if (Keyword.startswith("DIFlag")) {
943 StrVal.assign(Keyword.begin(), Keyword.end());
944 return lltok::DIFlag;
945 }
946
947 if (Keyword.startswith("DISPFlag")) {
948 StrVal.assign(Keyword.begin(), Keyword.end());
949 return lltok::DISPFlag;
950 }
951
952 if (Keyword.startswith("CSK_")) {
953 StrVal.assign(Keyword.begin(), Keyword.end());
954 return lltok::ChecksumKind;
955 }
956
957 if (Keyword == "NoDebug" || Keyword == "FullDebug" ||
958 Keyword == "LineTablesOnly" || Keyword == "DebugDirectivesOnly") {
959 StrVal.assign(Keyword.begin(), Keyword.end());
960 return lltok::EmissionKind;
961 }
962
963 if (Keyword == "GNU" || Keyword == "None" || Keyword == "Default") {
964 StrVal.assign(Keyword.begin(), Keyword.end());
965 return lltok::NameTableKind;
966 }
967
968 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
969 // the CFE to avoid forcing it to deal with 64-bit numbers.
970 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
971 TokStart[1] == '0' && TokStart[2] == 'x' &&
972 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
973 int len = CurPtr-TokStart-3;
974 uint32_t bits = len * 4;
975 StringRef HexStr(TokStart + 3, len);
976 if (!all_of(HexStr, isxdigit)) {
977 // Bad token, return it as an error.
978 CurPtr = TokStart+3;
979 return lltok::Error;
980 }
981 APInt Tmp(bits, HexStr, 16);
982 uint32_t activeBits = Tmp.getActiveBits();
983 if (activeBits > 0 && activeBits < bits)
984 Tmp = Tmp.trunc(activeBits);
985 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
986 return lltok::APSInt;
987 }
988
989 // If this is "cc1234", return this as just "cc".
990 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
991 CurPtr = TokStart+2;
992 return lltok::kw_cc;
993 }
994
995 // Finally, if this isn't known, return an error.
996 CurPtr = TokStart+1;
997 return lltok::Error;
998 }
999
1000 /// Lex all tokens that start with a 0x prefix, knowing they match and are not
1001 /// labels.
1002 /// HexFPConstant 0x[0-9A-Fa-f]+
1003 /// HexFP80Constant 0xK[0-9A-Fa-f]+
1004 /// HexFP128Constant 0xL[0-9A-Fa-f]+
1005 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
1006 /// HexHalfConstant 0xH[0-9A-Fa-f]+
1007 /// HexBFloatConstant 0xR[0-9A-Fa-f]+
Lex0x()1008 lltok::Kind LLLexer::Lex0x() {
1009 CurPtr = TokStart + 2;
1010
1011 char Kind;
1012 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H' ||
1013 CurPtr[0] == 'R') {
1014 Kind = *CurPtr++;
1015 } else {
1016 Kind = 'J';
1017 }
1018
1019 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
1020 // Bad token, return it as an error.
1021 CurPtr = TokStart+1;
1022 return lltok::Error;
1023 }
1024
1025 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
1026 ++CurPtr;
1027
1028 if (Kind == 'J') {
1029 // HexFPConstant - Floating point constant represented in IEEE format as a
1030 // hexadecimal number for when exponential notation is not precise enough.
1031 // Half, BFloat, Float, and double only.
1032 APFloatVal = APFloat(APFloat::IEEEdouble(),
1033 APInt(64, HexIntToVal(TokStart + 2, CurPtr)));
1034 return lltok::APFloat;
1035 }
1036
1037 uint64_t Pair[2];
1038 switch (Kind) {
1039 default: llvm_unreachable("Unknown kind!");
1040 case 'K':
1041 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
1042 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
1043 APFloatVal = APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair));
1044 return lltok::APFloat;
1045 case 'L':
1046 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
1047 HexToIntPair(TokStart+3, CurPtr, Pair);
1048 APFloatVal = APFloat(APFloat::IEEEquad(), APInt(128, Pair));
1049 return lltok::APFloat;
1050 case 'M':
1051 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
1052 HexToIntPair(TokStart+3, CurPtr, Pair);
1053 APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair));
1054 return lltok::APFloat;
1055 case 'H':
1056 APFloatVal = APFloat(APFloat::IEEEhalf(),
1057 APInt(16,HexIntToVal(TokStart+3, CurPtr)));
1058 return lltok::APFloat;
1059 case 'R':
1060 // Brain floating point
1061 APFloatVal = APFloat(APFloat::BFloat(),
1062 APInt(16, HexIntToVal(TokStart + 3, CurPtr)));
1063 return lltok::APFloat;
1064 }
1065 }
1066
1067 /// Lex tokens for a label or a numeric constant, possibly starting with -.
1068 /// Label [-a-zA-Z$._0-9]+:
1069 /// NInteger -[0-9]+
1070 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1071 /// PInteger [0-9]+
1072 /// HexFPConstant 0x[0-9A-Fa-f]+
1073 /// HexFP80Constant 0xK[0-9A-Fa-f]+
1074 /// HexFP128Constant 0xL[0-9A-Fa-f]+
1075 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
LexDigitOrNegative()1076 lltok::Kind LLLexer::LexDigitOrNegative() {
1077 // If the letter after the negative is not a number, this is probably a label.
1078 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
1079 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
1080 // Okay, this is not a number after the -, it's probably a label.
1081 if (const char *End = isLabelTail(CurPtr)) {
1082 StrVal.assign(TokStart, End-1);
1083 CurPtr = End;
1084 return lltok::LabelStr;
1085 }
1086
1087 return lltok::Error;
1088 }
1089
1090 // At this point, it is either a label, int or fp constant.
1091
1092 // Skip digits, we have at least one.
1093 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1094 /*empty*/;
1095
1096 // Check if this is a fully-numeric label:
1097 if (isdigit(TokStart[0]) && CurPtr[0] == ':') {
1098 uint64_t Val = atoull(TokStart, CurPtr);
1099 ++CurPtr; // Skip the colon.
1100 if ((unsigned)Val != Val)
1101 Error("invalid value number (too large)!");
1102 UIntVal = unsigned(Val);
1103 return lltok::LabelID;
1104 }
1105
1106 // Check to see if this really is a string label, e.g. "-1:".
1107 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
1108 if (const char *End = isLabelTail(CurPtr)) {
1109 StrVal.assign(TokStart, End-1);
1110 CurPtr = End;
1111 return lltok::LabelStr;
1112 }
1113 }
1114
1115 // If the next character is a '.', then it is a fp value, otherwise its
1116 // integer.
1117 if (CurPtr[0] != '.') {
1118 if (TokStart[0] == '0' && TokStart[1] == 'x')
1119 return Lex0x();
1120 APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart));
1121 return lltok::APSInt;
1122 }
1123
1124 ++CurPtr;
1125
1126 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1127 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1128
1129 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1130 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1131 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1132 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1133 CurPtr += 2;
1134 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1135 }
1136 }
1137
1138 APFloatVal = APFloat(APFloat::IEEEdouble(),
1139 StringRef(TokStart, CurPtr - TokStart));
1140 return lltok::APFloat;
1141 }
1142
1143 /// Lex a floating point constant starting with +.
1144 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
LexPositive()1145 lltok::Kind LLLexer::LexPositive() {
1146 // If the letter after the negative is a number, this is probably not a
1147 // label.
1148 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
1149 return lltok::Error;
1150
1151 // Skip digits.
1152 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1153 /*empty*/;
1154
1155 // At this point, we need a '.'.
1156 if (CurPtr[0] != '.') {
1157 CurPtr = TokStart+1;
1158 return lltok::Error;
1159 }
1160
1161 ++CurPtr;
1162
1163 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1164 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1165
1166 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1167 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1168 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1169 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1170 CurPtr += 2;
1171 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1172 }
1173 }
1174
1175 APFloatVal = APFloat(APFloat::IEEEdouble(),
1176 StringRef(TokStart, CurPtr - TokStart));
1177 return lltok::APFloat;
1178 }
1179