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"
25 //===----------------------------------------------------------------------===//
27 //===----------------------------------------------------------------------===//
29 // atoull - Convert an ascii string of decimal digits into the unsigned long
30 // long representation... this does not have to do input error checking,
31 // because we know that the input will be matched by a suitable regex...
33 static uint64_t atoull(const char *Buffer, const char *End) {
35 for (; Buffer != End; Buffer++) {
36 uint64_t OldRes = Result;
38 Result += *Buffer-'0';
39 if (Result < OldRes) { // Uh, oh, overflow detected!!!
40 GenerateError("constant bigger than 64 bits detected!");
47 static uint64_t HexIntToVal(const char *Buffer, const char *End) {
49 for (; Buffer != End; ++Buffer) {
50 uint64_t OldRes = Result;
53 if (C >= '0' && C <= '9')
55 else if (C >= 'A' && C <= 'F')
57 else if (C >= 'a' && C <= 'f')
60 if (Result < OldRes) { // Uh, oh, overflow detected!!!
61 GenerateError("constant bigger than 64 bits detected!");
68 // HexToFP - Convert the ascii string in hexadecimal format to the floating
69 // point representation of it.
71 static double HexToFP(const char *Buffer, const char *End) {
72 return BitsToDouble(HexIntToVal(Buffer, End)); // Cast Hex constant to double
75 static void HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]){
77 for (int i=0; i<16; i++, Buffer++) {
78 assert(Buffer != End);
81 if (C >= '0' && C <= '9')
83 else if (C >= 'A' && C <= 'F')
85 else if (C >= 'a' && C <= 'f')
89 for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
92 if (C >= '0' && C <= '9')
94 else if (C >= 'A' && C <= 'F')
96 else if (C >= 'a' && C <= 'f')
100 GenerateError("constant bigger than 128 bits detected!");
103 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
104 // appropriate character.
105 static void UnEscapeLexed(std::string &Str) {
106 if (Str.empty()) return;
108 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
110 for (char *BIn = Buffer; BIn != EndBuffer; ) {
111 if (BIn[0] == '\\') {
112 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
113 *BOut++ = '\\'; // Two \ becomes one
115 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
116 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
117 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
118 BIn[3] = Tmp; // Restore character
119 BIn += 3; // Skip over handled chars
128 Str.resize(BOut-Buffer);
131 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
132 static bool isLabelChar(char C) {
133 return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
137 /// isLabelTail - Return true if this pointer points to a valid end of a label.
138 static const char *isLabelTail(const char *CurPtr) {
140 if (CurPtr[0] == ':') return CurPtr+1;
141 if (!isLabelChar(CurPtr[0])) return 0;
148 //===----------------------------------------------------------------------===//
150 //===----------------------------------------------------------------------===//
152 // FIXME: REMOVE THIS.
156 LLLexer::LLLexer(MemoryBuffer *StartBuf) : CurLineNo(1), CurBuf(StartBuf) {
157 CurPtr = CurBuf->getBufferStart();
160 std::string LLLexer::getFilename() const {
161 return CurBuf->getBufferIdentifier();
164 int LLLexer::getNextChar() {
165 char CurChar = *CurPtr++;
167 default: return (unsigned char)CurChar;
169 // A nul character in the stream is either the end of the current buffer or
170 // a random nul in the file. Disambiguate that here.
171 if (CurPtr-1 != CurBuf->getBufferEnd())
172 return 0; // Just whitespace.
174 // Otherwise, return end of file.
175 --CurPtr; // Another call to lex will return EOF again.
179 // Handle the newline character by ignoring it and incrementing the line
180 // count. However, be careful about 'dos style' files with \n\r in them.
181 // Only treat a \n\r or \r\n as a single line.
182 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
184 ++CurPtr; // Eat the two char newline sequence.
192 int LLLexer::LexToken() {
195 int CurChar = getNextChar();
199 // Handle letters: [a-zA-Z_]
200 if (isalpha(CurChar) || CurChar == '_')
201 return LexIdentifier();
204 case EOF: return YYEOF;
210 // Ignore whitespace.
212 case '+': return LexPositive();
213 case '@': return LexAt();
214 case '%': return LexPercent();
215 case '"': return LexQuote();
217 if (const char *Ptr = isLabelTail(CurPtr)) {
219 llvmAsmlval.StrVal = new std::string(TokStart, CurPtr-1);
222 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
228 if (const char *Ptr = isLabelTail(CurPtr)) {
230 llvmAsmlval.StrVal = new std::string(TokStart, CurPtr-1);
237 case '0': case '1': case '2': case '3': case '4':
238 case '5': case '6': case '7': case '8': case '9':
240 return LexDigitOrNegative();
244 void LLLexer::SkipLineComment() {
246 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
251 /// LexAt - Lex all tokens that start with an @ character:
252 /// AtStringConstant @\"[^\"]*\"
253 /// GlobalVarName @[-a-zA-Z$._][-a-zA-Z$._0-9]*
254 /// GlobalVarID @[0-9]+
255 int LLLexer::LexAt() {
256 // Handle AtStringConstant: @\"[^\"]*\"
257 if (CurPtr[0] == '"') {
261 int CurChar = getNextChar();
263 if (CurChar == EOF) {
264 GenerateError("End of file in global variable name");
267 if (CurChar == '"') {
268 llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
269 UnEscapeLexed(*llvmAsmlval.StrVal);
270 return ATSTRINGCONSTANT;
275 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
276 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
277 CurPtr[0] == '.' || CurPtr[0] == '_') {
279 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
280 CurPtr[0] == '.' || CurPtr[0] == '_')
283 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr); // Skip @
287 // Handle GlobalVarID: @[0-9]+
288 if (isdigit(CurPtr[0])) {
289 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
292 uint64_t Val = atoull(TokStart+1, CurPtr);
293 if ((unsigned)Val != Val)
294 GenerateError("Invalid value number (too large)!");
295 llvmAsmlval.UIntVal = unsigned(Val);
303 /// LexPercent - Lex all tokens that start with a % character:
304 /// PctStringConstant %\"[^\"]*\"
305 /// LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
306 /// LocalVarID %[0-9]+
307 int LLLexer::LexPercent() {
308 // Handle PctStringConstant: %\"[^\"]*\"
309 if (CurPtr[0] == '"') {
313 int CurChar = getNextChar();
315 if (CurChar == EOF) {
316 GenerateError("End of file in local variable name");
319 if (CurChar == '"') {
320 llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
321 UnEscapeLexed(*llvmAsmlval.StrVal);
322 return PCTSTRINGCONSTANT;
327 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
328 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
329 CurPtr[0] == '.' || CurPtr[0] == '_') {
331 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
332 CurPtr[0] == '.' || CurPtr[0] == '_')
335 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr); // Skip %
339 // Handle LocalVarID: %[0-9]+
340 if (isdigit(CurPtr[0])) {
341 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
344 uint64_t Val = atoull(TokStart+1, CurPtr);
345 if ((unsigned)Val != Val)
346 GenerateError("Invalid value number (too large)!");
347 llvmAsmlval.UIntVal = unsigned(Val);
354 /// LexQuote - Lex all tokens that start with a " character:
355 /// QuoteLabel "[^"]+":
356 /// StringConstant "[^"]*"
357 int LLLexer::LexQuote() {
359 int CurChar = getNextChar();
361 if (CurChar == EOF) {
362 GenerateError("End of file in quoted string");
366 if (CurChar != '"') continue;
368 if (CurPtr[0] != ':') {
369 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-1);
370 UnEscapeLexed(*llvmAsmlval.StrVal);
371 return STRINGCONSTANT;
375 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-2);
376 UnEscapeLexed(*llvmAsmlval.StrVal);
381 static bool JustWhitespaceNewLine(const char *&Ptr) {
382 const char *ThisPtr = Ptr;
383 while (*ThisPtr == ' ' || *ThisPtr == '\t')
385 if (*ThisPtr == '\n' || *ThisPtr == '\r') {
393 /// LexIdentifier: Handle several related productions:
394 /// Label [-a-zA-Z$._0-9]+:
395 /// IntegerType i[0-9]+
396 /// Keyword sdiv, float, ...
397 /// HexIntConstant [us]0x[0-9A-Fa-f]+
398 int LLLexer::LexIdentifier() {
399 const char *StartChar = CurPtr;
400 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
401 const char *KeywordEnd = 0;
403 for (; isLabelChar(*CurPtr); ++CurPtr) {
404 // If we decide this is an integer, remember the end of the sequence.
405 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
406 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
409 // If we stopped due to a colon, this really is a label.
410 if (*CurPtr == ':') {
411 llvmAsmlval.StrVal = new std::string(StartChar-1, CurPtr++);
415 // Otherwise, this wasn't a label. If this was valid as an integer type,
417 if (IntEnd == 0) IntEnd = CurPtr;
418 if (IntEnd != StartChar) {
420 uint64_t NumBits = atoull(StartChar, CurPtr);
421 if (NumBits < IntegerType::MIN_INT_BITS ||
422 NumBits > IntegerType::MAX_INT_BITS) {
423 GenerateError("Bitwidth for integer type out of range!");
426 const Type* Ty = IntegerType::get(NumBits);
427 llvmAsmlval.PrimType = Ty;
431 // Otherwise, this was a letter sequence. See which keyword this is.
432 if (KeywordEnd == 0) KeywordEnd = CurPtr;
435 unsigned Len = CurPtr-StartChar;
436 #define KEYWORD(STR, TOK) \
437 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) return TOK;
439 KEYWORD("begin", BEGINTOK);
440 KEYWORD("end", ENDTOK);
441 KEYWORD("true", TRUETOK);
442 KEYWORD("false", FALSETOK);
443 KEYWORD("declare", DECLARE);
444 KEYWORD("define", DEFINE);
445 KEYWORD("global", GLOBAL);
446 KEYWORD("constant", CONSTANT);
448 KEYWORD("internal", INTERNAL);
449 KEYWORD("linkonce", LINKONCE);
450 KEYWORD("weak", WEAK);
451 KEYWORD("appending", APPENDING);
452 KEYWORD("dllimport", DLLIMPORT);
453 KEYWORD("dllexport", DLLEXPORT);
454 KEYWORD("hidden", HIDDEN);
455 KEYWORD("protected", PROTECTED);
456 KEYWORD("extern_weak", EXTERN_WEAK);
457 KEYWORD("external", EXTERNAL);
458 KEYWORD("thread_local", THREAD_LOCAL);
459 KEYWORD("zeroinitializer", ZEROINITIALIZER);
460 KEYWORD("undef", UNDEF);
461 KEYWORD("null", NULL_TOK);
463 KEYWORD("tail", TAIL);
464 KEYWORD("target", TARGET);
465 KEYWORD("triple", TRIPLE);
466 KEYWORD("deplibs", DEPLIBS);
467 KEYWORD("datalayout", DATALAYOUT);
468 KEYWORD("volatile", VOLATILE);
469 KEYWORD("align", ALIGN);
470 KEYWORD("addrspace", ADDRSPACE);
471 KEYWORD("section", SECTION);
472 KEYWORD("alias", ALIAS);
473 KEYWORD("module", MODULE);
474 KEYWORD("asm", ASM_TOK);
475 KEYWORD("sideeffect", SIDEEFFECT);
477 KEYWORD("unwind_to", UNWIND_TO);
479 KEYWORD("cc", CC_TOK);
480 KEYWORD("ccc", CCC_TOK);
481 KEYWORD("fastcc", FASTCC_TOK);
482 KEYWORD("coldcc", COLDCC_TOK);
483 KEYWORD("x86_stdcallcc", X86_STDCALLCC_TOK);
484 KEYWORD("x86_fastcallcc", X86_FASTCALLCC_TOK);
486 KEYWORD("signext", SIGNEXT);
487 KEYWORD("zeroext", ZEROEXT);
488 KEYWORD("inreg", INREG);
489 KEYWORD("sret", SRET);
490 KEYWORD("nounwind", NOUNWIND);
491 KEYWORD("noreturn", NORETURN);
492 KEYWORD("noalias", NOALIAS);
493 KEYWORD("byval", BYVAL);
494 KEYWORD("nest", NEST);
495 KEYWORD("readnone", READNONE);
496 KEYWORD("readonly", READONLY);
498 KEYWORD("type", TYPE);
499 KEYWORD("opaque", OPAQUE);
523 // Keywords for types.
524 #define TYPEKEYWORD(STR, LLVMTY, TOK) \
525 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
526 llvmAsmlval.PrimType = LLVMTY; return TOK; }
527 TYPEKEYWORD("void", Type::VoidTy, VOID);
528 TYPEKEYWORD("float", Type::FloatTy, FLOAT);
529 TYPEKEYWORD("double", Type::DoubleTy, DOUBLE);
530 TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty, X86_FP80);
531 TYPEKEYWORD("fp128", Type::FP128Ty, FP128);
532 TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty, PPC_FP128);
533 TYPEKEYWORD("label", Type::LabelTy, LABEL);
536 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
537 // to avoid conflicting with the sext/zext instructions, below.
538 if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
539 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
540 if (JustWhitespaceNewLine(CurPtr))
542 } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
543 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
544 if (JustWhitespaceNewLine(CurPtr))
548 // Keywords for instructions.
549 #define INSTKEYWORD(STR, type, Enum, TOK) \
550 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
551 llvmAsmlval.type = Instruction::Enum; return TOK; }
553 INSTKEYWORD("add", BinaryOpVal, Add, ADD);
554 INSTKEYWORD("sub", BinaryOpVal, Sub, SUB);
555 INSTKEYWORD("mul", BinaryOpVal, Mul, MUL);
556 INSTKEYWORD("udiv", BinaryOpVal, UDiv, UDIV);
557 INSTKEYWORD("sdiv", BinaryOpVal, SDiv, SDIV);
558 INSTKEYWORD("fdiv", BinaryOpVal, FDiv, FDIV);
559 INSTKEYWORD("urem", BinaryOpVal, URem, UREM);
560 INSTKEYWORD("srem", BinaryOpVal, SRem, SREM);
561 INSTKEYWORD("frem", BinaryOpVal, FRem, FREM);
562 INSTKEYWORD("shl", BinaryOpVal, Shl, SHL);
563 INSTKEYWORD("lshr", BinaryOpVal, LShr, LSHR);
564 INSTKEYWORD("ashr", BinaryOpVal, AShr, ASHR);
565 INSTKEYWORD("and", BinaryOpVal, And, AND);
566 INSTKEYWORD("or", BinaryOpVal, Or , OR );
567 INSTKEYWORD("xor", BinaryOpVal, Xor, XOR);
568 INSTKEYWORD("icmp", OtherOpVal, ICmp, ICMP);
569 INSTKEYWORD("fcmp", OtherOpVal, FCmp, FCMP);
571 INSTKEYWORD("phi", OtherOpVal, PHI, PHI_TOK);
572 INSTKEYWORD("call", OtherOpVal, Call, CALL);
573 INSTKEYWORD("trunc", CastOpVal, Trunc, TRUNC);
574 INSTKEYWORD("zext", CastOpVal, ZExt, ZEXT);
575 INSTKEYWORD("sext", CastOpVal, SExt, SEXT);
576 INSTKEYWORD("fptrunc", CastOpVal, FPTrunc, FPTRUNC);
577 INSTKEYWORD("fpext", CastOpVal, FPExt, FPEXT);
578 INSTKEYWORD("uitofp", CastOpVal, UIToFP, UITOFP);
579 INSTKEYWORD("sitofp", CastOpVal, SIToFP, SITOFP);
580 INSTKEYWORD("fptoui", CastOpVal, FPToUI, FPTOUI);
581 INSTKEYWORD("fptosi", CastOpVal, FPToSI, FPTOSI);
582 INSTKEYWORD("inttoptr", CastOpVal, IntToPtr, INTTOPTR);
583 INSTKEYWORD("ptrtoint", CastOpVal, PtrToInt, PTRTOINT);
584 INSTKEYWORD("bitcast", CastOpVal, BitCast, BITCAST);
585 INSTKEYWORD("select", OtherOpVal, Select, SELECT);
586 INSTKEYWORD("va_arg", OtherOpVal, VAArg , VAARG);
587 INSTKEYWORD("ret", TermOpVal, Ret, RET);
588 INSTKEYWORD("br", TermOpVal, Br, BR);
589 INSTKEYWORD("switch", TermOpVal, Switch, SWITCH);
590 INSTKEYWORD("invoke", TermOpVal, Invoke, INVOKE);
591 INSTKEYWORD("unwind", TermOpVal, Unwind, UNWIND);
592 INSTKEYWORD("unreachable", TermOpVal, Unreachable, UNREACHABLE);
594 INSTKEYWORD("malloc", MemOpVal, Malloc, MALLOC);
595 INSTKEYWORD("alloca", MemOpVal, Alloca, ALLOCA);
596 INSTKEYWORD("free", MemOpVal, Free, FREE);
597 INSTKEYWORD("load", MemOpVal, Load, LOAD);
598 INSTKEYWORD("store", MemOpVal, Store, STORE);
599 INSTKEYWORD("getelementptr", MemOpVal, GetElementPtr, GETELEMENTPTR);
601 INSTKEYWORD("extractelement", OtherOpVal, ExtractElement, EXTRACTELEMENT);
602 INSTKEYWORD("insertelement", OtherOpVal, InsertElement, INSERTELEMENT);
603 INSTKEYWORD("shufflevector", OtherOpVal, ShuffleVector, SHUFFLEVECTOR);
604 INSTKEYWORD("getresult", OtherOpVal, GetResult, GETRESULT);
607 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
608 // the CFE to avoid forcing it to deal with 64-bit numbers.
609 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
610 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
611 int len = CurPtr-TokStart-3;
612 uint32_t bits = len * 4;
613 APInt Tmp(bits, TokStart+3, len, 16);
614 uint32_t activeBits = Tmp.getActiveBits();
615 if (activeBits > 0 && activeBits < bits)
616 Tmp.trunc(activeBits);
617 if (Tmp.getBitWidth() > 64) {
618 llvmAsmlval.APIntVal = new APInt(Tmp);
619 return TokStart[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
620 } else if (TokStart[0] == 's') {
621 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
624 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
629 // If this is "cc1234", return this as just "cc".
630 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
635 // If this starts with "call", return it as CALL. This is to support old
636 // broken .ll files. FIXME: remove this with LLVM 3.0.
637 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
639 llvmAsmlval.OtherOpVal = Instruction::Call;
643 // Finally, if this isn't known, return just a single character.
649 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
650 /// that this is not a label:
651 /// HexFPConstant 0x[0-9A-Fa-f]+
652 /// HexFP80Constant 0xK[0-9A-Fa-f]+
653 /// HexFP128Constant 0xL[0-9A-Fa-f]+
654 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
655 int LLLexer::Lex0x() {
656 CurPtr = TokStart + 2;
659 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
665 if (!isxdigit(CurPtr[0])) {
666 // Bad token, return it as just zero.
671 while (isxdigit(CurPtr[0]))
675 // HexFPConstant - Floating point constant represented in IEEE format as a
676 // hexadecimal number for when exponential notation is not precise enough.
677 // Float and double only.
678 llvmAsmlval.FPVal = new APFloat(HexToFP(TokStart+2, CurPtr));
683 HexToIntPair(TokStart+3, CurPtr, Pair);
685 default: assert(0 && "Unknown kind!");
687 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
688 llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
691 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
692 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair), true);
695 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
696 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
701 /// LexIdentifier: Handle several related productions:
702 /// Label [-a-zA-Z$._0-9]+:
704 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
706 /// HexFPConstant 0x[0-9A-Fa-f]+
707 /// HexFP80Constant 0xK[0-9A-Fa-f]+
708 /// HexFP128Constant 0xL[0-9A-Fa-f]+
709 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
710 int LLLexer::LexDigitOrNegative() {
711 // If the letter after the negative is a number, this is probably a label.
712 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
713 // Okay, this is not a number after the -, it's probably a label.
714 if (const char *End = isLabelTail(CurPtr)) {
715 llvmAsmlval.StrVal = new std::string(TokStart, End-1);
723 // At this point, it is either a label, int or fp constant.
725 // Skip digits, we have at least one.
726 for (; isdigit(CurPtr[0]); ++CurPtr)
729 // Check to see if this really is a label afterall, e.g. "-1:".
730 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
731 if (const char *End = isLabelTail(CurPtr)) {
732 llvmAsmlval.StrVal = new std::string(TokStart, End-1);
738 // If the next character is a '.', then it is a fp value, otherwise its
740 if (CurPtr[0] != '.') {
741 if (TokStart[0] == '0' && TokStart[1] == 'x')
743 unsigned Len = CurPtr-TokStart;
744 uint32_t numBits = ((Len * 64) / 19) + 2;
745 APInt Tmp(numBits, TokStart, Len, 10);
746 if (TokStart[0] == '-') {
747 uint32_t minBits = Tmp.getMinSignedBits();
748 if (minBits > 0 && minBits < numBits)
750 if (Tmp.getBitWidth() > 64) {
751 llvmAsmlval.APIntVal = new APInt(Tmp);
754 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
758 uint32_t activeBits = Tmp.getActiveBits();
759 if (activeBits > 0 && activeBits < numBits)
760 Tmp.trunc(activeBits);
761 if (Tmp.getBitWidth() > 64) {
762 llvmAsmlval.APIntVal = new APInt(Tmp);
765 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
773 // Skip over [0-9]*([eE][-+]?[0-9]+)?
774 while (isdigit(CurPtr[0])) ++CurPtr;
776 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
777 if (isdigit(CurPtr[1]) ||
778 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
780 while (isdigit(CurPtr[0])) ++CurPtr;
784 llvmAsmlval.FPVal = new APFloat(atof(TokStart));
788 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
789 int LLLexer::LexPositive() {
790 // If the letter after the negative is a number, this is probably not a
792 if (!isdigit(CurPtr[0]))
796 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
799 // At this point, we need a '.'.
800 if (CurPtr[0] != '.') {
807 // Skip over [0-9]*([eE][-+]?[0-9]+)?
808 while (isdigit(CurPtr[0])) ++CurPtr;
810 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
811 if (isdigit(CurPtr[1]) ||
812 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
814 while (isdigit(CurPtr[0])) ++CurPtr;
818 llvmAsmlval.FPVal = new APFloat(atof(TokStart));
823 //===----------------------------------------------------------------------===//
824 // Define the interface to this file.
825 //===----------------------------------------------------------------------===//
827 static LLLexer *TheLexer;
829 void InitLLLexer(llvm::MemoryBuffer *MB) {
830 assert(TheLexer == 0 && "LL Lexer isn't reentrant yet");
831 TheLexer = new LLLexer(MB);
835 return TheLexer->LexToken();
837 const char *LLLgetTokenStart() { return TheLexer->getTokStart(); }
838 unsigned LLLgetTokenLength() { return TheLexer->getTokLength(); }
839 std::string LLLgetFilename() { return TheLexer->getFilename(); }
840 unsigned LLLgetLineNo() { return TheLexer->getLineNo(); }