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 "ParserInternals.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Support/MathExtras.h"
20 #include "llvmAsmParser.h"
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 // atoull - Convert an ascii string of decimal digits into the unsigned long
28 // long representation... this does not have to do input error checking,
29 // because we know that the input will be matched by a suitable regex...
31 static uint64_t atoull(const char *Buffer, const char *End) {
33 for (; Buffer != End; Buffer++) {
34 uint64_t OldRes = Result;
36 Result += *Buffer-'0';
37 if (Result < OldRes) { // Uh, oh, overflow detected!!!
38 GenerateError("constant bigger than 64 bits detected!");
45 static uint64_t HexIntToVal(const char *Buffer, const char *End) {
47 for (; Buffer != End; ++Buffer) {
48 uint64_t OldRes = Result;
51 if (C >= '0' && C <= '9')
53 else if (C >= 'A' && C <= 'F')
55 else if (C >= 'a' && C <= 'f')
58 if (Result < OldRes) { // Uh, oh, overflow detected!!!
59 GenerateError("constant bigger than 64 bits detected!");
66 // HexToFP - Convert the ascii string in hexadecimal format to the floating
67 // point representation of it.
69 static double HexToFP(const char *Buffer, const char *End) {
70 return BitsToDouble(HexIntToVal(Buffer, End)); // Cast Hex constant to double
73 static void HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]){
75 for (int i=0; i<16; i++, Buffer++) {
76 assert(Buffer != End);
79 if (C >= '0' && C <= '9')
81 else if (C >= 'A' && C <= 'F')
83 else if (C >= 'a' && C <= 'f')
87 for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
90 if (C >= '0' && C <= '9')
92 else if (C >= 'A' && C <= 'F')
94 else if (C >= 'a' && C <= 'f')
98 GenerateError("constant bigger than 128 bits detected!");
101 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
102 // appropriate character.
103 static void UnEscapeLexed(std::string &Str) {
104 if (Str.empty()) return;
106 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
108 for (char *BIn = Buffer; BIn != EndBuffer; ) {
109 if (BIn[0] == '\\') {
110 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
111 *BOut++ = '\\'; // Two \ becomes one
113 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
114 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
115 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
116 BIn[3] = Tmp; // Restore character
117 BIn += 3; // Skip over handled chars
126 Str.resize(BOut-Buffer);
129 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
130 static bool isLabelChar(char C) {
131 return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
135 /// isLabelTail - Return true if this pointer points to a valid end of a label.
136 static const char *isLabelTail(const char *CurPtr) {
138 if (CurPtr[0] == ':') return CurPtr+1;
139 if (!isLabelChar(CurPtr[0])) return 0;
146 //===----------------------------------------------------------------------===//
148 //===----------------------------------------------------------------------===//
150 // FIXME: REMOVE THIS.
154 LLLexer::LLLexer(MemoryBuffer *StartBuf) : CurLineNo(1), CurBuf(StartBuf) {
155 CurPtr = CurBuf->getBufferStart();
158 std::string LLLexer::getFilename() const {
159 return CurBuf->getBufferIdentifier();
162 int LLLexer::getNextChar() {
163 char CurChar = *CurPtr++;
165 default: return (unsigned char)CurChar;
167 // A nul character in the stream is either the end of the current buffer or
168 // a random nul in the file. Disambiguate that here.
169 if (CurPtr-1 != CurBuf->getBufferEnd())
170 return 0; // Just whitespace.
172 // Otherwise, return end of file.
173 --CurPtr; // Another call to lex will return EOF again.
177 // Handle the newline character by ignoring it and incrementing the line
178 // count. However, be careful about 'dos style' files with \n\r in them.
179 // Only treat a \n\r or \r\n as a single line.
180 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
182 ++CurPtr; // Eat the two char newline sequence.
190 int LLLexer::LexToken() {
193 int CurChar = getNextChar();
197 // Handle letters: [a-zA-Z_]
198 if (isalpha(CurChar) || CurChar == '_')
199 return LexIdentifier();
202 case EOF: return YYEOF;
208 // Ignore whitespace.
210 case '+': return LexPositive();
211 case '@': return LexAt();
212 case '%': return LexPercent();
213 case '"': return LexQuote();
215 if (const char *Ptr = isLabelTail(CurPtr)) {
217 llvmAsmlval.StrVal = new std::string(TokStart, CurPtr-1);
220 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
226 if (const char *Ptr = isLabelTail(CurPtr)) {
228 llvmAsmlval.StrVal = new std::string(TokStart, CurPtr-1);
235 case '0': case '1': case '2': case '3': case '4':
236 case '5': case '6': case '7': case '8': case '9':
238 return LexDigitOrNegative();
242 void LLLexer::SkipLineComment() {
244 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
249 /// LexAt - Lex all tokens that start with an @ character:
250 /// AtStringConstant @\"[^\"]*\"
251 /// GlobalVarName @[-a-zA-Z$._][-a-zA-Z$._0-9]*
252 /// GlobalVarID @[0-9]+
253 int LLLexer::LexAt() {
254 // Handle AtStringConstant: @\"[^\"]*\"
255 if (CurPtr[0] == '"') {
259 int CurChar = getNextChar();
261 if (CurChar == EOF) {
262 GenerateError("End of file in global variable name");
265 if (CurChar == '"') {
266 llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
267 UnEscapeLexed(*llvmAsmlval.StrVal);
268 return ATSTRINGCONSTANT;
273 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
274 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
275 CurPtr[0] == '.' || CurPtr[0] == '_') {
277 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
278 CurPtr[0] == '.' || CurPtr[0] == '_')
281 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr); // Skip @
285 // Handle GlobalVarID: @[0-9]+
286 if (isdigit(CurPtr[0])) {
287 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
290 uint64_t Val = atoull(TokStart+1, CurPtr);
291 if ((unsigned)Val != Val)
292 GenerateError("Invalid value number (too large)!");
293 llvmAsmlval.UIntVal = unsigned(Val);
301 /// LexPercent - Lex all tokens that start with a % character:
302 /// PctStringConstant %\"[^\"]*\"
303 /// LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
304 /// LocalVarID %[0-9]+
305 int LLLexer::LexPercent() {
306 // Handle PctStringConstant: %\"[^\"]*\"
307 if (CurPtr[0] == '"') {
311 int CurChar = getNextChar();
313 if (CurChar == EOF) {
314 GenerateError("End of file in local variable name");
317 if (CurChar == '"') {
318 llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
319 UnEscapeLexed(*llvmAsmlval.StrVal);
320 return PCTSTRINGCONSTANT;
325 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
326 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
327 CurPtr[0] == '.' || CurPtr[0] == '_') {
329 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
330 CurPtr[0] == '.' || CurPtr[0] == '_')
333 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr); // Skip %
337 // Handle LocalVarID: %[0-9]+
338 if (isdigit(CurPtr[0])) {
339 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
342 uint64_t Val = atoull(TokStart+1, CurPtr);
343 if ((unsigned)Val != Val)
344 GenerateError("Invalid value number (too large)!");
345 llvmAsmlval.UIntVal = unsigned(Val);
352 /// LexQuote - Lex all tokens that start with a " character:
353 /// QuoteLabel "[^"]+":
354 /// StringConstant "[^"]*"
355 int LLLexer::LexQuote() {
357 int CurChar = getNextChar();
359 if (CurChar == EOF) {
360 GenerateError("End of file in quoted string");
364 if (CurChar != '"') continue;
366 if (CurPtr[0] != ':') {
367 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-1);
368 UnEscapeLexed(*llvmAsmlval.StrVal);
369 return STRINGCONSTANT;
373 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-2);
374 UnEscapeLexed(*llvmAsmlval.StrVal);
379 static bool JustWhitespaceNewLine(const char *&Ptr) {
380 const char *ThisPtr = Ptr;
381 while (*ThisPtr == ' ' || *ThisPtr == '\t')
383 if (*ThisPtr == '\n' || *ThisPtr == '\r') {
391 /// LexIdentifier: Handle several related productions:
392 /// Label [-a-zA-Z$._0-9]+:
393 /// IntegerType i[0-9]+
394 /// Keyword sdiv, float, ...
395 /// HexIntConstant [us]0x[0-9A-Fa-f]+
396 int LLLexer::LexIdentifier() {
397 const char *StartChar = CurPtr;
398 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
399 const char *KeywordEnd = 0;
401 for (; isLabelChar(*CurPtr); ++CurPtr) {
402 // If we decide this is an integer, remember the end of the sequence.
403 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
404 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
407 // If we stopped due to a colon, this really is a label.
408 if (*CurPtr == ':') {
409 llvmAsmlval.StrVal = new std::string(StartChar-1, CurPtr++);
413 // Otherwise, this wasn't a label. If this was valid as an integer type,
415 if (IntEnd == 0) IntEnd = CurPtr;
416 if (IntEnd != StartChar) {
418 uint64_t NumBits = atoull(StartChar, CurPtr);
419 if (NumBits < IntegerType::MIN_INT_BITS ||
420 NumBits > IntegerType::MAX_INT_BITS) {
421 GenerateError("Bitwidth for integer type out of range!");
424 const Type* Ty = IntegerType::get(NumBits);
425 llvmAsmlval.PrimType = Ty;
429 // Otherwise, this was a letter sequence. See which keyword this is.
430 if (KeywordEnd == 0) KeywordEnd = CurPtr;
433 unsigned Len = CurPtr-StartChar;
434 #define KEYWORD(STR, TOK) \
435 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) return TOK;
437 KEYWORD("begin", BEGINTOK);
438 KEYWORD("end", ENDTOK);
439 KEYWORD("true", TRUETOK);
440 KEYWORD("false", FALSETOK);
441 KEYWORD("declare", DECLARE);
442 KEYWORD("define", DEFINE);
443 KEYWORD("global", GLOBAL);
444 KEYWORD("constant", CONSTANT);
446 KEYWORD("internal", INTERNAL);
447 KEYWORD("linkonce", LINKONCE);
448 KEYWORD("weak", WEAK);
449 KEYWORD("appending", APPENDING);
450 KEYWORD("dllimport", DLLIMPORT);
451 KEYWORD("dllexport", DLLEXPORT);
452 KEYWORD("hidden", HIDDEN);
453 KEYWORD("protected", PROTECTED);
454 KEYWORD("extern_weak", EXTERN_WEAK);
455 KEYWORD("external", EXTERNAL);
456 KEYWORD("thread_local", THREAD_LOCAL);
457 KEYWORD("zeroinitializer", ZEROINITIALIZER);
458 KEYWORD("undef", UNDEF);
459 KEYWORD("null", NULL_TOK);
461 KEYWORD("tail", TAIL);
462 KEYWORD("target", TARGET);
463 KEYWORD("triple", TRIPLE);
464 KEYWORD("deplibs", DEPLIBS);
465 KEYWORD("datalayout", DATALAYOUT);
466 KEYWORD("volatile", VOLATILE);
467 KEYWORD("align", ALIGN);
468 KEYWORD("addrspace", ADDRSPACE);
469 KEYWORD("section", SECTION);
470 KEYWORD("alias", ALIAS);
471 KEYWORD("module", MODULE);
472 KEYWORD("asm", ASM_TOK);
473 KEYWORD("sideeffect", SIDEEFFECT);
476 KEYWORD("cc", CC_TOK);
477 KEYWORD("ccc", CCC_TOK);
478 KEYWORD("fastcc", FASTCC_TOK);
479 KEYWORD("coldcc", COLDCC_TOK);
480 KEYWORD("x86_stdcallcc", X86_STDCALLCC_TOK);
481 KEYWORD("x86_fastcallcc", X86_FASTCALLCC_TOK);
483 KEYWORD("signext", SIGNEXT);
484 KEYWORD("zeroext", ZEROEXT);
485 KEYWORD("inreg", INREG);
486 KEYWORD("sret", SRET);
487 KEYWORD("nounwind", NOUNWIND);
488 KEYWORD("noreturn", NORETURN);
489 KEYWORD("noalias", NOALIAS);
490 KEYWORD("byval", BYVAL);
491 KEYWORD("nest", NEST);
492 KEYWORD("readnone", READNONE);
493 KEYWORD("readonly", READONLY);
495 KEYWORD("type", TYPE);
496 KEYWORD("opaque", OPAQUE);
520 // Keywords for types.
521 #define TYPEKEYWORD(STR, LLVMTY, TOK) \
522 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
523 llvmAsmlval.PrimType = LLVMTY; return TOK; }
524 TYPEKEYWORD("void", Type::VoidTy, VOID);
525 TYPEKEYWORD("float", Type::FloatTy, FLOAT);
526 TYPEKEYWORD("double", Type::DoubleTy, DOUBLE);
527 TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty, X86_FP80);
528 TYPEKEYWORD("fp128", Type::FP128Ty, FP128);
529 TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty, PPC_FP128);
530 TYPEKEYWORD("label", Type::LabelTy, LABEL);
533 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
534 // to avoid conflicting with the sext/zext instructions, below.
535 if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
536 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
537 if (JustWhitespaceNewLine(CurPtr))
539 } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
540 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
541 if (JustWhitespaceNewLine(CurPtr))
545 // Keywords for instructions.
546 #define INSTKEYWORD(STR, type, Enum, TOK) \
547 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
548 llvmAsmlval.type = Instruction::Enum; return TOK; }
550 INSTKEYWORD("add", BinaryOpVal, Add, ADD);
551 INSTKEYWORD("sub", BinaryOpVal, Sub, SUB);
552 INSTKEYWORD("mul", BinaryOpVal, Mul, MUL);
553 INSTKEYWORD("udiv", BinaryOpVal, UDiv, UDIV);
554 INSTKEYWORD("sdiv", BinaryOpVal, SDiv, SDIV);
555 INSTKEYWORD("fdiv", BinaryOpVal, FDiv, FDIV);
556 INSTKEYWORD("urem", BinaryOpVal, URem, UREM);
557 INSTKEYWORD("srem", BinaryOpVal, SRem, SREM);
558 INSTKEYWORD("frem", BinaryOpVal, FRem, FREM);
559 INSTKEYWORD("shl", BinaryOpVal, Shl, SHL);
560 INSTKEYWORD("lshr", BinaryOpVal, LShr, LSHR);
561 INSTKEYWORD("ashr", BinaryOpVal, AShr, ASHR);
562 INSTKEYWORD("and", BinaryOpVal, And, AND);
563 INSTKEYWORD("or", BinaryOpVal, Or , OR );
564 INSTKEYWORD("xor", BinaryOpVal, Xor, XOR);
565 INSTKEYWORD("icmp", OtherOpVal, ICmp, ICMP);
566 INSTKEYWORD("fcmp", OtherOpVal, FCmp, FCMP);
568 INSTKEYWORD("phi", OtherOpVal, PHI, PHI_TOK);
569 INSTKEYWORD("call", OtherOpVal, Call, CALL);
570 INSTKEYWORD("trunc", CastOpVal, Trunc, TRUNC);
571 INSTKEYWORD("zext", CastOpVal, ZExt, ZEXT);
572 INSTKEYWORD("sext", CastOpVal, SExt, SEXT);
573 INSTKEYWORD("fptrunc", CastOpVal, FPTrunc, FPTRUNC);
574 INSTKEYWORD("fpext", CastOpVal, FPExt, FPEXT);
575 INSTKEYWORD("uitofp", CastOpVal, UIToFP, UITOFP);
576 INSTKEYWORD("sitofp", CastOpVal, SIToFP, SITOFP);
577 INSTKEYWORD("fptoui", CastOpVal, FPToUI, FPTOUI);
578 INSTKEYWORD("fptosi", CastOpVal, FPToSI, FPTOSI);
579 INSTKEYWORD("inttoptr", CastOpVal, IntToPtr, INTTOPTR);
580 INSTKEYWORD("ptrtoint", CastOpVal, PtrToInt, PTRTOINT);
581 INSTKEYWORD("bitcast", CastOpVal, BitCast, BITCAST);
582 INSTKEYWORD("select", OtherOpVal, Select, SELECT);
583 INSTKEYWORD("va_arg", OtherOpVal, VAArg , VAARG);
584 INSTKEYWORD("ret", TermOpVal, Ret, RET);
585 INSTKEYWORD("br", TermOpVal, Br, BR);
586 INSTKEYWORD("switch", TermOpVal, Switch, SWITCH);
587 INSTKEYWORD("invoke", TermOpVal, Invoke, INVOKE);
588 INSTKEYWORD("unwind", TermOpVal, Unwind, UNWIND);
589 INSTKEYWORD("unreachable", TermOpVal, Unreachable, UNREACHABLE);
591 INSTKEYWORD("malloc", MemOpVal, Malloc, MALLOC);
592 INSTKEYWORD("alloca", MemOpVal, Alloca, ALLOCA);
593 INSTKEYWORD("free", MemOpVal, Free, FREE);
594 INSTKEYWORD("load", MemOpVal, Load, LOAD);
595 INSTKEYWORD("store", MemOpVal, Store, STORE);
596 INSTKEYWORD("getelementptr", MemOpVal, GetElementPtr, GETELEMENTPTR);
598 INSTKEYWORD("extractelement", OtherOpVal, ExtractElement, EXTRACTELEMENT);
599 INSTKEYWORD("insertelement", OtherOpVal, InsertElement, INSERTELEMENT);
600 INSTKEYWORD("shufflevector", OtherOpVal, ShuffleVector, SHUFFLEVECTOR);
603 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
604 // the CFE to avoid forcing it to deal with 64-bit numbers.
605 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
606 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
607 int len = CurPtr-TokStart-3;
608 uint32_t bits = len * 4;
609 APInt Tmp(bits, TokStart+3, len, 16);
610 uint32_t activeBits = Tmp.getActiveBits();
611 if (activeBits > 0 && activeBits < bits)
612 Tmp.trunc(activeBits);
613 if (Tmp.getBitWidth() > 64) {
614 llvmAsmlval.APIntVal = new APInt(Tmp);
615 return TokStart[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
616 } else if (TokStart[0] == 's') {
617 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
620 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
625 // If this is "cc1234", return this as just "cc".
626 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
631 // If this starts with "call", return it as CALL. This is to support old
632 // broken .ll files. FIXME: remove this with LLVM 3.0.
633 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
635 llvmAsmlval.OtherOpVal = Instruction::Call;
639 // Finally, if this isn't known, return just a single character.
645 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
646 /// that this is not a label:
647 /// HexFPConstant 0x[0-9A-Fa-f]+
648 /// HexFP80Constant 0xK[0-9A-Fa-f]+
649 /// HexFP128Constant 0xL[0-9A-Fa-f]+
650 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
651 int LLLexer::Lex0x() {
652 CurPtr = TokStart + 2;
655 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
661 if (!isxdigit(CurPtr[0])) {
662 // Bad token, return it as just zero.
667 while (isxdigit(CurPtr[0]))
671 // HexFPConstant - Floating point constant represented in IEEE format as a
672 // hexadecimal number for when exponential notation is not precise enough.
673 // Float and double only.
674 llvmAsmlval.FPVal = new APFloat(HexToFP(TokStart+2, CurPtr));
679 HexToIntPair(TokStart+3, CurPtr, Pair);
681 default: assert(0 && "Unknown kind!");
683 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
684 llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
687 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
688 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair), true);
691 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
692 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
697 /// LexIdentifier: Handle several related productions:
698 /// Label [-a-zA-Z$._0-9]+:
700 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
702 /// HexFPConstant 0x[0-9A-Fa-f]+
703 /// HexFP80Constant 0xK[0-9A-Fa-f]+
704 /// HexFP128Constant 0xL[0-9A-Fa-f]+
705 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
706 int LLLexer::LexDigitOrNegative() {
707 // If the letter after the negative is a number, this is probably a label.
708 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
709 // Okay, this is not a number after the -, it's probably a label.
710 if (const char *End = isLabelTail(CurPtr)) {
711 llvmAsmlval.StrVal = new std::string(TokStart, End-1);
719 // At this point, it is either a label, int or fp constant.
721 // Skip digits, we have at least one.
722 for (; isdigit(CurPtr[0]); ++CurPtr)
725 // Check to see if this really is a label afterall, e.g. "-1:".
726 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
727 if (const char *End = isLabelTail(CurPtr)) {
728 llvmAsmlval.StrVal = new std::string(TokStart, End-1);
734 // If the next character is a '.', then it is a fp value, otherwise its
736 if (CurPtr[0] != '.') {
737 if (TokStart[0] == '0' && TokStart[1] == 'x')
739 unsigned Len = CurPtr-TokStart;
740 uint32_t numBits = ((Len * 64) / 19) + 2;
741 APInt Tmp(numBits, TokStart, Len, 10);
742 if (TokStart[0] == '-') {
743 uint32_t minBits = Tmp.getMinSignedBits();
744 if (minBits > 0 && minBits < numBits)
746 if (Tmp.getBitWidth() > 64) {
747 llvmAsmlval.APIntVal = new APInt(Tmp);
750 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
754 uint32_t activeBits = Tmp.getActiveBits();
755 if (activeBits > 0 && activeBits < numBits)
756 Tmp.trunc(activeBits);
757 if (Tmp.getBitWidth() > 64) {
758 llvmAsmlval.APIntVal = new APInt(Tmp);
761 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
769 // Skip over [0-9]*([eE][-+]?[0-9]+)?
770 while (isdigit(CurPtr[0])) ++CurPtr;
772 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
773 if (isdigit(CurPtr[1]) ||
774 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
776 while (isdigit(CurPtr[0])) ++CurPtr;
780 llvmAsmlval.FPVal = new APFloat(atof(TokStart));
784 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
785 int LLLexer::LexPositive() {
786 // If the letter after the negative is a number, this is probably not a
788 if (!isdigit(CurPtr[0]))
792 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
795 // At this point, we need a '.'.
796 if (CurPtr[0] != '.') {
803 // Skip over [0-9]*([eE][-+]?[0-9]+)?
804 while (isdigit(CurPtr[0])) ++CurPtr;
806 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
807 if (isdigit(CurPtr[1]) ||
808 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
810 while (isdigit(CurPtr[0])) ++CurPtr;
814 llvmAsmlval.FPVal = new APFloat(atof(TokStart));
819 //===----------------------------------------------------------------------===//
820 // Define the interface to this file.
821 //===----------------------------------------------------------------------===//
823 static LLLexer *TheLexer;
825 void InitLLLexer(llvm::MemoryBuffer *MB) {
826 assert(TheLexer == 0 && "LL Lexer isn't reentrant yet");
827 TheLexer = new LLLexer(MB);
831 return TheLexer->LexToken();
833 const char *LLLgetTokenStart() { return TheLexer->getTokStart(); }
834 unsigned LLLgetTokenLength() { return TheLexer->getTokLength(); }
835 std::string LLLgetFilename() { return TheLexer->getFilename(); }
836 unsigned LLLgetLineNo() { return TheLexer->getLineNo(); }