LLVM API Documentation

LLLexer.cpp

Go to the documentation of this file.
00001 //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Implement the Lexer for .ll files.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "LLLexer.h"
00015 #include "llvm/DerivedTypes.h"
00016 #include "llvm/Instruction.h"
00017 #include "llvm/Support/MemoryBuffer.h"
00018 #include "llvm/Support/MathExtras.h"
00019 #include "llvm/Support/raw_ostream.h"
00020 #include "llvm/Assembly/Parser.h"
00021 #include <cstdlib>
00022 #include <cstring>
00023 using namespace llvm;
00024 
00025 bool LLLexer::Error(LocTy ErrorLoc, const std::string &Msg) const {
00026   // Scan backward to find the start of the line.
00027   const char *LineStart = ErrorLoc;
00028   while (LineStart != CurBuf->getBufferStart() &&
00029          LineStart[-1] != '\n' && LineStart[-1] != '\r')
00030     --LineStart;
00031   // Get the end of the line.
00032   const char *LineEnd = ErrorLoc;
00033   while (LineEnd != CurBuf->getBufferEnd() &&
00034          LineEnd[0] != '\n' && LineEnd[0] != '\r')
00035     ++LineEnd;
00036 
00037   unsigned LineNo = 1;
00038   for (const char *FP = CurBuf->getBufferStart(); FP != ErrorLoc; ++FP)
00039     if (*FP == '\n') ++LineNo;
00040 
00041   std::string LineContents(LineStart, LineEnd);
00042   ErrorInfo.setError(Msg, LineNo, ErrorLoc-LineStart, LineContents);
00043   return true;
00044 }
00045 
00046 //===----------------------------------------------------------------------===//
00047 // Helper functions.
00048 //===----------------------------------------------------------------------===//
00049 
00050 // atoull - Convert an ascii string of decimal digits into the unsigned long
00051 // long representation... this does not have to do input error checking,
00052 // because we know that the input will be matched by a suitable regex...
00053 //
00054 uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
00055   uint64_t Result = 0;
00056   for (; Buffer != End; Buffer++) {
00057     uint64_t OldRes = Result;
00058     Result *= 10;
00059     Result += *Buffer-'0';
00060     if (Result < OldRes) {  // Uh, oh, overflow detected!!!
00061       Error("constant bigger than 64 bits detected!");
00062       return 0;
00063     }
00064   }
00065   return Result;
00066 }
00067 
00068 uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
00069   uint64_t Result = 0;
00070   for (; Buffer != End; ++Buffer) {
00071     uint64_t OldRes = Result;
00072     Result *= 16;
00073     char C = *Buffer;
00074     if (C >= '0' && C <= '9')
00075       Result += C-'0';
00076     else if (C >= 'A' && C <= 'F')
00077       Result += C-'A'+10;
00078     else if (C >= 'a' && C <= 'f')
00079       Result += C-'a'+10;
00080 
00081     if (Result < OldRes) {   // Uh, oh, overflow detected!!!
00082       Error("constant bigger than 64 bits detected!");
00083       return 0;
00084     }
00085   }
00086   return Result;
00087 }
00088 
00089 void LLLexer::HexToIntPair(const char *Buffer, const char *End,
00090                            uint64_t Pair[2]) {
00091   Pair[0] = 0;
00092   for (int i=0; i<16; i++, Buffer++) {
00093     assert(Buffer != End);
00094     Pair[0] *= 16;
00095     char C = *Buffer;
00096     if (C >= '0' && C <= '9')
00097       Pair[0] += C-'0';
00098     else if (C >= 'A' && C <= 'F')
00099       Pair[0] += C-'A'+10;
00100     else if (C >= 'a' && C <= 'f')
00101       Pair[0] += C-'a'+10;
00102   }
00103   Pair[1] = 0;
00104   for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
00105     Pair[1] *= 16;
00106     char C = *Buffer;
00107     if (C >= '0' && C <= '9')
00108       Pair[1] += C-'0';
00109     else if (C >= 'A' && C <= 'F')
00110       Pair[1] += C-'A'+10;
00111     else if (C >= 'a' && C <= 'f')
00112       Pair[1] += C-'a'+10;
00113   }
00114   if (Buffer != End)
00115     Error("constant bigger than 128 bits detected!");
00116 }
00117 
00118 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
00119 // appropriate character.
00120 static void UnEscapeLexed(std::string &Str) {
00121   if (Str.empty()) return;
00122 
00123   char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
00124   char *BOut = Buffer;
00125   for (char *BIn = Buffer; BIn != EndBuffer; ) {
00126     if (BIn[0] == '\\') {
00127       if (BIn < EndBuffer-1 && BIn[1] == '\\') {
00128         *BOut++ = '\\'; // Two \ becomes one
00129         BIn += 2;
00130       } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
00131         char Tmp = BIn[3]; BIn[3] = 0;      // Terminate string
00132         *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
00133         BIn[3] = Tmp;                       // Restore character
00134         BIn += 3;                           // Skip over handled chars
00135         ++BOut;
00136       } else {
00137         *BOut++ = *BIn++;
00138       }
00139     } else {
00140       *BOut++ = *BIn++;
00141     }
00142   }
00143   Str.resize(BOut-Buffer);
00144 }
00145 
00146 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
00147 static bool isLabelChar(char C) {
00148   return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
00149 }
00150 
00151 
00152 /// isLabelTail - Return true if this pointer points to a valid end of a label.
00153 static const char *isLabelTail(const char *CurPtr) {
00154   while (1) {
00155     if (CurPtr[0] == ':') return CurPtr+1;
00156     if (!isLabelChar(CurPtr[0])) return 0;
00157     ++CurPtr;
00158   }
00159 }
00160 
00161 
00162 
00163 //===----------------------------------------------------------------------===//
00164 // Lexer definition.
00165 //===----------------------------------------------------------------------===//
00166 
00167 LLLexer::LLLexer(MemoryBuffer *StartBuf, ParseError &Err)
00168   : CurBuf(StartBuf), ErrorInfo(Err), APFloatVal(0.0) {
00169   CurPtr = CurBuf->getBufferStart();
00170 }
00171 
00172 std::string LLLexer::getFilename() const {
00173   return CurBuf->getBufferIdentifier();
00174 }
00175 
00176 int LLLexer::getNextChar() {
00177   char CurChar = *CurPtr++;
00178   switch (CurChar) {
00179   default: return (unsigned char)CurChar;
00180   case 0:
00181     // A nul character in the stream is either the end of the current buffer or
00182     // a random nul in the file.  Disambiguate that here.
00183     if (CurPtr-1 != CurBuf->getBufferEnd())
00184       return 0;  // Just whitespace.
00185 
00186     // Otherwise, return end of file.
00187     --CurPtr;  // Another call to lex will return EOF again.
00188     return EOF;
00189   }
00190 }
00191 
00192 
00193 lltok::Kind LLLexer::LexToken() {
00194   TokStart = CurPtr;
00195 
00196   int CurChar = getNextChar();
00197   switch (CurChar) {
00198   default:
00199     // Handle letters: [a-zA-Z_]
00200     if (isalpha(CurChar) || CurChar == '_')
00201       return LexIdentifier();
00202 
00203     return lltok::Error;
00204   case EOF: return lltok::Eof;
00205   case 0:
00206   case ' ':
00207   case '\t':
00208   case '\n':
00209   case '\r':
00210     // Ignore whitespace.
00211     return LexToken();
00212   case '+': return LexPositive();
00213   case '@': return LexAt();
00214   case '%': return LexPercent();
00215   case '"': return LexQuote();
00216   case '.':
00217     if (const char *Ptr = isLabelTail(CurPtr)) {
00218       CurPtr = Ptr;
00219       StrVal.assign(TokStart, CurPtr-1);
00220       return lltok::LabelStr;
00221     }
00222     if (CurPtr[0] == '.' && CurPtr[1] == '.') {
00223       CurPtr += 2;
00224       return lltok::dotdotdot;
00225     }
00226     return lltok::Error;
00227   case '$':
00228     if (const char *Ptr = isLabelTail(CurPtr)) {
00229       CurPtr = Ptr;
00230       StrVal.assign(TokStart, CurPtr-1);
00231       return lltok::LabelStr;
00232     }
00233     return lltok::Error;
00234   case ';':
00235     SkipLineComment();
00236     return LexToken();
00237   case '0': case '1': case '2': case '3': case '4':
00238   case '5': case '6': case '7': case '8': case '9':
00239   case '-':
00240     return LexDigitOrNegative();
00241   case '=': return lltok::equal;
00242   case '[': return lltok::lsquare;
00243   case ']': return lltok::rsquare;
00244   case '{': return lltok::lbrace;
00245   case '}': return lltok::rbrace;
00246   case '<': return lltok::less;
00247   case '>': return lltok::greater;
00248   case '(': return lltok::lparen;
00249   case ')': return lltok::rparen;
00250   case ',': return lltok::comma;
00251   case '*': return lltok::star;
00252   case '\\': return lltok::backslash;
00253   }
00254 }
00255 
00256 void LLLexer::SkipLineComment() {
00257   while (1) {
00258     if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
00259       return;
00260   }
00261 }
00262 
00263 /// LexAt - Lex all tokens that start with an @ character:
00264 ///   GlobalVar   @\"[^\"]*\"
00265 ///   GlobalVar   @[-a-zA-Z$._][-a-zA-Z$._0-9]*
00266 ///   GlobalVarID @[0-9]+
00267 lltok::Kind LLLexer::LexAt() {
00268   // Handle AtStringConstant: @\"[^\"]*\"
00269   if (CurPtr[0] == '"') {
00270     ++CurPtr;
00271 
00272     while (1) {
00273       int CurChar = getNextChar();
00274 
00275       if (CurChar == EOF) {
00276         Error("end of file in global variable name");
00277         return lltok::Error;
00278       }
00279       if (CurChar == '"') {
00280         StrVal.assign(TokStart+2, CurPtr-1);
00281         UnEscapeLexed(StrVal);
00282         return lltok::GlobalVar;
00283       }
00284     }
00285   }
00286 
00287   // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
00288   if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
00289       CurPtr[0] == '.' || CurPtr[0] == '_') {
00290     ++CurPtr;
00291     while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
00292            CurPtr[0] == '.' || CurPtr[0] == '_')
00293       ++CurPtr;
00294 
00295     StrVal.assign(TokStart+1, CurPtr);   // Skip @
00296     return lltok::GlobalVar;
00297   }
00298 
00299   // Handle GlobalVarID: @[0-9]+
00300   if (isdigit(CurPtr[0])) {
00301     for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
00302       /*empty*/;
00303 
00304     uint64_t Val = atoull(TokStart+1, CurPtr);
00305     if ((unsigned)Val != Val)
00306       Error("invalid value number (too large)!");
00307     UIntVal = unsigned(Val);
00308     return lltok::GlobalID;
00309   }
00310 
00311   return lltok::Error;
00312 }
00313 
00314 
00315 /// LexPercent - Lex all tokens that start with a % character:
00316 ///   LocalVar   ::= %\"[^\"]*\"
00317 ///   LocalVar   ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
00318 ///   LocalVarID ::= %[0-9]+
00319 lltok::Kind LLLexer::LexPercent() {
00320   // Handle LocalVarName: %\"[^\"]*\"
00321   if (CurPtr[0] == '"') {
00322     ++CurPtr;
00323 
00324     while (1) {
00325       int CurChar = getNextChar();
00326 
00327       if (CurChar == EOF) {
00328         Error("end of file in string constant");
00329         return lltok::Error;
00330       }
00331       if (CurChar == '"') {
00332         StrVal.assign(TokStart+2, CurPtr-1);
00333         UnEscapeLexed(StrVal);
00334         return lltok::LocalVar;
00335       }
00336     }
00337   }
00338 
00339   // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
00340   if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
00341       CurPtr[0] == '.' || CurPtr[0] == '_') {
00342     ++CurPtr;
00343     while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
00344            CurPtr[0] == '.' || CurPtr[0] == '_')
00345       ++CurPtr;
00346 
00347     StrVal.assign(TokStart+1, CurPtr);   // Skip %
00348     return lltok::LocalVar;
00349   }
00350 
00351   // Handle LocalVarID: %[0-9]+
00352   if (isdigit(CurPtr[0])) {
00353     for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
00354       /*empty*/;
00355 
00356     uint64_t Val = atoull(TokStart+1, CurPtr);
00357     if ((unsigned)Val != Val)
00358       Error("invalid value number (too large)!");
00359     UIntVal = unsigned(Val);
00360     return lltok::LocalVarID;
00361   }
00362 
00363   return lltok::Error;
00364 }
00365 
00366 /// LexQuote - Lex all tokens that start with a " character:
00367 ///   QuoteLabel        "[^"]+":
00368 ///   StringConstant    "[^"]*"
00369 lltok::Kind LLLexer::LexQuote() {
00370   while (1) {
00371     int CurChar = getNextChar();
00372 
00373     if (CurChar == EOF) {
00374       Error("end of file in quoted string");
00375       return lltok::Error;
00376     }
00377 
00378     if (CurChar != '"') continue;
00379 
00380     if (CurPtr[0] != ':') {
00381       StrVal.assign(TokStart+1, CurPtr-1);
00382       UnEscapeLexed(StrVal);
00383       return lltok::StringConstant;
00384     }
00385 
00386     ++CurPtr;
00387     StrVal.assign(TokStart+1, CurPtr-2);
00388     UnEscapeLexed(StrVal);
00389     return lltok::LabelStr;
00390   }
00391 }
00392 
00393 static bool JustWhitespaceNewLine(const char *&Ptr) {
00394   const char *ThisPtr = Ptr;
00395   while (*ThisPtr == ' ' || *ThisPtr == '\t')
00396     ++ThisPtr;
00397   if (*ThisPtr == '\n' || *ThisPtr == '\r') {
00398     Ptr = ThisPtr;
00399     return true;
00400   }
00401   return false;
00402 }
00403 
00404 
00405 /// LexIdentifier: Handle several related productions:
00406 ///    Label           [-a-zA-Z$._0-9]+:
00407 ///    IntegerType     i[0-9]+
00408 ///    Keyword         sdiv, float, ...
00409 ///    HexIntConstant  [us]0x[0-9A-Fa-f]+
00410 lltok::Kind LLLexer::LexIdentifier() {
00411   const char *StartChar = CurPtr;
00412   const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
00413   const char *KeywordEnd = 0;
00414 
00415   for (; isLabelChar(*CurPtr); ++CurPtr) {
00416     // If we decide this is an integer, remember the end of the sequence.
00417     if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
00418     if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
00419   }
00420 
00421   // If we stopped due to a colon, this really is a label.
00422   if (*CurPtr == ':') {
00423     StrVal.assign(StartChar-1, CurPtr++);
00424     return lltok::LabelStr;
00425   }
00426 
00427   // Otherwise, this wasn't a label.  If this was valid as an integer type,
00428   // return it.
00429   if (IntEnd == 0) IntEnd = CurPtr;
00430   if (IntEnd != StartChar) {
00431     CurPtr = IntEnd;
00432     uint64_t NumBits = atoull(StartChar, CurPtr);
00433     if (NumBits < IntegerType::MIN_INT_BITS ||
00434         NumBits > IntegerType::MAX_INT_BITS) {
00435       Error("bitwidth for integer type out of range!");
00436       return lltok::Error;
00437     }
00438     TyVal = IntegerType::get(NumBits);
00439     return lltok::Type;
00440   }
00441 
00442   // Otherwise, this was a letter sequence.  See which keyword this is.
00443   if (KeywordEnd == 0) KeywordEnd = CurPtr;
00444   CurPtr = KeywordEnd;
00445   --StartChar;
00446   unsigned Len = CurPtr-StartChar;
00447 #define KEYWORD(STR) \
00448   if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
00449     return lltok::kw_##STR;
00450 
00451   KEYWORD(begin);   KEYWORD(end);
00452   KEYWORD(true);    KEYWORD(false);
00453   KEYWORD(declare); KEYWORD(define);
00454   KEYWORD(global);  KEYWORD(constant);
00455 
00456   KEYWORD(private);
00457   KEYWORD(internal);
00458   KEYWORD(linkonce);
00459   KEYWORD(weak);
00460   KEYWORD(appending);
00461   KEYWORD(dllimport);
00462   KEYWORD(dllexport);
00463   KEYWORD(common);
00464   KEYWORD(default);
00465   KEYWORD(hidden);
00466   KEYWORD(protected);
00467   KEYWORD(extern_weak);
00468   KEYWORD(external);
00469   KEYWORD(thread_local);
00470   KEYWORD(zeroinitializer);
00471   KEYWORD(undef);
00472   KEYWORD(null);
00473   KEYWORD(to);
00474   KEYWORD(tail);
00475   KEYWORD(target);
00476   KEYWORD(triple);
00477   KEYWORD(deplibs);
00478   KEYWORD(datalayout);
00479   KEYWORD(volatile);
00480   KEYWORD(align);
00481   KEYWORD(addrspace);
00482   KEYWORD(section);
00483   KEYWORD(alias);
00484   KEYWORD(module);
00485   KEYWORD(asm);
00486   KEYWORD(sideeffect);
00487   KEYWORD(gc);
00488 
00489   KEYWORD(ccc);
00490   KEYWORD(fastcc);
00491   KEYWORD(coldcc);
00492   KEYWORD(x86_stdcallcc);
00493   KEYWORD(x86_fastcallcc);
00494   KEYWORD(cc);
00495   KEYWORD(c);
00496 
00497   KEYWORD(signext);
00498   KEYWORD(zeroext);
00499   KEYWORD(inreg);
00500   KEYWORD(sret);
00501   KEYWORD(nounwind);
00502   KEYWORD(noreturn);
00503   KEYWORD(noalias);
00504   KEYWORD(nocapture);
00505   KEYWORD(byval);
00506   KEYWORD(nest);
00507   KEYWORD(readnone);
00508   KEYWORD(readonly);
00509 
00510   KEYWORD(noinline);
00511   KEYWORD(alwaysinline);
00512   KEYWORD(optsize);
00513   KEYWORD(ssp);
00514   KEYWORD(sspreq);
00515 
00516   KEYWORD(type);
00517   KEYWORD(opaque);
00518 
00519   KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
00520   KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
00521   KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
00522   KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
00523 
00524   KEYWORD(x);
00525 #undef KEYWORD
00526 
00527   // Keywords for types.
00528 #define TYPEKEYWORD(STR, LLVMTY) \
00529   if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
00530     TyVal = LLVMTY; return lltok::Type; }
00531   TYPEKEYWORD("void",      Type::VoidTy);
00532   TYPEKEYWORD("float",     Type::FloatTy);
00533   TYPEKEYWORD("double",    Type::DoubleTy);
00534   TYPEKEYWORD("x86_fp80",  Type::X86_FP80Ty);
00535   TYPEKEYWORD("fp128",     Type::FP128Ty);
00536   TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty);
00537   TYPEKEYWORD("label",     Type::LabelTy);
00538 #undef TYPEKEYWORD
00539 
00540   // Handle special forms for autoupgrading.  Drop these in LLVM 3.0.  This is
00541   // to avoid conflicting with the sext/zext instructions, below.
00542   if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
00543     // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
00544     if (JustWhitespaceNewLine(CurPtr))
00545       return lltok::kw_signext;
00546   } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
00547     // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
00548     if (JustWhitespaceNewLine(CurPtr))
00549       return lltok::kw_zeroext;
00550   }
00551 
00552   // Keywords for instructions.
00553 #define INSTKEYWORD(STR, Enum) \
00554   if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
00555     UIntVal = Instruction::Enum; return lltok::kw_##STR; }
00556 
00557   INSTKEYWORD(add,   Add);  INSTKEYWORD(sub,   Sub);  INSTKEYWORD(mul,   Mul);
00558   INSTKEYWORD(udiv,  UDiv); INSTKEYWORD(sdiv,  SDiv); INSTKEYWORD(fdiv,  FDiv);
00559   INSTKEYWORD(urem,  URem); INSTKEYWORD(srem,  SRem); INSTKEYWORD(frem,  FRem);
00560   INSTKEYWORD(shl,   Shl);  INSTKEYWORD(lshr,  LShr); INSTKEYWORD(ashr,  AShr);
00561   INSTKEYWORD(and,   And);  INSTKEYWORD(or,    Or);   INSTKEYWORD(xor,   Xor);
00562   INSTKEYWORD(icmp,  ICmp); INSTKEYWORD(fcmp,  FCmp);
00563 
00564   INSTKEYWORD(phi,         PHI);
00565   INSTKEYWORD(call,        Call);
00566   INSTKEYWORD(trunc,       Trunc);
00567   INSTKEYWORD(zext,        ZExt);
00568   INSTKEYWORD(sext,        SExt);
00569   INSTKEYWORD(fptrunc,     FPTrunc);
00570   INSTKEYWORD(fpext,       FPExt);
00571   INSTKEYWORD(uitofp,      UIToFP);
00572   INSTKEYWORD(sitofp,      SIToFP);
00573   INSTKEYWORD(fptoui,      FPToUI);
00574   INSTKEYWORD(fptosi,      FPToSI);
00575   INSTKEYWORD(inttoptr,    IntToPtr);
00576   INSTKEYWORD(ptrtoint,    PtrToInt);
00577   INSTKEYWORD(bitcast,     BitCast);
00578   INSTKEYWORD(select,      Select);
00579   INSTKEYWORD(va_arg,      VAArg);
00580   INSTKEYWORD(ret,         Ret);
00581   INSTKEYWORD(br,          Br);
00582   INSTKEYWORD(switch,      Switch);
00583   INSTKEYWORD(invoke,      Invoke);
00584   INSTKEYWORD(unwind,      Unwind);
00585   INSTKEYWORD(unreachable, Unreachable);
00586 
00587   INSTKEYWORD(malloc,      Malloc);
00588   INSTKEYWORD(alloca,      Alloca);
00589   INSTKEYWORD(free,        Free);
00590   INSTKEYWORD(load,        Load);
00591   INSTKEYWORD(store,       Store);
00592   INSTKEYWORD(getelementptr, GetElementPtr);
00593 
00594   INSTKEYWORD(extractelement, ExtractElement);
00595   INSTKEYWORD(insertelement,  InsertElement);
00596   INSTKEYWORD(shufflevector,  ShuffleVector);
00597   INSTKEYWORD(getresult,      ExtractValue);
00598   INSTKEYWORD(extractvalue,   ExtractValue);
00599   INSTKEYWORD(insertvalue,    InsertValue);
00600 #undef INSTKEYWORD
00601 
00602   // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
00603   // the CFE to avoid forcing it to deal with 64-bit numbers.
00604   if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
00605       TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
00606     int len = CurPtr-TokStart-3;
00607     uint32_t bits = len * 4;
00608     APInt Tmp(bits, TokStart+3, len, 16);
00609     uint32_t activeBits = Tmp.getActiveBits();
00610     if (activeBits > 0 && activeBits < bits)
00611       Tmp.trunc(activeBits);
00612     APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
00613     return lltok::APSInt;
00614   }
00615 
00616   // If this is "cc1234", return this as just "cc".
00617   if (TokStart[0] == 'c' && TokStart[1] == 'c') {
00618     CurPtr = TokStart+2;
00619     return lltok::kw_cc;
00620   }
00621 
00622   // If this starts with "call", return it as CALL.  This is to support old
00623   // broken .ll files.  FIXME: remove this with LLVM 3.0.
00624   if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
00625     CurPtr = TokStart+4;
00626     UIntVal = Instruction::Call;
00627     return lltok::kw_call;
00628   }
00629 
00630   // Finally, if this isn't known, return an error.
00631   CurPtr = TokStart+1;
00632   return lltok::Error;
00633 }
00634 
00635 
00636 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
00637 /// that this is not a label:
00638 ///    HexFPConstant     0x[0-9A-Fa-f]+
00639 ///    HexFP80Constant   0xK[0-9A-Fa-f]+
00640 ///    HexFP128Constant  0xL[0-9A-Fa-f]+
00641 ///    HexPPC128Constant 0xM[0-9A-Fa-f]+
00642 lltok::Kind LLLexer::Lex0x() {
00643   CurPtr = TokStart + 2;
00644 
00645   char Kind;
00646   if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
00647     Kind = *CurPtr++;
00648   } else {
00649     Kind = 'J';
00650   }
00651 
00652   if (!isxdigit(CurPtr[0])) {
00653     // Bad token, return it as an error.
00654     CurPtr = TokStart+1;
00655     return lltok::Error;
00656   }
00657 
00658   while (isxdigit(CurPtr[0]))
00659     ++CurPtr;
00660 
00661   if (Kind == 'J') {
00662     // HexFPConstant - Floating point constant represented in IEEE format as a
00663     // hexadecimal number for when exponential notation is not precise enough.
00664     // Float and double only.
00665     APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
00666     return lltok::APFloat;
00667   }
00668 
00669   uint64_t Pair[2];
00670   HexToIntPair(TokStart+3, CurPtr, Pair);
00671   switch (Kind) {
00672   default: assert(0 && "Unknown kind!");
00673   case 'K':
00674     // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
00675     APFloatVal = APFloat(APInt(80, 2, Pair));
00676     return lltok::APFloat;
00677   case 'L':
00678     // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
00679     APFloatVal = APFloat(APInt(128, 2, Pair), true);
00680     return lltok::APFloat;
00681   case 'M':
00682     // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
00683     APFloatVal = APFloat(APInt(128, 2, Pair));
00684     return lltok::APFloat;
00685   }
00686 }
00687 
00688 /// LexIdentifier: Handle several related productions:
00689 ///    Label             [-a-zA-Z$._0-9]+:
00690 ///    NInteger          -[0-9]+
00691 ///    FPConstant        [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
00692 ///    PInteger          [0-9]+
00693 ///    HexFPConstant     0x[0-9A-Fa-f]+
00694 ///    HexFP80Constant   0xK[0-9A-Fa-f]+
00695 ///    HexFP128Constant  0xL[0-9A-Fa-f]+
00696 ///    HexPPC128Constant 0xM[0-9A-Fa-f]+
00697 lltok::Kind LLLexer::LexDigitOrNegative() {
00698   // If the letter after the negative is a number, this is probably a label.
00699   if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
00700     // Okay, this is not a number after the -, it's probably a label.
00701     if (const char *End = isLabelTail(CurPtr)) {
00702       StrVal.assign(TokStart, End-1);
00703       CurPtr = End;
00704       return lltok::LabelStr;
00705     }
00706 
00707     return lltok::Error;
00708   }
00709 
00710   // At this point, it is either a label, int or fp constant.
00711 
00712   // Skip digits, we have at least one.
00713   for (; isdigit(CurPtr[0]); ++CurPtr)
00714     /*empty*/;
00715 
00716   // Check to see if this really is a label afterall, e.g. "-1:".
00717   if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
00718     if (const char *End = isLabelTail(CurPtr)) {
00719       StrVal.assign(TokStart, End-1);
00720       CurPtr = End;
00721       return lltok::LabelStr;
00722     }
00723   }
00724 
00725   // If the next character is a '.', then it is a fp value, otherwise its
00726   // integer.
00727   if (CurPtr[0] != '.') {
00728     if (TokStart[0] == '0' && TokStart[1] == 'x')
00729       return Lex0x();
00730     unsigned Len = CurPtr-TokStart;
00731     uint32_t numBits = ((Len * 64) / 19) + 2;
00732     APInt Tmp(numBits, TokStart, Len, 10);
00733     if (TokStart[0] == '-') {
00734       uint32_t minBits = Tmp.getMinSignedBits();
00735       if (minBits > 0 && minBits < numBits)
00736         Tmp.trunc(minBits);
00737       APSIntVal = APSInt(Tmp, false);
00738     } else {
00739       uint32_t activeBits = Tmp.getActiveBits();
00740       if (activeBits > 0 && activeBits < numBits)
00741         Tmp.trunc(activeBits);
00742       APSIntVal = APSInt(Tmp, true);
00743     }
00744     return lltok::APSInt;
00745   }
00746 
00747   ++CurPtr;
00748 
00749   // Skip over [0-9]*([eE][-+]?[0-9]+)?
00750   while (isdigit(CurPtr[0])) ++CurPtr;
00751 
00752   if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
00753     if (isdigit(CurPtr[1]) ||
00754         ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
00755       CurPtr += 2;
00756       while (isdigit(CurPtr[0])) ++CurPtr;
00757     }
00758   }
00759 
00760   APFloatVal = APFloat(atof(TokStart));
00761   return lltok::APFloat;
00762 }
00763 
00764 ///    FPConstant  [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
00765 lltok::Kind LLLexer::LexPositive() {
00766   // If the letter after the negative is a number, this is probably not a
00767   // label.
00768   if (!isdigit(CurPtr[0]))
00769     return lltok::Error;
00770 
00771   // Skip digits.
00772   for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
00773     /*empty*/;
00774 
00775   // At this point, we need a '.'.
00776   if (CurPtr[0] != '.') {
00777     CurPtr = TokStart+1;
00778     return lltok::Error;
00779   }
00780 
00781   ++CurPtr;
00782 
00783   // Skip over [0-9]*([eE][-+]?[0-9]+)?
00784   while (isdigit(CurPtr[0])) ++CurPtr;
00785 
00786   if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
00787     if (isdigit(CurPtr[1]) ||
00788         ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
00789       CurPtr += 2;
00790       while (isdigit(CurPtr[0])) ++CurPtr;
00791     }
00792   }
00793 
00794   APFloatVal = APFloat(atof(TokStart));
00795   return lltok::APFloat;
00796 }