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