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