1 //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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);
289 uint64_t Val = atoull(TokStart+1, CurPtr);
290 if ((unsigned)Val != Val)
291 GenerateError("Invalid value number (too large)!");
292 llvmAsmlval.UIntVal = unsigned(Val);
300 /// LexPercent - Lex all tokens that start with a % character:
301 /// PctStringConstant %\"[^\"]*\"
302 /// LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
303 /// LocalVarID %[0-9]+
304 int LLLexer::LexPercent() {
305 // Handle PctStringConstant: %\"[^\"]*\"
306 if (CurPtr[0] == '"') {
310 int CurChar = getNextChar();
312 if (CurChar == EOF) {
313 GenerateError("End of file in local variable name");
316 if (CurChar == '"') {
317 llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
318 UnEscapeLexed(*llvmAsmlval.StrVal);
319 return PCTSTRINGCONSTANT;
324 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
325 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
326 CurPtr[0] == '.' || CurPtr[0] == '_') {
328 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
329 CurPtr[0] == '.' || CurPtr[0] == '_')
332 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr); // Skip %
336 // Handle LocalVarID: %[0-9]+
337 if (isdigit(CurPtr[0])) {
338 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr);
340 uint64_t Val = atoull(TokStart+1, CurPtr);
341 if ((unsigned)Val != Val)
342 GenerateError("Invalid value number (too large)!");
343 llvmAsmlval.UIntVal = unsigned(Val);
350 /// LexQuote - Lex all tokens that start with a " character:
351 /// QuoteLabel "[^"]+":
352 /// StringConstant "[^"]*"
353 int LLLexer::LexQuote() {
355 int CurChar = getNextChar();
357 if (CurChar == EOF) {
358 GenerateError("End of file in quoted string");
362 if (CurChar != '"') continue;
364 if (CurPtr[0] != ':') {
365 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-1);
366 UnEscapeLexed(*llvmAsmlval.StrVal);
367 return STRINGCONSTANT;
371 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-2);
372 UnEscapeLexed(*llvmAsmlval.StrVal);
377 static bool JustWhitespaceNewLine(const char *&Ptr) {
378 const char *ThisPtr = Ptr;
379 while (*ThisPtr == ' ' || *ThisPtr == '\t')
381 if (*ThisPtr == '\n' || *ThisPtr == '\r') {
389 /// LexIdentifier: Handle several related productions:
390 /// Label [-a-zA-Z$._0-9]+:
391 /// IntegerType i[0-9]+
392 /// Keyword sdiv, float, ...
393 /// HexIntConstant [us]0x[0-9A-Fa-f]+
394 int LLLexer::LexIdentifier() {
395 const char *StartChar = CurPtr;
396 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
397 const char *KeywordEnd = 0;
399 for (; isLabelChar(*CurPtr); ++CurPtr) {
400 // If we decide this is an integer, remember the end of the sequence.
401 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
402 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
405 // If we stopped due to a colon, this really is a label.
406 if (*CurPtr == ':') {
407 llvmAsmlval.StrVal = new std::string(StartChar-1, CurPtr++);
411 // Otherwise, this wasn't a label. If this was valid as an integer type,
413 if (IntEnd == 0) IntEnd = CurPtr;
414 if (IntEnd != StartChar) {
416 uint64_t NumBits = atoull(StartChar, CurPtr);
417 if (NumBits < IntegerType::MIN_INT_BITS ||
418 NumBits > IntegerType::MAX_INT_BITS) {
419 GenerateError("Bitwidth for integer type out of range!");
422 const Type* Ty = IntegerType::get(NumBits);
423 llvmAsmlval.PrimType = Ty;
427 // Otherwise, this was a letter sequence. See which keyword this is.
428 if (KeywordEnd == 0) KeywordEnd = CurPtr;
431 unsigned Len = CurPtr-StartChar;
432 #define KEYWORD(STR, TOK) \
433 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) return TOK;
435 KEYWORD("begin", BEGINTOK);
436 KEYWORD("end", ENDTOK);
437 KEYWORD("true", TRUETOK);
438 KEYWORD("false", FALSETOK);
439 KEYWORD("declare", DECLARE);
440 KEYWORD("define", DEFINE);
441 KEYWORD("global", GLOBAL);
442 KEYWORD("constant", CONSTANT);
444 KEYWORD("internal", INTERNAL);
445 KEYWORD("linkonce", LINKONCE);
446 KEYWORD("weak", WEAK);
447 KEYWORD("appending", APPENDING);
448 KEYWORD("dllimport", DLLIMPORT);
449 KEYWORD("dllexport", DLLEXPORT);
450 KEYWORD("hidden", HIDDEN);
451 KEYWORD("protected", PROTECTED);
452 KEYWORD("extern_weak", EXTERN_WEAK);
453 KEYWORD("external", EXTERNAL);
454 KEYWORD("thread_local", THREAD_LOCAL);
455 KEYWORD("zeroinitializer", ZEROINITIALIZER);
456 KEYWORD("undef", UNDEF);
457 KEYWORD("null", NULL_TOK);
459 KEYWORD("tail", TAIL);
460 KEYWORD("target", TARGET);
461 KEYWORD("triple", TRIPLE);
462 KEYWORD("deplibs", DEPLIBS);
463 KEYWORD("datalayout", DATALAYOUT);
464 KEYWORD("volatile", VOLATILE);
465 KEYWORD("align", ALIGN);
466 KEYWORD("section", SECTION);
467 KEYWORD("alias", ALIAS);
468 KEYWORD("module", MODULE);
469 KEYWORD("asm", ASM_TOK);
470 KEYWORD("sideeffect", SIDEEFFECT);
472 KEYWORD("cc", CC_TOK);
473 KEYWORD("ccc", CCC_TOK);
474 KEYWORD("fastcc", FASTCC_TOK);
475 KEYWORD("coldcc", COLDCC_TOK);
476 KEYWORD("x86_stdcallcc", X86_STDCALLCC_TOK);
477 KEYWORD("x86_fastcallcc", X86_FASTCALLCC_TOK);
479 KEYWORD("signext", SIGNEXT);
480 KEYWORD("zeroext", ZEROEXT);
481 KEYWORD("inreg", INREG);
482 KEYWORD("sret", SRET);
483 KEYWORD("nounwind", NOUNWIND);
484 KEYWORD("noreturn", NORETURN);
485 KEYWORD("noalias", NOALIAS);
486 KEYWORD("byval", BYVAL);
487 KEYWORD("nest", NEST);
488 KEYWORD("readnone", READNONE);
489 KEYWORD("readonly", READONLY);
491 KEYWORD("type", TYPE);
492 KEYWORD("opaque", OPAQUE);
516 // Keywords for types.
517 #define TYPEKEYWORD(STR, LLVMTY, TOK) \
518 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
519 llvmAsmlval.PrimType = LLVMTY; return TOK; }
520 TYPEKEYWORD("void", Type::VoidTy, VOID);
521 TYPEKEYWORD("float", Type::FloatTy, FLOAT);
522 TYPEKEYWORD("double", Type::DoubleTy, DOUBLE);
523 TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty, X86_FP80);
524 TYPEKEYWORD("fp128", Type::FP128Ty, FP128);
525 TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty, PPC_FP128);
526 TYPEKEYWORD("label", Type::LabelTy, LABEL);
529 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
530 // to avoid conflicting with the sext/zext instructions, below.
531 if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
532 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
533 if (JustWhitespaceNewLine(CurPtr))
535 } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
536 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
537 if (JustWhitespaceNewLine(CurPtr))
541 // Keywords for instructions.
542 #define INSTKEYWORD(STR, type, Enum, TOK) \
543 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
544 llvmAsmlval.type = Instruction::Enum; return TOK; }
546 INSTKEYWORD("add", BinaryOpVal, Add, ADD);
547 INSTKEYWORD("sub", BinaryOpVal, Sub, SUB);
548 INSTKEYWORD("mul", BinaryOpVal, Mul, MUL);
549 INSTKEYWORD("udiv", BinaryOpVal, UDiv, UDIV);
550 INSTKEYWORD("sdiv", BinaryOpVal, SDiv, SDIV);
551 INSTKEYWORD("fdiv", BinaryOpVal, FDiv, FDIV);
552 INSTKEYWORD("urem", BinaryOpVal, URem, UREM);
553 INSTKEYWORD("srem", BinaryOpVal, SRem, SREM);
554 INSTKEYWORD("frem", BinaryOpVal, FRem, FREM);
555 INSTKEYWORD("shl", BinaryOpVal, Shl, SHL);
556 INSTKEYWORD("lshr", BinaryOpVal, LShr, LSHR);
557 INSTKEYWORD("ashr", BinaryOpVal, AShr, ASHR);
558 INSTKEYWORD("and", BinaryOpVal, And, AND);
559 INSTKEYWORD("or", BinaryOpVal, Or , OR );
560 INSTKEYWORD("xor", BinaryOpVal, Xor, XOR);
561 INSTKEYWORD("icmp", OtherOpVal, ICmp, ICMP);
562 INSTKEYWORD("fcmp", OtherOpVal, FCmp, FCMP);
564 INSTKEYWORD("phi", OtherOpVal, PHI, PHI_TOK);
565 INSTKEYWORD("call", OtherOpVal, Call, CALL);
566 INSTKEYWORD("trunc", CastOpVal, Trunc, TRUNC);
567 INSTKEYWORD("zext", CastOpVal, ZExt, ZEXT);
568 INSTKEYWORD("sext", CastOpVal, SExt, SEXT);
569 INSTKEYWORD("fptrunc", CastOpVal, FPTrunc, FPTRUNC);
570 INSTKEYWORD("fpext", CastOpVal, FPExt, FPEXT);
571 INSTKEYWORD("uitofp", CastOpVal, UIToFP, UITOFP);
572 INSTKEYWORD("sitofp", CastOpVal, SIToFP, SITOFP);
573 INSTKEYWORD("fptoui", CastOpVal, FPToUI, FPTOUI);
574 INSTKEYWORD("fptosi", CastOpVal, FPToSI, FPTOSI);
575 INSTKEYWORD("inttoptr", CastOpVal, IntToPtr, INTTOPTR);
576 INSTKEYWORD("ptrtoint", CastOpVal, PtrToInt, PTRTOINT);
577 INSTKEYWORD("bitcast", CastOpVal, BitCast, BITCAST);
578 INSTKEYWORD("select", OtherOpVal, Select, SELECT);
579 INSTKEYWORD("va_arg", OtherOpVal, VAArg , VAARG);
580 INSTKEYWORD("ret", TermOpVal, Ret, RET);
581 INSTKEYWORD("br", TermOpVal, Br, BR);
582 INSTKEYWORD("switch", TermOpVal, Switch, SWITCH);
583 INSTKEYWORD("invoke", TermOpVal, Invoke, INVOKE);
584 INSTKEYWORD("unwind", TermOpVal, Unwind, UNWIND);
585 INSTKEYWORD("unreachable", TermOpVal, Unreachable, UNREACHABLE);
587 INSTKEYWORD("malloc", MemOpVal, Malloc, MALLOC);
588 INSTKEYWORD("alloca", MemOpVal, Alloca, ALLOCA);
589 INSTKEYWORD("free", MemOpVal, Free, FREE);
590 INSTKEYWORD("load", MemOpVal, Load, LOAD);
591 INSTKEYWORD("store", MemOpVal, Store, STORE);
592 INSTKEYWORD("getelementptr", MemOpVal, GetElementPtr, GETELEMENTPTR);
594 INSTKEYWORD("extractelement", OtherOpVal, ExtractElement, EXTRACTELEMENT);
595 INSTKEYWORD("insertelement", OtherOpVal, InsertElement, INSERTELEMENT);
596 INSTKEYWORD("shufflevector", OtherOpVal, ShuffleVector, SHUFFLEVECTOR);
599 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
600 // the CFE to avoid forcing it to deal with 64-bit numbers.
601 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
602 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
603 int len = CurPtr-TokStart-3;
604 uint32_t bits = len * 4;
605 APInt Tmp(bits, TokStart+3, len, 16);
606 uint32_t activeBits = Tmp.getActiveBits();
607 if (activeBits > 0 && activeBits < bits)
608 Tmp.trunc(activeBits);
609 if (Tmp.getBitWidth() > 64) {
610 llvmAsmlval.APIntVal = new APInt(Tmp);
611 return TokStart[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
612 } else if (TokStart[0] == 's') {
613 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
616 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
621 // If this is "cc1234", return this as just "cc".
622 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
627 // If this starts with "call", return it as CALL. This is to support old
628 // broken .ll files. FIXME: remove this with LLVM 3.0.
629 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
631 llvmAsmlval.OtherOpVal = Instruction::Call;
635 // Finally, if this isn't known, return just a single character.
641 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
642 /// that this is not a label:
643 /// HexFPConstant 0x[0-9A-Fa-f]+
644 /// HexFP80Constant 0xK[0-9A-Fa-f]+
645 /// HexFP128Constant 0xL[0-9A-Fa-f]+
646 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
647 int LLLexer::Lex0x() {
648 CurPtr = TokStart + 2;
651 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
657 if (!isxdigit(CurPtr[0])) {
658 // Bad token, return it as just zero.
663 while (isxdigit(CurPtr[0]))
667 // HexFPConstant - Floating point constant represented in IEEE format as a
668 // hexadecimal number for when exponential notation is not precise enough.
669 // Float and double only.
670 llvmAsmlval.FPVal = new APFloat(HexToFP(TokStart+2, CurPtr));
675 HexToIntPair(TokStart+3, CurPtr, Pair);
677 default: assert(0 && "Unknown kind!");
679 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
680 llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
683 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
684 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair), true);
687 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
688 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
693 /// LexIdentifier: Handle several related productions:
694 /// Label [-a-zA-Z$._0-9]+:
696 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
698 /// HexFPConstant 0x[0-9A-Fa-f]+
699 /// HexFP80Constant 0xK[0-9A-Fa-f]+
700 /// HexFP128Constant 0xL[0-9A-Fa-f]+
701 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
702 int LLLexer::LexDigitOrNegative() {
703 // If the letter after the negative is a number, this is probably a label.
704 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
705 // Okay, this is not a number after the -, it's probably a label.
706 if (const char *End = isLabelTail(CurPtr)) {
707 llvmAsmlval.StrVal = new std::string(TokStart, End-1);
715 // At this point, it is either a label, int or fp constant.
717 // Skip digits, we have at least one.
718 for (; isdigit(CurPtr[0]); ++CurPtr);
720 // Check to see if this really is a label afterall, e.g. "-1:".
721 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
722 if (const char *End = isLabelTail(CurPtr)) {
723 llvmAsmlval.StrVal = new std::string(TokStart, End-1);
729 // If the next character is a '.', then it is a fp value, otherwise its
731 if (CurPtr[0] != '.') {
732 if (TokStart[0] == '0' && TokStart[1] == 'x')
734 unsigned Len = CurPtr-TokStart;
735 uint32_t numBits = ((Len * 64) / 19) + 2;
736 APInt Tmp(numBits, TokStart, Len, 10);
737 if (TokStart[0] == '-') {
738 uint32_t minBits = Tmp.getMinSignedBits();
739 if (minBits > 0 && minBits < numBits)
741 if (Tmp.getBitWidth() > 64) {
742 llvmAsmlval.APIntVal = new APInt(Tmp);
745 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
749 uint32_t activeBits = Tmp.getActiveBits();
750 if (activeBits > 0 && activeBits < numBits)
751 Tmp.trunc(activeBits);
752 if (Tmp.getBitWidth() > 64) {
753 llvmAsmlval.APIntVal = new APInt(Tmp);
756 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
764 // Skip over [0-9]*([eE][-+]?[0-9]+)?
765 while (isdigit(CurPtr[0])) ++CurPtr;
767 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
768 if (isdigit(CurPtr[1]) ||
769 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
771 while (isdigit(CurPtr[0])) ++CurPtr;
775 llvmAsmlval.FPVal = new APFloat(atof(TokStart));
779 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
780 int LLLexer::LexPositive() {
781 // If the letter after the negative is a number, this is probably not a
783 if (!isdigit(CurPtr[0]))
787 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr);
789 // At this point, we need a '.'.
790 if (CurPtr[0] != '.') {
797 // Skip over [0-9]*([eE][-+]?[0-9]+)?
798 while (isdigit(CurPtr[0])) ++CurPtr;
800 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
801 if (isdigit(CurPtr[1]) ||
802 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
804 while (isdigit(CurPtr[0])) ++CurPtr;
808 llvmAsmlval.FPVal = new APFloat(atof(TokStart));
813 //===----------------------------------------------------------------------===//
814 // Define the interface to this file.
815 //===----------------------------------------------------------------------===//
817 static LLLexer *TheLexer;
819 void InitLLLexer(llvm::MemoryBuffer *MB) {
820 assert(TheLexer == 0 && "LL Lexer isn't reentrant yet");
821 TheLexer = new LLLexer(MB);
825 return TheLexer->LexToken();
827 const char *LLLgetTokenStart() { return TheLexer->getTokStart(); }
828 unsigned LLLgetTokenLength() { return TheLexer->getTokLength(); }
829 std::string LLLgetFilename() { return TheLexer->getFilename(); }
830 unsigned LLLgetLineNo() { return TheLexer->getLineNo(); }