blob: 10e5a60ec9e2e2409035b1b5d1781db9b1e02fb8 [file] [log] [blame]
Chris Lattner8e3a8e02007-11-18 08:46:26 +00001//===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner8e3a8e02007-11-18 08:46:26 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Implement the Lexer for .ll files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "LLLexer.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000015#include "llvm/DerivedTypes.h"
16#include "llvm/Instruction.h"
Owen Andersonff6c91e2009-07-07 18:44:11 +000017#include "llvm/LLVMContext.h"
Chris Lattner8e3a8e02007-11-18 08:46:26 +000018#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerd185f642007-12-08 19:03:30 +000019#include "llvm/Support/MathExtras.h"
Chris Lattner92bcb422009-07-02 22:46:18 +000020#include "llvm/Support/SourceMgr.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000021#include "llvm/Support/raw_ostream.h"
22#include "llvm/Assembly/Parser.h"
Chris Lattnera8961762009-01-02 07:18:46 +000023#include <cstdlib>
Misha Brukman5679d182009-01-02 22:49:28 +000024#include <cstring>
Chris Lattner8e3a8e02007-11-18 08:46:26 +000025using namespace llvm;
26
Chris Lattnerdf986172009-01-02 07:01:27 +000027bool LLLexer::Error(LocTy ErrorLoc, const std::string &Msg) const {
Chris Lattnereeb4a842009-07-02 23:08:13 +000028 ErrorInfo = SM.GetMessage(ErrorLoc, Msg, "error");
Chris Lattnerdf986172009-01-02 07:01:27 +000029 return true;
30}
31
Chris Lattner8e3a8e02007-11-18 08:46:26 +000032//===----------------------------------------------------------------------===//
33// Helper functions.
34//===----------------------------------------------------------------------===//
35
36// atoull - Convert an ascii string of decimal digits into the unsigned long
37// long representation... this does not have to do input error checking,
38// because we know that the input will be matched by a suitable regex...
39//
Chris Lattnerdf986172009-01-02 07:01:27 +000040uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000041 uint64_t Result = 0;
42 for (; Buffer != End; Buffer++) {
43 uint64_t OldRes = Result;
44 Result *= 10;
45 Result += *Buffer-'0';
46 if (Result < OldRes) { // Uh, oh, overflow detected!!!
Chris Lattnerdf986172009-01-02 07:01:27 +000047 Error("constant bigger than 64 bits detected!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +000048 return 0;
49 }
50 }
51 return Result;
52}
53
Chris Lattnerdf986172009-01-02 07:01:27 +000054uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000055 uint64_t Result = 0;
56 for (; Buffer != End; ++Buffer) {
57 uint64_t OldRes = Result;
58 Result *= 16;
59 char C = *Buffer;
60 if (C >= '0' && C <= '9')
61 Result += C-'0';
62 else if (C >= 'A' && C <= 'F')
63 Result += C-'A'+10;
64 else if (C >= 'a' && C <= 'f')
65 Result += C-'a'+10;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +000066
Chris Lattner8e3a8e02007-11-18 08:46:26 +000067 if (Result < OldRes) { // Uh, oh, overflow detected!!!
Chris Lattnerdf986172009-01-02 07:01:27 +000068 Error("constant bigger than 64 bits detected!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +000069 return 0;
70 }
71 }
72 return Result;
73}
74
Chris Lattnerdf986172009-01-02 07:01:27 +000075void LLLexer::HexToIntPair(const char *Buffer, const char *End,
76 uint64_t Pair[2]) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000077 Pair[0] = 0;
78 for (int i=0; i<16; i++, Buffer++) {
79 assert(Buffer != End);
80 Pair[0] *= 16;
81 char C = *Buffer;
82 if (C >= '0' && C <= '9')
83 Pair[0] += C-'0';
84 else if (C >= 'A' && C <= 'F')
85 Pair[0] += C-'A'+10;
86 else if (C >= 'a' && C <= 'f')
87 Pair[0] += C-'a'+10;
88 }
89 Pair[1] = 0;
90 for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
91 Pair[1] *= 16;
92 char C = *Buffer;
93 if (C >= '0' && C <= '9')
94 Pair[1] += C-'0';
95 else if (C >= 'A' && C <= 'F')
96 Pair[1] += C-'A'+10;
97 else if (C >= 'a' && C <= 'f')
98 Pair[1] += C-'a'+10;
99 }
Chris Lattnerd343c6b2007-11-18 18:25:18 +0000100 if (Buffer != End)
Chris Lattnerdf986172009-01-02 07:01:27 +0000101 Error("constant bigger than 128 bits detected!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000102}
103
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000104/// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
105/// { low64, high16 } as usual for an APInt.
106void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
107 uint64_t Pair[2]) {
108 Pair[1] = 0;
109 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
110 assert(Buffer != End);
111 Pair[1] *= 16;
112 char C = *Buffer;
113 if (C >= '0' && C <= '9')
114 Pair[1] += C-'0';
115 else if (C >= 'A' && C <= 'F')
116 Pair[1] += C-'A'+10;
117 else if (C >= 'a' && C <= 'f')
118 Pair[1] += C-'a'+10;
119 }
120 Pair[0] = 0;
121 for (int i=0; i<16; i++, Buffer++) {
122 Pair[0] *= 16;
123 char C = *Buffer;
124 if (C >= '0' && C <= '9')
125 Pair[0] += C-'0';
126 else if (C >= 'A' && C <= 'F')
127 Pair[0] += C-'A'+10;
128 else if (C >= 'a' && C <= 'f')
129 Pair[0] += C-'a'+10;
130 }
131 if (Buffer != End)
132 Error("constant bigger than 128 bits detected!");
133}
134
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000135// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
136// appropriate character.
137static void UnEscapeLexed(std::string &Str) {
138 if (Str.empty()) return;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000139
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000140 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
141 char *BOut = Buffer;
142 for (char *BIn = Buffer; BIn != EndBuffer; ) {
143 if (BIn[0] == '\\') {
144 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
145 *BOut++ = '\\'; // Two \ becomes one
146 BIn += 2;
147 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
148 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
149 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
150 BIn[3] = Tmp; // Restore character
151 BIn += 3; // Skip over handled chars
152 ++BOut;
153 } else {
154 *BOut++ = *BIn++;
155 }
156 } else {
157 *BOut++ = *BIn++;
158 }
159 }
160 Str.resize(BOut-Buffer);
161}
162
163/// isLabelChar - Return true for [-a-zA-Z$._0-9].
164static bool isLabelChar(char C) {
165 return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
166}
167
168
169/// isLabelTail - Return true if this pointer points to a valid end of a label.
170static const char *isLabelTail(const char *CurPtr) {
171 while (1) {
172 if (CurPtr[0] == ':') return CurPtr+1;
173 if (!isLabelChar(CurPtr[0])) return 0;
174 ++CurPtr;
175 }
176}
177
178
179
180//===----------------------------------------------------------------------===//
181// Lexer definition.
182//===----------------------------------------------------------------------===//
183
Owen Andersonff6c91e2009-07-07 18:44:11 +0000184LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err,
185 LLVMContext &C)
186 : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000187 CurPtr = CurBuf->getBufferStart();
188}
189
190std::string LLLexer::getFilename() const {
191 return CurBuf->getBufferIdentifier();
192}
193
194int LLLexer::getNextChar() {
195 char CurChar = *CurPtr++;
196 switch (CurChar) {
197 default: return (unsigned char)CurChar;
198 case 0:
199 // A nul character in the stream is either the end of the current buffer or
200 // a random nul in the file. Disambiguate that here.
201 if (CurPtr-1 != CurBuf->getBufferEnd())
202 return 0; // Just whitespace.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000203
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000204 // Otherwise, return end of file.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000205 --CurPtr; // Another call to lex will return EOF again.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000206 return EOF;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000207 }
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000208}
209
210
Chris Lattnerdf986172009-01-02 07:01:27 +0000211lltok::Kind LLLexer::LexToken() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000212 TokStart = CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000213
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000214 int CurChar = getNextChar();
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000215 switch (CurChar) {
216 default:
217 // Handle letters: [a-zA-Z_]
218 if (isalpha(CurChar) || CurChar == '_')
219 return LexIdentifier();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000220
Chris Lattnerdf986172009-01-02 07:01:27 +0000221 return lltok::Error;
222 case EOF: return lltok::Eof;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000223 case 0:
224 case ' ':
225 case '\t':
226 case '\n':
227 case '\r':
228 // Ignore whitespace.
229 return LexToken();
230 case '+': return LexPositive();
231 case '@': return LexAt();
232 case '%': return LexPercent();
233 case '"': return LexQuote();
234 case '.':
235 if (const char *Ptr = isLabelTail(CurPtr)) {
236 CurPtr = Ptr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000237 StrVal.assign(TokStart, CurPtr-1);
238 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000239 }
240 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
241 CurPtr += 2;
Chris Lattnerdf986172009-01-02 07:01:27 +0000242 return lltok::dotdotdot;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000243 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000244 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000245 case '$':
246 if (const char *Ptr = isLabelTail(CurPtr)) {
247 CurPtr = Ptr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000248 StrVal.assign(TokStart, CurPtr-1);
249 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000250 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000251 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000252 case ';':
253 SkipLineComment();
254 return LexToken();
Nick Lewycky21cc4462009-04-04 07:22:01 +0000255 case '!': return lltok::Metadata;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000256 case '0': case '1': case '2': case '3': case '4':
257 case '5': case '6': case '7': case '8': case '9':
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000258 case '-':
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000259 return LexDigitOrNegative();
Chris Lattnerdf986172009-01-02 07:01:27 +0000260 case '=': return lltok::equal;
261 case '[': return lltok::lsquare;
262 case ']': return lltok::rsquare;
263 case '{': return lltok::lbrace;
264 case '}': return lltok::rbrace;
265 case '<': return lltok::less;
266 case '>': return lltok::greater;
267 case '(': return lltok::lparen;
268 case ')': return lltok::rparen;
269 case ',': return lltok::comma;
270 case '*': return lltok::star;
271 case '\\': return lltok::backslash;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000272 }
273}
274
275void LLLexer::SkipLineComment() {
276 while (1) {
277 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
278 return;
279 }
280}
281
282/// LexAt - Lex all tokens that start with an @ character:
Chris Lattnerdf986172009-01-02 07:01:27 +0000283/// GlobalVar @\"[^\"]*\"
284/// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
285/// GlobalVarID @[0-9]+
286lltok::Kind LLLexer::LexAt() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000287 // Handle AtStringConstant: @\"[^\"]*\"
288 if (CurPtr[0] == '"') {
289 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000290
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000291 while (1) {
292 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000293
294 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000295 Error("end of file in global variable name");
296 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000297 }
298 if (CurChar == '"') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000299 StrVal.assign(TokStart+2, CurPtr-1);
300 UnEscapeLexed(StrVal);
301 return lltok::GlobalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000302 }
303 }
304 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000305
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000306 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000307 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000308 CurPtr[0] == '.' || CurPtr[0] == '_') {
309 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000310 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000311 CurPtr[0] == '.' || CurPtr[0] == '_')
312 ++CurPtr;
313
Chris Lattnerdf986172009-01-02 07:01:27 +0000314 StrVal.assign(TokStart+1, CurPtr); // Skip @
315 return lltok::GlobalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000316 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000317
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000318 // Handle GlobalVarID: @[0-9]+
319 if (isdigit(CurPtr[0])) {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000320 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
321 /*empty*/;
322
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000323 uint64_t Val = atoull(TokStart+1, CurPtr);
324 if ((unsigned)Val != Val)
Chris Lattnerdf986172009-01-02 07:01:27 +0000325 Error("invalid value number (too large)!");
326 UIntVal = unsigned(Val);
327 return lltok::GlobalID;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000328 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000329
Chris Lattnerdf986172009-01-02 07:01:27 +0000330 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000331}
332
333
334/// LexPercent - Lex all tokens that start with a % character:
Chris Lattnerdf986172009-01-02 07:01:27 +0000335/// LocalVar ::= %\"[^\"]*\"
336/// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
337/// LocalVarID ::= %[0-9]+
338lltok::Kind LLLexer::LexPercent() {
339 // Handle LocalVarName: %\"[^\"]*\"
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000340 if (CurPtr[0] == '"') {
341 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000342
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000343 while (1) {
344 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000345
346 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000347 Error("end of file in string constant");
348 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000349 }
350 if (CurChar == '"') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000351 StrVal.assign(TokStart+2, CurPtr-1);
352 UnEscapeLexed(StrVal);
353 return lltok::LocalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000354 }
355 }
356 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000357
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000358 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000359 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000360 CurPtr[0] == '.' || CurPtr[0] == '_') {
361 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000362 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000363 CurPtr[0] == '.' || CurPtr[0] == '_')
364 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000365
Chris Lattnerdf986172009-01-02 07:01:27 +0000366 StrVal.assign(TokStart+1, CurPtr); // Skip %
367 return lltok::LocalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000368 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000369
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000370 // Handle LocalVarID: %[0-9]+
371 if (isdigit(CurPtr[0])) {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000372 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
373 /*empty*/;
374
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000375 uint64_t Val = atoull(TokStart+1, CurPtr);
376 if ((unsigned)Val != Val)
Chris Lattnerdf986172009-01-02 07:01:27 +0000377 Error("invalid value number (too large)!");
378 UIntVal = unsigned(Val);
379 return lltok::LocalVarID;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000380 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000381
Chris Lattnerdf986172009-01-02 07:01:27 +0000382 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000383}
384
385/// LexQuote - Lex all tokens that start with a " character:
386/// QuoteLabel "[^"]+":
387/// StringConstant "[^"]*"
Chris Lattnerdf986172009-01-02 07:01:27 +0000388lltok::Kind LLLexer::LexQuote() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000389 while (1) {
390 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000391
392 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000393 Error("end of file in quoted string");
394 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000395 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000396
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000397 if (CurChar != '"') continue;
398
399 if (CurPtr[0] != ':') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000400 StrVal.assign(TokStart+1, CurPtr-1);
401 UnEscapeLexed(StrVal);
402 return lltok::StringConstant;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000403 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000404
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000405 ++CurPtr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000406 StrVal.assign(TokStart+1, CurPtr-2);
407 UnEscapeLexed(StrVal);
408 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000409 }
410}
411
412static bool JustWhitespaceNewLine(const char *&Ptr) {
413 const char *ThisPtr = Ptr;
414 while (*ThisPtr == ' ' || *ThisPtr == '\t')
415 ++ThisPtr;
416 if (*ThisPtr == '\n' || *ThisPtr == '\r') {
417 Ptr = ThisPtr;
418 return true;
419 }
420 return false;
421}
422
423
424/// LexIdentifier: Handle several related productions:
425/// Label [-a-zA-Z$._0-9]+:
426/// IntegerType i[0-9]+
427/// Keyword sdiv, float, ...
428/// HexIntConstant [us]0x[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000429lltok::Kind LLLexer::LexIdentifier() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000430 const char *StartChar = CurPtr;
431 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
432 const char *KeywordEnd = 0;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000433
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000434 for (; isLabelChar(*CurPtr); ++CurPtr) {
435 // If we decide this is an integer, remember the end of the sequence.
436 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
437 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
438 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000439
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000440 // If we stopped due to a colon, this really is a label.
441 if (*CurPtr == ':') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000442 StrVal.assign(StartChar-1, CurPtr++);
443 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000444 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000445
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000446 // Otherwise, this wasn't a label. If this was valid as an integer type,
447 // return it.
448 if (IntEnd == 0) IntEnd = CurPtr;
449 if (IntEnd != StartChar) {
450 CurPtr = IntEnd;
451 uint64_t NumBits = atoull(StartChar, CurPtr);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000452 if (NumBits < IntegerType::MIN_INT_BITS ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000453 NumBits > IntegerType::MAX_INT_BITS) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000454 Error("bitwidth for integer type out of range!");
455 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000456 }
Owen Andersonff6c91e2009-07-07 18:44:11 +0000457 TyVal = Context.getIntegerType(NumBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000458 return lltok::Type;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000459 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000460
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000461 // Otherwise, this was a letter sequence. See which keyword this is.
462 if (KeywordEnd == 0) KeywordEnd = CurPtr;
463 CurPtr = KeywordEnd;
464 --StartChar;
465 unsigned Len = CurPtr-StartChar;
Chris Lattnerdf986172009-01-02 07:01:27 +0000466#define KEYWORD(STR) \
467 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
468 return lltok::kw_##STR;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000469
Chris Lattnerdf986172009-01-02 07:01:27 +0000470 KEYWORD(begin); KEYWORD(end);
471 KEYWORD(true); KEYWORD(false);
472 KEYWORD(declare); KEYWORD(define);
473 KEYWORD(global); KEYWORD(constant);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000474
Rafael Espindolabb46f522009-01-15 20:18:42 +0000475 KEYWORD(private);
Chris Lattnerdf986172009-01-02 07:01:27 +0000476 KEYWORD(internal);
Chris Lattner266c7bb2009-04-13 05:44:34 +0000477 KEYWORD(available_externally);
Chris Lattnerdf986172009-01-02 07:01:27 +0000478 KEYWORD(linkonce);
Duncan Sands667d4b82009-03-07 15:45:40 +0000479 KEYWORD(linkonce_odr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000480 KEYWORD(weak);
Duncan Sands667d4b82009-03-07 15:45:40 +0000481 KEYWORD(weak_odr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000482 KEYWORD(appending);
483 KEYWORD(dllimport);
484 KEYWORD(dllexport);
485 KEYWORD(common);
486 KEYWORD(default);
487 KEYWORD(hidden);
488 KEYWORD(protected);
489 KEYWORD(extern_weak);
490 KEYWORD(external);
491 KEYWORD(thread_local);
492 KEYWORD(zeroinitializer);
493 KEYWORD(undef);
494 KEYWORD(null);
495 KEYWORD(to);
496 KEYWORD(tail);
497 KEYWORD(target);
498 KEYWORD(triple);
499 KEYWORD(deplibs);
500 KEYWORD(datalayout);
501 KEYWORD(volatile);
502 KEYWORD(align);
503 KEYWORD(addrspace);
504 KEYWORD(section);
505 KEYWORD(alias);
506 KEYWORD(module);
507 KEYWORD(asm);
508 KEYWORD(sideeffect);
509 KEYWORD(gc);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000510
Chris Lattnerdf986172009-01-02 07:01:27 +0000511 KEYWORD(ccc);
512 KEYWORD(fastcc);
513 KEYWORD(coldcc);
514 KEYWORD(x86_stdcallcc);
515 KEYWORD(x86_fastcallcc);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000516 KEYWORD(arm_apcscc);
517 KEYWORD(arm_aapcscc);
518 KEYWORD(arm_aapcs_vfpcc);
519
Chris Lattnerdf986172009-01-02 07:01:27 +0000520 KEYWORD(cc);
521 KEYWORD(c);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000522
Chris Lattnerdf986172009-01-02 07:01:27 +0000523 KEYWORD(signext);
524 KEYWORD(zeroext);
525 KEYWORD(inreg);
526 KEYWORD(sret);
527 KEYWORD(nounwind);
528 KEYWORD(noreturn);
529 KEYWORD(noalias);
530 KEYWORD(nocapture);
531 KEYWORD(byval);
532 KEYWORD(nest);
533 KEYWORD(readnone);
534 KEYWORD(readonly);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000535
Chris Lattnerdf986172009-01-02 07:01:27 +0000536 KEYWORD(noinline);
537 KEYWORD(alwaysinline);
538 KEYWORD(optsize);
539 KEYWORD(ssp);
540 KEYWORD(sspreq);
Devang Pateld18e31a2009-06-04 22:05:33 +0000541 KEYWORD(noredzone);
Devang Patel578efa92009-06-05 21:57:13 +0000542 KEYWORD(noimplicitfloat);
Devang Pateld4980812008-09-02 20:52:40 +0000543
Chris Lattnerdf986172009-01-02 07:01:27 +0000544 KEYWORD(type);
545 KEYWORD(opaque);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000546
Chris Lattnerdf986172009-01-02 07:01:27 +0000547 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
548 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
549 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
550 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
Misha Brukman9ea40342009-01-02 22:46:48 +0000551
Chris Lattnerdf986172009-01-02 07:01:27 +0000552 KEYWORD(x);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000553#undef KEYWORD
554
555 // Keywords for types.
Chris Lattnerdf986172009-01-02 07:01:27 +0000556#define TYPEKEYWORD(STR, LLVMTY) \
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000557 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
Chris Lattnerdf986172009-01-02 07:01:27 +0000558 TyVal = LLVMTY; return lltok::Type; }
559 TYPEKEYWORD("void", Type::VoidTy);
560 TYPEKEYWORD("float", Type::FloatTy);
561 TYPEKEYWORD("double", Type::DoubleTy);
562 TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty);
563 TYPEKEYWORD("fp128", Type::FP128Ty);
564 TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty);
565 TYPEKEYWORD("label", Type::LabelTy);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000566 TYPEKEYWORD("metadata", Type::MetadataTy);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000567#undef TYPEKEYWORD
568
569 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
570 // to avoid conflicting with the sext/zext instructions, below.
571 if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
572 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
573 if (JustWhitespaceNewLine(CurPtr))
Chris Lattnerdf986172009-01-02 07:01:27 +0000574 return lltok::kw_signext;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000575 } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
576 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
577 if (JustWhitespaceNewLine(CurPtr))
Chris Lattnerdf986172009-01-02 07:01:27 +0000578 return lltok::kw_zeroext;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000579 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000580
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000581 // Keywords for instructions.
Chris Lattnerdf986172009-01-02 07:01:27 +0000582#define INSTKEYWORD(STR, Enum) \
583 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
584 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000585
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000586 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
587 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
588 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
Chris Lattnerdf986172009-01-02 07:01:27 +0000589 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
590 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
591 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
592 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
593 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000594
Chris Lattnerdf986172009-01-02 07:01:27 +0000595 INSTKEYWORD(phi, PHI);
596 INSTKEYWORD(call, Call);
597 INSTKEYWORD(trunc, Trunc);
598 INSTKEYWORD(zext, ZExt);
599 INSTKEYWORD(sext, SExt);
600 INSTKEYWORD(fptrunc, FPTrunc);
601 INSTKEYWORD(fpext, FPExt);
602 INSTKEYWORD(uitofp, UIToFP);
603 INSTKEYWORD(sitofp, SIToFP);
604 INSTKEYWORD(fptoui, FPToUI);
605 INSTKEYWORD(fptosi, FPToSI);
606 INSTKEYWORD(inttoptr, IntToPtr);
607 INSTKEYWORD(ptrtoint, PtrToInt);
608 INSTKEYWORD(bitcast, BitCast);
609 INSTKEYWORD(select, Select);
610 INSTKEYWORD(va_arg, VAArg);
611 INSTKEYWORD(ret, Ret);
612 INSTKEYWORD(br, Br);
613 INSTKEYWORD(switch, Switch);
614 INSTKEYWORD(invoke, Invoke);
615 INSTKEYWORD(unwind, Unwind);
616 INSTKEYWORD(unreachable, Unreachable);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000617
Chris Lattnerdf986172009-01-02 07:01:27 +0000618 INSTKEYWORD(malloc, Malloc);
619 INSTKEYWORD(alloca, Alloca);
620 INSTKEYWORD(free, Free);
621 INSTKEYWORD(load, Load);
622 INSTKEYWORD(store, Store);
623 INSTKEYWORD(getelementptr, GetElementPtr);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000624
Chris Lattnerdf986172009-01-02 07:01:27 +0000625 INSTKEYWORD(extractelement, ExtractElement);
626 INSTKEYWORD(insertelement, InsertElement);
627 INSTKEYWORD(shufflevector, ShuffleVector);
628 INSTKEYWORD(getresult, ExtractValue);
629 INSTKEYWORD(extractvalue, ExtractValue);
630 INSTKEYWORD(insertvalue, InsertValue);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000631#undef INSTKEYWORD
632
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000633 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
634 // the CFE to avoid forcing it to deal with 64-bit numbers.
635 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
636 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
637 int len = CurPtr-TokStart-3;
638 uint32_t bits = len * 4;
639 APInt Tmp(bits, TokStart+3, len, 16);
640 uint32_t activeBits = Tmp.getActiveBits();
641 if (activeBits > 0 && activeBits < bits)
642 Tmp.trunc(activeBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000643 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
644 return lltok::APSInt;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000645 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000646
Chris Lattner4ce0df62007-11-18 18:43:24 +0000647 // If this is "cc1234", return this as just "cc".
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000648 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
649 CurPtr = TokStart+2;
Chris Lattnerdf986172009-01-02 07:01:27 +0000650 return lltok::kw_cc;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000651 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000652
Chris Lattner4ce0df62007-11-18 18:43:24 +0000653 // If this starts with "call", return it as CALL. This is to support old
654 // broken .ll files. FIXME: remove this with LLVM 3.0.
655 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
656 CurPtr = TokStart+4;
Chris Lattnerdf986172009-01-02 07:01:27 +0000657 UIntVal = Instruction::Call;
658 return lltok::kw_call;
Chris Lattner4ce0df62007-11-18 18:43:24 +0000659 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000660
Chris Lattnerdf986172009-01-02 07:01:27 +0000661 // Finally, if this isn't known, return an error.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000662 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000663 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000664}
665
666
667/// Lex0x: Handle productions that start with 0x, knowing that it matches and
668/// that this is not a label:
669/// HexFPConstant 0x[0-9A-Fa-f]+
670/// HexFP80Constant 0xK[0-9A-Fa-f]+
671/// HexFP128Constant 0xL[0-9A-Fa-f]+
672/// HexPPC128Constant 0xM[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000673lltok::Kind LLLexer::Lex0x() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000674 CurPtr = TokStart + 2;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000675
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000676 char Kind;
677 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
678 Kind = *CurPtr++;
679 } else {
680 Kind = 'J';
681 }
682
683 if (!isxdigit(CurPtr[0])) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000684 // Bad token, return it as an error.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000685 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000686 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000687 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000688
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000689 while (isxdigit(CurPtr[0]))
690 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000691
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000692 if (Kind == 'J') {
693 // HexFPConstant - Floating point constant represented in IEEE format as a
694 // hexadecimal number for when exponential notation is not precise enough.
695 // Float and double only.
Chris Lattnerdf986172009-01-02 07:01:27 +0000696 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
697 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000698 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000699
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000700 uint64_t Pair[2];
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000701 switch (Kind) {
702 default: assert(0 && "Unknown kind!");
703 case 'K':
704 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000705 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
Chris Lattnerdf986172009-01-02 07:01:27 +0000706 APFloatVal = APFloat(APInt(80, 2, Pair));
707 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000708 case 'L':
709 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000710 HexToIntPair(TokStart+3, CurPtr, Pair);
Chris Lattnerdf986172009-01-02 07:01:27 +0000711 APFloatVal = APFloat(APInt(128, 2, Pair), true);
712 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000713 case 'M':
714 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000715 HexToIntPair(TokStart+3, CurPtr, Pair);
Chris Lattnerdf986172009-01-02 07:01:27 +0000716 APFloatVal = APFloat(APInt(128, 2, Pair));
717 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000718 }
719}
720
721/// LexIdentifier: Handle several related productions:
722/// Label [-a-zA-Z$._0-9]+:
723/// NInteger -[0-9]+
724/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
725/// PInteger [0-9]+
726/// HexFPConstant 0x[0-9A-Fa-f]+
727/// HexFP80Constant 0xK[0-9A-Fa-f]+
728/// HexFP128Constant 0xL[0-9A-Fa-f]+
729/// HexPPC128Constant 0xM[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000730lltok::Kind LLLexer::LexDigitOrNegative() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000731 // If the letter after the negative is a number, this is probably a label.
732 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
733 // Okay, this is not a number after the -, it's probably a label.
734 if (const char *End = isLabelTail(CurPtr)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000735 StrVal.assign(TokStart, End-1);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000736 CurPtr = End;
Chris Lattnerdf986172009-01-02 07:01:27 +0000737 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000738 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000739
Chris Lattnerdf986172009-01-02 07:01:27 +0000740 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000741 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000742
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000743 // At this point, it is either a label, int or fp constant.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000744
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000745 // Skip digits, we have at least one.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000746 for (; isdigit(CurPtr[0]); ++CurPtr)
747 /*empty*/;
748
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000749 // Check to see if this really is a label afterall, e.g. "-1:".
750 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
751 if (const char *End = isLabelTail(CurPtr)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000752 StrVal.assign(TokStart, End-1);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000753 CurPtr = End;
Chris Lattnerdf986172009-01-02 07:01:27 +0000754 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000755 }
756 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000757
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000758 // If the next character is a '.', then it is a fp value, otherwise its
759 // integer.
760 if (CurPtr[0] != '.') {
761 if (TokStart[0] == '0' && TokStart[1] == 'x')
762 return Lex0x();
763 unsigned Len = CurPtr-TokStart;
764 uint32_t numBits = ((Len * 64) / 19) + 2;
765 APInt Tmp(numBits, TokStart, Len, 10);
766 if (TokStart[0] == '-') {
767 uint32_t minBits = Tmp.getMinSignedBits();
768 if (minBits > 0 && minBits < numBits)
769 Tmp.trunc(minBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000770 APSIntVal = APSInt(Tmp, false);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000771 } else {
772 uint32_t activeBits = Tmp.getActiveBits();
773 if (activeBits > 0 && activeBits < numBits)
774 Tmp.trunc(activeBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000775 APSIntVal = APSInt(Tmp, true);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000776 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000777 return lltok::APSInt;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000778 }
779
780 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000781
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000782 // Skip over [0-9]*([eE][-+]?[0-9]+)?
783 while (isdigit(CurPtr[0])) ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000784
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000785 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000786 if (isdigit(CurPtr[1]) ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000787 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
788 CurPtr += 2;
789 while (isdigit(CurPtr[0])) ++CurPtr;
790 }
791 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000792
Chris Lattnerdf986172009-01-02 07:01:27 +0000793 APFloatVal = APFloat(atof(TokStart));
794 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000795}
796
797/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattnerdf986172009-01-02 07:01:27 +0000798lltok::Kind LLLexer::LexPositive() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000799 // If the letter after the negative is a number, this is probably not a
800 // label.
801 if (!isdigit(CurPtr[0]))
Chris Lattnerdf986172009-01-02 07:01:27 +0000802 return lltok::Error;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000803
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000804 // Skip digits.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000805 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
806 /*empty*/;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000807
808 // At this point, we need a '.'.
809 if (CurPtr[0] != '.') {
810 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000811 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000812 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000813
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000814 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000815
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000816 // Skip over [0-9]*([eE][-+]?[0-9]+)?
817 while (isdigit(CurPtr[0])) ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000818
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000819 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000820 if (isdigit(CurPtr[1]) ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000821 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
822 CurPtr += 2;
823 while (isdigit(CurPtr[0])) ++CurPtr;
824 }
825 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000826
Chris Lattnerdf986172009-01-02 07:01:27 +0000827 APFloatVal = APFloat(atof(TokStart));
828 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000829}