blob: 6f1d5eefe8c8121df8d604a76f0faff190bc1d20 [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"
Chris Lattner8e3a8e02007-11-18 08:46:26 +000017#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerd185f642007-12-08 19:03:30 +000018#include "llvm/Support/MathExtras.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000019#include "llvm/Support/raw_ostream.h"
20#include "llvm/Assembly/Parser.h"
Chris Lattnera8961762009-01-02 07:18:46 +000021#include <cstdlib>
Misha Brukman5679d182009-01-02 22:49:28 +000022#include <cstring>
Chris Lattner8e3a8e02007-11-18 08:46:26 +000023using namespace llvm;
24
Chris Lattnerdf986172009-01-02 07:01:27 +000025bool LLLexer::Error(LocTy ErrorLoc, const std::string &Msg) const {
26 // Scan backward to find the start of the line.
27 const char *LineStart = ErrorLoc;
Misha Brukman9ea40342009-01-02 22:46:48 +000028 while (LineStart != CurBuf->getBufferStart() &&
Chris Lattnerdf986172009-01-02 07:01:27 +000029 LineStart[-1] != '\n' && LineStart[-1] != '\r')
30 --LineStart;
31 // Get the end of the line.
32 const char *LineEnd = ErrorLoc;
Misha Brukman9ea40342009-01-02 22:46:48 +000033 while (LineEnd != CurBuf->getBufferEnd() &&
Chris Lattnerdf986172009-01-02 07:01:27 +000034 LineEnd[0] != '\n' && LineEnd[0] != '\r')
35 ++LineEnd;
Misha Brukman9ea40342009-01-02 22:46:48 +000036
Chris Lattnerdf986172009-01-02 07:01:27 +000037 unsigned LineNo = 1;
38 for (const char *FP = CurBuf->getBufferStart(); FP != ErrorLoc; ++FP)
39 if (*FP == '\n') ++LineNo;
40
Misha Brukman9ea40342009-01-02 22:46:48 +000041 std::string LineContents(LineStart, LineEnd);
Chris Lattnerdf986172009-01-02 07:01:27 +000042 ErrorInfo.setError(Msg, LineNo, ErrorLoc-LineStart, LineContents);
43 return true;
44}
45
Chris Lattner8e3a8e02007-11-18 08:46:26 +000046//===----------------------------------------------------------------------===//
47// Helper functions.
48//===----------------------------------------------------------------------===//
49
50// atoull - Convert an ascii string of decimal digits into the unsigned long
51// long representation... this does not have to do input error checking,
52// because we know that the input will be matched by a suitable regex...
53//
Chris Lattnerdf986172009-01-02 07:01:27 +000054uint64_t LLLexer::atoull(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 *= 10;
59 Result += *Buffer-'0';
60 if (Result < OldRes) { // Uh, oh, overflow detected!!!
Chris Lattnerdf986172009-01-02 07:01:27 +000061 Error("constant bigger than 64 bits detected!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +000062 return 0;
63 }
64 }
65 return Result;
66}
67
Chris Lattnerdf986172009-01-02 07:01:27 +000068uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000069 uint64_t Result = 0;
70 for (; Buffer != End; ++Buffer) {
71 uint64_t OldRes = Result;
72 Result *= 16;
73 char C = *Buffer;
74 if (C >= '0' && C <= '9')
75 Result += C-'0';
76 else if (C >= 'A' && C <= 'F')
77 Result += C-'A'+10;
78 else if (C >= 'a' && C <= 'f')
79 Result += C-'a'+10;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +000080
Chris Lattner8e3a8e02007-11-18 08:46:26 +000081 if (Result < OldRes) { // Uh, oh, overflow detected!!!
Chris Lattnerdf986172009-01-02 07:01:27 +000082 Error("constant bigger than 64 bits detected!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +000083 return 0;
84 }
85 }
86 return Result;
87}
88
Chris Lattnerdf986172009-01-02 07:01:27 +000089void LLLexer::HexToIntPair(const char *Buffer, const char *End,
90 uint64_t Pair[2]) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000091 Pair[0] = 0;
92 for (int i=0; i<16; i++, Buffer++) {
93 assert(Buffer != End);
94 Pair[0] *= 16;
95 char C = *Buffer;
96 if (C >= '0' && C <= '9')
97 Pair[0] += C-'0';
98 else if (C >= 'A' && C <= 'F')
99 Pair[0] += C-'A'+10;
100 else if (C >= 'a' && C <= 'f')
101 Pair[0] += C-'a'+10;
102 }
103 Pair[1] = 0;
104 for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
105 Pair[1] *= 16;
106 char C = *Buffer;
107 if (C >= '0' && C <= '9')
108 Pair[1] += C-'0';
109 else if (C >= 'A' && C <= 'F')
110 Pair[1] += C-'A'+10;
111 else if (C >= 'a' && C <= 'f')
112 Pair[1] += C-'a'+10;
113 }
Chris Lattnerd343c6b2007-11-18 18:25:18 +0000114 if (Buffer != End)
Chris Lattnerdf986172009-01-02 07:01:27 +0000115 Error("constant bigger than 128 bits detected!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000116}
117
118// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
119// appropriate character.
120static void UnEscapeLexed(std::string &Str) {
121 if (Str.empty()) return;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000122
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000123 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
124 char *BOut = Buffer;
125 for (char *BIn = Buffer; BIn != EndBuffer; ) {
126 if (BIn[0] == '\\') {
127 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
128 *BOut++ = '\\'; // Two \ becomes one
129 BIn += 2;
130 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
131 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
132 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
133 BIn[3] = Tmp; // Restore character
134 BIn += 3; // Skip over handled chars
135 ++BOut;
136 } else {
137 *BOut++ = *BIn++;
138 }
139 } else {
140 *BOut++ = *BIn++;
141 }
142 }
143 Str.resize(BOut-Buffer);
144}
145
146/// isLabelChar - Return true for [-a-zA-Z$._0-9].
147static bool isLabelChar(char C) {
148 return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
149}
150
151
152/// isLabelTail - Return true if this pointer points to a valid end of a label.
153static const char *isLabelTail(const char *CurPtr) {
154 while (1) {
155 if (CurPtr[0] == ':') return CurPtr+1;
156 if (!isLabelChar(CurPtr[0])) return 0;
157 ++CurPtr;
158 }
159}
160
161
162
163//===----------------------------------------------------------------------===//
164// Lexer definition.
165//===----------------------------------------------------------------------===//
166
Chris Lattnerdf986172009-01-02 07:01:27 +0000167LLLexer::LLLexer(MemoryBuffer *StartBuf, ParseError &Err)
168 : CurBuf(StartBuf), ErrorInfo(Err), APFloatVal(0.0) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000169 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.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000185
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000186 // Otherwise, return end of file.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000187 --CurPtr; // Another call to lex will return EOF again.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000188 return EOF;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000189 }
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000190}
191
192
Chris Lattnerdf986172009-01-02 07:01:27 +0000193lltok::Kind LLLexer::LexToken() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000194 TokStart = CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000195
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000196 int CurChar = getNextChar();
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000197 switch (CurChar) {
198 default:
199 // Handle letters: [a-zA-Z_]
200 if (isalpha(CurChar) || CurChar == '_')
201 return LexIdentifier();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000202
Chris Lattnerdf986172009-01-02 07:01:27 +0000203 return lltok::Error;
204 case EOF: return lltok::Eof;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000205 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;
Chris Lattnerdf986172009-01-02 07:01:27 +0000219 StrVal.assign(TokStart, CurPtr-1);
220 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000221 }
222 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
223 CurPtr += 2;
Chris Lattnerdf986172009-01-02 07:01:27 +0000224 return lltok::dotdotdot;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000225 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000226 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000227 case '$':
228 if (const char *Ptr = isLabelTail(CurPtr)) {
229 CurPtr = Ptr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000230 StrVal.assign(TokStart, CurPtr-1);
231 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000232 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000233 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000234 case ';':
235 SkipLineComment();
236 return LexToken();
237 case '0': case '1': case '2': case '3': case '4':
238 case '5': case '6': case '7': case '8': case '9':
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000239 case '-':
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000240 return LexDigitOrNegative();
Chris Lattnerdf986172009-01-02 07:01:27 +0000241 case '=': return lltok::equal;
242 case '[': return lltok::lsquare;
243 case ']': return lltok::rsquare;
244 case '{': return lltok::lbrace;
245 case '}': return lltok::rbrace;
246 case '<': return lltok::less;
247 case '>': return lltok::greater;
248 case '(': return lltok::lparen;
249 case ')': return lltok::rparen;
250 case ',': return lltok::comma;
251 case '*': return lltok::star;
252 case '\\': return lltok::backslash;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000253 }
254}
255
256void LLLexer::SkipLineComment() {
257 while (1) {
258 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
259 return;
260 }
261}
262
263/// LexAt - Lex all tokens that start with an @ character:
Chris Lattnerdf986172009-01-02 07:01:27 +0000264/// GlobalVar @\"[^\"]*\"
265/// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
266/// GlobalVarID @[0-9]+
267lltok::Kind LLLexer::LexAt() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000268 // Handle AtStringConstant: @\"[^\"]*\"
269 if (CurPtr[0] == '"') {
270 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000271
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000272 while (1) {
273 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000274
275 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000276 Error("end of file in global variable name");
277 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000278 }
279 if (CurChar == '"') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000280 StrVal.assign(TokStart+2, CurPtr-1);
281 UnEscapeLexed(StrVal);
282 return lltok::GlobalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000283 }
284 }
285 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000286
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000287 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000288 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000289 CurPtr[0] == '.' || CurPtr[0] == '_') {
290 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000291 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000292 CurPtr[0] == '.' || CurPtr[0] == '_')
293 ++CurPtr;
294
Chris Lattnerdf986172009-01-02 07:01:27 +0000295 StrVal.assign(TokStart+1, CurPtr); // Skip @
296 return lltok::GlobalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000297 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000298
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000299 // Handle GlobalVarID: @[0-9]+
300 if (isdigit(CurPtr[0])) {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000301 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
302 /*empty*/;
303
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000304 uint64_t Val = atoull(TokStart+1, CurPtr);
305 if ((unsigned)Val != Val)
Chris Lattnerdf986172009-01-02 07:01:27 +0000306 Error("invalid value number (too large)!");
307 UIntVal = unsigned(Val);
308 return lltok::GlobalID;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000309 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000310
Chris Lattnerdf986172009-01-02 07:01:27 +0000311 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000312}
313
314
315/// LexPercent - Lex all tokens that start with a % character:
Chris Lattnerdf986172009-01-02 07:01:27 +0000316/// LocalVar ::= %\"[^\"]*\"
317/// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
318/// LocalVarID ::= %[0-9]+
319lltok::Kind LLLexer::LexPercent() {
320 // Handle LocalVarName: %\"[^\"]*\"
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000321 if (CurPtr[0] == '"') {
322 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000323
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000324 while (1) {
325 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000326
327 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000328 Error("end of file in string constant");
329 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000330 }
331 if (CurChar == '"') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000332 StrVal.assign(TokStart+2, CurPtr-1);
333 UnEscapeLexed(StrVal);
334 return lltok::LocalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000335 }
336 }
337 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000338
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000339 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000340 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000341 CurPtr[0] == '.' || CurPtr[0] == '_') {
342 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000343 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000344 CurPtr[0] == '.' || CurPtr[0] == '_')
345 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000346
Chris Lattnerdf986172009-01-02 07:01:27 +0000347 StrVal.assign(TokStart+1, CurPtr); // Skip %
348 return lltok::LocalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000349 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000350
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000351 // Handle LocalVarID: %[0-9]+
352 if (isdigit(CurPtr[0])) {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000353 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
354 /*empty*/;
355
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000356 uint64_t Val = atoull(TokStart+1, CurPtr);
357 if ((unsigned)Val != Val)
Chris Lattnerdf986172009-01-02 07:01:27 +0000358 Error("invalid value number (too large)!");
359 UIntVal = unsigned(Val);
360 return lltok::LocalVarID;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000361 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000362
Chris Lattnerdf986172009-01-02 07:01:27 +0000363 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000364}
365
366/// LexQuote - Lex all tokens that start with a " character:
367/// QuoteLabel "[^"]+":
368/// StringConstant "[^"]*"
Chris Lattnerdf986172009-01-02 07:01:27 +0000369lltok::Kind LLLexer::LexQuote() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000370 while (1) {
371 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000372
373 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000374 Error("end of file in quoted string");
375 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000376 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000377
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000378 if (CurChar != '"') continue;
379
380 if (CurPtr[0] != ':') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000381 StrVal.assign(TokStart+1, CurPtr-1);
382 UnEscapeLexed(StrVal);
383 return lltok::StringConstant;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000384 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000385
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000386 ++CurPtr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000387 StrVal.assign(TokStart+1, CurPtr-2);
388 UnEscapeLexed(StrVal);
389 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000390 }
391}
392
393static bool JustWhitespaceNewLine(const char *&Ptr) {
394 const char *ThisPtr = Ptr;
395 while (*ThisPtr == ' ' || *ThisPtr == '\t')
396 ++ThisPtr;
397 if (*ThisPtr == '\n' || *ThisPtr == '\r') {
398 Ptr = ThisPtr;
399 return true;
400 }
401 return false;
402}
403
404
405/// LexIdentifier: Handle several related productions:
406/// Label [-a-zA-Z$._0-9]+:
407/// IntegerType i[0-9]+
408/// Keyword sdiv, float, ...
409/// HexIntConstant [us]0x[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000410lltok::Kind LLLexer::LexIdentifier() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000411 const char *StartChar = CurPtr;
412 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
413 const char *KeywordEnd = 0;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000414
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000415 for (; isLabelChar(*CurPtr); ++CurPtr) {
416 // If we decide this is an integer, remember the end of the sequence.
417 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
418 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
419 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000420
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000421 // If we stopped due to a colon, this really is a label.
422 if (*CurPtr == ':') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000423 StrVal.assign(StartChar-1, CurPtr++);
424 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000425 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000426
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000427 // Otherwise, this wasn't a label. If this was valid as an integer type,
428 // return it.
429 if (IntEnd == 0) IntEnd = CurPtr;
430 if (IntEnd != StartChar) {
431 CurPtr = IntEnd;
432 uint64_t NumBits = atoull(StartChar, CurPtr);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000433 if (NumBits < IntegerType::MIN_INT_BITS ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000434 NumBits > IntegerType::MAX_INT_BITS) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000435 Error("bitwidth for integer type out of range!");
436 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000437 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000438 TyVal = IntegerType::get(NumBits);
439 return lltok::Type;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000440 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000441
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000442 // Otherwise, this was a letter sequence. See which keyword this is.
443 if (KeywordEnd == 0) KeywordEnd = CurPtr;
444 CurPtr = KeywordEnd;
445 --StartChar;
446 unsigned Len = CurPtr-StartChar;
Chris Lattnerdf986172009-01-02 07:01:27 +0000447#define KEYWORD(STR) \
448 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
449 return lltok::kw_##STR;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000450
Chris Lattnerdf986172009-01-02 07:01:27 +0000451 KEYWORD(begin); KEYWORD(end);
452 KEYWORD(true); KEYWORD(false);
453 KEYWORD(declare); KEYWORD(define);
454 KEYWORD(global); KEYWORD(constant);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000455
Rafael Espindolabb46f522009-01-15 20:18:42 +0000456 KEYWORD(private);
Chris Lattnerdf986172009-01-02 07:01:27 +0000457 KEYWORD(internal);
458 KEYWORD(linkonce);
Duncan Sands667d4b82009-03-07 15:45:40 +0000459 KEYWORD(linkonce_odr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000460 KEYWORD(weak);
Duncan Sands667d4b82009-03-07 15:45:40 +0000461 KEYWORD(weak_odr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000462 KEYWORD(appending);
463 KEYWORD(dllimport);
464 KEYWORD(dllexport);
465 KEYWORD(common);
Duncan Sands667d4b82009-03-07 15:45:40 +0000466 KEYWORD(common_odr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000467 KEYWORD(default);
468 KEYWORD(hidden);
469 KEYWORD(protected);
470 KEYWORD(extern_weak);
Duncan Sands667d4b82009-03-07 15:45:40 +0000471 KEYWORD(extern_weak_odr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000472 KEYWORD(external);
473 KEYWORD(thread_local);
474 KEYWORD(zeroinitializer);
475 KEYWORD(undef);
476 KEYWORD(null);
477 KEYWORD(to);
478 KEYWORD(tail);
479 KEYWORD(target);
480 KEYWORD(triple);
481 KEYWORD(deplibs);
482 KEYWORD(datalayout);
483 KEYWORD(volatile);
484 KEYWORD(align);
485 KEYWORD(addrspace);
486 KEYWORD(section);
487 KEYWORD(alias);
488 KEYWORD(module);
489 KEYWORD(asm);
490 KEYWORD(sideeffect);
491 KEYWORD(gc);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000492
Chris Lattnerdf986172009-01-02 07:01:27 +0000493 KEYWORD(ccc);
494 KEYWORD(fastcc);
495 KEYWORD(coldcc);
496 KEYWORD(x86_stdcallcc);
497 KEYWORD(x86_fastcallcc);
498 KEYWORD(cc);
499 KEYWORD(c);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000500
Chris Lattnerdf986172009-01-02 07:01:27 +0000501 KEYWORD(signext);
502 KEYWORD(zeroext);
503 KEYWORD(inreg);
504 KEYWORD(sret);
505 KEYWORD(nounwind);
506 KEYWORD(noreturn);
507 KEYWORD(noalias);
508 KEYWORD(nocapture);
509 KEYWORD(byval);
510 KEYWORD(nest);
511 KEYWORD(readnone);
512 KEYWORD(readonly);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000513
Chris Lattnerdf986172009-01-02 07:01:27 +0000514 KEYWORD(noinline);
515 KEYWORD(alwaysinline);
516 KEYWORD(optsize);
517 KEYWORD(ssp);
518 KEYWORD(sspreq);
Devang Pateld4980812008-09-02 20:52:40 +0000519
Chris Lattnerdf986172009-01-02 07:01:27 +0000520 KEYWORD(type);
521 KEYWORD(opaque);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000522
Chris Lattnerdf986172009-01-02 07:01:27 +0000523 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
524 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
525 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
526 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
Misha Brukman9ea40342009-01-02 22:46:48 +0000527
Chris Lattnerdf986172009-01-02 07:01:27 +0000528 KEYWORD(x);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000529#undef KEYWORD
530
531 // Keywords for types.
Chris Lattnerdf986172009-01-02 07:01:27 +0000532#define TYPEKEYWORD(STR, LLVMTY) \
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000533 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
Chris Lattnerdf986172009-01-02 07:01:27 +0000534 TyVal = LLVMTY; return lltok::Type; }
535 TYPEKEYWORD("void", Type::VoidTy);
536 TYPEKEYWORD("float", Type::FloatTy);
537 TYPEKEYWORD("double", Type::DoubleTy);
538 TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty);
539 TYPEKEYWORD("fp128", Type::FP128Ty);
540 TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty);
541 TYPEKEYWORD("label", Type::LabelTy);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000542#undef TYPEKEYWORD
543
544 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
545 // to avoid conflicting with the sext/zext instructions, below.
546 if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
547 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
548 if (JustWhitespaceNewLine(CurPtr))
Chris Lattnerdf986172009-01-02 07:01:27 +0000549 return lltok::kw_signext;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000550 } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
551 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
552 if (JustWhitespaceNewLine(CurPtr))
Chris Lattnerdf986172009-01-02 07:01:27 +0000553 return lltok::kw_zeroext;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000554 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000555
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000556 // Keywords for instructions.
Chris Lattnerdf986172009-01-02 07:01:27 +0000557#define INSTKEYWORD(STR, Enum) \
558 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
559 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000560
Chris Lattnerdf986172009-01-02 07:01:27 +0000561 INSTKEYWORD(add, Add); INSTKEYWORD(sub, Sub); INSTKEYWORD(mul, Mul);
562 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
563 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
564 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
565 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
566 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
567 INSTKEYWORD(vicmp, VICmp); INSTKEYWORD(vfcmp, VFCmp);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000568
Chris Lattnerdf986172009-01-02 07:01:27 +0000569 INSTKEYWORD(phi, PHI);
570 INSTKEYWORD(call, Call);
571 INSTKEYWORD(trunc, Trunc);
572 INSTKEYWORD(zext, ZExt);
573 INSTKEYWORD(sext, SExt);
574 INSTKEYWORD(fptrunc, FPTrunc);
575 INSTKEYWORD(fpext, FPExt);
576 INSTKEYWORD(uitofp, UIToFP);
577 INSTKEYWORD(sitofp, SIToFP);
578 INSTKEYWORD(fptoui, FPToUI);
579 INSTKEYWORD(fptosi, FPToSI);
580 INSTKEYWORD(inttoptr, IntToPtr);
581 INSTKEYWORD(ptrtoint, PtrToInt);
582 INSTKEYWORD(bitcast, BitCast);
583 INSTKEYWORD(select, Select);
584 INSTKEYWORD(va_arg, VAArg);
585 INSTKEYWORD(ret, Ret);
586 INSTKEYWORD(br, Br);
587 INSTKEYWORD(switch, Switch);
588 INSTKEYWORD(invoke, Invoke);
589 INSTKEYWORD(unwind, Unwind);
590 INSTKEYWORD(unreachable, Unreachable);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000591
Chris Lattnerdf986172009-01-02 07:01:27 +0000592 INSTKEYWORD(malloc, Malloc);
593 INSTKEYWORD(alloca, Alloca);
594 INSTKEYWORD(free, Free);
595 INSTKEYWORD(load, Load);
596 INSTKEYWORD(store, Store);
597 INSTKEYWORD(getelementptr, GetElementPtr);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000598
Chris Lattnerdf986172009-01-02 07:01:27 +0000599 INSTKEYWORD(extractelement, ExtractElement);
600 INSTKEYWORD(insertelement, InsertElement);
601 INSTKEYWORD(shufflevector, ShuffleVector);
602 INSTKEYWORD(getresult, ExtractValue);
603 INSTKEYWORD(extractvalue, ExtractValue);
604 INSTKEYWORD(insertvalue, InsertValue);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000605#undef INSTKEYWORD
606
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000607 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
608 // the CFE to avoid forcing it to deal with 64-bit numbers.
609 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
610 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
611 int len = CurPtr-TokStart-3;
612 uint32_t bits = len * 4;
613 APInt Tmp(bits, TokStart+3, len, 16);
614 uint32_t activeBits = Tmp.getActiveBits();
615 if (activeBits > 0 && activeBits < bits)
616 Tmp.trunc(activeBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000617 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
618 return lltok::APSInt;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000619 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000620
Chris Lattner4ce0df62007-11-18 18:43:24 +0000621 // If this is "cc1234", return this as just "cc".
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000622 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
623 CurPtr = TokStart+2;
Chris Lattnerdf986172009-01-02 07:01:27 +0000624 return lltok::kw_cc;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000625 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000626
Chris Lattner4ce0df62007-11-18 18:43:24 +0000627 // If this starts with "call", return it as CALL. This is to support old
628 // broken .ll files. FIXME: remove this with LLVM 3.0.
629 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
630 CurPtr = TokStart+4;
Chris Lattnerdf986172009-01-02 07:01:27 +0000631 UIntVal = Instruction::Call;
632 return lltok::kw_call;
Chris Lattner4ce0df62007-11-18 18:43:24 +0000633 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000634
Chris Lattnerdf986172009-01-02 07:01:27 +0000635 // Finally, if this isn't known, return an error.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000636 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000637 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000638}
639
640
641/// Lex0x: Handle productions that start with 0x, knowing that it matches and
642/// that this is not a label:
643/// HexFPConstant 0x[0-9A-Fa-f]+
644/// HexFP80Constant 0xK[0-9A-Fa-f]+
645/// HexFP128Constant 0xL[0-9A-Fa-f]+
646/// HexPPC128Constant 0xM[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000647lltok::Kind LLLexer::Lex0x() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000648 CurPtr = TokStart + 2;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000649
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000650 char Kind;
651 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
652 Kind = *CurPtr++;
653 } else {
654 Kind = 'J';
655 }
656
657 if (!isxdigit(CurPtr[0])) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000658 // Bad token, return it as an error.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000659 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000660 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000661 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000662
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000663 while (isxdigit(CurPtr[0]))
664 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000665
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000666 if (Kind == 'J') {
667 // HexFPConstant - Floating point constant represented in IEEE format as a
668 // hexadecimal number for when exponential notation is not precise enough.
669 // Float and double only.
Chris Lattnerdf986172009-01-02 07:01:27 +0000670 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
671 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000672 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000673
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000674 uint64_t Pair[2];
675 HexToIntPair(TokStart+3, CurPtr, Pair);
676 switch (Kind) {
677 default: assert(0 && "Unknown kind!");
678 case 'K':
679 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
Chris Lattnerdf986172009-01-02 07:01:27 +0000680 APFloatVal = APFloat(APInt(80, 2, Pair));
681 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000682 case 'L':
683 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
Chris Lattnerdf986172009-01-02 07:01:27 +0000684 APFloatVal = APFloat(APInt(128, 2, Pair), true);
685 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000686 case 'M':
687 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
Chris Lattnerdf986172009-01-02 07:01:27 +0000688 APFloatVal = APFloat(APInt(128, 2, Pair));
689 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000690 }
691}
692
693/// LexIdentifier: Handle several related productions:
694/// Label [-a-zA-Z$._0-9]+:
695/// NInteger -[0-9]+
696/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
697/// PInteger [0-9]+
698/// HexFPConstant 0x[0-9A-Fa-f]+
699/// HexFP80Constant 0xK[0-9A-Fa-f]+
700/// HexFP128Constant 0xL[0-9A-Fa-f]+
701/// HexPPC128Constant 0xM[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000702lltok::Kind LLLexer::LexDigitOrNegative() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000703 // If the letter after the negative is a number, this is probably a label.
704 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
705 // Okay, this is not a number after the -, it's probably a label.
706 if (const char *End = isLabelTail(CurPtr)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000707 StrVal.assign(TokStart, End-1);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000708 CurPtr = End;
Chris Lattnerdf986172009-01-02 07:01:27 +0000709 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000710 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000711
Chris Lattnerdf986172009-01-02 07:01:27 +0000712 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000713 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000714
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000715 // At this point, it is either a label, int or fp constant.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000716
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000717 // Skip digits, we have at least one.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000718 for (; isdigit(CurPtr[0]); ++CurPtr)
719 /*empty*/;
720
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000721 // Check to see if this really is a label afterall, e.g. "-1:".
722 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
723 if (const char *End = isLabelTail(CurPtr)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000724 StrVal.assign(TokStart, End-1);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000725 CurPtr = End;
Chris Lattnerdf986172009-01-02 07:01:27 +0000726 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000727 }
728 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000729
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000730 // If the next character is a '.', then it is a fp value, otherwise its
731 // integer.
732 if (CurPtr[0] != '.') {
733 if (TokStart[0] == '0' && TokStart[1] == 'x')
734 return Lex0x();
735 unsigned Len = CurPtr-TokStart;
736 uint32_t numBits = ((Len * 64) / 19) + 2;
737 APInt Tmp(numBits, TokStart, Len, 10);
738 if (TokStart[0] == '-') {
739 uint32_t minBits = Tmp.getMinSignedBits();
740 if (minBits > 0 && minBits < numBits)
741 Tmp.trunc(minBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000742 APSIntVal = APSInt(Tmp, false);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000743 } else {
744 uint32_t activeBits = Tmp.getActiveBits();
745 if (activeBits > 0 && activeBits < numBits)
746 Tmp.trunc(activeBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000747 APSIntVal = APSInt(Tmp, true);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000748 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000749 return lltok::APSInt;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000750 }
751
752 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000753
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000754 // Skip over [0-9]*([eE][-+]?[0-9]+)?
755 while (isdigit(CurPtr[0])) ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000756
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000757 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000758 if (isdigit(CurPtr[1]) ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000759 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
760 CurPtr += 2;
761 while (isdigit(CurPtr[0])) ++CurPtr;
762 }
763 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000764
Chris Lattnerdf986172009-01-02 07:01:27 +0000765 APFloatVal = APFloat(atof(TokStart));
766 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000767}
768
769/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattnerdf986172009-01-02 07:01:27 +0000770lltok::Kind LLLexer::LexPositive() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000771 // If the letter after the negative is a number, this is probably not a
772 // label.
773 if (!isdigit(CurPtr[0]))
Chris Lattnerdf986172009-01-02 07:01:27 +0000774 return lltok::Error;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000775
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000776 // Skip digits.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000777 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
778 /*empty*/;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000779
780 // At this point, we need a '.'.
781 if (CurPtr[0] != '.') {
782 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000783 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000784 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000785
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000786 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000787
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000788 // Skip over [0-9]*([eE][-+]?[0-9]+)?
789 while (isdigit(CurPtr[0])) ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000790
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000791 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000792 if (isdigit(CurPtr[1]) ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000793 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
794 CurPtr += 2;
795 while (isdigit(CurPtr[0])) ++CurPtr;
796 }
797 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000798
Chris Lattnerdf986172009-01-02 07:01:27 +0000799 APFloatVal = APFloat(atof(TokStart));
800 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000801}