1 //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implement the Lexer for .ll files.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Instruction.h"
17 #include "llvm/LLVMContext.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Assembly/Parser.h"
28 bool LLLexer::Error(LocTy ErrorLoc
, const std::string
&Msg
) const {
29 ErrorInfo
= SM
.GetMessage(ErrorLoc
, Msg
, "error");
33 //===----------------------------------------------------------------------===//
35 //===----------------------------------------------------------------------===//
37 // atoull - Convert an ascii string of decimal digits into the unsigned long
38 // long representation... this does not have to do input error checking,
39 // because we know that the input will be matched by a suitable regex...
41 uint64_t LLLexer::atoull(const char *Buffer
, const char *End
) {
43 for (; Buffer
!= End
; Buffer
++) {
44 uint64_t OldRes
= Result
;
46 Result
+= *Buffer
-'0';
47 if (Result
< OldRes
) { // Uh, oh, overflow detected!!!
48 Error("constant bigger than 64 bits detected!");
55 uint64_t LLLexer::HexIntToVal(const char *Buffer
, const char *End
) {
57 for (; Buffer
!= End
; ++Buffer
) {
58 uint64_t OldRes
= Result
;
61 if (C
>= '0' && C
<= '9')
63 else if (C
>= 'A' && C
<= 'F')
65 else if (C
>= 'a' && C
<= 'f')
68 if (Result
< OldRes
) { // Uh, oh, overflow detected!!!
69 Error("constant bigger than 64 bits detected!");
76 void LLLexer::HexToIntPair(const char *Buffer
, const char *End
,
79 for (int i
=0; i
<16; i
++, Buffer
++) {
80 assert(Buffer
!= End
);
83 if (C
>= '0' && C
<= '9')
85 else if (C
>= 'A' && C
<= 'F')
87 else if (C
>= 'a' && C
<= 'f')
91 for (int i
=0; i
<16 && Buffer
!= End
; i
++, Buffer
++) {
94 if (C
>= '0' && C
<= '9')
96 else if (C
>= 'A' && C
<= 'F')
98 else if (C
>= 'a' && C
<= 'f')
102 Error("constant bigger than 128 bits detected!");
105 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
106 /// { low64, high16 } as usual for an APInt.
107 void LLLexer::FP80HexToIntPair(const char *Buffer
, const char *End
,
110 for (int i
=0; i
<4 && Buffer
!= End
; i
++, Buffer
++) {
111 assert(Buffer
!= End
);
114 if (C
>= '0' && C
<= '9')
116 else if (C
>= 'A' && C
<= 'F')
118 else if (C
>= 'a' && C
<= 'f')
122 for (int i
=0; i
<16; i
++, Buffer
++) {
125 if (C
>= '0' && C
<= '9')
127 else if (C
>= 'A' && C
<= 'F')
129 else if (C
>= 'a' && C
<= 'f')
133 Error("constant bigger than 128 bits detected!");
136 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
137 // appropriate character.
138 static void UnEscapeLexed(std::string
&Str
) {
139 if (Str
.empty()) return;
141 char *Buffer
= &Str
[0], *EndBuffer
= Buffer
+Str
.size();
143 for (char *BIn
= Buffer
; BIn
!= EndBuffer
; ) {
144 if (BIn
[0] == '\\') {
145 if (BIn
< EndBuffer
-1 && BIn
[1] == '\\') {
146 *BOut
++ = '\\'; // Two \ becomes one
148 } else if (BIn
< EndBuffer
-2 && isxdigit(BIn
[1]) && isxdigit(BIn
[2])) {
149 char Tmp
= BIn
[3]; BIn
[3] = 0; // Terminate string
150 *BOut
= (char)strtol(BIn
+1, 0, 16); // Convert to number
151 BIn
[3] = Tmp
; // Restore character
152 BIn
+= 3; // Skip over handled chars
161 Str
.resize(BOut
-Buffer
);
164 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
165 static bool isLabelChar(char C
) {
166 return isalnum(C
) || C
== '-' || C
== '$' || C
== '.' || C
== '_';
170 /// isLabelTail - Return true if this pointer points to a valid end of a label.
171 static const char *isLabelTail(const char *CurPtr
) {
173 if (CurPtr
[0] == ':') return CurPtr
+1;
174 if (!isLabelChar(CurPtr
[0])) return 0;
181 //===----------------------------------------------------------------------===//
183 //===----------------------------------------------------------------------===//
185 LLLexer::LLLexer(MemoryBuffer
*StartBuf
, SourceMgr
&sm
, SMDiagnostic
&Err
,
187 : CurBuf(StartBuf
), ErrorInfo(Err
), SM(sm
), Context(C
), APFloatVal(0.0) {
188 CurPtr
= CurBuf
->getBufferStart();
191 std::string
LLLexer::getFilename() const {
192 return CurBuf
->getBufferIdentifier();
195 int LLLexer::getNextChar() {
196 char CurChar
= *CurPtr
++;
198 default: return (unsigned char)CurChar
;
200 // A nul character in the stream is either the end of the current buffer or
201 // a random nul in the file. Disambiguate that here.
202 if (CurPtr
-1 != CurBuf
->getBufferEnd())
203 return 0; // Just whitespace.
205 // Otherwise, return end of file.
206 --CurPtr
; // Another call to lex will return EOF again.
212 lltok::Kind
LLLexer::LexToken() {
215 int CurChar
= getNextChar();
218 // Handle letters: [a-zA-Z_]
219 if (isalpha(CurChar
) || CurChar
== '_')
220 return LexIdentifier();
223 case EOF
: return lltok::Eof
;
229 // Ignore whitespace.
231 case '+': return LexPositive();
232 case '@': return LexAt();
233 case '%': return LexPercent();
234 case '"': return LexQuote();
236 if (const char *Ptr
= isLabelTail(CurPtr
)) {
238 StrVal
.assign(TokStart
, CurPtr
-1);
239 return lltok::LabelStr
;
241 if (CurPtr
[0] == '.' && CurPtr
[1] == '.') {
243 return lltok::dotdotdot
;
247 if (const char *Ptr
= isLabelTail(CurPtr
)) {
249 StrVal
.assign(TokStart
, CurPtr
-1);
250 return lltok::LabelStr
;
256 case '!': return LexMetadata();
257 case '0': case '1': case '2': case '3': case '4':
258 case '5': case '6': case '7': case '8': case '9':
260 return LexDigitOrNegative();
261 case '=': return lltok::equal
;
262 case '[': return lltok::lsquare
;
263 case ']': return lltok::rsquare
;
264 case '{': return lltok::lbrace
;
265 case '}': return lltok::rbrace
;
266 case '<': return lltok::less
;
267 case '>': return lltok::greater
;
268 case '(': return lltok::lparen
;
269 case ')': return lltok::rparen
;
270 case ',': return lltok::comma
;
271 case '*': return lltok::star
;
272 case '\\': return lltok::backslash
;
276 void LLLexer::SkipLineComment() {
278 if (CurPtr
[0] == '\n' || CurPtr
[0] == '\r' || getNextChar() == EOF
)
283 /// LexAt - Lex all tokens that start with an @ character:
284 /// GlobalVar @\"[^\"]*\"
285 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
286 /// GlobalVarID @[0-9]+
287 lltok::Kind
LLLexer::LexAt() {
288 // Handle AtStringConstant: @\"[^\"]*\"
289 if (CurPtr
[0] == '"') {
293 int CurChar
= getNextChar();
295 if (CurChar
== EOF
) {
296 Error("end of file in global variable name");
299 if (CurChar
== '"') {
300 StrVal
.assign(TokStart
+2, CurPtr
-1);
301 UnEscapeLexed(StrVal
);
302 return lltok::GlobalVar
;
307 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
308 if (isalpha(CurPtr
[0]) || CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
309 CurPtr
[0] == '.' || CurPtr
[0] == '_') {
311 while (isalnum(CurPtr
[0]) || CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
312 CurPtr
[0] == '.' || CurPtr
[0] == '_')
315 StrVal
.assign(TokStart
+1, CurPtr
); // Skip @
316 return lltok::GlobalVar
;
319 // Handle GlobalVarID: @[0-9]+
320 if (isdigit(CurPtr
[0])) {
321 for (++CurPtr
; isdigit(CurPtr
[0]); ++CurPtr
)
324 uint64_t Val
= atoull(TokStart
+1, CurPtr
);
325 if ((unsigned)Val
!= Val
)
326 Error("invalid value number (too large)!");
327 UIntVal
= unsigned(Val
);
328 return lltok::GlobalID
;
335 /// LexPercent - Lex all tokens that start with a % character:
336 /// LocalVar ::= %\"[^\"]*\"
337 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
338 /// LocalVarID ::= %[0-9]+
339 lltok::Kind
LLLexer::LexPercent() {
340 // Handle LocalVarName: %\"[^\"]*\"
341 if (CurPtr
[0] == '"') {
345 int CurChar
= getNextChar();
347 if (CurChar
== EOF
) {
348 Error("end of file in string constant");
351 if (CurChar
== '"') {
352 StrVal
.assign(TokStart
+2, CurPtr
-1);
353 UnEscapeLexed(StrVal
);
354 return lltok::LocalVar
;
359 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
360 if (isalpha(CurPtr
[0]) || CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
361 CurPtr
[0] == '.' || CurPtr
[0] == '_') {
363 while (isalnum(CurPtr
[0]) || CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
364 CurPtr
[0] == '.' || CurPtr
[0] == '_')
367 StrVal
.assign(TokStart
+1, CurPtr
); // Skip %
368 return lltok::LocalVar
;
371 // Handle LocalVarID: %[0-9]+
372 if (isdigit(CurPtr
[0])) {
373 for (++CurPtr
; isdigit(CurPtr
[0]); ++CurPtr
)
376 uint64_t Val
= atoull(TokStart
+1, CurPtr
);
377 if ((unsigned)Val
!= Val
)
378 Error("invalid value number (too large)!");
379 UIntVal
= unsigned(Val
);
380 return lltok::LocalVarID
;
386 /// LexQuote - Lex all tokens that start with a " character:
387 /// QuoteLabel "[^"]+":
388 /// StringConstant "[^"]*"
389 lltok::Kind
LLLexer::LexQuote() {
391 int CurChar
= getNextChar();
393 if (CurChar
== EOF
) {
394 Error("end of file in quoted string");
398 if (CurChar
!= '"') continue;
400 if (CurPtr
[0] != ':') {
401 StrVal
.assign(TokStart
+1, CurPtr
-1);
402 UnEscapeLexed(StrVal
);
403 return lltok::StringConstant
;
407 StrVal
.assign(TokStart
+1, CurPtr
-2);
408 UnEscapeLexed(StrVal
);
409 return lltok::LabelStr
;
413 static bool JustWhitespaceNewLine(const char *&Ptr
) {
414 const char *ThisPtr
= Ptr
;
415 while (*ThisPtr
== ' ' || *ThisPtr
== '\t')
417 if (*ThisPtr
== '\n' || *ThisPtr
== '\r') {
428 lltok::Kind
LLLexer::LexMetadata() {
429 if (isalpha(CurPtr
[0])) {
431 while (isalnum(CurPtr
[0]) || CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
432 CurPtr
[0] == '.' || CurPtr
[0] == '_')
435 StrVal
.assign(TokStart
+1, CurPtr
); // Skip !
436 return lltok::NamedMD
;
438 return lltok::Metadata
;
441 /// LexIdentifier: Handle several related productions:
442 /// Label [-a-zA-Z$._0-9]+:
443 /// IntegerType i[0-9]+
444 /// Keyword sdiv, float, ...
445 /// HexIntConstant [us]0x[0-9A-Fa-f]+
446 lltok::Kind
LLLexer::LexIdentifier() {
447 const char *StartChar
= CurPtr
;
448 const char *IntEnd
= CurPtr
[-1] == 'i' ? 0 : StartChar
;
449 const char *KeywordEnd
= 0;
451 for (; isLabelChar(*CurPtr
); ++CurPtr
) {
452 // If we decide this is an integer, remember the end of the sequence.
453 if (!IntEnd
&& !isdigit(*CurPtr
)) IntEnd
= CurPtr
;
454 if (!KeywordEnd
&& !isalnum(*CurPtr
) && *CurPtr
!= '_') KeywordEnd
= CurPtr
;
457 // If we stopped due to a colon, this really is a label.
458 if (*CurPtr
== ':') {
459 StrVal
.assign(StartChar
-1, CurPtr
++);
460 return lltok::LabelStr
;
463 // Otherwise, this wasn't a label. If this was valid as an integer type,
465 if (IntEnd
== 0) IntEnd
= CurPtr
;
466 if (IntEnd
!= StartChar
) {
468 uint64_t NumBits
= atoull(StartChar
, CurPtr
);
469 if (NumBits
< IntegerType::MIN_INT_BITS
||
470 NumBits
> IntegerType::MAX_INT_BITS
) {
471 Error("bitwidth for integer type out of range!");
474 TyVal
= IntegerType::get(Context
, NumBits
);
478 // Otherwise, this was a letter sequence. See which keyword this is.
479 if (KeywordEnd
== 0) KeywordEnd
= CurPtr
;
482 unsigned Len
= CurPtr
-StartChar
;
483 #define KEYWORD(STR) \
484 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
485 return lltok::kw_##STR;
487 KEYWORD(begin
); KEYWORD(end
);
488 KEYWORD(true); KEYWORD(false);
489 KEYWORD(declare
); KEYWORD(define
);
490 KEYWORD(global
); KEYWORD(constant
);
493 KEYWORD(linker_private
);
495 KEYWORD(available_externally
);
497 KEYWORD(linkonce_odr
);
507 KEYWORD(extern_weak
);
509 KEYWORD(thread_local
);
510 KEYWORD(zeroinitializer
);
536 KEYWORD(x86_stdcallcc
);
537 KEYWORD(x86_fastcallcc
);
539 KEYWORD(arm_aapcscc
);
540 KEYWORD(arm_aapcs_vfpcc
);
559 KEYWORD(alwaysinline
);
564 KEYWORD(noimplicitfloat
);
570 KEYWORD(eq
); KEYWORD(ne
); KEYWORD(slt
); KEYWORD(sgt
); KEYWORD(sle
);
571 KEYWORD(sge
); KEYWORD(ult
); KEYWORD(ugt
); KEYWORD(ule
); KEYWORD(uge
);
572 KEYWORD(oeq
); KEYWORD(one
); KEYWORD(olt
); KEYWORD(ogt
); KEYWORD(ole
);
573 KEYWORD(oge
); KEYWORD(ord
); KEYWORD(uno
); KEYWORD(ueq
); KEYWORD(une
);
578 // Keywords for types.
579 #define TYPEKEYWORD(STR, LLVMTY) \
580 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
581 TyVal = LLVMTY; return lltok::Type; }
582 TYPEKEYWORD("void", Type::getVoidTy(Context
));
583 TYPEKEYWORD("float", Type::getFloatTy(Context
));
584 TYPEKEYWORD("double", Type::getDoubleTy(Context
));
585 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context
));
586 TYPEKEYWORD("fp128", Type::getFP128Ty(Context
));
587 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context
));
588 TYPEKEYWORD("label", Type::getLabelTy(Context
));
589 TYPEKEYWORD("metadata", Type::getMetadataTy(Context
));
592 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
593 // to avoid conflicting with the sext/zext instructions, below.
594 if (Len
== 4 && !memcmp(StartChar
, "sext", 4)) {
595 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
596 if (JustWhitespaceNewLine(CurPtr
))
597 return lltok::kw_signext
;
598 } else if (Len
== 4 && !memcmp(StartChar
, "zext", 4)) {
599 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
600 if (JustWhitespaceNewLine(CurPtr
))
601 return lltok::kw_zeroext
;
604 // Keywords for instructions.
605 #define INSTKEYWORD(STR, Enum) \
606 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
607 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
609 INSTKEYWORD(add
, Add
); INSTKEYWORD(fadd
, FAdd
);
610 INSTKEYWORD(sub
, Sub
); INSTKEYWORD(fsub
, FSub
);
611 INSTKEYWORD(mul
, Mul
); INSTKEYWORD(fmul
, FMul
);
612 INSTKEYWORD(udiv
, UDiv
); INSTKEYWORD(sdiv
, SDiv
); INSTKEYWORD(fdiv
, FDiv
);
613 INSTKEYWORD(urem
, URem
); INSTKEYWORD(srem
, SRem
); INSTKEYWORD(frem
, FRem
);
614 INSTKEYWORD(shl
, Shl
); INSTKEYWORD(lshr
, LShr
); INSTKEYWORD(ashr
, AShr
);
615 INSTKEYWORD(and, And
); INSTKEYWORD(or, Or
); INSTKEYWORD(xor, Xor
);
616 INSTKEYWORD(icmp
, ICmp
); INSTKEYWORD(fcmp
, FCmp
);
618 INSTKEYWORD(phi
, PHI
);
619 INSTKEYWORD(call
, Call
);
620 INSTKEYWORD(trunc
, Trunc
);
621 INSTKEYWORD(zext
, ZExt
);
622 INSTKEYWORD(sext
, SExt
);
623 INSTKEYWORD(fptrunc
, FPTrunc
);
624 INSTKEYWORD(fpext
, FPExt
);
625 INSTKEYWORD(uitofp
, UIToFP
);
626 INSTKEYWORD(sitofp
, SIToFP
);
627 INSTKEYWORD(fptoui
, FPToUI
);
628 INSTKEYWORD(fptosi
, FPToSI
);
629 INSTKEYWORD(inttoptr
, IntToPtr
);
630 INSTKEYWORD(ptrtoint
, PtrToInt
);
631 INSTKEYWORD(bitcast
, BitCast
);
632 INSTKEYWORD(select
, Select
);
633 INSTKEYWORD(va_arg
, VAArg
);
634 INSTKEYWORD(ret
, Ret
);
636 INSTKEYWORD(switch, Switch
);
637 INSTKEYWORD(invoke
, Invoke
);
638 INSTKEYWORD(unwind
, Unwind
);
639 INSTKEYWORD(unreachable
, Unreachable
);
641 INSTKEYWORD(malloc
, Malloc
);
642 INSTKEYWORD(alloca
, Alloca
);
643 INSTKEYWORD(free
, Free
);
644 INSTKEYWORD(load
, Load
);
645 INSTKEYWORD(store
, Store
);
646 INSTKEYWORD(getelementptr
, GetElementPtr
);
648 INSTKEYWORD(extractelement
, ExtractElement
);
649 INSTKEYWORD(insertelement
, InsertElement
);
650 INSTKEYWORD(shufflevector
, ShuffleVector
);
651 INSTKEYWORD(getresult
, ExtractValue
);
652 INSTKEYWORD(extractvalue
, ExtractValue
);
653 INSTKEYWORD(insertvalue
, InsertValue
);
656 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
657 // the CFE to avoid forcing it to deal with 64-bit numbers.
658 if ((TokStart
[0] == 'u' || TokStart
[0] == 's') &&
659 TokStart
[1] == '0' && TokStart
[2] == 'x' && isxdigit(TokStart
[3])) {
660 int len
= CurPtr
-TokStart
-3;
661 uint32_t bits
= len
* 4;
662 APInt
Tmp(bits
, StringRef(TokStart
+3, len
), 16);
663 uint32_t activeBits
= Tmp
.getActiveBits();
664 if (activeBits
> 0 && activeBits
< bits
)
665 Tmp
.trunc(activeBits
);
666 APSIntVal
= APSInt(Tmp
, TokStart
[0] == 'u');
667 return lltok::APSInt
;
670 // If this is "cc1234", return this as just "cc".
671 if (TokStart
[0] == 'c' && TokStart
[1] == 'c') {
676 // If this starts with "call", return it as CALL. This is to support old
677 // broken .ll files. FIXME: remove this with LLVM 3.0.
678 if (CurPtr
-TokStart
> 4 && !memcmp(TokStart
, "call", 4)) {
680 UIntVal
= Instruction::Call
;
681 return lltok::kw_call
;
684 // Finally, if this isn't known, return an error.
690 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
691 /// that this is not a label:
692 /// HexFPConstant 0x[0-9A-Fa-f]+
693 /// HexFP80Constant 0xK[0-9A-Fa-f]+
694 /// HexFP128Constant 0xL[0-9A-Fa-f]+
695 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
696 lltok::Kind
LLLexer::Lex0x() {
697 CurPtr
= TokStart
+ 2;
700 if (CurPtr
[0] >= 'K' && CurPtr
[0] <= 'M') {
706 if (!isxdigit(CurPtr
[0])) {
707 // Bad token, return it as an error.
712 while (isxdigit(CurPtr
[0]))
716 // HexFPConstant - Floating point constant represented in IEEE format as a
717 // hexadecimal number for when exponential notation is not precise enough.
718 // Float and double only.
719 APFloatVal
= APFloat(BitsToDouble(HexIntToVal(TokStart
+2, CurPtr
)));
720 return lltok::APFloat
;
725 default: llvm_unreachable("Unknown kind!");
727 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
728 FP80HexToIntPair(TokStart
+3, CurPtr
, Pair
);
729 APFloatVal
= APFloat(APInt(80, 2, Pair
));
730 return lltok::APFloat
;
732 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
733 HexToIntPair(TokStart
+3, CurPtr
, Pair
);
734 APFloatVal
= APFloat(APInt(128, 2, Pair
), true);
735 return lltok::APFloat
;
737 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
738 HexToIntPair(TokStart
+3, CurPtr
, Pair
);
739 APFloatVal
= APFloat(APInt(128, 2, Pair
));
740 return lltok::APFloat
;
744 /// LexIdentifier: Handle several related productions:
745 /// Label [-a-zA-Z$._0-9]+:
747 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
749 /// HexFPConstant 0x[0-9A-Fa-f]+
750 /// HexFP80Constant 0xK[0-9A-Fa-f]+
751 /// HexFP128Constant 0xL[0-9A-Fa-f]+
752 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
753 lltok::Kind
LLLexer::LexDigitOrNegative() {
754 // If the letter after the negative is a number, this is probably a label.
755 if (!isdigit(TokStart
[0]) && !isdigit(CurPtr
[0])) {
756 // Okay, this is not a number after the -, it's probably a label.
757 if (const char *End
= isLabelTail(CurPtr
)) {
758 StrVal
.assign(TokStart
, End
-1);
760 return lltok::LabelStr
;
766 // At this point, it is either a label, int or fp constant.
768 // Skip digits, we have at least one.
769 for (; isdigit(CurPtr
[0]); ++CurPtr
)
772 // Check to see if this really is a label afterall, e.g. "-1:".
773 if (isLabelChar(CurPtr
[0]) || CurPtr
[0] == ':') {
774 if (const char *End
= isLabelTail(CurPtr
)) {
775 StrVal
.assign(TokStart
, End
-1);
777 return lltok::LabelStr
;
781 // If the next character is a '.', then it is a fp value, otherwise its
783 if (CurPtr
[0] != '.') {
784 if (TokStart
[0] == '0' && TokStart
[1] == 'x')
786 unsigned Len
= CurPtr
-TokStart
;
787 uint32_t numBits
= ((Len
* 64) / 19) + 2;
788 APInt
Tmp(numBits
, StringRef(TokStart
, Len
), 10);
789 if (TokStart
[0] == '-') {
790 uint32_t minBits
= Tmp
.getMinSignedBits();
791 if (minBits
> 0 && minBits
< numBits
)
793 APSIntVal
= APSInt(Tmp
, false);
795 uint32_t activeBits
= Tmp
.getActiveBits();
796 if (activeBits
> 0 && activeBits
< numBits
)
797 Tmp
.trunc(activeBits
);
798 APSIntVal
= APSInt(Tmp
, true);
800 return lltok::APSInt
;
805 // Skip over [0-9]*([eE][-+]?[0-9]+)?
806 while (isdigit(CurPtr
[0])) ++CurPtr
;
808 if (CurPtr
[0] == 'e' || CurPtr
[0] == 'E') {
809 if (isdigit(CurPtr
[1]) ||
810 ((CurPtr
[1] == '-' || CurPtr
[1] == '+') && isdigit(CurPtr
[2]))) {
812 while (isdigit(CurPtr
[0])) ++CurPtr
;
816 APFloatVal
= APFloat(atof(TokStart
));
817 return lltok::APFloat
;
820 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
821 lltok::Kind
LLLexer::LexPositive() {
822 // If the letter after the negative is a number, this is probably not a
824 if (!isdigit(CurPtr
[0]))
828 for (++CurPtr
; isdigit(CurPtr
[0]); ++CurPtr
)
831 // At this point, we need a '.'.
832 if (CurPtr
[0] != '.') {
839 // Skip over [0-9]*([eE][-+]?[0-9]+)?
840 while (isdigit(CurPtr
[0])) ++CurPtr
;
842 if (CurPtr
[0] == 'e' || CurPtr
[0] == 'E') {
843 if (isdigit(CurPtr
[1]) ||
844 ((CurPtr
[1] == '-' || CurPtr
[1] == '+') && isdigit(CurPtr
[2]))) {
846 while (isdigit(CurPtr
[0])) ++CurPtr
;
850 APFloatVal
= APFloat(atof(TokStart
));
851 return lltok::APFloat
;