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