LLLexer.cpp revision 09d9ef4122414a1a2ec95f52d660d6500f2819d0
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===// 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LLVM Compiler Infrastructure 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Implement the Lexer for .ll files. 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "LLLexer.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/DerivedTypes.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Instruction.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/LLVMContext.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/ErrorHandling.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/MemoryBuffer.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/MathExtras.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/SourceMgr.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Support/raw_ostream.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Assembly/Parser.h" 24d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)#include <cstdio> 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <cstdlib> 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <cstring> 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using namespace llvm; 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 29d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)bool LLLexer::Error(LocTy ErrorLoc, const std::string &Msg) const { 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ErrorInfo = SM.GetMessage(ErrorLoc, Msg, "error"); 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return true; 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Helper functions. 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// atoull - Convert an ascii string of decimal digits into the unsigned long 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// long representation... this does not have to do input error checking, 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// because we know that the input will be matched by a suitable regex... 41d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)// 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)uint64_t LLLexer::atoull(const char *Buffer, const char *End) { 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint64_t Result = 0; 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (; Buffer != End; Buffer++) { 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint64_t OldRes = Result; 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Result *= 10; 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Result += *Buffer-'0'; 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (Result < OldRes) { // Uh, oh, overflow detected!!! 49d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) Error("constant bigger than 64 bits detected!"); 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0; 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 52d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) } 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return Result; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) { 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint64_t Result = 0; 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (; Buffer != End; ++Buffer) { 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint64_t OldRes = Result; 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Result *= 16; 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char C = *Buffer; 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (C >= '0' && C <= '9') 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Result += C-'0'; 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (C >= 'A' && C <= 'F') 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Result += C-'A'+10; 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (C >= 'a' && C <= 'f') 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Result += C-'a'+10; 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (Result < OldRes) { // Uh, oh, overflow detected!!! 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Error("constant bigger than 64 bits detected!"); 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0; 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return Result; 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LLLexer::HexToIntPair(const char *Buffer, const char *End, 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint64_t Pair[2]) { 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[0] = 0; 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (int i=0; i<16; i++, Buffer++) { 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) assert(Buffer != End); 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[0] *= 16; 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char C = *Buffer; 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (C >= '0' && C <= '9') 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[0] += C-'0'; 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (C >= 'A' && C <= 'F') 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[0] += C-'A'+10; 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (C >= 'a' && C <= 'f') 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[0] += C-'a'+10; 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[1] = 0; 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (int i=0; i<16 && Buffer != End; i++, Buffer++) { 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[1] *= 16; 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char C = *Buffer; 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (C >= '0' && C <= '9') 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[1] += C-'0'; 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (C >= 'A' && C <= 'F') 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[1] += C-'A'+10; 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (C >= 'a' && C <= 'f') 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[1] += C-'a'+10; 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (Buffer != End) 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Error("constant bigger than 128 bits detected!"); 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// { low64, high16 } as usual for an APInt. 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End, 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) uint64_t Pair[2]) { 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[1] = 0; 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (int i=0; i<4 && Buffer != End; i++, Buffer++) { 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) assert(Buffer != End); 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[1] *= 16; 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char C = *Buffer; 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (C >= '0' && C <= '9') 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[1] += C-'0'; 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (C >= 'A' && C <= 'F') 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[1] += C-'A'+10; 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (C >= 'a' && C <= 'f') 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[1] += C-'a'+10; 121d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) } 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[0] = 0; 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (int i=0; i<16; i++, Buffer++) { 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[0] *= 16; 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char C = *Buffer; 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (C >= '0' && C <= '9') 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[0] += C-'0'; 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (C >= 'A' && C <= 'F') 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[0] += C-'A'+10; 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else if (C >= 'a' && C <= 'f') 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Pair[0] += C-'a'+10; 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (Buffer != End) 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Error("constant bigger than 128 bits detected!"); 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// UnEscapeLexed - Run through the specified buffer and change \xx codes to the 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// appropriate character. 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void UnEscapeLexed(std::string &Str) { 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (Str.empty()) return; 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size(); 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char *BOut = Buffer; 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (char *BIn = Buffer; BIn != EndBuffer; ) { 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (BIn[0] == '\\') { 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (BIn < EndBuffer-1 && BIn[1] == '\\') { 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *BOut++ = '\\'; // Two \ becomes one 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) BIn += 2; 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) { 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char Tmp = BIn[3]; BIn[3] = 0; // Terminate string 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) BIn[3] = Tmp; // Restore character 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) BIn += 3; // Skip over handled chars 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ++BOut; 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *BOut++ = *BIn++; 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *BOut++ = *BIn++; 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Str.resize(BOut-Buffer); 163d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)} 1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// isLabelChar - Return true for [-a-zA-Z$._0-9]. 1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static bool isLabelChar(char C) { 1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_'; 1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// isLabelTail - Return true if this pointer points to a valid end of a label. 1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static const char *isLabelTail(const char *CurPtr) { 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (1) { 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (CurPtr[0] == ':') return CurPtr+1; 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!isLabelChar(CurPtr[0])) return 0; 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ++CurPtr; 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 180d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) 181d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Lexer definition. 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err, 1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) LLVMContext &C) 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) { 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) CurPtr = CurBuf->getBufferStart(); 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string LLLexer::getFilename() const { 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return CurBuf->getBufferIdentifier(); 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int LLLexer::getNextChar() { 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) char CurChar = *CurPtr++; 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) switch (CurChar) { 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) default: return (unsigned char)CurChar; 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) case 0: 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // A nul character in the stream is either the end of the current buffer or 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // a random nul in the file. Disambiguate that here. 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (CurPtr-1 != CurBuf->getBufferEnd()) 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 0; // Just whitespace. 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Otherwise, return end of file. 2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) --CurPtr; // Another call to lex will return EOF again. 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return EOF; 2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)lltok::Kind LLLexer::LexToken() { 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TokStart = CurPtr; 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int CurChar = getNextChar(); 2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) switch (CurChar) { 2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) default: 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Handle letters: [a-zA-Z_] 220 if (isalpha(CurChar) || CurChar == '_') 221 return LexIdentifier(); 222 223 return lltok::Error; 224 case EOF: return lltok::Eof; 225 case 0: 226 case ' ': 227 case '\t': 228 case '\n': 229 case '\r': 230 // Ignore whitespace. 231 return LexToken(); 232 case '+': return LexPositive(); 233 case '@': return LexAt(); 234 case '%': return LexPercent(); 235 case '"': return LexQuote(); 236 case '.': 237 if (const char *Ptr = isLabelTail(CurPtr)) { 238 CurPtr = Ptr; 239 StrVal.assign(TokStart, CurPtr-1); 240 return lltok::LabelStr; 241 } 242 if (CurPtr[0] == '.' && CurPtr[1] == '.') { 243 CurPtr += 2; 244 return lltok::dotdotdot; 245 } 246 return lltok::Error; 247 case '$': 248 if (const char *Ptr = isLabelTail(CurPtr)) { 249 CurPtr = Ptr; 250 StrVal.assign(TokStart, CurPtr-1); 251 return lltok::LabelStr; 252 } 253 return lltok::Error; 254 case ';': 255 SkipLineComment(); 256 return LexToken(); 257 case '!': return LexMetadata(); 258 case '0': case '1': case '2': case '3': case '4': 259 case '5': case '6': case '7': case '8': case '9': 260 case '-': 261 return LexDigitOrNegative(); 262 case '=': return lltok::equal; 263 case '[': return lltok::lsquare; 264 case ']': return lltok::rsquare; 265 case '{': return lltok::lbrace; 266 case '}': return lltok::rbrace; 267 case '<': return lltok::less; 268 case '>': return lltok::greater; 269 case '(': return lltok::lparen; 270 case ')': return lltok::rparen; 271 case ',': return lltok::comma; 272 case '*': return lltok::star; 273 case '\\': return lltok::backslash; 274 } 275} 276 277void LLLexer::SkipLineComment() { 278 while (1) { 279 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF) 280 return; 281 } 282} 283 284/// LexAt - Lex all tokens that start with an @ character: 285/// GlobalVar @\"[^\"]*\" 286/// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]* 287/// GlobalVarID @[0-9]+ 288lltok::Kind LLLexer::LexAt() { 289 // Handle AtStringConstant: @\"[^\"]*\" 290 if (CurPtr[0] == '"') { 291 ++CurPtr; 292 293 while (1) { 294 int CurChar = getNextChar(); 295 296 if (CurChar == EOF) { 297 Error("end of file in global variable name"); 298 return lltok::Error; 299 } 300 if (CurChar == '"') { 301 StrVal.assign(TokStart+2, CurPtr-1); 302 UnEscapeLexed(StrVal); 303 return lltok::GlobalVar; 304 } 305 } 306 } 307 308 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]* 309 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' || 310 CurPtr[0] == '.' || CurPtr[0] == '_') { 311 ++CurPtr; 312 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' || 313 CurPtr[0] == '.' || CurPtr[0] == '_') 314 ++CurPtr; 315 316 StrVal.assign(TokStart+1, CurPtr); // Skip @ 317 return lltok::GlobalVar; 318 } 319 320 // Handle GlobalVarID: @[0-9]+ 321 if (isdigit(CurPtr[0])) { 322 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr) 323 /*empty*/; 324 325 uint64_t Val = atoull(TokStart+1, CurPtr); 326 if ((unsigned)Val != Val) 327 Error("invalid value number (too large)!"); 328 UIntVal = unsigned(Val); 329 return lltok::GlobalID; 330 } 331 332 return lltok::Error; 333} 334 335 336/// LexPercent - Lex all tokens that start with a % character: 337/// LocalVar ::= %\"[^\"]*\" 338/// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]* 339/// LocalVarID ::= %[0-9]+ 340lltok::Kind LLLexer::LexPercent() { 341 // Handle LocalVarName: %\"[^\"]*\" 342 if (CurPtr[0] == '"') { 343 ++CurPtr; 344 345 while (1) { 346 int CurChar = getNextChar(); 347 348 if (CurChar == EOF) { 349 Error("end of file in string constant"); 350 return lltok::Error; 351 } 352 if (CurChar == '"') { 353 StrVal.assign(TokStart+2, CurPtr-1); 354 UnEscapeLexed(StrVal); 355 return lltok::LocalVar; 356 } 357 } 358 } 359 360 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]* 361 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' || 362 CurPtr[0] == '.' || CurPtr[0] == '_') { 363 ++CurPtr; 364 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' || 365 CurPtr[0] == '.' || CurPtr[0] == '_') 366 ++CurPtr; 367 368 StrVal.assign(TokStart+1, CurPtr); // Skip % 369 return lltok::LocalVar; 370 } 371 372 // Handle LocalVarID: %[0-9]+ 373 if (isdigit(CurPtr[0])) { 374 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr) 375 /*empty*/; 376 377 uint64_t Val = atoull(TokStart+1, CurPtr); 378 if ((unsigned)Val != Val) 379 Error("invalid value number (too large)!"); 380 UIntVal = unsigned(Val); 381 return lltok::LocalVarID; 382 } 383 384 return lltok::Error; 385} 386 387/// LexQuote - Lex all tokens that start with a " character: 388/// QuoteLabel "[^"]+": 389/// StringConstant "[^"]*" 390lltok::Kind LLLexer::LexQuote() { 391 while (1) { 392 int CurChar = getNextChar(); 393 394 if (CurChar == EOF) { 395 Error("end of file in quoted string"); 396 return lltok::Error; 397 } 398 399 if (CurChar != '"') continue; 400 401 if (CurPtr[0] != ':') { 402 StrVal.assign(TokStart+1, CurPtr-1); 403 UnEscapeLexed(StrVal); 404 return lltok::StringConstant; 405 } 406 407 ++CurPtr; 408 StrVal.assign(TokStart+1, CurPtr-2); 409 UnEscapeLexed(StrVal); 410 return lltok::LabelStr; 411 } 412} 413 414static bool JustWhitespaceNewLine(const char *&Ptr) { 415 const char *ThisPtr = Ptr; 416 while (*ThisPtr == ' ' || *ThisPtr == '\t') 417 ++ThisPtr; 418 if (*ThisPtr == '\n' || *ThisPtr == '\r') { 419 Ptr = ThisPtr; 420 return true; 421 } 422 return false; 423} 424 425/// LexMetadata: 426/// !{...} 427/// !42 428/// !foo 429lltok::Kind LLLexer::LexMetadata() { 430 if (isalpha(CurPtr[0])) { 431 ++CurPtr; 432 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' || 433 CurPtr[0] == '.' || CurPtr[0] == '_') 434 ++CurPtr; 435 436 StrVal.assign(TokStart+1, CurPtr); // Skip ! 437 return lltok::NamedOrCustomMD; 438 } 439 return lltok::Metadata; 440} 441 442/// LexIdentifier: Handle several related productions: 443/// Label [-a-zA-Z$._0-9]+: 444/// IntegerType i[0-9]+ 445/// Keyword sdiv, float, ... 446/// HexIntConstant [us]0x[0-9A-Fa-f]+ 447lltok::Kind LLLexer::LexIdentifier() { 448 const char *StartChar = CurPtr; 449 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar; 450 const char *KeywordEnd = 0; 451 452 for (; isLabelChar(*CurPtr); ++CurPtr) { 453 // If we decide this is an integer, remember the end of the sequence. 454 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr; 455 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr; 456 } 457 458 // If we stopped due to a colon, this really is a label. 459 if (*CurPtr == ':') { 460 StrVal.assign(StartChar-1, CurPtr++); 461 return lltok::LabelStr; 462 } 463 464 // Otherwise, this wasn't a label. If this was valid as an integer type, 465 // return it. 466 if (IntEnd == 0) IntEnd = CurPtr; 467 if (IntEnd != StartChar) { 468 CurPtr = IntEnd; 469 uint64_t NumBits = atoull(StartChar, CurPtr); 470 if (NumBits < IntegerType::MIN_INT_BITS || 471 NumBits > IntegerType::MAX_INT_BITS) { 472 Error("bitwidth for integer type out of range!"); 473 return lltok::Error; 474 } 475 TyVal = IntegerType::get(Context, NumBits); 476 return lltok::Type; 477 } 478 479 // Otherwise, this was a letter sequence. See which keyword this is. 480 if (KeywordEnd == 0) KeywordEnd = CurPtr; 481 CurPtr = KeywordEnd; 482 --StartChar; 483 unsigned Len = CurPtr-StartChar; 484#define KEYWORD(STR) \ 485 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \ 486 return lltok::kw_##STR; 487 488 KEYWORD(begin); KEYWORD(end); 489 KEYWORD(true); KEYWORD(false); 490 KEYWORD(declare); KEYWORD(define); 491 KEYWORD(global); KEYWORD(constant); 492 493 KEYWORD(private); 494 KEYWORD(linker_private); 495 KEYWORD(internal); 496 KEYWORD(available_externally); 497 KEYWORD(linkonce); 498 KEYWORD(linkonce_odr); 499 KEYWORD(weak); 500 KEYWORD(weak_odr); 501 KEYWORD(appending); 502 KEYWORD(dllimport); 503 KEYWORD(dllexport); 504 KEYWORD(common); 505 KEYWORD(default); 506 KEYWORD(hidden); 507 KEYWORD(protected); 508 KEYWORD(extern_weak); 509 KEYWORD(external); 510 KEYWORD(thread_local); 511 KEYWORD(zeroinitializer); 512 KEYWORD(undef); 513 KEYWORD(null); 514 KEYWORD(to); 515 KEYWORD(tail); 516 KEYWORD(target); 517 KEYWORD(triple); 518 KEYWORD(deplibs); 519 KEYWORD(datalayout); 520 KEYWORD(volatile); 521 KEYWORD(nuw); 522 KEYWORD(nsw); 523 KEYWORD(exact); 524 KEYWORD(inbounds); 525 KEYWORD(align); 526 KEYWORD(addrspace); 527 KEYWORD(section); 528 KEYWORD(alias); 529 KEYWORD(module); 530 KEYWORD(asm); 531 KEYWORD(sideeffect); 532 KEYWORD(alignstack); 533 KEYWORD(gc); 534 535 KEYWORD(ccc); 536 KEYWORD(fastcc); 537 KEYWORD(coldcc); 538 KEYWORD(x86_stdcallcc); 539 KEYWORD(x86_fastcallcc); 540 KEYWORD(arm_apcscc); 541 KEYWORD(arm_aapcscc); 542 KEYWORD(arm_aapcs_vfpcc); 543 544 KEYWORD(cc); 545 KEYWORD(c); 546 547 KEYWORD(signext); 548 KEYWORD(zeroext); 549 KEYWORD(inreg); 550 KEYWORD(sret); 551 KEYWORD(nounwind); 552 KEYWORD(noreturn); 553 KEYWORD(noalias); 554 KEYWORD(nocapture); 555 KEYWORD(byval); 556 KEYWORD(nest); 557 KEYWORD(readnone); 558 KEYWORD(readonly); 559 560 KEYWORD(inlinehint); 561 KEYWORD(noinline); 562 KEYWORD(alwaysinline); 563 KEYWORD(optsize); 564 KEYWORD(ssp); 565 KEYWORD(sspreq); 566 KEYWORD(noredzone); 567 KEYWORD(noimplicitfloat); 568 KEYWORD(naked); 569 570 KEYWORD(type); 571 KEYWORD(opaque); 572 573 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle); 574 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge); 575 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole); 576 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une); 577 578 KEYWORD(x); 579 KEYWORD(blockaddress); 580#undef KEYWORD 581 582 // Keywords for types. 583#define TYPEKEYWORD(STR, LLVMTY) \ 584 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \ 585 TyVal = LLVMTY; return lltok::Type; } 586 TYPEKEYWORD("void", Type::getVoidTy(Context)); 587 TYPEKEYWORD("float", Type::getFloatTy(Context)); 588 TYPEKEYWORD("double", Type::getDoubleTy(Context)); 589 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context)); 590 TYPEKEYWORD("fp128", Type::getFP128Ty(Context)); 591 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context)); 592 TYPEKEYWORD("label", Type::getLabelTy(Context)); 593 TYPEKEYWORD("metadata", Type::getMetadataTy(Context)); 594#undef TYPEKEYWORD 595 596 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is 597 // to avoid conflicting with the sext/zext instructions, below. 598 if (Len == 4 && !memcmp(StartChar, "sext", 4)) { 599 // Scan CurPtr ahead, seeing if there is just whitespace before the newline. 600 if (JustWhitespaceNewLine(CurPtr)) 601 return lltok::kw_signext; 602 } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) { 603 // Scan CurPtr ahead, seeing if there is just whitespace before the newline. 604 if (JustWhitespaceNewLine(CurPtr)) 605 return lltok::kw_zeroext; 606 } else if (Len == 6 && !memcmp(StartChar, "malloc", 6)) { 607 // FIXME: Remove in LLVM 3.0. 608 // Autoupgrade malloc instruction. 609 return lltok::kw_malloc; 610 } else if (Len == 4 && !memcmp(StartChar, "free", 4)) { 611 // FIXME: Remove in LLVM 3.0. 612 // Autoupgrade malloc instruction. 613 return lltok::kw_free; 614 } 615 616 // Keywords for instructions. 617#define INSTKEYWORD(STR, Enum) \ 618 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \ 619 UIntVal = Instruction::Enum; return lltok::kw_##STR; } 620 621 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd); 622 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub); 623 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul); 624 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv); 625 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem); 626 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr); 627 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor); 628 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp); 629 630 INSTKEYWORD(phi, PHI); 631 INSTKEYWORD(call, Call); 632 INSTKEYWORD(trunc, Trunc); 633 INSTKEYWORD(zext, ZExt); 634 INSTKEYWORD(sext, SExt); 635 INSTKEYWORD(fptrunc, FPTrunc); 636 INSTKEYWORD(fpext, FPExt); 637 INSTKEYWORD(uitofp, UIToFP); 638 INSTKEYWORD(sitofp, SIToFP); 639 INSTKEYWORD(fptoui, FPToUI); 640 INSTKEYWORD(fptosi, FPToSI); 641 INSTKEYWORD(inttoptr, IntToPtr); 642 INSTKEYWORD(ptrtoint, PtrToInt); 643 INSTKEYWORD(bitcast, BitCast); 644 INSTKEYWORD(select, Select); 645 INSTKEYWORD(va_arg, VAArg); 646 INSTKEYWORD(ret, Ret); 647 INSTKEYWORD(br, Br); 648 INSTKEYWORD(switch, Switch); 649 INSTKEYWORD(indirectbr, IndirectBr); 650 INSTKEYWORD(invoke, Invoke); 651 INSTKEYWORD(unwind, Unwind); 652 INSTKEYWORD(unreachable, Unreachable); 653 654 INSTKEYWORD(alloca, Alloca); 655 INSTKEYWORD(load, Load); 656 INSTKEYWORD(store, Store); 657 INSTKEYWORD(getelementptr, GetElementPtr); 658 659 INSTKEYWORD(extractelement, ExtractElement); 660 INSTKEYWORD(insertelement, InsertElement); 661 INSTKEYWORD(shufflevector, ShuffleVector); 662 INSTKEYWORD(getresult, ExtractValue); 663 INSTKEYWORD(extractvalue, ExtractValue); 664 INSTKEYWORD(insertvalue, InsertValue); 665#undef INSTKEYWORD 666 667 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by 668 // the CFE to avoid forcing it to deal with 64-bit numbers. 669 if ((TokStart[0] == 'u' || TokStart[0] == 's') && 670 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) { 671 int len = CurPtr-TokStart-3; 672 uint32_t bits = len * 4; 673 APInt Tmp(bits, StringRef(TokStart+3, len), 16); 674 uint32_t activeBits = Tmp.getActiveBits(); 675 if (activeBits > 0 && activeBits < bits) 676 Tmp.trunc(activeBits); 677 APSIntVal = APSInt(Tmp, TokStart[0] == 'u'); 678 return lltok::APSInt; 679 } 680 681 // If this is "cc1234", return this as just "cc". 682 if (TokStart[0] == 'c' && TokStart[1] == 'c') { 683 CurPtr = TokStart+2; 684 return lltok::kw_cc; 685 } 686 687 // If this starts with "call", return it as CALL. This is to support old 688 // broken .ll files. FIXME: remove this with LLVM 3.0. 689 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) { 690 CurPtr = TokStart+4; 691 UIntVal = Instruction::Call; 692 return lltok::kw_call; 693 } 694 695 // Finally, if this isn't known, return an error. 696 CurPtr = TokStart+1; 697 return lltok::Error; 698} 699 700 701/// Lex0x: Handle productions that start with 0x, knowing that it matches and 702/// that this is not a label: 703/// HexFPConstant 0x[0-9A-Fa-f]+ 704/// HexFP80Constant 0xK[0-9A-Fa-f]+ 705/// HexFP128Constant 0xL[0-9A-Fa-f]+ 706/// HexPPC128Constant 0xM[0-9A-Fa-f]+ 707lltok::Kind LLLexer::Lex0x() { 708 CurPtr = TokStart + 2; 709 710 char Kind; 711 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') { 712 Kind = *CurPtr++; 713 } else { 714 Kind = 'J'; 715 } 716 717 if (!isxdigit(CurPtr[0])) { 718 // Bad token, return it as an error. 719 CurPtr = TokStart+1; 720 return lltok::Error; 721 } 722 723 while (isxdigit(CurPtr[0])) 724 ++CurPtr; 725 726 if (Kind == 'J') { 727 // HexFPConstant - Floating point constant represented in IEEE format as a 728 // hexadecimal number for when exponential notation is not precise enough. 729 // Float and double only. 730 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr))); 731 return lltok::APFloat; 732 } 733 734 uint64_t Pair[2]; 735 switch (Kind) { 736 default: llvm_unreachable("Unknown kind!"); 737 case 'K': 738 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes) 739 FP80HexToIntPair(TokStart+3, CurPtr, Pair); 740 APFloatVal = APFloat(APInt(80, 2, Pair)); 741 return lltok::APFloat; 742 case 'L': 743 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes) 744 HexToIntPair(TokStart+3, CurPtr, Pair); 745 APFloatVal = APFloat(APInt(128, 2, Pair), true); 746 return lltok::APFloat; 747 case 'M': 748 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes) 749 HexToIntPair(TokStart+3, CurPtr, Pair); 750 APFloatVal = APFloat(APInt(128, 2, Pair)); 751 return lltok::APFloat; 752 } 753} 754 755/// LexIdentifier: Handle several related productions: 756/// Label [-a-zA-Z$._0-9]+: 757/// NInteger -[0-9]+ 758/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? 759/// PInteger [0-9]+ 760/// HexFPConstant 0x[0-9A-Fa-f]+ 761/// HexFP80Constant 0xK[0-9A-Fa-f]+ 762/// HexFP128Constant 0xL[0-9A-Fa-f]+ 763/// HexPPC128Constant 0xM[0-9A-Fa-f]+ 764lltok::Kind LLLexer::LexDigitOrNegative() { 765 // If the letter after the negative is a number, this is probably a label. 766 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) { 767 // Okay, this is not a number after the -, it's probably a label. 768 if (const char *End = isLabelTail(CurPtr)) { 769 StrVal.assign(TokStart, End-1); 770 CurPtr = End; 771 return lltok::LabelStr; 772 } 773 774 return lltok::Error; 775 } 776 777 // At this point, it is either a label, int or fp constant. 778 779 // Skip digits, we have at least one. 780 for (; isdigit(CurPtr[0]); ++CurPtr) 781 /*empty*/; 782 783 // Check to see if this really is a label afterall, e.g. "-1:". 784 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') { 785 if (const char *End = isLabelTail(CurPtr)) { 786 StrVal.assign(TokStart, End-1); 787 CurPtr = End; 788 return lltok::LabelStr; 789 } 790 } 791 792 // If the next character is a '.', then it is a fp value, otherwise its 793 // integer. 794 if (CurPtr[0] != '.') { 795 if (TokStart[0] == '0' && TokStart[1] == 'x') 796 return Lex0x(); 797 unsigned Len = CurPtr-TokStart; 798 uint32_t numBits = ((Len * 64) / 19) + 2; 799 APInt Tmp(numBits, StringRef(TokStart, Len), 10); 800 if (TokStart[0] == '-') { 801 uint32_t minBits = Tmp.getMinSignedBits(); 802 if (minBits > 0 && minBits < numBits) 803 Tmp.trunc(minBits); 804 APSIntVal = APSInt(Tmp, false); 805 } else { 806 uint32_t activeBits = Tmp.getActiveBits(); 807 if (activeBits > 0 && activeBits < numBits) 808 Tmp.trunc(activeBits); 809 APSIntVal = APSInt(Tmp, true); 810 } 811 return lltok::APSInt; 812 } 813 814 ++CurPtr; 815 816 // Skip over [0-9]*([eE][-+]?[0-9]+)? 817 while (isdigit(CurPtr[0])) ++CurPtr; 818 819 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') { 820 if (isdigit(CurPtr[1]) || 821 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) { 822 CurPtr += 2; 823 while (isdigit(CurPtr[0])) ++CurPtr; 824 } 825 } 826 827 APFloatVal = APFloat(atof(TokStart)); 828 return lltok::APFloat; 829} 830 831/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)? 832lltok::Kind LLLexer::LexPositive() { 833 // If the letter after the negative is a number, this is probably not a 834 // label. 835 if (!isdigit(CurPtr[0])) 836 return lltok::Error; 837 838 // Skip digits. 839 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr) 840 /*empty*/; 841 842 // At this point, we need a '.'. 843 if (CurPtr[0] != '.') { 844 CurPtr = TokStart+1; 845 return lltok::Error; 846 } 847 848 ++CurPtr; 849 850 // Skip over [0-9]*([eE][-+]?[0-9]+)? 851 while (isdigit(CurPtr[0])) ++CurPtr; 852 853 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') { 854 if (isdigit(CurPtr[1]) || 855 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) { 856 CurPtr += 2; 857 while (isdigit(CurPtr[0])) ++CurPtr; 858 } 859 } 860 861 APFloatVal = APFloat(atof(TokStart)); 862 return lltok::APFloat; 863} 864