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
631 KEYWORD(no_sanitize_address);
632 KEYWORD(no_sanitize_hwaddress);
633 KEYWORD(sanitize_address_dyninit);
634
635 KEYWORD(ccc);
636 KEYWORD(fastcc);
637 KEYWORD(coldcc);
638 KEYWORD(cfguard_checkcc);
639 KEYWORD(x86_stdcallcc);
640 KEYWORD(x86_fastcallcc);
641 KEYWORD(x86_thiscallcc);
642 KEYWORD(x86_vectorcallcc);
643 KEYWORD(arm_apcscc);
644 KEYWORD(arm_aapcscc);
645 KEYWORD(arm_aapcs_vfpcc);
646 KEYWORD(aarch64_vector_pcs);
647 KEYWORD(aarch64_sve_vector_pcs);
648 KEYWORD(aarch64_sme_preservemost_from_x0);
649 KEYWORD(aarch64_sme_preservemost_from_x1);
650 KEYWORD(aarch64_sme_preservemost_from_x2);
651 KEYWORD(msp430_intrcc);
652 KEYWORD(avr_intrcc);
653 KEYWORD(avr_signalcc);
654 KEYWORD(ptx_kernel);
655 KEYWORD(ptx_device);
656 KEYWORD(spir_kernel);
657 KEYWORD(spir_func);
658 KEYWORD(intel_ocl_bicc);
659 KEYWORD(x86_64_sysvcc);
660 KEYWORD(win64cc);
661 KEYWORD(x86_regcallcc);
662 KEYWORD(swiftcc);
663 KEYWORD(swifttailcc);
664 KEYWORD(anyregcc);
665 KEYWORD(preserve_mostcc);
666 KEYWORD(preserve_allcc);
667 KEYWORD(preserve_nonecc);
668 KEYWORD(ghccc);
669 KEYWORD(x86_intrcc);
670 KEYWORD(hhvmcc);
671 KEYWORD(hhvm_ccc);
672 KEYWORD(cxx_fast_tlscc);
673 KEYWORD(amdgpu_vs);
674 KEYWORD(amdgpu_ls);
675 KEYWORD(amdgpu_hs);
676 KEYWORD(amdgpu_es);
677 KEYWORD(amdgpu_gs);
678 KEYWORD(amdgpu_ps);
679 KEYWORD(amdgpu_cs);
680 KEYWORD(amdgpu_cs_chain);
681 KEYWORD(amdgpu_cs_chain_preserve);
682 KEYWORD(amdgpu_kernel);
683 KEYWORD(amdgpu_gfx);
684 KEYWORD(amdgpu_gfx_whole_wave);
685 KEYWORD(tailcc);
686 KEYWORD(m68k_rtdcc);
687 KEYWORD(graalcc);
688 KEYWORD(riscv_vector_cc);
689 KEYWORD(riscv_vls_cc);
690 KEYWORD(cheriot_compartmentcallcc);
691 KEYWORD(cheriot_compartmentcalleecc);
692 KEYWORD(cheriot_librarycallcc);
693
694 KEYWORD(cc);
695 KEYWORD(c);
696
697 KEYWORD(attributes);
698 KEYWORD(sync);
699 KEYWORD(async);
700
701#define GET_ATTR_NAMES
702#define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
703 KEYWORD(DISPLAY_NAME);
704#include "llvm/IR/Attributes.inc"
705
706 KEYWORD(read);
707 KEYWORD(write);
708 KEYWORD(readwrite);
709 KEYWORD(argmem);
710 KEYWORD(target_mem0);
711 KEYWORD(target_mem1);
712 KEYWORD(inaccessiblemem);
713 KEYWORD(errnomem);
714 KEYWORD(argmemonly);
715 KEYWORD(inaccessiblememonly);
716 KEYWORD(inaccessiblemem_or_argmemonly);
717 KEYWORD(nocapture);
718 KEYWORD(address_is_null);
719 KEYWORD(address);
720 KEYWORD(provenance);
721 KEYWORD(read_provenance);
722
723 // nofpclass attribute
724 KEYWORD(all);
725 KEYWORD(nan);
726 KEYWORD(snan);
727 KEYWORD(qnan);
728 KEYWORD(inf);
729 // ninf already a keyword
730 KEYWORD(pinf);
731 KEYWORD(norm);
732 KEYWORD(nnorm);
733 KEYWORD(pnorm);
734 // sub already a keyword
735 KEYWORD(nsub);
736 KEYWORD(psub);
737 KEYWORD(zero);
738 KEYWORD(nzero);
739 KEYWORD(pzero);
740
741 KEYWORD(type);
742 KEYWORD(opaque);
743
744 KEYWORD(comdat);
745
746 // Comdat types
747 KEYWORD(any);
748 KEYWORD(exactmatch);
749 KEYWORD(largest);
750 KEYWORD(nodeduplicate);
751 KEYWORD(samesize);
752
753 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
754 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
755 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
756 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
757
758 KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
759 KEYWORD(umin); KEYWORD(fmax); KEYWORD(fmin);
760 KEYWORD(fmaximum);
761 KEYWORD(fminimum);
762 KEYWORD(uinc_wrap);
763 KEYWORD(udec_wrap);
764 KEYWORD(usub_cond);
765 KEYWORD(usub_sat);
766
767 KEYWORD(splat);
768 KEYWORD(vscale);
769 KEYWORD(x);
770 KEYWORD(blockaddress);
771 KEYWORD(dso_local_equivalent);
772 KEYWORD(no_cfi);
773 KEYWORD(ptrauth);
774
775 // Metadata types.
776 KEYWORD(distinct);
777
778 // Use-list order directives.
779 KEYWORD(uselistorder);
780 KEYWORD(uselistorder_bb);
781
782 KEYWORD(personality);
783 KEYWORD(cleanup);
784 KEYWORD(catch);
785 KEYWORD(filter);
786
787 // Summary index keywords.
788 KEYWORD(path);
789 KEYWORD(hash);
790 KEYWORD(gv);
791 KEYWORD(guid);
792 KEYWORD(name);
793 KEYWORD(summaries);
794 KEYWORD(flags);
795 KEYWORD(blockcount);
796 KEYWORD(linkage);
797 KEYWORD(visibility);
798 KEYWORD(notEligibleToImport);
799 KEYWORD(live);
800 KEYWORD(dsoLocal);
801 KEYWORD(canAutoHide);
802 KEYWORD(importType);
803 KEYWORD(definition);
804 KEYWORD(declaration);
805 KEYWORD(function);
806 KEYWORD(insts);
807 KEYWORD(funcFlags);
808 KEYWORD(readNone);
809 KEYWORD(readOnly);
810 KEYWORD(noRecurse);
811 KEYWORD(returnDoesNotAlias);
812 KEYWORD(noInline);
813 KEYWORD(alwaysInline);
814 KEYWORD(noUnwind);
815 KEYWORD(mayThrow);
816 KEYWORD(hasUnknownCall);
817 KEYWORD(mustBeUnreachable);
818 KEYWORD(calls);
819 KEYWORD(callee);
820 KEYWORD(params);
821 KEYWORD(param);
822 KEYWORD(hotness);
823 KEYWORD(unknown);
824 KEYWORD(critical);
825 // Deprecated, keep in order to support old files.
826 KEYWORD(relbf);
827 KEYWORD(variable);
828 KEYWORD(vTableFuncs);
829 KEYWORD(virtFunc);
830 KEYWORD(aliasee);
831 KEYWORD(refs);
832 KEYWORD(typeIdInfo);
833 KEYWORD(typeTests);
834 KEYWORD(typeTestAssumeVCalls);
835 KEYWORD(typeCheckedLoadVCalls);
836 KEYWORD(typeTestAssumeConstVCalls);
837 KEYWORD(typeCheckedLoadConstVCalls);
838 KEYWORD(vFuncId);
839 KEYWORD(offset);
840 KEYWORD(args);
841 KEYWORD(typeid);
842 KEYWORD(typeidCompatibleVTable);
843 KEYWORD(summary);
844 KEYWORD(typeTestRes);
845 KEYWORD(kind);
846 KEYWORD(unsat);
847 KEYWORD(byteArray);
848 KEYWORD(inline);
849 KEYWORD(single);
850 KEYWORD(allOnes);
851 KEYWORD(sizeM1BitWidth);
852 KEYWORD(alignLog2);
853 KEYWORD(sizeM1);
854 KEYWORD(bitMask);
855 KEYWORD(inlineBits);
856 KEYWORD(vcall_visibility);
857 KEYWORD(wpdResolutions);
858 KEYWORD(wpdRes);
859 KEYWORD(indir);
860 KEYWORD(singleImpl);
861 KEYWORD(branchFunnel);
862 KEYWORD(singleImplName);
863 KEYWORD(resByArg);
864 KEYWORD(byArg);
865 KEYWORD(uniformRetVal);
866 KEYWORD(uniqueRetVal);
867 KEYWORD(virtualConstProp);
868 KEYWORD(info);
869 KEYWORD(byte);
870 KEYWORD(bit);
871 KEYWORD(varFlags);
872 KEYWORD(callsites);
873 KEYWORD(clones);
874 KEYWORD(stackIds);
875 KEYWORD(allocs);
876 KEYWORD(versions);
877 KEYWORD(memProf);
878 KEYWORD(notcold);
879
880#undef KEYWORD
881
882 // Keywords for types.
883#define TYPEKEYWORD(STR, LLVMTY) \
884 do { \
885 if (Keyword == STR) { \
886 TyVal = LLVMTY; \
887 return lltok::Type; \
888 } \
889 } while (false)
890
891 TYPEKEYWORD("void", Type::getVoidTy(Context));
892 TYPEKEYWORD("half", Type::getHalfTy(Context));
893 TYPEKEYWORD("bfloat", Type::getBFloatTy(Context));
894 TYPEKEYWORD("float", Type::getFloatTy(Context));
895 TYPEKEYWORD("double", Type::getDoubleTy(Context));
896 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
897 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
898 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
899 TYPEKEYWORD("label", Type::getLabelTy(Context));
900 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
901 TYPEKEYWORD("x86_amx", Type::getX86_AMXTy(Context));
902 TYPEKEYWORD("token", Type::getTokenTy(Context));
903 TYPEKEYWORD("ptr", PointerType::getUnqual(Context));
904
905#undef TYPEKEYWORD
906
907 // Keywords for instructions.
908#define INSTKEYWORD(STR, Enum) \
909 do { \
910 if (Keyword == #STR) { \
911 UIntVal = Instruction::Enum; \
912 return lltok::kw_##STR; \
913 } \
914 } while (false)
915
916 INSTKEYWORD(fneg, FNeg);
917
918 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
919 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
920 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
921 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
922 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
923 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
924 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
925 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
926
927 INSTKEYWORD(phi, PHI);
928 INSTKEYWORD(call, Call);
929 INSTKEYWORD(trunc, Trunc);
930 INSTKEYWORD(zext, ZExt);
931 INSTKEYWORD(sext, SExt);
932 INSTKEYWORD(fptrunc, FPTrunc);
933 INSTKEYWORD(fpext, FPExt);
934 INSTKEYWORD(uitofp, UIToFP);
935 INSTKEYWORD(sitofp, SIToFP);
936 INSTKEYWORD(fptoui, FPToUI);
937 INSTKEYWORD(fptosi, FPToSI);
938 INSTKEYWORD(inttoptr, IntToPtr);
939 INSTKEYWORD(ptrtoaddr, PtrToAddr);
940 INSTKEYWORD(ptrtoint, PtrToInt);
941 INSTKEYWORD(bitcast, BitCast);
942 INSTKEYWORD(addrspacecast, AddrSpaceCast);
943 INSTKEYWORD(select, Select);
944 INSTKEYWORD(va_arg, VAArg);
945 INSTKEYWORD(ret, Ret);
946 INSTKEYWORD(br, Br);
947 INSTKEYWORD(switch, Switch);
948 INSTKEYWORD(indirectbr, IndirectBr);
949 INSTKEYWORD(invoke, Invoke);
950 INSTKEYWORD(resume, Resume);
951 INSTKEYWORD(unreachable, Unreachable);
952 INSTKEYWORD(callbr, CallBr);
953
954 INSTKEYWORD(alloca, Alloca);
955 INSTKEYWORD(load, Load);
956 INSTKEYWORD(store, Store);
957 INSTKEYWORD(cmpxchg, AtomicCmpXchg);
958 INSTKEYWORD(atomicrmw, AtomicRMW);
959 INSTKEYWORD(fence, Fence);
960 INSTKEYWORD(getelementptr, GetElementPtr);
961
962 INSTKEYWORD(extractelement, ExtractElement);
963 INSTKEYWORD(insertelement, InsertElement);
964 INSTKEYWORD(shufflevector, ShuffleVector);
965 INSTKEYWORD(extractvalue, ExtractValue);
966 INSTKEYWORD(insertvalue, InsertValue);
967 INSTKEYWORD(landingpad, LandingPad);
968 INSTKEYWORD(cleanupret, CleanupRet);
969 INSTKEYWORD(catchret, CatchRet);
970 INSTKEYWORD(catchswitch, CatchSwitch);
971 INSTKEYWORD(catchpad, CatchPad);
972 INSTKEYWORD(cleanuppad, CleanupPad);
973
974 INSTKEYWORD(freeze, Freeze);
975
976#undef INSTKEYWORD
977
978#define DWKEYWORD(TYPE, TOKEN) \
979 do { \
980 if (Keyword.starts_with("DW_" #TYPE "_")) { \
981 StrVal.assign(Keyword.begin(), Keyword.end()); \
982 return lltok::TOKEN; \
983 } \
984 } while (false)
985
986 DWKEYWORD(TAG, DwarfTag);
987 DWKEYWORD(ATE, DwarfAttEncoding);
988 DWKEYWORD(VIRTUALITY, DwarfVirtuality);
989 DWKEYWORD(LANG, DwarfLang);
990 DWKEYWORD(LNAME, DwarfSourceLangName);
991 DWKEYWORD(CC, DwarfCC);
992 DWKEYWORD(OP, DwarfOp);
993 DWKEYWORD(MACINFO, DwarfMacinfo);
994 DWKEYWORD(APPLE_ENUM_KIND, DwarfEnumKind);
995
996#undef DWKEYWORD
997
998// Keywords for debug record types.
999#define DBGRECORDTYPEKEYWORD(STR) \
1000 do { \
1001 if (Keyword == "dbg_" #STR) { \
1002 StrVal = #STR; \
1003 return lltok::DbgRecordType; \
1004 } \
1005 } while (false)
1006
1007 DBGRECORDTYPEKEYWORD(value);
1008 DBGRECORDTYPEKEYWORD(declare);
1009 DBGRECORDTYPEKEYWORD(assign);
1010 DBGRECORDTYPEKEYWORD(label);
1011 DBGRECORDTYPEKEYWORD(declare_value);
1012#undef DBGRECORDTYPEKEYWORD
1013
1014 if (Keyword.starts_with(Prefix: "DIFlag")) {
1015 StrVal.assign(first: Keyword.begin(), last: Keyword.end());
1016 return lltok::DIFlag;
1017 }
1018
1019 if (Keyword.starts_with(Prefix: "DISPFlag")) {
1020 StrVal.assign(first: Keyword.begin(), last: Keyword.end());
1021 return lltok::DISPFlag;
1022 }
1023
1024 if (Keyword.starts_with(Prefix: "CSK_")) {
1025 StrVal.assign(first: Keyword.begin(), last: Keyword.end());
1026 return lltok::ChecksumKind;
1027 }
1028
1029 if (Keyword == "NoDebug" || Keyword == "FullDebug" ||
1030 Keyword == "LineTablesOnly" || Keyword == "DebugDirectivesOnly") {
1031 StrVal.assign(first: Keyword.begin(), last: Keyword.end());
1032 return lltok::EmissionKind;
1033 }
1034
1035 if (Keyword == "GNU" || Keyword == "Apple" || Keyword == "None" ||
1036 Keyword == "Default") {
1037 StrVal.assign(first: Keyword.begin(), last: Keyword.end());
1038 return lltok::NameTableKind;
1039 }
1040
1041 if (Keyword == "Binary" || Keyword == "Decimal" || Keyword == "Rational") {
1042 StrVal.assign(first: Keyword.begin(), last: Keyword.end());
1043 return lltok::FixedPointKind;
1044 }
1045
1046 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
1047 // the CFE to avoid forcing it to deal with 64-bit numbers.
1048 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
1049 TokStart[1] == '0' && TokStart[2] == 'x' &&
1050 isxdigit(static_cast<unsigned char>(TokStart[3]))) {
1051 int len = CurPtr-TokStart-3;
1052 uint32_t bits = len * 4;
1053 StringRef HexStr(TokStart + 3, len);
1054 if (!all_of(Range&: HexStr, P: isxdigit)) {
1055 // Bad token, return it as an error.
1056 CurPtr = TokStart+3;
1057 return lltok::Error;
1058 }
1059 APInt Tmp(bits, HexStr, 16);
1060 uint32_t activeBits = Tmp.getActiveBits();
1061 if (activeBits > 0 && activeBits < bits)
1062 Tmp = Tmp.trunc(width: activeBits);
1063 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
1064 return lltok::APSInt;
1065 }
1066
1067 // If this is "cc1234", return this as just "cc".
1068 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
1069 CurPtr = TokStart+2;
1070 return lltok::kw_cc;
1071 }
1072
1073 // Finally, if this isn't known, return an error.
1074 CurPtr = TokStart+1;
1075 return lltok::Error;
1076}
1077
1078/// Lex all tokens that start with a 0x prefix, knowing they match and are not
1079/// labels.
1080/// HexFPConstant 0x[0-9A-Fa-f]+
1081/// HexFP80Constant 0xK[0-9A-Fa-f]+
1082/// HexFP128Constant 0xL[0-9A-Fa-f]+
1083/// HexPPC128Constant 0xM[0-9A-Fa-f]+
1084/// HexHalfConstant 0xH[0-9A-Fa-f]+
1085/// HexBFloatConstant 0xR[0-9A-Fa-f]+
1086lltok::Kind LLLexer::Lex0x() {
1087 CurPtr = TokStart + 2;
1088
1089 char Kind;
1090 if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H' ||
1091 CurPtr[0] == 'R') {
1092 Kind = *CurPtr++;
1093 } else {
1094 Kind = 'J';
1095 }
1096
1097 if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
1098 // Bad token, return it as an error.
1099 CurPtr = TokStart+1;
1100 return lltok::Error;
1101 }
1102
1103 while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
1104 ++CurPtr;
1105
1106 if (Kind == 'J') {
1107 // HexFPConstant - Floating point constant represented in IEEE format as a
1108 // hexadecimal number for when exponential notation is not precise enough.
1109 // Half, BFloat, Float, and double only.
1110 APFloatVal = APFloat(APFloat::IEEEdouble(),
1111 APInt(64, HexIntToVal(Buffer: TokStart + 2, End: CurPtr)));
1112 return lltok::APFloat;
1113 }
1114
1115 uint64_t Pair[2];
1116 switch (Kind) {
1117 default: llvm_unreachable("Unknown kind!");
1118 case 'K':
1119 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
1120 FP80HexToIntPair(Buffer: TokStart+3, End: CurPtr, Pair);
1121 APFloatVal = APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair));
1122 return lltok::APFloat;
1123 case 'L':
1124 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
1125 HexToIntPair(Buffer: TokStart+3, End: CurPtr, Pair);
1126 APFloatVal = APFloat(APFloat::IEEEquad(), APInt(128, Pair));
1127 return lltok::APFloat;
1128 case 'M':
1129 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
1130 HexToIntPair(Buffer: TokStart+3, End: CurPtr, Pair);
1131 APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair));
1132 return lltok::APFloat;
1133 case 'H': {
1134 uint64_t Val = HexIntToVal(Buffer: TokStart + 3, End: CurPtr);
1135 if (!llvm::isUInt<16>(x: Val)) {
1136 LexError(Msg: "hexadecimal constant too large for half (16-bit)");
1137 return lltok::Error;
1138 }
1139 APFloatVal = APFloat(APFloat::IEEEhalf(), APInt(16, Val));
1140 return lltok::APFloat;
1141 }
1142 case 'R': {
1143 // Brain floating point
1144 uint64_t Val = HexIntToVal(Buffer: TokStart + 3, End: CurPtr);
1145 if (!llvm::isUInt<16>(x: Val)) {
1146 LexError(Msg: "hexadecimal constant too large for bfloat (16-bit)");
1147 return lltok::Error;
1148 }
1149 APFloatVal = APFloat(APFloat::BFloat(), APInt(16, Val));
1150 return lltok::APFloat;
1151 }
1152 }
1153}
1154
1155/// Lex tokens for a label or a numeric constant, possibly starting with -.
1156/// Label [-a-zA-Z$._0-9]+:
1157/// NInteger -[0-9]+
1158/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1159/// PInteger [0-9]+
1160/// HexFPConstant 0x[0-9A-Fa-f]+
1161/// HexFP80Constant 0xK[0-9A-Fa-f]+
1162/// HexFP128Constant 0xL[0-9A-Fa-f]+
1163/// HexPPC128Constant 0xM[0-9A-Fa-f]+
1164lltok::Kind LLLexer::LexDigitOrNegative() {
1165 // If the letter after the negative is not a number, this is probably a label.
1166 if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
1167 !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
1168 // Okay, this is not a number after the -, it's probably a label.
1169 if (const char *End = isLabelTail(CurPtr)) {
1170 StrVal.assign(first: TokStart, last: End-1);
1171 CurPtr = End;
1172 return lltok::LabelStr;
1173 }
1174
1175 return lltok::Error;
1176 }
1177
1178 // At this point, it is either a label, int or fp constant.
1179
1180 // Skip digits, we have at least one.
1181 for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1182 /*empty*/;
1183
1184 // Check if this is a fully-numeric label:
1185 if (isdigit(TokStart[0]) && CurPtr[0] == ':') {
1186 uint64_t Val = atoull(Buffer: TokStart, End: CurPtr);
1187 ++CurPtr; // Skip the colon.
1188 if ((unsigned)Val != Val)
1189 LexError(Msg: "invalid value number (too large)");
1190 UIntVal = unsigned(Val);
1191 return lltok::LabelID;
1192 }
1193
1194 // Check to see if this really is a string label, e.g. "-1:".
1195 if (isLabelChar(C: CurPtr[0]) || CurPtr[0] == ':') {
1196 if (const char *End = isLabelTail(CurPtr)) {
1197 StrVal.assign(first: TokStart, last: End-1);
1198 CurPtr = End;
1199 return lltok::LabelStr;
1200 }
1201 }
1202
1203 // If the next character is a '.', then it is a fp value, otherwise its
1204 // integer.
1205 if (CurPtr[0] != '.') {
1206 if (TokStart[0] == '0' && TokStart[1] == 'x')
1207 return Lex0x();
1208 APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart));
1209 return lltok::APSInt;
1210 }
1211
1212 ++CurPtr;
1213
1214 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1215 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1216
1217 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1218 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1219 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1220 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1221 CurPtr += 2;
1222 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1223 }
1224 }
1225
1226 APFloatVal = APFloat(APFloat::IEEEdouble(),
1227 StringRef(TokStart, CurPtr - TokStart));
1228 return lltok::APFloat;
1229}
1230
1231/// Lex a floating point constant starting with +.
1232/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
1233lltok::Kind LLLexer::LexPositive() {
1234 // If the letter after the negative is a number, this is probably not a
1235 // label.
1236 if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
1237 return lltok::Error;
1238
1239 // Skip digits.
1240 for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1241 /*empty*/;
1242
1243 // At this point, we need a '.'.
1244 if (CurPtr[0] != '.') {
1245 CurPtr = TokStart+1;
1246 return lltok::Error;
1247 }
1248
1249 ++CurPtr;
1250
1251 // Skip over [0-9]*([eE][-+]?[0-9]+)?
1252 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1253
1254 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1255 if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1256 ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1257 isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1258 CurPtr += 2;
1259 while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1260 }
1261 }
1262
1263 APFloatVal = APFloat(APFloat::IEEEdouble(),
1264 StringRef(TokStart, CurPtr - TokStart));
1265 return lltok::APFloat;
1266}
1267