don't load element before checking to see if it is valid.
[llvm/stm8.git] / lib / AsmParser / LLLexer.cpp
blob3c63106e8c3b52f4dd089a1dcfcf19ed8724b41d
1 //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implement the Lexer for .ll files.
12 //===----------------------------------------------------------------------===//
14 #include "LLLexer.h"
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"
25 #include <cctype>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29 using namespace llvm;
31 bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
32 ErrorInfo = SM.GetMessage(ErrorLoc, Msg, "error");
33 return true;
36 //===----------------------------------------------------------------------===//
37 // Helper functions.
38 //===----------------------------------------------------------------------===//
40 // atoull - Convert an ascii string of decimal digits into the unsigned long
41 // long representation... this does not have to do input error checking,
42 // because we know that the input will be matched by a suitable regex...
44 uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
45 uint64_t Result = 0;
46 for (; Buffer != End; Buffer++) {
47 uint64_t OldRes = Result;
48 Result *= 10;
49 Result += *Buffer-'0';
50 if (Result < OldRes) { // Uh, oh, overflow detected!!!
51 Error("constant bigger than 64 bits detected!");
52 return 0;
55 return Result;
58 uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
59 uint64_t Result = 0;
60 for (; Buffer != End; ++Buffer) {
61 uint64_t OldRes = Result;
62 Result *= 16;
63 char C = *Buffer;
64 if (C >= '0' && C <= '9')
65 Result += C-'0';
66 else if (C >= 'A' && C <= 'F')
67 Result += C-'A'+10;
68 else if (C >= 'a' && C <= 'f')
69 Result += C-'a'+10;
71 if (Result < OldRes) { // Uh, oh, overflow detected!!!
72 Error("constant bigger than 64 bits detected!");
73 return 0;
76 return Result;
79 void LLLexer::HexToIntPair(const char *Buffer, const char *End,
80 uint64_t Pair[2]) {
81 Pair[0] = 0;
82 for (int i=0; i<16; i++, Buffer++) {
83 assert(Buffer != End);
84 Pair[0] *= 16;
85 char C = *Buffer;
86 if (C >= '0' && C <= '9')
87 Pair[0] += C-'0';
88 else if (C >= 'A' && C <= 'F')
89 Pair[0] += C-'A'+10;
90 else if (C >= 'a' && C <= 'f')
91 Pair[0] += C-'a'+10;
93 Pair[1] = 0;
94 for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
95 Pair[1] *= 16;
96 char C = *Buffer;
97 if (C >= '0' && C <= '9')
98 Pair[1] += C-'0';
99 else if (C >= 'A' && C <= 'F')
100 Pair[1] += C-'A'+10;
101 else if (C >= 'a' && C <= 'f')
102 Pair[1] += C-'a'+10;
104 if (Buffer != End)
105 Error("constant bigger than 128 bits detected!");
108 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
109 /// { low64, high16 } as usual for an APInt.
110 void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
111 uint64_t Pair[2]) {
112 Pair[1] = 0;
113 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
114 assert(Buffer != End);
115 Pair[1] *= 16;
116 char C = *Buffer;
117 if (C >= '0' && C <= '9')
118 Pair[1] += C-'0';
119 else if (C >= 'A' && C <= 'F')
120 Pair[1] += C-'A'+10;
121 else if (C >= 'a' && C <= 'f')
122 Pair[1] += C-'a'+10;
124 Pair[0] = 0;
125 for (int i=0; i<16; i++, Buffer++) {
126 Pair[0] *= 16;
127 char C = *Buffer;
128 if (C >= '0' && C <= '9')
129 Pair[0] += C-'0';
130 else if (C >= 'A' && C <= 'F')
131 Pair[0] += C-'A'+10;
132 else if (C >= 'a' && C <= 'f')
133 Pair[0] += C-'a'+10;
135 if (Buffer != End)
136 Error("constant bigger than 128 bits detected!");
139 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
140 // appropriate character.
141 static void UnEscapeLexed(std::string &Str) {
142 if (Str.empty()) return;
144 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
145 char *BOut = Buffer;
146 for (char *BIn = Buffer; BIn != EndBuffer; ) {
147 if (BIn[0] == '\\') {
148 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
149 *BOut++ = '\\'; // Two \ becomes one
150 BIn += 2;
151 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
152 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
153 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
154 BIn[3] = Tmp; // Restore character
155 BIn += 3; // Skip over handled chars
156 ++BOut;
157 } else {
158 *BOut++ = *BIn++;
160 } else {
161 *BOut++ = *BIn++;
164 Str.resize(BOut-Buffer);
167 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
168 static bool isLabelChar(char C) {
169 return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
173 /// isLabelTail - Return true if this pointer points to a valid end of a label.
174 static const char *isLabelTail(const char *CurPtr) {
175 while (1) {
176 if (CurPtr[0] == ':') return CurPtr+1;
177 if (!isLabelChar(CurPtr[0])) return 0;
178 ++CurPtr;
184 //===----------------------------------------------------------------------===//
185 // Lexer definition.
186 //===----------------------------------------------------------------------===//
188 LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err,
189 LLVMContext &C)
190 : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
191 CurPtr = CurBuf->getBufferStart();
194 std::string LLLexer::getFilename() const {
195 return CurBuf->getBufferIdentifier();
198 int LLLexer::getNextChar() {
199 char CurChar = *CurPtr++;
200 switch (CurChar) {
201 default: return (unsigned char)CurChar;
202 case 0:
203 // A nul character in the stream is either the end of the current buffer or
204 // a random nul in the file. Disambiguate that here.
205 if (CurPtr-1 != CurBuf->getBufferEnd())
206 return 0; // Just whitespace.
208 // Otherwise, return end of file.
209 --CurPtr; // Another call to lex will return EOF again.
210 return EOF;
215 lltok::Kind LLLexer::LexToken() {
216 TokStart = CurPtr;
218 int CurChar = getNextChar();
219 switch (CurChar) {
220 default:
221 // Handle letters: [a-zA-Z_]
222 if (isalpha(CurChar) || CurChar == '_')
223 return LexIdentifier();
225 return lltok::Error;
226 case EOF: return lltok::Eof;
227 case 0:
228 case ' ':
229 case '\t':
230 case '\n':
231 case '\r':
232 // Ignore whitespace.
233 return LexToken();
234 case '+': return LexPositive();
235 case '@': return LexAt();
236 case '%': return LexPercent();
237 case '"': return LexQuote();
238 case '.':
239 if (const char *Ptr = isLabelTail(CurPtr)) {
240 CurPtr = Ptr;
241 StrVal.assign(TokStart, CurPtr-1);
242 return lltok::LabelStr;
244 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
245 CurPtr += 2;
246 return lltok::dotdotdot;
248 return lltok::Error;
249 case '$':
250 if (const char *Ptr = isLabelTail(CurPtr)) {
251 CurPtr = Ptr;
252 StrVal.assign(TokStart, CurPtr-1);
253 return lltok::LabelStr;
255 return lltok::Error;
256 case ';':
257 SkipLineComment();
258 return LexToken();
259 case '!': return LexExclaim();
260 case '0': case '1': case '2': case '3': case '4':
261 case '5': case '6': case '7': case '8': case '9':
262 case '-':
263 return LexDigitOrNegative();
264 case '=': return lltok::equal;
265 case '[': return lltok::lsquare;
266 case ']': return lltok::rsquare;
267 case '{': return lltok::lbrace;
268 case '}': return lltok::rbrace;
269 case '<': return lltok::less;
270 case '>': return lltok::greater;
271 case '(': return lltok::lparen;
272 case ')': return lltok::rparen;
273 case ',': return lltok::comma;
274 case '*': return lltok::star;
275 case '\\': return lltok::backslash;
279 void LLLexer::SkipLineComment() {
280 while (1) {
281 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
282 return;
286 /// LexAt - Lex all tokens that start with an @ character:
287 /// GlobalVar @\"[^\"]*\"
288 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
289 /// GlobalVarID @[0-9]+
290 lltok::Kind LLLexer::LexAt() {
291 // Handle AtStringConstant: @\"[^\"]*\"
292 if (CurPtr[0] == '"') {
293 ++CurPtr;
295 while (1) {
296 int CurChar = getNextChar();
298 if (CurChar == EOF) {
299 Error("end of file in global variable name");
300 return lltok::Error;
302 if (CurChar == '"') {
303 StrVal.assign(TokStart+2, CurPtr-1);
304 UnEscapeLexed(StrVal);
305 return lltok::GlobalVar;
310 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
311 if (ReadVarName())
312 return lltok::GlobalVar;
314 // Handle GlobalVarID: @[0-9]+
315 if (isdigit(CurPtr[0])) {
316 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
317 /*empty*/;
319 uint64_t Val = atoull(TokStart+1, CurPtr);
320 if ((unsigned)Val != Val)
321 Error("invalid value number (too large)!");
322 UIntVal = unsigned(Val);
323 return lltok::GlobalID;
326 return lltok::Error;
329 /// ReadString - Read a string until the closing quote.
330 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
331 const char *Start = CurPtr;
332 while (1) {
333 int CurChar = getNextChar();
335 if (CurChar == EOF) {
336 Error("end of file in string constant");
337 return lltok::Error;
339 if (CurChar == '"') {
340 StrVal.assign(Start, CurPtr-1);
341 UnEscapeLexed(StrVal);
342 return kind;
347 /// ReadVarName - Read the rest of a token containing a variable name.
348 bool LLLexer::ReadVarName() {
349 const char *NameStart = CurPtr;
350 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
351 CurPtr[0] == '.' || CurPtr[0] == '_') {
352 ++CurPtr;
353 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
354 CurPtr[0] == '.' || CurPtr[0] == '_')
355 ++CurPtr;
357 StrVal.assign(NameStart, CurPtr);
358 return true;
360 return false;
363 /// LexPercent - Lex all tokens that start with a % character:
364 /// LocalVar ::= %\"[^\"]*\"
365 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
366 /// LocalVarID ::= %[0-9]+
367 lltok::Kind LLLexer::LexPercent() {
368 // Handle LocalVarName: %\"[^\"]*\"
369 if (CurPtr[0] == '"') {
370 ++CurPtr;
371 return ReadString(lltok::LocalVar);
374 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
375 if (ReadVarName())
376 return lltok::LocalVar;
378 // Handle LocalVarID: %[0-9]+
379 if (isdigit(CurPtr[0])) {
380 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
381 /*empty*/;
383 uint64_t Val = atoull(TokStart+1, CurPtr);
384 if ((unsigned)Val != Val)
385 Error("invalid value number (too large)!");
386 UIntVal = unsigned(Val);
387 return lltok::LocalVarID;
390 return lltok::Error;
393 /// LexQuote - Lex all tokens that start with a " character:
394 /// QuoteLabel "[^"]+":
395 /// StringConstant "[^"]*"
396 lltok::Kind LLLexer::LexQuote() {
397 lltok::Kind kind = ReadString(lltok::StringConstant);
398 if (kind == lltok::Error || kind == lltok::Eof)
399 return kind;
401 if (CurPtr[0] == ':') {
402 ++CurPtr;
403 kind = lltok::LabelStr;
406 return kind;
409 /// LexExclaim:
410 /// !foo
411 /// !
412 lltok::Kind LLLexer::LexExclaim() {
413 // Lex a metadata name as a MetadataVar.
414 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
415 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
416 ++CurPtr;
417 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
418 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
419 ++CurPtr;
421 StrVal.assign(TokStart+1, CurPtr); // Skip !
422 UnEscapeLexed(StrVal);
423 return lltok::MetadataVar;
425 return lltok::exclaim;
428 /// LexIdentifier: Handle several related productions:
429 /// Label [-a-zA-Z$._0-9]+:
430 /// IntegerType i[0-9]+
431 /// Keyword sdiv, float, ...
432 /// HexIntConstant [us]0x[0-9A-Fa-f]+
433 lltok::Kind LLLexer::LexIdentifier() {
434 const char *StartChar = CurPtr;
435 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
436 const char *KeywordEnd = 0;
438 for (; isLabelChar(*CurPtr); ++CurPtr) {
439 // If we decide this is an integer, remember the end of the sequence.
440 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
441 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
444 // If we stopped due to a colon, this really is a label.
445 if (*CurPtr == ':') {
446 StrVal.assign(StartChar-1, CurPtr++);
447 return lltok::LabelStr;
450 // Otherwise, this wasn't a label. If this was valid as an integer type,
451 // return it.
452 if (IntEnd == 0) IntEnd = CurPtr;
453 if (IntEnd != StartChar) {
454 CurPtr = IntEnd;
455 uint64_t NumBits = atoull(StartChar, CurPtr);
456 if (NumBits < IntegerType::MIN_INT_BITS ||
457 NumBits > IntegerType::MAX_INT_BITS) {
458 Error("bitwidth for integer type out of range!");
459 return lltok::Error;
461 TyVal = IntegerType::get(Context, NumBits);
462 return lltok::Type;
465 // Otherwise, this was a letter sequence. See which keyword this is.
466 if (KeywordEnd == 0) KeywordEnd = CurPtr;
467 CurPtr = KeywordEnd;
468 --StartChar;
469 unsigned Len = CurPtr-StartChar;
470 #define KEYWORD(STR) \
471 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
472 return lltok::kw_##STR;
474 KEYWORD(true); KEYWORD(false);
475 KEYWORD(declare); KEYWORD(define);
476 KEYWORD(global); KEYWORD(constant);
478 KEYWORD(private);
479 KEYWORD(linker_private);
480 KEYWORD(linker_private_weak);
481 KEYWORD(linker_private_weak_def_auto);
482 KEYWORD(internal);
483 KEYWORD(available_externally);
484 KEYWORD(linkonce);
485 KEYWORD(linkonce_odr);
486 KEYWORD(weak);
487 KEYWORD(weak_odr);
488 KEYWORD(appending);
489 KEYWORD(dllimport);
490 KEYWORD(dllexport);
491 KEYWORD(common);
492 KEYWORD(default);
493 KEYWORD(hidden);
494 KEYWORD(protected);
495 KEYWORD(unnamed_addr);
496 KEYWORD(extern_weak);
497 KEYWORD(external);
498 KEYWORD(thread_local);
499 KEYWORD(zeroinitializer);
500 KEYWORD(undef);
501 KEYWORD(null);
502 KEYWORD(to);
503 KEYWORD(tail);
504 KEYWORD(target);
505 KEYWORD(triple);
506 KEYWORD(deplibs);
507 KEYWORD(datalayout);
508 KEYWORD(volatile);
509 KEYWORD(nuw);
510 KEYWORD(nsw);
511 KEYWORD(exact);
512 KEYWORD(inbounds);
513 KEYWORD(align);
514 KEYWORD(addrspace);
515 KEYWORD(section);
516 KEYWORD(alias);
517 KEYWORD(module);
518 KEYWORD(asm);
519 KEYWORD(sideeffect);
520 KEYWORD(alignstack);
521 KEYWORD(gc);
523 KEYWORD(ccc);
524 KEYWORD(fastcc);
525 KEYWORD(coldcc);
526 KEYWORD(x86_stdcallcc);
527 KEYWORD(x86_fastcallcc);
528 KEYWORD(x86_thiscallcc);
529 KEYWORD(arm_apcscc);
530 KEYWORD(arm_aapcscc);
531 KEYWORD(arm_aapcs_vfpcc);
532 KEYWORD(msp430_intrcc);
533 KEYWORD(ptx_kernel);
534 KEYWORD(ptx_device);
536 KEYWORD(cc);
537 KEYWORD(c);
539 KEYWORD(signext);
540 KEYWORD(zeroext);
541 KEYWORD(inreg);
542 KEYWORD(sret);
543 KEYWORD(nounwind);
544 KEYWORD(noreturn);
545 KEYWORD(noalias);
546 KEYWORD(nocapture);
547 KEYWORD(byval);
548 KEYWORD(nest);
549 KEYWORD(readnone);
550 KEYWORD(readonly);
551 KEYWORD(uwtable);
553 KEYWORD(inlinehint);
554 KEYWORD(noinline);
555 KEYWORD(alwaysinline);
556 KEYWORD(optsize);
557 KEYWORD(ssp);
558 KEYWORD(sspreq);
559 KEYWORD(noredzone);
560 KEYWORD(noimplicitfloat);
561 KEYWORD(naked);
562 KEYWORD(hotpatch);
563 KEYWORD(nonlazybind);
565 KEYWORD(type);
566 KEYWORD(opaque);
568 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
569 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
570 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
571 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
573 KEYWORD(x);
574 KEYWORD(blockaddress);
575 #undef KEYWORD
577 // Keywords for types.
578 #define TYPEKEYWORD(STR, LLVMTY) \
579 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
580 TyVal = LLVMTY; return lltok::Type; }
581 TYPEKEYWORD("void", Type::getVoidTy(Context));
582 TYPEKEYWORD("float", Type::getFloatTy(Context));
583 TYPEKEYWORD("double", Type::getDoubleTy(Context));
584 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
585 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
586 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
587 TYPEKEYWORD("label", Type::getLabelTy(Context));
588 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
589 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
590 #undef TYPEKEYWORD
592 // Keywords for instructions.
593 #define INSTKEYWORD(STR, Enum) \
594 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
595 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
597 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
598 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
599 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
600 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
601 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
602 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
603 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
604 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
606 INSTKEYWORD(phi, PHI);
607 INSTKEYWORD(call, Call);
608 INSTKEYWORD(trunc, Trunc);
609 INSTKEYWORD(zext, ZExt);
610 INSTKEYWORD(sext, SExt);
611 INSTKEYWORD(fptrunc, FPTrunc);
612 INSTKEYWORD(fpext, FPExt);
613 INSTKEYWORD(uitofp, UIToFP);
614 INSTKEYWORD(sitofp, SIToFP);
615 INSTKEYWORD(fptoui, FPToUI);
616 INSTKEYWORD(fptosi, FPToSI);
617 INSTKEYWORD(inttoptr, IntToPtr);
618 INSTKEYWORD(ptrtoint, PtrToInt);
619 INSTKEYWORD(bitcast, BitCast);
620 INSTKEYWORD(select, Select);
621 INSTKEYWORD(va_arg, VAArg);
622 INSTKEYWORD(ret, Ret);
623 INSTKEYWORD(br, Br);
624 INSTKEYWORD(switch, Switch);
625 INSTKEYWORD(indirectbr, IndirectBr);
626 INSTKEYWORD(invoke, Invoke);
627 INSTKEYWORD(unwind, Unwind);
628 INSTKEYWORD(unreachable, Unreachable);
630 INSTKEYWORD(alloca, Alloca);
631 INSTKEYWORD(load, Load);
632 INSTKEYWORD(store, Store);
633 INSTKEYWORD(getelementptr, GetElementPtr);
635 INSTKEYWORD(extractelement, ExtractElement);
636 INSTKEYWORD(insertelement, InsertElement);
637 INSTKEYWORD(shufflevector, ShuffleVector);
638 INSTKEYWORD(extractvalue, ExtractValue);
639 INSTKEYWORD(insertvalue, InsertValue);
640 #undef INSTKEYWORD
642 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
643 // the CFE to avoid forcing it to deal with 64-bit numbers.
644 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
645 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
646 int len = CurPtr-TokStart-3;
647 uint32_t bits = len * 4;
648 APInt Tmp(bits, StringRef(TokStart+3, len), 16);
649 uint32_t activeBits = Tmp.getActiveBits();
650 if (activeBits > 0 && activeBits < bits)
651 Tmp = Tmp.trunc(activeBits);
652 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
653 return lltok::APSInt;
656 // If this is "cc1234", return this as just "cc".
657 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
658 CurPtr = TokStart+2;
659 return lltok::kw_cc;
662 // Finally, if this isn't known, return an error.
663 CurPtr = TokStart+1;
664 return lltok::Error;
668 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
669 /// that this is not a label:
670 /// HexFPConstant 0x[0-9A-Fa-f]+
671 /// HexFP80Constant 0xK[0-9A-Fa-f]+
672 /// HexFP128Constant 0xL[0-9A-Fa-f]+
673 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
674 lltok::Kind LLLexer::Lex0x() {
675 CurPtr = TokStart + 2;
677 char Kind;
678 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
679 Kind = *CurPtr++;
680 } else {
681 Kind = 'J';
684 if (!isxdigit(CurPtr[0])) {
685 // Bad token, return it as an error.
686 CurPtr = TokStart+1;
687 return lltok::Error;
690 while (isxdigit(CurPtr[0]))
691 ++CurPtr;
693 if (Kind == 'J') {
694 // HexFPConstant - Floating point constant represented in IEEE format as a
695 // hexadecimal number for when exponential notation is not precise enough.
696 // Float and double only.
697 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
698 return lltok::APFloat;
701 uint64_t Pair[2];
702 switch (Kind) {
703 default: llvm_unreachable("Unknown kind!");
704 case 'K':
705 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
706 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
707 APFloatVal = APFloat(APInt(80, 2, Pair));
708 return lltok::APFloat;
709 case 'L':
710 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
711 HexToIntPair(TokStart+3, CurPtr, Pair);
712 APFloatVal = APFloat(APInt(128, 2, Pair), true);
713 return lltok::APFloat;
714 case 'M':
715 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
716 HexToIntPair(TokStart+3, CurPtr, Pair);
717 APFloatVal = APFloat(APInt(128, 2, Pair));
718 return lltok::APFloat;
722 /// LexIdentifier: Handle several related productions:
723 /// Label [-a-zA-Z$._0-9]+:
724 /// NInteger -[0-9]+
725 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
726 /// PInteger [0-9]+
727 /// HexFPConstant 0x[0-9A-Fa-f]+
728 /// HexFP80Constant 0xK[0-9A-Fa-f]+
729 /// HexFP128Constant 0xL[0-9A-Fa-f]+
730 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
731 lltok::Kind LLLexer::LexDigitOrNegative() {
732 // If the letter after the negative is a number, this is probably a label.
733 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
734 // Okay, this is not a number after the -, it's probably a label.
735 if (const char *End = isLabelTail(CurPtr)) {
736 StrVal.assign(TokStart, End-1);
737 CurPtr = End;
738 return lltok::LabelStr;
741 return lltok::Error;
744 // At this point, it is either a label, int or fp constant.
746 // Skip digits, we have at least one.
747 for (; isdigit(CurPtr[0]); ++CurPtr)
748 /*empty*/;
750 // Check to see if this really is a label afterall, e.g. "-1:".
751 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
752 if (const char *End = isLabelTail(CurPtr)) {
753 StrVal.assign(TokStart, End-1);
754 CurPtr = End;
755 return lltok::LabelStr;
759 // If the next character is a '.', then it is a fp value, otherwise its
760 // integer.
761 if (CurPtr[0] != '.') {
762 if (TokStart[0] == '0' && TokStart[1] == 'x')
763 return Lex0x();
764 unsigned Len = CurPtr-TokStart;
765 uint32_t numBits = ((Len * 64) / 19) + 2;
766 APInt Tmp(numBits, StringRef(TokStart, Len), 10);
767 if (TokStart[0] == '-') {
768 uint32_t minBits = Tmp.getMinSignedBits();
769 if (minBits > 0 && minBits < numBits)
770 Tmp = Tmp.trunc(minBits);
771 APSIntVal = APSInt(Tmp, false);
772 } else {
773 uint32_t activeBits = Tmp.getActiveBits();
774 if (activeBits > 0 && activeBits < numBits)
775 Tmp = Tmp.trunc(activeBits);
776 APSIntVal = APSInt(Tmp, true);
778 return lltok::APSInt;
781 ++CurPtr;
783 // Skip over [0-9]*([eE][-+]?[0-9]+)?
784 while (isdigit(CurPtr[0])) ++CurPtr;
786 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
787 if (isdigit(CurPtr[1]) ||
788 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
789 CurPtr += 2;
790 while (isdigit(CurPtr[0])) ++CurPtr;
794 APFloatVal = APFloat(std::atof(TokStart));
795 return lltok::APFloat;
798 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
799 lltok::Kind LLLexer::LexPositive() {
800 // If the letter after the negative is a number, this is probably not a
801 // label.
802 if (!isdigit(CurPtr[0]))
803 return lltok::Error;
805 // Skip digits.
806 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
807 /*empty*/;
809 // At this point, we need a '.'.
810 if (CurPtr[0] != '.') {
811 CurPtr = TokStart+1;
812 return lltok::Error;
815 ++CurPtr;
817 // Skip over [0-9]*([eE][-+]?[0-9]+)?
818 while (isdigit(CurPtr[0])) ++CurPtr;
820 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
821 if (isdigit(CurPtr[1]) ||
822 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
823 CurPtr += 2;
824 while (isdigit(CurPtr[0])) ++CurPtr;
828 APFloatVal = APFloat(std::atof(TokStart));
829 return lltok::APFloat;