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
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(Loc: ErrorLoc, Kind: SourceMgr::DK_Error, Msg);
42 ErrorInfo.Priority = Priority;
43}
44
45void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const {
46 SM.PrintMessage(Loc: WarningLoc, Kind: 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(Msg: "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(C: *Buffer);
77
78 if (Result < OldRes) { // overflow detected.
79 LexError(Msg: "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(C: *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(C: *Buffer);
100 }
101 if (Buffer != End)
102 LexError(Msg: "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(C: *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(C: *Buffer);
119 }
120 if (Buffer != End)
121 LexError(Msg: "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(C: BIn[1]) * 16 + hexDigitValue(C: 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(n: 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(C: CurPtr[0])) return nullptr;
163 ++CurPtr;
164 }
165}
166
167//===----------------------------------------------------------------------===//
168// Lexer definition.
169//===----------------------------------------------------------------------===//
170
171LLLexer::LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &Err,
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(first: TokStart, last: 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(Msg: "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(Msg: "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(Var: lltok::GlobalVar, VarID: lltok::GlobalID);
300}
301
302lltok::Kind LLLexer::LexDollar() {
303 if (const char *Ptr = isLabelTail(CurPtr: TokStart)) {
304 CurPtr = Ptr;
305 StrVal.assign(first: TokStart, last: 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(Msg: "end of file in COMDAT variable name");
318 return lltok::Error;
319 }
320 if (CurChar == '"') {
321 StrVal.assign(first: TokStart + 2, last: CurPtr - 1);
322 UnEscapeLexed(Str&: StrVal);
323 if (StringRef(StrVal).contains(C: 0)) {
324 LexError(Msg: "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(Msg: "end of file in string constant");
347 return lltok::Error;
348 }
349 if (CurChar == '"') {
350 StrVal.assign(first: Start, last: CurPtr-1);
351 UnEscapeLexed(Str&: 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(first: NameStart, last: 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(Buffer: TokStart + 1, End: CurPtr);
385 if ((unsigned)Val != Val)
386 LexError(Msg: "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(Msg: "end of file in global variable name");
401 return lltok::Error;
402 }
403 if (CurChar == '"') {
404 StrVal.assign(first: TokStart+2, last: CurPtr-1);
405 UnEscapeLexed(Str&: StrVal);
406 if (StringRef(StrVal).contains(C: 0)) {
407 LexError(Msg: "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(Token: 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(Var: lltok::LocalVar, VarID: 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(kind: 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(C: 0)) {
442 LexError(Msg: "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(first: TokStart+1, last: CurPtr); // Skip !
467 UnEscapeLexed(Str&: 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(Token: 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(Token: lltok::AttrGrpID);
487 return lltok::hash;
488}
489
490/// Lex a label, integer type, keyword, or hexadecimal integer constant.
491/// Label [-a-zA-Z$._0-9]+:
492/// IntegerType i[0-9]+
493/// Keyword sdiv, float, ...
494/// HexIntConstant [us]0x[0-9A-Fa-f]+
495lltok::Kind LLLexer::LexIdentifier() {
496 const char *StartChar = CurPtr;
497 const char *IntEnd = CurPtr[-1] == 'i' ? nullptr : StartChar;
498 const char *KeywordEnd = nullptr;
499
500 for (; isLabelChar(C: *CurPtr); ++CurPtr) {
501 // If we decide this is an integer, remember the end of the sequence.
502 if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
503 IntEnd = CurPtr;
504 if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
505 *CurPtr != '_')
506 KeywordEnd = CurPtr;
507 }
508
509 // If we stopped due to a colon, unless we were directed to ignore it,
510 // this really is a label.
511 if (!IgnoreColonInIdentifiers && *CurPtr == ':') {
512 StrVal.assign(first: StartChar-1, last: CurPtr++);
513 return lltok::LabelStr;
514 }
515
516 // Otherwise, this wasn't a label. If this was valid as an integer type,
517 // return it.
518 if (!IntEnd) IntEnd = CurPtr;
519 if (IntEnd != StartChar) {
520 CurPtr = IntEnd;
521 uint64_t NumBits = atoull(Buffer: StartChar, End: CurPtr);
522 if (NumBits < IntegerType::MIN_INT_BITS ||
523 NumBits > IntegerType::MAX_INT_BITS) {
524 LexError(Msg: "bitwidth for integer type out of range");
525 return lltok::Error;
526 }
527 TyVal = IntegerType::get(C&: Context, NumBits);
528 return lltok::Type;
529 }
530
531 // Otherwise, this was a letter sequence. See which keyword this is.
532 if (!KeywordEnd) KeywordEnd = CurPtr;
533 CurPtr = KeywordEnd;
534 --StartChar;
535 StringRef Keyword(StartChar, CurPtr - StartChar);
536
537#define KEYWORD(STR) \
538 do { \
539 if (Keyword == #STR) \
540 return lltok::kw_##STR; \
541 } while (false)
542
543 KEYWORD(true); KEYWORD(false);
544 KEYWORD(declare); KEYWORD(define);
545 KEYWORD(global); KEYWORD(constant);
546
547 KEYWORD(dso_local);
548 KEYWORD(dso_preemptable);
549
550 KEYWORD(private);
551 KEYWORD(internal);
552 KEYWORD(available_externally);
553 KEYWORD(linkonce);
554 KEYWORD(linkonce_odr);
555 KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
556 KEYWORD(weak_odr);
557 KEYWORD(appending);
558 KEYWORD(dllimport);
559 KEYWORD(dllexport);
560 KEYWORD(common);
561 KEYWORD(default);
562 KEYWORD(hidden);
563 KEYWORD(protected);
564 KEYWORD(unnamed_addr);
565 KEYWORD(local_unnamed_addr);
566 KEYWORD(externally_initialized);
567 KEYWORD(extern_weak);
568 KEYWORD(external);
569 KEYWORD(thread_local);
570 KEYWORD(localdynamic);
571 KEYWORD(initialexec);
572 KEYWORD(localexec);
573 KEYWORD(zeroinitializer);
574 KEYWORD(undef);
575 KEYWORD(null);
576 KEYWORD(none);
577 KEYWORD(poison);
578 KEYWORD(to);
579 KEYWORD(caller);
580 KEYWORD(within);
581 KEYWORD(from);
582 KEYWORD(tail);
583 KEYWORD(musttail);
584 KEYWORD(notail);
585 KEYWORD(target);
586 KEYWORD(triple);
587 KEYWORD(source_filename);
588 KEYWORD(unwind);
589 KEYWORD(datalayout);
590 KEYWORD(volatile);
591 KEYWORD(atomic);
592 KEYWORD(unordered);
593 KEYWORD(monotonic);
594 KEYWORD(acquire);
595 KEYWORD(release);
596 KEYWORD(acq_rel);
597 KEYWORD(seq_cst);
598 KEYWORD(syncscope);
599
600 KEYWORD(nnan);
601 KEYWORD(ninf);
602 KEYWORD(nsz);
603 KEYWORD(arcp);
604 KEYWORD(contract);
605 KEYWORD(reassoc);
606 KEYWORD(afn);
607 KEYWORD(fast);
608 KEYWORD(nuw);
609 KEYWORD(nsw);
610 KEYWORD(nusw);
611 KEYWORD(exact);
612 KEYWORD(disjoint);
613 KEYWORD(inbounds);
614 KEYWORD(nneg);
615 KEYWORD(samesign);
616 KEYWORD(inrange);
617 KEYWORD(addrspace);
618 KEYWORD(section);
619 KEYWORD(partition);
620 KEYWORD(code_model);
621 KEYWORD(alias);
622 KEYWORD(ifunc);
623 KEYWORD(module);
624 KEYWORD(asm);
625 KEYWORD(sideeffect);
626 KEYWORD(inteldialect);
627 KEYWORD(gc);
628 KEYWORD(prefix);
629 KEYWORD(prologue);
630 KEYWORD(prefalign);
631
632 KEYWORD(no_sanitize_address);
633 KEYWORD(no_sanitize_hwaddress);
634 KEYWORD(sanitize_address_dyninit);
635
636 KEYWORD(ccc);
637 KEYWORD(fastcc);
638 KEYWORD(coldcc);
639 KEYWORD(cfguard_checkcc);
640 KEYWORD(x86_stdcallcc);
641 KEYWORD(x86_fastcallcc);
642 KEYWORD(x86_thiscallcc);
643 KEYWORD(x86_vectorcallcc);
644 KEYWORD(arm_apcscc);
645 KEYWORD(arm_aapcscc);
646 KEYWORD(arm_aapcs_vfpcc);
647 KEYWORD(aarch64_vector_pcs);
648 KEYWORD(aarch64_sve_vector_pcs);
649 KEYWORD(aarch64_sme_preservemost_from_x0);
650 KEYWORD(aarch64_sme_preservemost_from_x1);
651 KEYWORD(aarch64_sme_preservemost_from_x2);
652 KEYWORD(msp430_intrcc);
653 KEYWORD(avr_intrcc);
654 KEYWORD(avr_signalcc);
655 KEYWORD(ptx_kernel);
656 KEYWORD(ptx_device);
657 KEYWORD(spir_kernel);
658 KEYWORD(spir_func);
659 KEYWORD(intel_ocl_bicc);
660 KEYWORD(x86_64_sysvcc);
661 KEYWORD(win64cc);
662 KEYWORD(x86_regcallcc);
663 KEYWORD(swiftcc);
664 KEYWORD(swifttailcc);
665 KEYWORD(anyregcc);
666 KEYWORD(preserve_mostcc);
667 KEYWORD(preserve_allcc);
668 KEYWORD(preserve_nonecc);
669 KEYWORD(ghccc);
670 KEYWORD(x86_intrcc);
671 KEYWORD(hhvmcc);
672 KEYWORD(hhvm_ccc);
673 KEYWORD(cxx_fast_tlscc);
674 KEYWORD(amdgpu_vs);
675 KEYWORD(amdgpu_ls);
676 KEYWORD(amdgpu_hs);
677 KEYWORD(amdgpu_es);
678 KEYWORD(amdgpu_gs);
679 KEYWORD(amdgpu_ps);
680 KEYWORD(amdgpu_cs);
681 KEYWORD(amdgpu_cs_chain);
682 KEYWORD(amdgpu_cs_chain_preserve);
683 KEYWORD(amdgpu_kernel);
684 KEYWORD(amdgpu_gfx);
685 KEYWORD(amdgpu_gfx_whole_wave);
686 KEYWORD(tailcc);
687 KEYWORD(m68k_rtdcc);
688 KEYWORD(graalcc);
689 KEYWORD(riscv_vector_cc);
690 KEYWORD(riscv_vls_cc);
691 KEYWORD(cheriot_compartmentcallcc);
692 KEYWORD(cheriot_compartmentcalleecc);
693 KEYWORD(cheriot_librarycallcc);
694
695 KEYWORD(cc);
696 KEYWORD(c);
697
698 KEYWORD(attributes);
699 KEYWORD(sync);
700 KEYWORD(async);
701
702#define GET_ATTR_NAMES
703#define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
704 KEYWORD(DISPLAY_NAME);
705#include "llvm/IR/Attributes.inc"
706
707 KEYWORD(read);
708 KEYWORD(write);
709 KEYWORD(readwrite);
710 KEYWORD(argmem);
711 KEYWORD(target_mem0);
712 KEYWORD(target_mem1);
713 KEYWORD(inaccessiblemem);
714 KEYWORD(errnomem);
715 KEYWORD(argmemonly);
716 KEYWORD(inaccessiblememonly);
717 KEYWORD(inaccessiblemem_or_argmemonly);
718 KEYWORD(nocapture);
719 KEYWORD(address_is_null);
720 KEYWORD(address);
721 KEYWORD(provenance);
722 KEYWORD(read_provenance);
723
724 // denormal_fpenv attribute
725 KEYWORD(ieee);
726 KEYWORD(preservesign);
727 KEYWORD(positivezero);
728 KEYWORD(dynamic);
729
730 // nofpclass attribute
731 KEYWORD(all);
732 KEYWORD(nan);
733 KEYWORD(snan);
734 KEYWORD(qnan);
735 KEYWORD(inf);
736 // ninf already a keyword
737 KEYWORD(pinf);
738 KEYWORD(norm);
739 KEYWORD(nnorm);
740 KEYWORD(pnorm);
741 // sub already a keyword
742 KEYWORD(nsub);
743 KEYWORD(psub);
744 KEYWORD(zero);
745 KEYWORD(nzero);
746 KEYWORD(pzero);
747
748 KEYWORD(type);
749 KEYWORD(opaque);
750
751 KEYWORD(comdat);
752
753 // Comdat types
754 KEYWORD(any);
755 KEYWORD(exactmatch);
756 KEYWORD(largest);
757 KEYWORD(nodeduplicate);
758 KEYWORD(samesize);
759
760 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
761 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
762 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
763 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
764
765 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
766 KEYWORD(umin); KEYWORD(fmax); KEYWORD(fmin);
767 KEYWORD(fmaximum);
768 KEYWORD(fminimum);
769 KEYWORD(uinc_wrap);
770 KEYWORD(udec_wrap);
771 KEYWORD(usub_cond);
772 KEYWORD(usub_sat);
773
774 KEYWORD(splat);
775 KEYWORD(vscale);
776 KEYWORD(x);
777 KEYWORD(blockaddress);
778 KEYWORD(dso_local_equivalent);
779 KEYWORD(no_cfi);
780 KEYWORD(ptrauth);
781
782 // Metadata types.
783 KEYWORD(distinct);
784
785 // Use-list order directives.
786 KEYWORD(uselistorder);
787 KEYWORD(uselistorder_bb);
788
789 KEYWORD(personality);
790 KEYWORD(cleanup);
791 KEYWORD(catch);
792 KEYWORD(filter);
793
794 // Summary index keywords.
795 KEYWORD(path);
796 KEYWORD(hash);
797 KEYWORD(gv);
798 KEYWORD(guid);
799 KEYWORD(name);
800 KEYWORD(summaries);
801 KEYWORD(flags);
802 KEYWORD(blockcount);
803 KEYWORD(linkage);
804 KEYWORD(visibility);
805 KEYWORD(notEligibleToImport);
806 KEYWORD(live);
807 KEYWORD(dsoLocal);
808 KEYWORD(canAutoHide);
809 KEYWORD(importType);
810 KEYWORD(definition);
811 KEYWORD(declaration);
812 KEYWORD(function);
813 KEYWORD(insts);
814 KEYWORD(funcFlags);
815 KEYWORD(readNone);
816 KEYWORD(readOnly);
817 KEYWORD(noRecurse);
818 KEYWORD(returnDoesNotAlias);
819 KEYWORD(noInline);
820 KEYWORD(alwaysInline);
821 KEYWORD(noUnwind);
822 KEYWORD(mayThrow);
823 KEYWORD(hasUnknownCall);
824 KEYWORD(mustBeUnreachable);
825 KEYWORD(calls);
826 KEYWORD(callee);
827 KEYWORD(params);
828 KEYWORD(param);
829 KEYWORD(hotness);
830 KEYWORD(unknown);
831 KEYWORD(critical);
832 // Deprecated, keep in order to support old files.
833 KEYWORD(relbf);
834 KEYWORD(variable);
835 KEYWORD(vTableFuncs);
836 KEYWORD(virtFunc);
837 KEYWORD(aliasee);
838 KEYWORD(refs);
839 KEYWORD(typeIdInfo);
840 KEYWORD(typeTests);
841 KEYWORD(typeTestAssumeVCalls);
842 KEYWORD(typeCheckedLoadVCalls);
843 KEYWORD(typeTestAssumeConstVCalls);
844 KEYWORD(typeCheckedLoadConstVCalls);
845 KEYWORD(vFuncId);
846 KEYWORD(offset);
847 KEYWORD(args);
848 KEYWORD(typeid);
849 KEYWORD(typeidCompatibleVTable);
850 KEYWORD(summary);
851 KEYWORD(typeTestRes);
852 KEYWORD(kind);
853 KEYWORD(unsat);
854 KEYWORD(byteArray);
855 KEYWORD(inline);
856 KEYWORD(single);
857 KEYWORD(allOnes);
858 KEYWORD(sizeM1BitWidth);
859 KEYWORD(alignLog2);
860 KEYWORD(sizeM1);
861 KEYWORD(bitMask);
862 KEYWORD(inlineBits);
863 KEYWORD(vcall_visibility);
864 KEYWORD(wpdResolutions);
865 KEYWORD(wpdRes);
866 KEYWORD(indir);
867 KEYWORD(singleImpl);
868 KEYWORD(branchFunnel);
869 KEYWORD(singleImplName);
870 KEYWORD(resByArg);
871 KEYWORD(byArg);
872 KEYWORD(uniformRetVal);
873 KEYWORD(uniqueRetVal);
874 KEYWORD(virtualConstProp);
875 KEYWORD(info);
876 KEYWORD(byte);
877 KEYWORD(bit);
878 KEYWORD(varFlags);
879 KEYWORD(callsites);
880 KEYWORD(clones);
881 KEYWORD(stackIds);
882 KEYWORD(allocs);
883 KEYWORD(versions);
884 KEYWORD(memProf);
885 KEYWORD(notcold);
886
887#undef KEYWORD
888
889 // Keywords for types.
890#define TYPEKEYWORD(STR, LLVMTY) \
891 do { \
892 if (Keyword == STR) { \
893 TyVal = LLVMTY; \
894 return lltok::Type; \
895 } \
896 } while (false)
897
898 TYPEKEYWORD("void", Type::getVoidTy(Context));
899 TYPEKEYWORD("half", Type::getHalfTy(Context));
900 TYPEKEYWORD("bfloat", Type::getBFloatTy(Context));
901 TYPEKEYWORD("float", Type::getFloatTy(Context));
902 TYPEKEYWORD("double", Type::getDoubleTy(Context));
903 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
904 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
905 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
906 TYPEKEYWORD("label", Type::getLabelTy(Context));
907 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
908 TYPEKEYWORD("x86_amx", Type::getX86_AMXTy(Context));
909 TYPEKEYWORD("token", Type::getTokenTy(Context));
910 TYPEKEYWORD("ptr", PointerType::getUnqual(Context));
911
912#undef TYPEKEYWORD
913
914 // Keywords for instructions.
915#define INSTKEYWORD(STR, Enum) \
916 do { \
917 if (Keyword == #STR) { \
918 UIntVal = Instruction::Enum; \
919 return lltok::kw_##STR; \
920 } \
921 } while (false)
922
923 INSTKEYWORD(fneg, FNeg);
924
925 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
926 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
927 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
928 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
929 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
930 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
931 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
932 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
933
934 INSTKEYWORD(phi, PHI);
935 INSTKEYWORD(call, Call);
936 INSTKEYWORD(trunc, Trunc);
937 INSTKEYWORD(zext, ZExt);
938 INSTKEYWORD(sext, SExt);
939 INSTKEYWORD(fptrunc, FPTrunc);
940 INSTKEYWORD(fpext, FPExt);
941 INSTKEYWORD(uitofp, UIToFP);
942 INSTKEYWORD(sitofp, SIToFP);
943 INSTKEYWORD(fptoui, FPToUI);
944 INSTKEYWORD(fptosi, FPToSI);
945 INSTKEYWORD(inttoptr, IntToPtr);
946 INSTKEYWORD(ptrtoaddr, PtrToAddr);
947 INSTKEYWORD(ptrtoint, PtrToInt);
948 INSTKEYWORD(bitcast, BitCast);
949 INSTKEYWORD(addrspacecast, AddrSpaceCast);
950 INSTKEYWORD(select, Select);
951 INSTKEYWORD(va_arg, VAArg);
952 INSTKEYWORD(ret, Ret);
953 INSTKEYWORD(br, Br);
954 INSTKEYWORD(switch, Switch);
955 INSTKEYWORD(indirectbr, IndirectBr);
956 INSTKEYWORD(invoke, Invoke);
957 INSTKEYWORD(resume, Resume);
958 INSTKEYWORD(unreachable, Unreachable);
959 INSTKEYWORD(callbr, CallBr);
960
961 INSTKEYWORD(alloca, Alloca);
962 INSTKEYWORD(load, Load);
963 INSTKEYWORD(store, Store);
964 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
965 INSTKEYWORD(atomicrmw, AtomicRMW);
966 INSTKEYWORD(fence, Fence);
967 INSTKEYWORD(getelementptr, GetElementPtr);
968
969 INSTKEYWORD(extractelement, ExtractElement);
970 INSTKEYWORD(insertelement, InsertElement);
971 INSTKEYWORD(shufflevector, ShuffleVector);
972 INSTKEYWORD(extractvalue, ExtractValue);
973 INSTKEYWORD(insertvalue, InsertValue);
974 INSTKEYWORD(landingpad, LandingPad);
975 INSTKEYWORD(cleanupret, CleanupRet);
976 INSTKEYWORD(catchret, CatchRet);
977 INSTKEYWORD(catchswitch, CatchSwitch);
978 INSTKEYWORD(catchpad, CatchPad);
979 INSTKEYWORD(cleanuppad, CleanupPad);
980
981 INSTKEYWORD(freeze, Freeze);
982
983#undef INSTKEYWORD
984
985#define DWKEYWORD(TYPE, TOKEN) \
986 do { \
987 if (Keyword.starts_with("DW_" #TYPE "_")) { \
988 StrVal.assign(Keyword.begin(), Keyword.end()); \
989 return lltok::TOKEN; \
990 } \
991 } while (false)
992
993 DWKEYWORD(TAG, DwarfTag);
994 DWKEYWORD(ATE, DwarfAttEncoding);
995 DWKEYWORD(VIRTUALITY, DwarfVirtuality);
996 DWKEYWORD(LANG, DwarfLang);
997 DWKEYWORD(LNAME, DwarfSourceLangName);
998 DWKEYWORD(CC, DwarfCC);
999 DWKEYWORD(OP, DwarfOp);
1000 DWKEYWORD(MACINFO, DwarfMacinfo);
1001 DWKEYWORD(APPLE_ENUM_KIND, DwarfEnumKind);
1002
1003#undef DWKEYWORD
1004
1005// Keywords for debug record types.
1006#define DBGRECORDTYPEKEYWORD(STR) \
1007 do { \
1008 if (Keyword == "dbg_" #STR) { \
1009 StrVal = #STR; \
1010 return lltok::DbgRecordType; \
1011 } \
1012 } while (false)
1013
1014 DBGRECORDTYPEKEYWORD(value);
1015 DBGRECORDTYPEKEYWORD(declare);
1016 DBGRECORDTYPEKEYWORD(assign);
1017 DBGRECORDTYPEKEYWORD(label);
1018 DBGRECORDTYPEKEYWORD(declare_value);
1019#undef DBGRECORDTYPEKEYWORD
1020
1021 if (Keyword.starts_with(Prefix: "DIFlag")) {
1022 StrVal.assign(first: Keyword.begin(), last: Keyword.end());
1023 return lltok::DIFlag;
1024 }
1025
1026 if (Keyword.starts_with(Prefix: "DISPFlag")) {
1027 StrVal.assign(first: Keyword.begin(), last: Keyword.end());
1028 return lltok::DISPFlag;
1029 }
1030
1031 if (Keyword.starts_with(Prefix: "CSK_")) {
1032 StrVal.assign(first: Keyword.begin(), last: Keyword.end());
1033 return lltok::ChecksumKind;
1034 }
1035
1036 if (Keyword == "NoDebug" || Keyword == "FullDebug" ||
1037 Keyword == "LineTablesOnly" || Keyword == "DebugDirectivesOnly") {
1038 StrVal.assign(first: Keyword.begin(), last: Keyword.end());
1039 return lltok::EmissionKind;
1040 }
1041
1042 if (Keyword == "GNU" || Keyword == "Apple" || Keyword == "None" ||
1043 Keyword == "Default") {
1044 StrVal.assign(first: Keyword.begin(), last: Keyword.end());
1045 return lltok::NameTableKind;
1046 }
1047
1048 if (Keyword == "Binary" || Keyword == "Decimal" || Keyword == "Rational") {
1049 StrVal.assign(first: Keyword.begin(), last: Keyword.end());
1050 return lltok::FixedPointKind;
1051 }
1052
1053 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
1054 // the CFE to avoid forcing it to deal with 64-bit numbers.
1055 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
1056 TokStart[1] == '0' && TokStart[2] == 'x' &&
1057 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
1058 int len = CurPtr-TokStart-3;
1059 uint32_t bits = len * 4;
1060 StringRef HexStr(TokStart + 3, len);
1061 if (!all_of(Range&: HexStr, P: isxdigit)) {
1062 // Bad token, return it as an error.
1063 CurPtr = TokStart+3;
1064 return lltok::Error;
1065 }
1066 APInt Tmp(bits, HexStr, 16);
1067 uint32_t activeBits = Tmp.getActiveBits();
1068 if (activeBits > 0 && activeBits < bits)
1069 Tmp = Tmp.trunc(width: activeBits);
1070 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
1071 return lltok::APSInt;
1072 }
1073
1074 // If this is "cc1234", return this as just "cc".
1075 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
1076 CurPtr = TokStart+2;
1077 return lltok::kw_cc;
1078 }
1079
1080 // Finally, if this isn't known, return an error.
1081 CurPtr = TokStart+1;
1082 return lltok::Error;
1083}
1084
1085/// Lex all tokens that start with a 0x prefix, knowing they match and are not
1086/// labels.
1087/// HexFPConstant 0x[0-9A-Fa-f]+
1088/// HexFP80Constant 0xK[0-9A-Fa-f]+
1089/// HexFP128Constant 0xL[0-9A-Fa-f]+
1090/// HexPPC128Constant 0xM[0-9A-Fa-f]+
1091/// HexHalfConstant 0xH[0-9A-Fa-f]+
1092/// HexBFloatConstant 0xR[0-9A-Fa-f]+
1093lltok::Kind LLLexer::Lex0x() {
1094 CurPtr = TokStart + 2;
1095
1096 char Kind;
1097 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H' ||
1098 CurPtr[0] == 'R') {
1099 Kind = *CurPtr++;
1100 } else {
1101 Kind = 'J';
1102 }
1103
1104 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
1105 // Bad token, return it as an error.
1106 CurPtr = TokStart+1;
1107 return lltok::Error;
1108 }
1109
1110 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
1111 ++CurPtr;
1112
1113 if (Kind == 'J') {
1114 // HexFPConstant - Floating point constant represented in IEEE format as a
1115 // hexadecimal number for when exponential notation is not precise enough.
1116 // Half, BFloat, Float, and double only.
1117 APFloatVal = APFloat(APFloat::IEEEdouble(),
1118 APInt(64, HexIntToVal(Buffer: TokStart + 2, End: CurPtr)));
1119 return lltok::APFloat;
1120 }
1121
1122 uint64_t Pair[2];
1123 switch (Kind) {
1124 default: llvm_unreachable("Unknown kind!");
1125 case 'K':
1126 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
1127 FP80HexToIntPair(Buffer: TokStart+3, End: CurPtr, Pair);
1128 APFloatVal = APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair));
1129 return lltok::APFloat;
1130 case 'L':
1131 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
1132 HexToIntPair(Buffer: TokStart+3, End: CurPtr, Pair);
1133 APFloatVal = APFloat(APFloat::IEEEquad(), APInt(128, Pair));
1134 return lltok::APFloat;
1135 case 'M':
1136 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
1137 HexToIntPair(Buffer: TokStart+3, End: CurPtr, Pair);
1138 APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair));
1139 return lltok::APFloat;
1140 case 'H': {
1141 uint64_t Val = HexIntToVal(Buffer: TokStart + 3, End: CurPtr);
1142 if (!llvm::isUInt<16>(x: Val)) {
1143 LexError(Msg: "hexadecimal constant too large for half (16-bit)");
1144 return lltok::Error;
1145 }
1146 APFloatVal = APFloat(APFloat::IEEEhalf(), APInt(16, Val));
1147 return lltok::APFloat;
1148 }
1149 case 'R': {
1150 // Brain floating point
1151 uint64_t Val = HexIntToVal(Buffer: TokStart + 3, End: CurPtr);
1152 if (!llvm::isUInt<16>(x: Val)) {
1153 LexError(Msg: "hexadecimal constant too large for bfloat (16-bit)");
1154 return lltok::Error;
1155 }
1156 APFloatVal = APFloat(APFloat::BFloat(), APInt(16, Val));
1157 return lltok::APFloat;
1158 }
1159 }
1160}
1161
1162/// Lex tokens for a label or a numeric constant, possibly starting with -.
1163/// Label [-a-zA-Z$._0-9]+:
1164/// NInteger -[0-9]+
1165/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1166/// PInteger [0-9]+
1167/// HexFPConstant 0x[0-9A-Fa-f]+
1168/// HexFP80Constant 0xK[0-9A-Fa-f]+
1169/// HexFP128Constant 0xL[0-9A-Fa-f]+
1170/// HexPPC128Constant 0xM[0-9A-Fa-f]+
1171lltok::Kind LLLexer::LexDigitOrNegative() {
1172 // If the letter after the negative is not a number, this is probably a label.
1173 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
1174 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
1175 // Okay, this is not a number after the -, it's probably a label.
1176 if (const char *End = isLabelTail(CurPtr)) {
1177 StrVal.assign(first: TokStart, last: End-1);
1178 CurPtr = End;
1179 return lltok::LabelStr;
1180 }
1181
1182 return lltok::Error;
1183 }
1184
1185 // At this point, it is either a label, int or fp constant.
1186
1187 // Skip digits, we have at least one.
1188 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1189 /*empty*/;
1190
1191 // Check if this is a fully-numeric label:
1192 if (isdigit(TokStart[0]) && CurPtr[0] == ':') {
1193 uint64_t Val = atoull(Buffer: TokStart, End: CurPtr);
1194 ++CurPtr; // Skip the colon.
1195 if ((unsigned)Val != Val)
1196 LexError(Msg: "invalid value number (too large)");
1197 UIntVal = unsigned(Val);
1198 return lltok::LabelID;
1199 }
1200
1201 // Check to see if this really is a string label, e.g. "-1:".
1202 if (isLabelChar(C: CurPtr[0]) || CurPtr[0] == ':') {
1203 if (const char *End = isLabelTail(CurPtr)) {
1204 StrVal.assign(first: TokStart, last: End-1);
1205 CurPtr = End;
1206 return lltok::LabelStr;
1207 }
1208 }
1209
1210 // If the next character is a '.', then it is a fp value, otherwise its
1211 // integer.
1212 if (CurPtr[0] != '.') {
1213 if (TokStart[0] == '0' && TokStart[1] == 'x')
1214 return Lex0x();
1215 APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart));
1216 return lltok::APSInt;
1217 }
1218
1219 ++CurPtr;
1220
1221 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1222 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1223
1224 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1225 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1226 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1227 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1228 CurPtr += 2;
1229 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1230 }
1231 }
1232
1233 APFloatVal = APFloat(APFloat::IEEEdouble(),
1234 StringRef(TokStart, CurPtr - TokStart));
1235 return lltok::APFloat;
1236}
1237
1238/// Lex a floating point constant starting with +.
1239/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1240lltok::Kind LLLexer::LexPositive() {
1241 // If the letter after the negative is a number, this is probably not a
1242 // label.
1243 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
1244 return lltok::Error;
1245
1246 // Skip digits.
1247 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1248 /*empty*/;
1249
1250 // At this point, we need a '.'.
1251 if (CurPtr[0] != '.') {
1252 CurPtr = TokStart+1;
1253 return lltok::Error;
1254 }
1255
1256 ++CurPtr;
1257
1258 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1259 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1260
1261 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1262 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1263 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1264 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1265 CurPtr += 2;
1266 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1267 }
1268 }
1269
1270 APFloatVal = APFloat(APFloat::IEEEdouble(),
1271 StringRef(TokStart, CurPtr - TokStart));
1272 return lltok::APFloat;
1273}
1274