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/ADT/Twine.h"
19 #include "llvm/Assembly/Parser.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/raw_ostream.h"
30 bool LLLexer::Error(LocTy ErrorLoc
, const Twine
&Msg
) const {
31 ErrorInfo
= SM
.GetMessage(ErrorLoc
, Msg
, "error");
35 //===----------------------------------------------------------------------===//
37 //===----------------------------------------------------------------------===//
39 // atoull - Convert an ascii string of decimal digits into the unsigned long
40 // long representation... this does not have to do input error checking,
41 // because we know that the input will be matched by a suitable regex...
43 uint64_t LLLexer::atoull(const char *Buffer
, const char *End
) {
45 for (; Buffer
!= End
; Buffer
++) {
46 uint64_t OldRes
= Result
;
48 Result
+= *Buffer
-'0';
49 if (Result
< OldRes
) { // Uh, oh, overflow detected!!!
50 Error("constant bigger than 64 bits detected!");
57 uint64_t LLLexer::HexIntToVal(const char *Buffer
, const char *End
) {
59 for (; Buffer
!= End
; ++Buffer
) {
60 uint64_t OldRes
= Result
;
63 if (C
>= '0' && C
<= '9')
65 else if (C
>= 'A' && C
<= 'F')
67 else if (C
>= 'a' && C
<= 'f')
70 if (Result
< OldRes
) { // Uh, oh, overflow detected!!!
71 Error("constant bigger than 64 bits detected!");
78 void LLLexer::HexToIntPair(const char *Buffer
, const char *End
,
81 for (int i
=0; i
<16; i
++, Buffer
++) {
82 assert(Buffer
!= End
);
85 if (C
>= '0' && C
<= '9')
87 else if (C
>= 'A' && C
<= 'F')
89 else if (C
>= 'a' && C
<= 'f')
93 for (int i
=0; i
<16 && Buffer
!= End
; i
++, Buffer
++) {
96 if (C
>= '0' && C
<= '9')
98 else if (C
>= 'A' && C
<= 'F')
100 else if (C
>= 'a' && C
<= 'f')
104 Error("constant bigger than 128 bits detected!");
107 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
108 /// { low64, high16 } as usual for an APInt.
109 void LLLexer::FP80HexToIntPair(const char *Buffer
, const char *End
,
112 for (int i
=0; i
<4 && Buffer
!= End
; i
++, Buffer
++) {
113 assert(Buffer
!= End
);
116 if (C
>= '0' && C
<= '9')
118 else if (C
>= 'A' && C
<= 'F')
120 else if (C
>= 'a' && C
<= 'f')
124 for (int i
=0; i
<16; i
++, Buffer
++) {
127 if (C
>= '0' && C
<= '9')
129 else if (C
>= 'A' && C
<= 'F')
131 else if (C
>= 'a' && C
<= 'f')
135 Error("constant bigger than 128 bits detected!");
138 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
139 // appropriate character.
140 static void UnEscapeLexed(std::string
&Str
) {
141 if (Str
.empty()) return;
143 char *Buffer
= &Str
[0], *EndBuffer
= Buffer
+Str
.size();
145 for (char *BIn
= Buffer
; BIn
!= EndBuffer
; ) {
146 if (BIn
[0] == '\\') {
147 if (BIn
< EndBuffer
-1 && BIn
[1] == '\\') {
148 *BOut
++ = '\\'; // Two \ becomes one
150 } else if (BIn
< EndBuffer
-2 && isxdigit(BIn
[1]) && isxdigit(BIn
[2])) {
151 char Tmp
= BIn
[3]; BIn
[3] = 0; // Terminate string
152 *BOut
= (char)strtol(BIn
+1, 0, 16); // Convert to number
153 BIn
[3] = Tmp
; // Restore character
154 BIn
+= 3; // Skip over handled chars
163 Str
.resize(BOut
-Buffer
);
166 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
167 static bool isLabelChar(char C
) {
168 return isalnum(C
) || C
== '-' || C
== '$' || C
== '.' || C
== '_';
172 /// isLabelTail - Return true if this pointer points to a valid end of a label.
173 static const char *isLabelTail(const char *CurPtr
) {
175 if (CurPtr
[0] == ':') return CurPtr
+1;
176 if (!isLabelChar(CurPtr
[0])) return 0;
183 //===----------------------------------------------------------------------===//
185 //===----------------------------------------------------------------------===//
187 LLLexer::LLLexer(MemoryBuffer
*StartBuf
, SourceMgr
&sm
, SMDiagnostic
&Err
,
189 : CurBuf(StartBuf
), ErrorInfo(Err
), SM(sm
), Context(C
), APFloatVal(0.0) {
190 CurPtr
= CurBuf
->getBufferStart();
193 std::string
LLLexer::getFilename() const {
194 return CurBuf
->getBufferIdentifier();
197 int LLLexer::getNextChar() {
198 char CurChar
= *CurPtr
++;
200 default: return (unsigned char)CurChar
;
202 // A nul character in the stream is either the end of the current buffer or
203 // a random nul in the file. Disambiguate that here.
204 if (CurPtr
-1 != CurBuf
->getBufferEnd())
205 return 0; // Just whitespace.
207 // Otherwise, return end of file.
208 --CurPtr
; // Another call to lex will return EOF again.
214 lltok::Kind
LLLexer::LexToken() {
217 int CurChar
= getNextChar();
220 // Handle letters: [a-zA-Z_]
221 if (isalpha(CurChar
) || CurChar
== '_')
222 return LexIdentifier();
225 case EOF
: return lltok::Eof
;
231 // Ignore whitespace.
233 case '+': return LexPositive();
234 case '@': return LexAt();
235 case '%': return LexPercent();
236 case '"': return LexQuote();
238 if (const char *Ptr
= isLabelTail(CurPtr
)) {
240 StrVal
.assign(TokStart
, CurPtr
-1);
241 return lltok::LabelStr
;
243 if (CurPtr
[0] == '.' && CurPtr
[1] == '.') {
245 return lltok::dotdotdot
;
249 if (const char *Ptr
= isLabelTail(CurPtr
)) {
251 StrVal
.assign(TokStart
, CurPtr
-1);
252 return lltok::LabelStr
;
258 case '!': return LexExclaim();
259 case '0': case '1': case '2': case '3': case '4':
260 case '5': case '6': case '7': case '8': case '9':
262 return LexDigitOrNegative();
263 case '=': return lltok::equal
;
264 case '[': return lltok::lsquare
;
265 case ']': return lltok::rsquare
;
266 case '{': return lltok::lbrace
;
267 case '}': return lltok::rbrace
;
268 case '<': return lltok::less
;
269 case '>': return lltok::greater
;
270 case '(': return lltok::lparen
;
271 case ')': return lltok::rparen
;
272 case ',': return lltok::comma
;
273 case '*': return lltok::star
;
274 case '\\': return lltok::backslash
;
278 void LLLexer::SkipLineComment() {
280 if (CurPtr
[0] == '\n' || CurPtr
[0] == '\r' || getNextChar() == EOF
)
285 /// LexAt - Lex all tokens that start with an @ character:
286 /// GlobalVar @\"[^\"]*\"
287 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
288 /// GlobalVarID @[0-9]+
289 lltok::Kind
LLLexer::LexAt() {
290 // Handle AtStringConstant: @\"[^\"]*\"
291 if (CurPtr
[0] == '"') {
295 int CurChar
= getNextChar();
297 if (CurChar
== EOF
) {
298 Error("end of file in global variable name");
301 if (CurChar
== '"') {
302 StrVal
.assign(TokStart
+2, CurPtr
-1);
303 UnEscapeLexed(StrVal
);
304 return lltok::GlobalVar
;
309 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
310 if (isalpha(CurPtr
[0]) || CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
311 CurPtr
[0] == '.' || CurPtr
[0] == '_') {
313 while (isalnum(CurPtr
[0]) || CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
314 CurPtr
[0] == '.' || CurPtr
[0] == '_')
317 StrVal
.assign(TokStart
+1, CurPtr
); // Skip @
318 return lltok::GlobalVar
;
321 // Handle GlobalVarID: @[0-9]+
322 if (isdigit(CurPtr
[0])) {
323 for (++CurPtr
; isdigit(CurPtr
[0]); ++CurPtr
)
326 uint64_t Val
= atoull(TokStart
+1, CurPtr
);
327 if ((unsigned)Val
!= Val
)
328 Error("invalid value number (too large)!");
329 UIntVal
= unsigned(Val
);
330 return lltok::GlobalID
;
337 /// LexPercent - Lex all tokens that start with a % character:
338 /// LocalVar ::= %\"[^\"]*\"
339 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
340 /// LocalVarID ::= %[0-9]+
341 lltok::Kind
LLLexer::LexPercent() {
342 // Handle LocalVarName: %\"[^\"]*\"
343 if (CurPtr
[0] == '"') {
347 int CurChar
= getNextChar();
349 if (CurChar
== EOF
) {
350 Error("end of file in string constant");
353 if (CurChar
== '"') {
354 StrVal
.assign(TokStart
+2, CurPtr
-1);
355 UnEscapeLexed(StrVal
);
356 return lltok::LocalVar
;
361 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
362 if (isalpha(CurPtr
[0]) || CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
363 CurPtr
[0] == '.' || CurPtr
[0] == '_') {
365 while (isalnum(CurPtr
[0]) || CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
366 CurPtr
[0] == '.' || CurPtr
[0] == '_')
369 StrVal
.assign(TokStart
+1, CurPtr
); // Skip %
370 return lltok::LocalVar
;
373 // Handle LocalVarID: %[0-9]+
374 if (isdigit(CurPtr
[0])) {
375 for (++CurPtr
; isdigit(CurPtr
[0]); ++CurPtr
)
378 uint64_t Val
= atoull(TokStart
+1, CurPtr
);
379 if ((unsigned)Val
!= Val
)
380 Error("invalid value number (too large)!");
381 UIntVal
= unsigned(Val
);
382 return lltok::LocalVarID
;
388 /// LexQuote - Lex all tokens that start with a " character:
389 /// QuoteLabel "[^"]+":
390 /// StringConstant "[^"]*"
391 lltok::Kind
LLLexer::LexQuote() {
393 int CurChar
= getNextChar();
395 if (CurChar
== EOF
) {
396 Error("end of file in quoted string");
400 if (CurChar
!= '"') continue;
402 if (CurPtr
[0] != ':') {
403 StrVal
.assign(TokStart
+1, CurPtr
-1);
404 UnEscapeLexed(StrVal
);
405 return lltok::StringConstant
;
409 StrVal
.assign(TokStart
+1, CurPtr
-2);
410 UnEscapeLexed(StrVal
);
411 return lltok::LabelStr
;
415 static bool JustWhitespaceNewLine(const char *&Ptr
) {
416 const char *ThisPtr
= Ptr
;
417 while (*ThisPtr
== ' ' || *ThisPtr
== '\t')
419 if (*ThisPtr
== '\n' || *ThisPtr
== '\r') {
429 lltok::Kind
LLLexer::LexExclaim() {
430 // Lex a metadata name as a MetadataVar.
431 if (isalpha(CurPtr
[0])) {
433 while (isalnum(CurPtr
[0]) || CurPtr
[0] == '-' || CurPtr
[0] == '$' ||
434 CurPtr
[0] == '.' || CurPtr
[0] == '_')
437 StrVal
.assign(TokStart
+1, CurPtr
); // Skip !
438 return lltok::MetadataVar
;
440 return lltok::exclaim
;
443 /// LexIdentifier: Handle several related productions:
444 /// Label [-a-zA-Z$._0-9]+:
445 /// IntegerType i[0-9]+
446 /// Keyword sdiv, float, ...
447 /// HexIntConstant [us]0x[0-9A-Fa-f]+
448 lltok::Kind
LLLexer::LexIdentifier() {
449 const char *StartChar
= CurPtr
;
450 const char *IntEnd
= CurPtr
[-1] == 'i' ? 0 : StartChar
;
451 const char *KeywordEnd
= 0;
453 for (; isLabelChar(*CurPtr
); ++CurPtr
) {
454 // If we decide this is an integer, remember the end of the sequence.
455 if (!IntEnd
&& !isdigit(*CurPtr
)) IntEnd
= CurPtr
;
456 if (!KeywordEnd
&& !isalnum(*CurPtr
) && *CurPtr
!= '_') KeywordEnd
= CurPtr
;
459 // If we stopped due to a colon, this really is a label.
460 if (*CurPtr
== ':') {
461 StrVal
.assign(StartChar
-1, CurPtr
++);
462 return lltok::LabelStr
;
465 // Otherwise, this wasn't a label. If this was valid as an integer type,
467 if (IntEnd
== 0) IntEnd
= CurPtr
;
468 if (IntEnd
!= StartChar
) {
470 uint64_t NumBits
= atoull(StartChar
, CurPtr
);
471 if (NumBits
< IntegerType::MIN_INT_BITS
||
472 NumBits
> IntegerType::MAX_INT_BITS
) {
473 Error("bitwidth for integer type out of range!");
476 TyVal
= IntegerType::get(Context
, NumBits
);
480 // Otherwise, this was a letter sequence. See which keyword this is.
481 if (KeywordEnd
== 0) KeywordEnd
= CurPtr
;
484 unsigned Len
= CurPtr
-StartChar
;
485 #define KEYWORD(STR) \
486 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
487 return lltok::kw_##STR;
489 KEYWORD(begin
); KEYWORD(end
);
490 KEYWORD(true); KEYWORD(false);
491 KEYWORD(declare
); KEYWORD(define
);
492 KEYWORD(global
); KEYWORD(constant
);
495 KEYWORD(linker_private
);
496 KEYWORD(linker_private_weak
);
497 KEYWORD(linker_private_weak_def_auto
);
499 KEYWORD(available_externally
);
501 KEYWORD(linkonce_odr
);
511 KEYWORD(extern_weak
);
513 KEYWORD(thread_local
);
514 KEYWORD(zeroinitializer
);
541 KEYWORD(x86_stdcallcc
);
542 KEYWORD(x86_fastcallcc
);
543 KEYWORD(x86_thiscallcc
);
545 KEYWORD(arm_aapcscc
);
546 KEYWORD(arm_aapcs_vfpcc
);
547 KEYWORD(msp430_intrcc
);
569 KEYWORD(alwaysinline
);
574 KEYWORD(noimplicitfloat
);
581 KEYWORD(eq
); KEYWORD(ne
); KEYWORD(slt
); KEYWORD(sgt
); KEYWORD(sle
);
582 KEYWORD(sge
); KEYWORD(ult
); KEYWORD(ugt
); KEYWORD(ule
); KEYWORD(uge
);
583 KEYWORD(oeq
); KEYWORD(one
); KEYWORD(olt
); KEYWORD(ogt
); KEYWORD(ole
);
584 KEYWORD(oge
); KEYWORD(ord
); KEYWORD(uno
); KEYWORD(ueq
); KEYWORD(une
);
587 KEYWORD(blockaddress
);
590 // Keywords for types.
591 #define TYPEKEYWORD(STR, LLVMTY) \
592 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
593 TyVal = LLVMTY; return lltok::Type; }
594 TYPEKEYWORD("void", Type::getVoidTy(Context
));
595 TYPEKEYWORD("float", Type::getFloatTy(Context
));
596 TYPEKEYWORD("double", Type::getDoubleTy(Context
));
597 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context
));
598 TYPEKEYWORD("fp128", Type::getFP128Ty(Context
));
599 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context
));
600 TYPEKEYWORD("label", Type::getLabelTy(Context
));
601 TYPEKEYWORD("metadata", Type::getMetadataTy(Context
));
602 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context
));
605 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
606 // to avoid conflicting with the sext/zext instructions, below.
607 if (Len
== 4 && !memcmp(StartChar
, "sext", 4)) {
608 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
609 if (JustWhitespaceNewLine(CurPtr
))
610 return lltok::kw_signext
;
611 } else if (Len
== 4 && !memcmp(StartChar
, "zext", 4)) {
612 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
613 if (JustWhitespaceNewLine(CurPtr
))
614 return lltok::kw_zeroext
;
615 } else if (Len
== 6 && !memcmp(StartChar
, "malloc", 6)) {
616 // FIXME: Remove in LLVM 3.0.
617 // Autoupgrade malloc instruction.
618 return lltok::kw_malloc
;
619 } else if (Len
== 4 && !memcmp(StartChar
, "free", 4)) {
620 // FIXME: Remove in LLVM 3.0.
621 // Autoupgrade malloc instruction.
622 return lltok::kw_free
;
625 // Keywords for instructions.
626 #define INSTKEYWORD(STR, Enum) \
627 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
628 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
630 INSTKEYWORD(add
, Add
); INSTKEYWORD(fadd
, FAdd
);
631 INSTKEYWORD(sub
, Sub
); INSTKEYWORD(fsub
, FSub
);
632 INSTKEYWORD(mul
, Mul
); INSTKEYWORD(fmul
, FMul
);
633 INSTKEYWORD(udiv
, UDiv
); INSTKEYWORD(sdiv
, SDiv
); INSTKEYWORD(fdiv
, FDiv
);
634 INSTKEYWORD(urem
, URem
); INSTKEYWORD(srem
, SRem
); INSTKEYWORD(frem
, FRem
);
635 INSTKEYWORD(shl
, Shl
); INSTKEYWORD(lshr
, LShr
); INSTKEYWORD(ashr
, AShr
);
636 INSTKEYWORD(and, And
); INSTKEYWORD(or, Or
); INSTKEYWORD(xor, Xor
);
637 INSTKEYWORD(icmp
, ICmp
); INSTKEYWORD(fcmp
, FCmp
);
639 INSTKEYWORD(phi
, PHI
);
640 INSTKEYWORD(call
, Call
);
641 INSTKEYWORD(trunc
, Trunc
);
642 INSTKEYWORD(zext
, ZExt
);
643 INSTKEYWORD(sext
, SExt
);
644 INSTKEYWORD(fptrunc
, FPTrunc
);
645 INSTKEYWORD(fpext
, FPExt
);
646 INSTKEYWORD(uitofp
, UIToFP
);
647 INSTKEYWORD(sitofp
, SIToFP
);
648 INSTKEYWORD(fptoui
, FPToUI
);
649 INSTKEYWORD(fptosi
, FPToSI
);
650 INSTKEYWORD(inttoptr
, IntToPtr
);
651 INSTKEYWORD(ptrtoint
, PtrToInt
);
652 INSTKEYWORD(bitcast
, BitCast
);
653 INSTKEYWORD(select
, Select
);
654 INSTKEYWORD(va_arg
, VAArg
);
655 INSTKEYWORD(ret
, Ret
);
657 INSTKEYWORD(switch, Switch
);
658 INSTKEYWORD(indirectbr
, IndirectBr
);
659 INSTKEYWORD(invoke
, Invoke
);
660 INSTKEYWORD(unwind
, Unwind
);
661 INSTKEYWORD(unreachable
, Unreachable
);
663 INSTKEYWORD(alloca
, Alloca
);
664 INSTKEYWORD(load
, Load
);
665 INSTKEYWORD(store
, Store
);
666 INSTKEYWORD(getelementptr
, GetElementPtr
);
668 INSTKEYWORD(extractelement
, ExtractElement
);
669 INSTKEYWORD(insertelement
, InsertElement
);
670 INSTKEYWORD(shufflevector
, ShuffleVector
);
671 INSTKEYWORD(getresult
, ExtractValue
);
672 INSTKEYWORD(extractvalue
, ExtractValue
);
673 INSTKEYWORD(insertvalue
, InsertValue
);
676 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
677 // the CFE to avoid forcing it to deal with 64-bit numbers.
678 if ((TokStart
[0] == 'u' || TokStart
[0] == 's') &&
679 TokStart
[1] == '0' && TokStart
[2] == 'x' && isxdigit(TokStart
[3])) {
680 int len
= CurPtr
-TokStart
-3;
681 uint32_t bits
= len
* 4;
682 APInt
Tmp(bits
, StringRef(TokStart
+3, len
), 16);
683 uint32_t activeBits
= Tmp
.getActiveBits();
684 if (activeBits
> 0 && activeBits
< bits
)
685 Tmp
.trunc(activeBits
);
686 APSIntVal
= APSInt(Tmp
, TokStart
[0] == 'u');
687 return lltok::APSInt
;
690 // If this is "cc1234", return this as just "cc".
691 if (TokStart
[0] == 'c' && TokStart
[1] == 'c') {
696 // If this starts with "call", return it as CALL. This is to support old
697 // broken .ll files. FIXME: remove this with LLVM 3.0.
698 if (CurPtr
-TokStart
> 4 && !memcmp(TokStart
, "call", 4)) {
700 UIntVal
= Instruction::Call
;
701 return lltok::kw_call
;
704 // Finally, if this isn't known, return an error.
710 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
711 /// that this is not a label:
712 /// HexFPConstant 0x[0-9A-Fa-f]+
713 /// HexFP80Constant 0xK[0-9A-Fa-f]+
714 /// HexFP128Constant 0xL[0-9A-Fa-f]+
715 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
716 lltok::Kind
LLLexer::Lex0x() {
717 CurPtr
= TokStart
+ 2;
720 if (CurPtr
[0] >= 'K' && CurPtr
[0] <= 'M') {
726 if (!isxdigit(CurPtr
[0])) {
727 // Bad token, return it as an error.
732 while (isxdigit(CurPtr
[0]))
736 // HexFPConstant - Floating point constant represented in IEEE format as a
737 // hexadecimal number for when exponential notation is not precise enough.
738 // Float and double only.
739 APFloatVal
= APFloat(BitsToDouble(HexIntToVal(TokStart
+2, CurPtr
)));
740 return lltok::APFloat
;
745 default: llvm_unreachable("Unknown kind!");
747 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
748 FP80HexToIntPair(TokStart
+3, CurPtr
, Pair
);
749 APFloatVal
= APFloat(APInt(80, 2, Pair
));
750 return lltok::APFloat
;
752 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
753 HexToIntPair(TokStart
+3, CurPtr
, Pair
);
754 APFloatVal
= APFloat(APInt(128, 2, Pair
), true);
755 return lltok::APFloat
;
757 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
758 HexToIntPair(TokStart
+3, CurPtr
, Pair
);
759 APFloatVal
= APFloat(APInt(128, 2, Pair
));
760 return lltok::APFloat
;
764 /// LexIdentifier: Handle several related productions:
765 /// Label [-a-zA-Z$._0-9]+:
767 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
769 /// HexFPConstant 0x[0-9A-Fa-f]+
770 /// HexFP80Constant 0xK[0-9A-Fa-f]+
771 /// HexFP128Constant 0xL[0-9A-Fa-f]+
772 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
773 lltok::Kind
LLLexer::LexDigitOrNegative() {
774 // If the letter after the negative is a number, this is probably a label.
775 if (!isdigit(TokStart
[0]) && !isdigit(CurPtr
[0])) {
776 // Okay, this is not a number after the -, it's probably a label.
777 if (const char *End
= isLabelTail(CurPtr
)) {
778 StrVal
.assign(TokStart
, End
-1);
780 return lltok::LabelStr
;
786 // At this point, it is either a label, int or fp constant.
788 // Skip digits, we have at least one.
789 for (; isdigit(CurPtr
[0]); ++CurPtr
)
792 // Check to see if this really is a label afterall, e.g. "-1:".
793 if (isLabelChar(CurPtr
[0]) || CurPtr
[0] == ':') {
794 if (const char *End
= isLabelTail(CurPtr
)) {
795 StrVal
.assign(TokStart
, End
-1);
797 return lltok::LabelStr
;
801 // If the next character is a '.', then it is a fp value, otherwise its
803 if (CurPtr
[0] != '.') {
804 if (TokStart
[0] == '0' && TokStart
[1] == 'x')
806 unsigned Len
= CurPtr
-TokStart
;
807 uint32_t numBits
= ((Len
* 64) / 19) + 2;
808 APInt
Tmp(numBits
, StringRef(TokStart
, Len
), 10);
809 if (TokStart
[0] == '-') {
810 uint32_t minBits
= Tmp
.getMinSignedBits();
811 if (minBits
> 0 && minBits
< numBits
)
813 APSIntVal
= APSInt(Tmp
, false);
815 uint32_t activeBits
= Tmp
.getActiveBits();
816 if (activeBits
> 0 && activeBits
< numBits
)
817 Tmp
.trunc(activeBits
);
818 APSIntVal
= APSInt(Tmp
, true);
820 return lltok::APSInt
;
825 // Skip over [0-9]*([eE][-+]?[0-9]+)?
826 while (isdigit(CurPtr
[0])) ++CurPtr
;
828 if (CurPtr
[0] == 'e' || CurPtr
[0] == 'E') {
829 if (isdigit(CurPtr
[1]) ||
830 ((CurPtr
[1] == '-' || CurPtr
[1] == '+') && isdigit(CurPtr
[2]))) {
832 while (isdigit(CurPtr
[0])) ++CurPtr
;
836 APFloatVal
= APFloat(atof(TokStart
));
837 return lltok::APFloat
;
840 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
841 lltok::Kind
LLLexer::LexPositive() {
842 // If the letter after the negative is a number, this is probably not a
844 if (!isdigit(CurPtr
[0]))
848 for (++CurPtr
; isdigit(CurPtr
[0]); ++CurPtr
)
851 // At this point, we need a '.'.
852 if (CurPtr
[0] != '.') {
859 // Skip over [0-9]*([eE][-+]?[0-9]+)?
860 while (isdigit(CurPtr
[0])) ++CurPtr
;
862 if (CurPtr
[0] == 'e' || CurPtr
[0] == 'E') {
863 if (isdigit(CurPtr
[1]) ||
864 ((CurPtr
[1] == '-' || CurPtr
[1] == '+') && isdigit(CurPtr
[2]))) {
866 while (isdigit(CurPtr
[0])) ++CurPtr
;
870 APFloatVal
= APFloat(atof(TokStart
));
871 return lltok::APFloat
;