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