Home | History | Annotate | Download | only in AsmParser
      1 //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // Implement the Lexer for .ll files.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "LLLexer.h"
     15 #include "llvm/ADT/StringExtras.h"
     16 #include "llvm/ADT/Twine.h"
     17 #include "llvm/Assembly/Parser.h"
     18 #include "llvm/IR/DerivedTypes.h"
     19 #include "llvm/IR/Instruction.h"
     20 #include "llvm/IR/LLVMContext.h"
     21 #include "llvm/Support/ErrorHandling.h"
     22 #include "llvm/Support/MathExtras.h"
     23 #include "llvm/Support/MemoryBuffer.h"
     24 #include "llvm/Support/SourceMgr.h"
     25 #include "llvm/Support/raw_ostream.h"
     26 #include <cctype>
     27 #include <cstdio>
     28 #include <cstdlib>
     29 #include <cstring>
     30 using namespace llvm;
     31 
     32 bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
     33   ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
     34   return true;
     35 }
     36 
     37 //===----------------------------------------------------------------------===//
     38 // Helper functions.
     39 //===----------------------------------------------------------------------===//
     40 
     41 // atoull - Convert an ascii string of decimal digits into the unsigned long
     42 // long representation... this does not have to do input error checking,
     43 // because we know that the input will be matched by a suitable regex...
     44 //
     45 uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
     46   uint64_t Result = 0;
     47   for (; Buffer != End; Buffer++) {
     48     uint64_t OldRes = Result;
     49     Result *= 10;
     50     Result += *Buffer-'0';
     51     if (Result < OldRes) {  // Uh, oh, overflow detected!!!
     52       Error("constant bigger than 64 bits detected!");
     53       return 0;
     54     }
     55   }
     56   return Result;
     57 }
     58 
     59 uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
     60   uint64_t Result = 0;
     61   for (; Buffer != End; ++Buffer) {
     62     uint64_t OldRes = Result;
     63     Result *= 16;
     64     Result += hexDigitValue(*Buffer);
     65 
     66     if (Result < OldRes) {   // Uh, oh, overflow detected!!!
     67       Error("constant bigger than 64 bits detected!");
     68       return 0;
     69     }
     70   }
     71   return Result;
     72 }
     73 
     74 void LLLexer::HexToIntPair(const char *Buffer, const char *End,
     75                            uint64_t Pair[2]) {
     76   Pair[0] = 0;
     77   for (int i=0; i<16; i++, Buffer++) {
     78     assert(Buffer != End);
     79     Pair[0] *= 16;
     80     Pair[0] += hexDigitValue(*Buffer);
     81   }
     82   Pair[1] = 0;
     83   for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
     84     Pair[1] *= 16;
     85     Pair[1] += hexDigitValue(*Buffer);
     86   }
     87   if (Buffer != End)
     88     Error("constant bigger than 128 bits detected!");
     89 }
     90 
     91 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
     92 /// { low64, high16 } as usual for an APInt.
     93 void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
     94                            uint64_t Pair[2]) {
     95   Pair[1] = 0;
     96   for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
     97     assert(Buffer != End);
     98     Pair[1] *= 16;
     99     Pair[1] += hexDigitValue(*Buffer);
    100   }
    101   Pair[0] = 0;
    102   for (int i=0; i<16; i++, Buffer++) {
    103     Pair[0] *= 16;
    104     Pair[0] += hexDigitValue(*Buffer);
    105   }
    106   if (Buffer != End)
    107     Error("constant bigger than 128 bits detected!");
    108 }
    109 
    110 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
    111 // appropriate character.
    112 static void UnEscapeLexed(std::string &Str) {
    113   if (Str.empty()) return;
    114 
    115   char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
    116   char *BOut = Buffer;
    117   for (char *BIn = Buffer; BIn != EndBuffer; ) {
    118     if (BIn[0] == '\\') {
    119       if (BIn < EndBuffer-1 && BIn[1] == '\\') {
    120         *BOut++ = '\\'; // Two \ becomes one
    121         BIn += 2;
    122       } else if (BIn < EndBuffer-2 &&
    123                  isxdigit(static_cast<unsigned char>(BIn[1])) &&
    124                  isxdigit(static_cast<unsigned char>(BIn[2]))) {
    125         *BOut = hexDigitValue(BIn[1]) * 16 + hexDigitValue(BIn[2]);
    126         BIn += 3;                           // Skip over handled chars
    127         ++BOut;
    128       } else {
    129         *BOut++ = *BIn++;
    130       }
    131     } else {
    132       *BOut++ = *BIn++;
    133     }
    134   }
    135   Str.resize(BOut-Buffer);
    136 }
    137 
    138 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
    139 static bool isLabelChar(char C) {
    140   return isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
    141          C == '.' || C == '_';
    142 }
    143 
    144 
    145 /// isLabelTail - Return true if this pointer points to a valid end of a label.
    146 static const char *isLabelTail(const char *CurPtr) {
    147   while (1) {
    148     if (CurPtr[0] == ':') return CurPtr+1;
    149     if (!isLabelChar(CurPtr[0])) return 0;
    150     ++CurPtr;
    151   }
    152 }
    153 
    154 
    155 
    156 //===----------------------------------------------------------------------===//
    157 // Lexer definition.
    158 //===----------------------------------------------------------------------===//
    159 
    160 LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err,
    161                  LLVMContext &C)
    162   : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
    163   CurPtr = CurBuf->getBufferStart();
    164 }
    165 
    166 std::string LLLexer::getFilename() const {
    167   return CurBuf->getBufferIdentifier();
    168 }
    169 
    170 int LLLexer::getNextChar() {
    171   char CurChar = *CurPtr++;
    172   switch (CurChar) {
    173   default: return (unsigned char)CurChar;
    174   case 0:
    175     // A nul character in the stream is either the end of the current buffer or
    176     // a random nul in the file.  Disambiguate that here.
    177     if (CurPtr-1 != CurBuf->getBufferEnd())
    178       return 0;  // Just whitespace.
    179 
    180     // Otherwise, return end of file.
    181     --CurPtr;  // Another call to lex will return EOF again.
    182     return EOF;
    183   }
    184 }
    185 
    186 
    187 lltok::Kind LLLexer::LexToken() {
    188   TokStart = CurPtr;
    189 
    190   int CurChar = getNextChar();
    191   switch (CurChar) {
    192   default:
    193     // Handle letters: [a-zA-Z_]
    194     if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
    195       return LexIdentifier();
    196 
    197     return lltok::Error;
    198   case EOF: return lltok::Eof;
    199   case 0:
    200   case ' ':
    201   case '\t':
    202   case '\n':
    203   case '\r':
    204     // Ignore whitespace.
    205     return LexToken();
    206   case '+': return LexPositive();
    207   case '@': return LexAt();
    208   case '%': return LexPercent();
    209   case '"': return LexQuote();
    210   case '.':
    211     if (const char *Ptr = isLabelTail(CurPtr)) {
    212       CurPtr = Ptr;
    213       StrVal.assign(TokStart, CurPtr-1);
    214       return lltok::LabelStr;
    215     }
    216     if (CurPtr[0] == '.' && CurPtr[1] == '.') {
    217       CurPtr += 2;
    218       return lltok::dotdotdot;
    219     }
    220     return lltok::Error;
    221   case '$':
    222     if (const char *Ptr = isLabelTail(CurPtr)) {
    223       CurPtr = Ptr;
    224       StrVal.assign(TokStart, CurPtr-1);
    225       return lltok::LabelStr;
    226     }
    227     return lltok::Error;
    228   case ';':
    229     SkipLineComment();
    230     return LexToken();
    231   case '!': return LexExclaim();
    232   case '#': return LexHash();
    233   case '0': case '1': case '2': case '3': case '4':
    234   case '5': case '6': case '7': case '8': case '9':
    235   case '-':
    236     return LexDigitOrNegative();
    237   case '=': return lltok::equal;
    238   case '[': return lltok::lsquare;
    239   case ']': return lltok::rsquare;
    240   case '{': return lltok::lbrace;
    241   case '}': return lltok::rbrace;
    242   case '<': return lltok::less;
    243   case '>': return lltok::greater;
    244   case '(': return lltok::lparen;
    245   case ')': return lltok::rparen;
    246   case ',': return lltok::comma;
    247   case '*': return lltok::star;
    248   case '\\': return lltok::backslash;
    249   }
    250 }
    251 
    252 void LLLexer::SkipLineComment() {
    253   while (1) {
    254     if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
    255       return;
    256   }
    257 }
    258 
    259 /// LexAt - Lex all tokens that start with an @ character:
    260 ///   GlobalVar   @\"[^\"]*\"
    261 ///   GlobalVar   @[-a-zA-Z$._][-a-zA-Z$._0-9]*
    262 ///   GlobalVarID @[0-9]+
    263 lltok::Kind LLLexer::LexAt() {
    264   // Handle AtStringConstant: @\"[^\"]*\"
    265   if (CurPtr[0] == '"') {
    266     ++CurPtr;
    267 
    268     while (1) {
    269       int CurChar = getNextChar();
    270 
    271       if (CurChar == EOF) {
    272         Error("end of file in global variable name");
    273         return lltok::Error;
    274       }
    275       if (CurChar == '"') {
    276         StrVal.assign(TokStart+2, CurPtr-1);
    277         UnEscapeLexed(StrVal);
    278         return lltok::GlobalVar;
    279       }
    280     }
    281   }
    282 
    283   // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
    284   if (ReadVarName())
    285     return lltok::GlobalVar;
    286 
    287   // Handle GlobalVarID: @[0-9]+
    288   if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
    289     for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
    290       /*empty*/;
    291 
    292     uint64_t Val = atoull(TokStart+1, CurPtr);
    293     if ((unsigned)Val != Val)
    294       Error("invalid value number (too large)!");
    295     UIntVal = unsigned(Val);
    296     return lltok::GlobalID;
    297   }
    298 
    299   return lltok::Error;
    300 }
    301 
    302 /// ReadString - Read a string until the closing quote.
    303 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
    304   const char *Start = CurPtr;
    305   while (1) {
    306     int CurChar = getNextChar();
    307 
    308     if (CurChar == EOF) {
    309       Error("end of file in string constant");
    310       return lltok::Error;
    311     }
    312     if (CurChar == '"') {
    313       StrVal.assign(Start, CurPtr-1);
    314       UnEscapeLexed(StrVal);
    315       return kind;
    316     }
    317   }
    318 }
    319 
    320 /// ReadVarName - Read the rest of a token containing a variable name.
    321 bool LLLexer::ReadVarName() {
    322   const char *NameStart = CurPtr;
    323   if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
    324       CurPtr[0] == '-' || CurPtr[0] == '$' ||
    325       CurPtr[0] == '.' || CurPtr[0] == '_') {
    326     ++CurPtr;
    327     while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
    328            CurPtr[0] == '-' || CurPtr[0] == '$' ||
    329            CurPtr[0] == '.' || CurPtr[0] == '_')
    330       ++CurPtr;
    331 
    332     StrVal.assign(NameStart, CurPtr);
    333     return true;
    334   }
    335   return false;
    336 }
    337 
    338 /// LexPercent - Lex all tokens that start with a % character:
    339 ///   LocalVar   ::= %\"[^\"]*\"
    340 ///   LocalVar   ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
    341 ///   LocalVarID ::= %[0-9]+
    342 lltok::Kind LLLexer::LexPercent() {
    343   // Handle LocalVarName: %\"[^\"]*\"
    344   if (CurPtr[0] == '"') {
    345     ++CurPtr;
    346     return ReadString(lltok::LocalVar);
    347   }
    348 
    349   // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
    350   if (ReadVarName())
    351     return lltok::LocalVar;
    352 
    353   // Handle LocalVarID: %[0-9]+
    354   if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
    355     for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
    356       /*empty*/;
    357 
    358     uint64_t Val = atoull(TokStart+1, CurPtr);
    359     if ((unsigned)Val != Val)
    360       Error("invalid value number (too large)!");
    361     UIntVal = unsigned(Val);
    362     return lltok::LocalVarID;
    363   }
    364 
    365   return lltok::Error;
    366 }
    367 
    368 /// LexQuote - Lex all tokens that start with a " character:
    369 ///   QuoteLabel        "[^"]+":
    370 ///   StringConstant    "[^"]*"
    371 lltok::Kind LLLexer::LexQuote() {
    372   lltok::Kind kind = ReadString(lltok::StringConstant);
    373   if (kind == lltok::Error || kind == lltok::Eof)
    374     return kind;
    375 
    376   if (CurPtr[0] == ':') {
    377     ++CurPtr;
    378     kind = lltok::LabelStr;
    379   }
    380 
    381   return kind;
    382 }
    383 
    384 /// LexExclaim:
    385 ///    !foo
    386 ///    !
    387 lltok::Kind LLLexer::LexExclaim() {
    388   // Lex a metadata name as a MetadataVar.
    389   if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
    390       CurPtr[0] == '-' || CurPtr[0] == '$' ||
    391       CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
    392     ++CurPtr;
    393     while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
    394            CurPtr[0] == '-' || CurPtr[0] == '$' ||
    395            CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
    396       ++CurPtr;
    397 
    398     StrVal.assign(TokStart+1, CurPtr);   // Skip !
    399     UnEscapeLexed(StrVal);
    400     return lltok::MetadataVar;
    401   }
    402   return lltok::exclaim;
    403 }
    404 
    405 /// LexHash - Lex all tokens that start with a # character:
    406 ///    AttrGrpID ::= #[0-9]+
    407 lltok::Kind LLLexer::LexHash() {
    408   // Handle AttrGrpID: #[0-9]+
    409   if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
    410     for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
    411       /*empty*/;
    412 
    413     uint64_t Val = atoull(TokStart+1, CurPtr);
    414     if ((unsigned)Val != Val)
    415       Error("invalid value number (too large)!");
    416     UIntVal = unsigned(Val);
    417     return lltok::AttrGrpID;
    418   }
    419 
    420   return lltok::Error;
    421 }
    422 
    423 /// LexIdentifier: Handle several related productions:
    424 ///    Label           [-a-zA-Z$._0-9]+:
    425 ///    IntegerType     i[0-9]+
    426 ///    Keyword         sdiv, float, ...
    427 ///    HexIntConstant  [us]0x[0-9A-Fa-f]+
    428 lltok::Kind LLLexer::LexIdentifier() {
    429   const char *StartChar = CurPtr;
    430   const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
    431   const char *KeywordEnd = 0;
    432 
    433   for (; isLabelChar(*CurPtr); ++CurPtr) {
    434     // If we decide this is an integer, remember the end of the sequence.
    435     if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
    436       IntEnd = CurPtr;
    437     if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
    438         *CurPtr != '_')
    439       KeywordEnd = CurPtr;
    440   }
    441 
    442   // If we stopped due to a colon, this really is a label.
    443   if (*CurPtr == ':') {
    444     StrVal.assign(StartChar-1, CurPtr++);
    445     return lltok::LabelStr;
    446   }
    447 
    448   // Otherwise, this wasn't a label.  If this was valid as an integer type,
    449   // return it.
    450   if (IntEnd == 0) IntEnd = CurPtr;
    451   if (IntEnd != StartChar) {
    452     CurPtr = IntEnd;
    453     uint64_t NumBits = atoull(StartChar, CurPtr);
    454     if (NumBits < IntegerType::MIN_INT_BITS ||
    455         NumBits > IntegerType::MAX_INT_BITS) {
    456       Error("bitwidth for integer type out of range!");
    457       return lltok::Error;
    458     }
    459     TyVal = IntegerType::get(Context, NumBits);
    460     return lltok::Type;
    461   }
    462 
    463   // Otherwise, this was a letter sequence.  See which keyword this is.
    464   if (KeywordEnd == 0) KeywordEnd = CurPtr;
    465   CurPtr = KeywordEnd;
    466   --StartChar;
    467   unsigned Len = CurPtr-StartChar;
    468 #define KEYWORD(STR)                                                    \
    469   do {                                                                  \
    470     if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR)))  \
    471       return lltok::kw_##STR;                                           \
    472   } while (0)
    473 
    474   KEYWORD(true);    KEYWORD(false);
    475   KEYWORD(declare); KEYWORD(define);
    476   KEYWORD(global);  KEYWORD(constant);
    477 
    478   KEYWORD(private);
    479   KEYWORD(linker_private);
    480   KEYWORD(linker_private_weak);
    481   KEYWORD(linker_private_weak_def_auto); // FIXME: For backwards compatibility.
    482   KEYWORD(internal);
    483   KEYWORD(available_externally);
    484   KEYWORD(linkonce);
    485   KEYWORD(linkonce_odr);
    486   KEYWORD(linkonce_odr_auto_hide);
    487   KEYWORD(weak);
    488   KEYWORD(weak_odr);
    489   KEYWORD(appending);
    490   KEYWORD(dllimport);
    491   KEYWORD(dllexport);
    492   KEYWORD(common);
    493   KEYWORD(default);
    494   KEYWORD(hidden);
    495   KEYWORD(protected);
    496   KEYWORD(unnamed_addr);
    497   KEYWORD(externally_initialized);
    498   KEYWORD(extern_weak);
    499   KEYWORD(external);
    500   KEYWORD(thread_local);
    501   KEYWORD(localdynamic);
    502   KEYWORD(initialexec);
    503   KEYWORD(localexec);
    504   KEYWORD(zeroinitializer);
    505   KEYWORD(undef);
    506   KEYWORD(null);
    507   KEYWORD(to);
    508   KEYWORD(tail);
    509   KEYWORD(target);
    510   KEYWORD(triple);
    511   KEYWORD(unwind);
    512   KEYWORD(deplibs);             // FIXME: Remove in 4.0.
    513   KEYWORD(datalayout);
    514   KEYWORD(volatile);
    515   KEYWORD(atomic);
    516   KEYWORD(unordered);
    517   KEYWORD(monotonic);
    518   KEYWORD(acquire);
    519   KEYWORD(release);
    520   KEYWORD(acq_rel);
    521   KEYWORD(seq_cst);
    522   KEYWORD(singlethread);
    523 
    524   KEYWORD(nnan);
    525   KEYWORD(ninf);
    526   KEYWORD(nsz);
    527   KEYWORD(arcp);
    528   KEYWORD(fast);
    529   KEYWORD(nuw);
    530   KEYWORD(nsw);
    531   KEYWORD(exact);
    532   KEYWORD(inbounds);
    533   KEYWORD(align);
    534   KEYWORD(addrspace);
    535   KEYWORD(section);
    536   KEYWORD(alias);
    537   KEYWORD(module);
    538   KEYWORD(asm);
    539   KEYWORD(sideeffect);
    540   KEYWORD(alignstack);
    541   KEYWORD(inteldialect);
    542   KEYWORD(gc);
    543 
    544   KEYWORD(ccc);
    545   KEYWORD(fastcc);
    546   KEYWORD(coldcc);
    547   KEYWORD(x86_stdcallcc);
    548   KEYWORD(x86_fastcallcc);
    549   KEYWORD(x86_thiscallcc);
    550   KEYWORD(arm_apcscc);
    551   KEYWORD(arm_aapcscc);
    552   KEYWORD(arm_aapcs_vfpcc);
    553   KEYWORD(msp430_intrcc);
    554   KEYWORD(ptx_kernel);
    555   KEYWORD(ptx_device);
    556   KEYWORD(spir_kernel);
    557   KEYWORD(spir_func);
    558   KEYWORD(intel_ocl_bicc);
    559   KEYWORD(x86_64_sysvcc);
    560   KEYWORD(x86_64_win64cc);
    561 
    562   KEYWORD(cc);
    563   KEYWORD(c);
    564 
    565   KEYWORD(attributes);
    566 
    567   KEYWORD(alwaysinline);
    568   KEYWORD(builtin);
    569   KEYWORD(byval);
    570   KEYWORD(cold);
    571   KEYWORD(inlinehint);
    572   KEYWORD(inreg);
    573   KEYWORD(minsize);
    574   KEYWORD(naked);
    575   KEYWORD(nest);
    576   KEYWORD(noalias);
    577   KEYWORD(nobuiltin);
    578   KEYWORD(nocapture);
    579   KEYWORD(noduplicate);
    580   KEYWORD(noimplicitfloat);
    581   KEYWORD(noinline);
    582   KEYWORD(nonlazybind);
    583   KEYWORD(noredzone);
    584   KEYWORD(noreturn);
    585   KEYWORD(nounwind);
    586   KEYWORD(optsize);
    587   KEYWORD(readnone);
    588   KEYWORD(readonly);
    589   KEYWORD(returned);
    590   KEYWORD(returns_twice);
    591   KEYWORD(signext);
    592   KEYWORD(sret);
    593   KEYWORD(ssp);
    594   KEYWORD(sspreq);
    595   KEYWORD(sspstrong);
    596   KEYWORD(sanitize_address);
    597   KEYWORD(sanitize_thread);
    598   KEYWORD(sanitize_memory);
    599   KEYWORD(uwtable);
    600   KEYWORD(zeroext);
    601 
    602   KEYWORD(type);
    603   KEYWORD(opaque);
    604 
    605   KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
    606   KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
    607   KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
    608   KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
    609 
    610   KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
    611   KEYWORD(umin);
    612 
    613   KEYWORD(x);
    614   KEYWORD(blockaddress);
    615 
    616   KEYWORD(personality);
    617   KEYWORD(cleanup);
    618   KEYWORD(catch);
    619   KEYWORD(filter);
    620 #undef KEYWORD
    621 
    622   // Keywords for types.
    623 #define TYPEKEYWORD(STR, LLVMTY) \
    624   if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
    625     TyVal = LLVMTY; return lltok::Type; }
    626   TYPEKEYWORD("void",      Type::getVoidTy(Context));
    627   TYPEKEYWORD("half",      Type::getHalfTy(Context));
    628   TYPEKEYWORD("float",     Type::getFloatTy(Context));
    629   TYPEKEYWORD("double",    Type::getDoubleTy(Context));
    630   TYPEKEYWORD("x86_fp80",  Type::getX86_FP80Ty(Context));
    631   TYPEKEYWORD("fp128",     Type::getFP128Ty(Context));
    632   TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
    633   TYPEKEYWORD("label",     Type::getLabelTy(Context));
    634   TYPEKEYWORD("metadata",  Type::getMetadataTy(Context));
    635   TYPEKEYWORD("x86_mmx",   Type::getX86_MMXTy(Context));
    636 #undef TYPEKEYWORD
    637 
    638   // Keywords for instructions.
    639 #define INSTKEYWORD(STR, Enum) \
    640   if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
    641     UIntVal = Instruction::Enum; return lltok::kw_##STR; }
    642 
    643   INSTKEYWORD(add,   Add);  INSTKEYWORD(fadd,   FAdd);
    644   INSTKEYWORD(sub,   Sub);  INSTKEYWORD(fsub,   FSub);
    645   INSTKEYWORD(mul,   Mul);  INSTKEYWORD(fmul,   FMul);
    646   INSTKEYWORD(udiv,  UDiv); INSTKEYWORD(sdiv,  SDiv); INSTKEYWORD(fdiv,  FDiv);
    647   INSTKEYWORD(urem,  URem); INSTKEYWORD(srem,  SRem); INSTKEYWORD(frem,  FRem);
    648   INSTKEYWORD(shl,   Shl);  INSTKEYWORD(lshr,  LShr); INSTKEYWORD(ashr,  AShr);
    649   INSTKEYWORD(and,   And);  INSTKEYWORD(or,    Or);   INSTKEYWORD(xor,   Xor);
    650   INSTKEYWORD(icmp,  ICmp); INSTKEYWORD(fcmp,  FCmp);
    651 
    652   INSTKEYWORD(phi,         PHI);
    653   INSTKEYWORD(call,        Call);
    654   INSTKEYWORD(trunc,       Trunc);
    655   INSTKEYWORD(zext,        ZExt);
    656   INSTKEYWORD(sext,        SExt);
    657   INSTKEYWORD(fptrunc,     FPTrunc);
    658   INSTKEYWORD(fpext,       FPExt);
    659   INSTKEYWORD(uitofp,      UIToFP);
    660   INSTKEYWORD(sitofp,      SIToFP);
    661   INSTKEYWORD(fptoui,      FPToUI);
    662   INSTKEYWORD(fptosi,      FPToSI);
    663   INSTKEYWORD(inttoptr,    IntToPtr);
    664   INSTKEYWORD(ptrtoint,    PtrToInt);
    665   INSTKEYWORD(bitcast,     BitCast);
    666   INSTKEYWORD(select,      Select);
    667   INSTKEYWORD(va_arg,      VAArg);
    668   INSTKEYWORD(ret,         Ret);
    669   INSTKEYWORD(br,          Br);
    670   INSTKEYWORD(switch,      Switch);
    671   INSTKEYWORD(indirectbr,  IndirectBr);
    672   INSTKEYWORD(invoke,      Invoke);
    673   INSTKEYWORD(resume,      Resume);
    674   INSTKEYWORD(unreachable, Unreachable);
    675 
    676   INSTKEYWORD(alloca,      Alloca);
    677   INSTKEYWORD(load,        Load);
    678   INSTKEYWORD(store,       Store);
    679   INSTKEYWORD(cmpxchg,     AtomicCmpXchg);
    680   INSTKEYWORD(atomicrmw,   AtomicRMW);
    681   INSTKEYWORD(fence,       Fence);
    682   INSTKEYWORD(getelementptr, GetElementPtr);
    683 
    684   INSTKEYWORD(extractelement, ExtractElement);
    685   INSTKEYWORD(insertelement,  InsertElement);
    686   INSTKEYWORD(shufflevector,  ShuffleVector);
    687   INSTKEYWORD(extractvalue,   ExtractValue);
    688   INSTKEYWORD(insertvalue,    InsertValue);
    689   INSTKEYWORD(landingpad,     LandingPad);
    690 #undef INSTKEYWORD
    691 
    692   // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
    693   // the CFE to avoid forcing it to deal with 64-bit numbers.
    694   if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
    695       TokStart[1] == '0' && TokStart[2] == 'x' &&
    696       isxdigit(static_cast<unsigned char>(TokStart[3]))) {
    697     int len = CurPtr-TokStart-3;
    698     uint32_t bits = len * 4;
    699     APInt Tmp(bits, StringRef(TokStart+3, len), 16);
    700     uint32_t activeBits = Tmp.getActiveBits();
    701     if (activeBits > 0 && activeBits < bits)
    702       Tmp = Tmp.trunc(activeBits);
    703     APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
    704     return lltok::APSInt;
    705   }
    706 
    707   // If this is "cc1234", return this as just "cc".
    708   if (TokStart[0] == 'c' && TokStart[1] == 'c') {
    709     CurPtr = TokStart+2;
    710     return lltok::kw_cc;
    711   }
    712 
    713   // Finally, if this isn't known, return an error.
    714   CurPtr = TokStart+1;
    715   return lltok::Error;
    716 }
    717 
    718 
    719 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
    720 /// that this is not a label:
    721 ///    HexFPConstant     0x[0-9A-Fa-f]+
    722 ///    HexFP80Constant   0xK[0-9A-Fa-f]+
    723 ///    HexFP128Constant  0xL[0-9A-Fa-f]+
    724 ///    HexPPC128Constant 0xM[0-9A-Fa-f]+
    725 ///    HexHalfConstant   0xH[0-9A-Fa-f]+
    726 lltok::Kind LLLexer::Lex0x() {
    727   CurPtr = TokStart + 2;
    728 
    729   char Kind;
    730   if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
    731     Kind = *CurPtr++;
    732   } else {
    733     Kind = 'J';
    734   }
    735 
    736   if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
    737     // Bad token, return it as an error.
    738     CurPtr = TokStart+1;
    739     return lltok::Error;
    740   }
    741 
    742   while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
    743     ++CurPtr;
    744 
    745   if (Kind == 'J') {
    746     // HexFPConstant - Floating point constant represented in IEEE format as a
    747     // hexadecimal number for when exponential notation is not precise enough.
    748     // Half, Float, and double only.
    749     APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
    750     return lltok::APFloat;
    751   }
    752 
    753   uint64_t Pair[2];
    754   switch (Kind) {
    755   default: llvm_unreachable("Unknown kind!");
    756   case 'K':
    757     // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
    758     FP80HexToIntPair(TokStart+3, CurPtr, Pair);
    759     APFloatVal = APFloat(APFloat::x87DoubleExtended, APInt(80, Pair));
    760     return lltok::APFloat;
    761   case 'L':
    762     // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
    763     HexToIntPair(TokStart+3, CurPtr, Pair);
    764     APFloatVal = APFloat(APFloat::IEEEquad, APInt(128, Pair));
    765     return lltok::APFloat;
    766   case 'M':
    767     // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
    768     HexToIntPair(TokStart+3, CurPtr, Pair);
    769     APFloatVal = APFloat(APFloat::PPCDoubleDouble, APInt(128, Pair));
    770     return lltok::APFloat;
    771   case 'H':
    772     APFloatVal = APFloat(APFloat::IEEEhalf,
    773                          APInt(16,HexIntToVal(TokStart+3, CurPtr)));
    774     return lltok::APFloat;
    775   }
    776 }
    777 
    778 /// LexIdentifier: Handle several related productions:
    779 ///    Label             [-a-zA-Z$._0-9]+:
    780 ///    NInteger          -[0-9]+
    781 ///    FPConstant        [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
    782 ///    PInteger          [0-9]+
    783 ///    HexFPConstant     0x[0-9A-Fa-f]+
    784 ///    HexFP80Constant   0xK[0-9A-Fa-f]+
    785 ///    HexFP128Constant  0xL[0-9A-Fa-f]+
    786 ///    HexPPC128Constant 0xM[0-9A-Fa-f]+
    787 lltok::Kind LLLexer::LexDigitOrNegative() {
    788   // If the letter after the negative is not a number, this is probably a label.
    789   if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
    790       !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
    791     // Okay, this is not a number after the -, it's probably a label.
    792     if (const char *End = isLabelTail(CurPtr)) {
    793       StrVal.assign(TokStart, End-1);
    794       CurPtr = End;
    795       return lltok::LabelStr;
    796     }
    797 
    798     return lltok::Error;
    799   }
    800 
    801   // At this point, it is either a label, int or fp constant.
    802 
    803   // Skip digits, we have at least one.
    804   for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
    805     /*empty*/;
    806 
    807   // Check to see if this really is a label afterall, e.g. "-1:".
    808   if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
    809     if (const char *End = isLabelTail(CurPtr)) {
    810       StrVal.assign(TokStart, End-1);
    811       CurPtr = End;
    812       return lltok::LabelStr;
    813     }
    814   }
    815 
    816   // If the next character is a '.', then it is a fp value, otherwise its
    817   // integer.
    818   if (CurPtr[0] != '.') {
    819     if (TokStart[0] == '0' && TokStart[1] == 'x')
    820       return Lex0x();
    821     unsigned Len = CurPtr-TokStart;
    822     uint32_t numBits = ((Len * 64) / 19) + 2;
    823     APInt Tmp(numBits, StringRef(TokStart, Len), 10);
    824     if (TokStart[0] == '-') {
    825       uint32_t minBits = Tmp.getMinSignedBits();
    826       if (minBits > 0 && minBits < numBits)
    827         Tmp = Tmp.trunc(minBits);
    828       APSIntVal = APSInt(Tmp, false);
    829     } else {
    830       uint32_t activeBits = Tmp.getActiveBits();
    831       if (activeBits > 0 && activeBits < numBits)
    832         Tmp = Tmp.trunc(activeBits);
    833       APSIntVal = APSInt(Tmp, true);
    834     }
    835     return lltok::APSInt;
    836   }
    837 
    838   ++CurPtr;
    839 
    840   // Skip over [0-9]*([eE][-+]?[0-9]+)?
    841   while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
    842 
    843   if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
    844     if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
    845         ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
    846           isdigit(static_cast<unsigned char>(CurPtr[2])))) {
    847       CurPtr += 2;
    848       while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
    849     }
    850   }
    851 
    852   APFloatVal = APFloat(std::atof(TokStart));
    853   return lltok::APFloat;
    854 }
    855 
    856 ///    FPConstant  [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
    857 lltok::Kind LLLexer::LexPositive() {
    858   // If the letter after the negative is a number, this is probably not a
    859   // label.
    860   if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
    861     return lltok::Error;
    862 
    863   // Skip digits.
    864   for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
    865     /*empty*/;
    866 
    867   // At this point, we need a '.'.
    868   if (CurPtr[0] != '.') {
    869     CurPtr = TokStart+1;
    870     return lltok::Error;
    871   }
    872 
    873   ++CurPtr;
    874 
    875   // Skip over [0-9]*([eE][-+]?[0-9]+)?
    876   while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
    877 
    878   if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
    879     if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
    880         ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
    881         isdigit(static_cast<unsigned char>(CurPtr[2])))) {
    882       CurPtr += 2;
    883       while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
    884     }
    885   }
    886 
    887   APFloatVal = APFloat(std::atof(TokStart));
    888   return lltok::APFloat;
    889 }
    890