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