blob: f2e6890176038efe87e23507c18b42e71bf68459 [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
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000118/// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
119/// { low64, high16 } as usual for an APInt.
120void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
121 uint64_t Pair[2]) {
122 Pair[1] = 0;
123 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
124 assert(Buffer != End);
125 Pair[1] *= 16;
126 char C = *Buffer;
127 if (C >= '0' && C <= '9')
128 Pair[1] += C-'0';
129 else if (C >= 'A' && C <= 'F')
130 Pair[1] += C-'A'+10;
131 else if (C >= 'a' && C <= 'f')
132 Pair[1] += C-'a'+10;
133 }
134 Pair[0] = 0;
135 for (int i=0; i<16; i++, Buffer++) {
136 Pair[0] *= 16;
137 char C = *Buffer;
138 if (C >= '0' && C <= '9')
139 Pair[0] += C-'0';
140 else if (C >= 'A' && C <= 'F')
141 Pair[0] += C-'A'+10;
142 else if (C >= 'a' && C <= 'f')
143 Pair[0] += C-'a'+10;
144 }
145 if (Buffer != End)
146 Error("constant bigger than 128 bits detected!");
147}
148
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000149// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
150// appropriate character.
151static void UnEscapeLexed(std::string &Str) {
152 if (Str.empty()) return;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000153
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000154 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
155 char *BOut = Buffer;
156 for (char *BIn = Buffer; BIn != EndBuffer; ) {
157 if (BIn[0] == '\\') {
158 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
159 *BOut++ = '\\'; // Two \ becomes one
160 BIn += 2;
161 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
162 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
163 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
164 BIn[3] = Tmp; // Restore character
165 BIn += 3; // Skip over handled chars
166 ++BOut;
167 } else {
168 *BOut++ = *BIn++;
169 }
170 } else {
171 *BOut++ = *BIn++;
172 }
173 }
174 Str.resize(BOut-Buffer);
175}
176
177/// isLabelChar - Return true for [-a-zA-Z$._0-9].
178static bool isLabelChar(char C) {
179 return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
180}
181
182
183/// isLabelTail - Return true if this pointer points to a valid end of a label.
184static const char *isLabelTail(const char *CurPtr) {
185 while (1) {
186 if (CurPtr[0] == ':') return CurPtr+1;
187 if (!isLabelChar(CurPtr[0])) return 0;
188 ++CurPtr;
189 }
190}
191
192
193
194//===----------------------------------------------------------------------===//
195// Lexer definition.
196//===----------------------------------------------------------------------===//
197
Chris Lattnerdf986172009-01-02 07:01:27 +0000198LLLexer::LLLexer(MemoryBuffer *StartBuf, ParseError &Err)
199 : CurBuf(StartBuf), ErrorInfo(Err), APFloatVal(0.0) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000200 CurPtr = CurBuf->getBufferStart();
201}
202
203std::string LLLexer::getFilename() const {
204 return CurBuf->getBufferIdentifier();
205}
206
207int LLLexer::getNextChar() {
208 char CurChar = *CurPtr++;
209 switch (CurChar) {
210 default: return (unsigned char)CurChar;
211 case 0:
212 // A nul character in the stream is either the end of the current buffer or
213 // a random nul in the file. Disambiguate that here.
214 if (CurPtr-1 != CurBuf->getBufferEnd())
215 return 0; // Just whitespace.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000216
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000217 // Otherwise, return end of file.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000218 --CurPtr; // Another call to lex will return EOF again.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000219 return EOF;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000220 }
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000221}
222
223
Chris Lattnerdf986172009-01-02 07:01:27 +0000224lltok::Kind LLLexer::LexToken() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000225 TokStart = CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000226
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000227 int CurChar = getNextChar();
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000228 switch (CurChar) {
229 default:
230 // Handle letters: [a-zA-Z_]
231 if (isalpha(CurChar) || CurChar == '_')
232 return LexIdentifier();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000233
Chris Lattnerdf986172009-01-02 07:01:27 +0000234 return lltok::Error;
235 case EOF: return lltok::Eof;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000236 case 0:
237 case ' ':
238 case '\t':
239 case '\n':
240 case '\r':
241 // Ignore whitespace.
242 return LexToken();
243 case '+': return LexPositive();
244 case '@': return LexAt();
245 case '%': return LexPercent();
246 case '"': return LexQuote();
247 case '.':
248 if (const char *Ptr = isLabelTail(CurPtr)) {
249 CurPtr = Ptr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000250 StrVal.assign(TokStart, CurPtr-1);
251 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000252 }
253 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
254 CurPtr += 2;
Chris Lattnerdf986172009-01-02 07:01:27 +0000255 return lltok::dotdotdot;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000256 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000257 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000258 case '$':
259 if (const char *Ptr = isLabelTail(CurPtr)) {
260 CurPtr = Ptr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000261 StrVal.assign(TokStart, CurPtr-1);
262 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000263 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000264 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000265 case ';':
266 SkipLineComment();
267 return LexToken();
Nick Lewycky21cc4462009-04-04 07:22:01 +0000268 case '!': return lltok::Metadata;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000269 case '0': case '1': case '2': case '3': case '4':
270 case '5': case '6': case '7': case '8': case '9':
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000271 case '-':
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000272 return LexDigitOrNegative();
Chris Lattnerdf986172009-01-02 07:01:27 +0000273 case '=': return lltok::equal;
274 case '[': return lltok::lsquare;
275 case ']': return lltok::rsquare;
276 case '{': return lltok::lbrace;
277 case '}': return lltok::rbrace;
278 case '<': return lltok::less;
279 case '>': return lltok::greater;
280 case '(': return lltok::lparen;
281 case ')': return lltok::rparen;
282 case ',': return lltok::comma;
283 case '*': return lltok::star;
284 case '\\': return lltok::backslash;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000285 }
286}
287
288void LLLexer::SkipLineComment() {
289 while (1) {
290 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
291 return;
292 }
293}
294
295/// LexAt - Lex all tokens that start with an @ character:
Chris Lattnerdf986172009-01-02 07:01:27 +0000296/// GlobalVar @\"[^\"]*\"
297/// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
298/// GlobalVarID @[0-9]+
299lltok::Kind LLLexer::LexAt() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000300 // Handle AtStringConstant: @\"[^\"]*\"
301 if (CurPtr[0] == '"') {
302 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000303
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000304 while (1) {
305 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000306
307 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000308 Error("end of file in global variable name");
309 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000310 }
311 if (CurChar == '"') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000312 StrVal.assign(TokStart+2, CurPtr-1);
313 UnEscapeLexed(StrVal);
314 return lltok::GlobalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000315 }
316 }
317 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000318
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000319 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000320 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000321 CurPtr[0] == '.' || CurPtr[0] == '_') {
322 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000323 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000324 CurPtr[0] == '.' || CurPtr[0] == '_')
325 ++CurPtr;
326
Chris Lattnerdf986172009-01-02 07:01:27 +0000327 StrVal.assign(TokStart+1, CurPtr); // Skip @
328 return lltok::GlobalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000329 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000330
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000331 // Handle GlobalVarID: @[0-9]+
332 if (isdigit(CurPtr[0])) {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000333 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
334 /*empty*/;
335
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000336 uint64_t Val = atoull(TokStart+1, CurPtr);
337 if ((unsigned)Val != Val)
Chris Lattnerdf986172009-01-02 07:01:27 +0000338 Error("invalid value number (too large)!");
339 UIntVal = unsigned(Val);
340 return lltok::GlobalID;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000341 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000342
Chris Lattnerdf986172009-01-02 07:01:27 +0000343 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000344}
345
346
347/// LexPercent - Lex all tokens that start with a % character:
Chris Lattnerdf986172009-01-02 07:01:27 +0000348/// LocalVar ::= %\"[^\"]*\"
349/// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
350/// LocalVarID ::= %[0-9]+
351lltok::Kind LLLexer::LexPercent() {
352 // Handle LocalVarName: %\"[^\"]*\"
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000353 if (CurPtr[0] == '"') {
354 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000355
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000356 while (1) {
357 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000358
359 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000360 Error("end of file in string constant");
361 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000362 }
363 if (CurChar == '"') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000364 StrVal.assign(TokStart+2, CurPtr-1);
365 UnEscapeLexed(StrVal);
366 return lltok::LocalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000367 }
368 }
369 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000370
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000371 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000372 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000373 CurPtr[0] == '.' || CurPtr[0] == '_') {
374 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000375 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000376 CurPtr[0] == '.' || CurPtr[0] == '_')
377 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000378
Chris Lattnerdf986172009-01-02 07:01:27 +0000379 StrVal.assign(TokStart+1, CurPtr); // Skip %
380 return lltok::LocalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000381 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000382
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000383 // Handle LocalVarID: %[0-9]+
384 if (isdigit(CurPtr[0])) {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000385 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
386 /*empty*/;
387
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000388 uint64_t Val = atoull(TokStart+1, CurPtr);
389 if ((unsigned)Val != Val)
Chris Lattnerdf986172009-01-02 07:01:27 +0000390 Error("invalid value number (too large)!");
391 UIntVal = unsigned(Val);
392 return lltok::LocalVarID;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000393 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000394
Chris Lattnerdf986172009-01-02 07:01:27 +0000395 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000396}
397
398/// LexQuote - Lex all tokens that start with a " character:
399/// QuoteLabel "[^"]+":
400/// StringConstant "[^"]*"
Chris Lattnerdf986172009-01-02 07:01:27 +0000401lltok::Kind LLLexer::LexQuote() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000402 while (1) {
403 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000404
405 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000406 Error("end of file in quoted string");
407 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000408 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000409
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000410 if (CurChar != '"') continue;
411
412 if (CurPtr[0] != ':') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000413 StrVal.assign(TokStart+1, CurPtr-1);
414 UnEscapeLexed(StrVal);
415 return lltok::StringConstant;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000416 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000417
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000418 ++CurPtr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000419 StrVal.assign(TokStart+1, CurPtr-2);
420 UnEscapeLexed(StrVal);
421 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000422 }
423}
424
425static bool JustWhitespaceNewLine(const char *&Ptr) {
426 const char *ThisPtr = Ptr;
427 while (*ThisPtr == ' ' || *ThisPtr == '\t')
428 ++ThisPtr;
429 if (*ThisPtr == '\n' || *ThisPtr == '\r') {
430 Ptr = ThisPtr;
431 return true;
432 }
433 return false;
434}
435
436
437/// LexIdentifier: Handle several related productions:
438/// Label [-a-zA-Z$._0-9]+:
439/// IntegerType i[0-9]+
440/// Keyword sdiv, float, ...
441/// HexIntConstant [us]0x[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000442lltok::Kind LLLexer::LexIdentifier() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000443 const char *StartChar = CurPtr;
444 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
445 const char *KeywordEnd = 0;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000446
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000447 for (; isLabelChar(*CurPtr); ++CurPtr) {
448 // If we decide this is an integer, remember the end of the sequence.
449 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
450 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
451 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000452
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000453 // If we stopped due to a colon, this really is a label.
454 if (*CurPtr == ':') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000455 StrVal.assign(StartChar-1, CurPtr++);
456 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000457 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000458
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000459 // Otherwise, this wasn't a label. If this was valid as an integer type,
460 // return it.
461 if (IntEnd == 0) IntEnd = CurPtr;
462 if (IntEnd != StartChar) {
463 CurPtr = IntEnd;
464 uint64_t NumBits = atoull(StartChar, CurPtr);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000465 if (NumBits < IntegerType::MIN_INT_BITS ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000466 NumBits > IntegerType::MAX_INT_BITS) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000467 Error("bitwidth for integer type out of range!");
468 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000469 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000470 TyVal = IntegerType::get(NumBits);
471 return lltok::Type;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000472 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000473
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000474 // Otherwise, this was a letter sequence. See which keyword this is.
475 if (KeywordEnd == 0) KeywordEnd = CurPtr;
476 CurPtr = KeywordEnd;
477 --StartChar;
478 unsigned Len = CurPtr-StartChar;
Chris Lattnerdf986172009-01-02 07:01:27 +0000479#define KEYWORD(STR) \
480 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
481 return lltok::kw_##STR;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000482
Chris Lattnerdf986172009-01-02 07:01:27 +0000483 KEYWORD(begin); KEYWORD(end);
484 KEYWORD(true); KEYWORD(false);
485 KEYWORD(declare); KEYWORD(define);
486 KEYWORD(global); KEYWORD(constant);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000487
Rafael Espindolabb46f522009-01-15 20:18:42 +0000488 KEYWORD(private);
Chris Lattnerdf986172009-01-02 07:01:27 +0000489 KEYWORD(internal);
Chris Lattner266c7bb2009-04-13 05:44:34 +0000490 KEYWORD(available_externally);
Chris Lattnerdf986172009-01-02 07:01:27 +0000491 KEYWORD(linkonce);
Duncan Sands667d4b82009-03-07 15:45:40 +0000492 KEYWORD(linkonce_odr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000493 KEYWORD(weak);
Duncan Sands667d4b82009-03-07 15:45:40 +0000494 KEYWORD(weak_odr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000495 KEYWORD(appending);
496 KEYWORD(dllimport);
497 KEYWORD(dllexport);
498 KEYWORD(common);
499 KEYWORD(default);
500 KEYWORD(hidden);
501 KEYWORD(protected);
502 KEYWORD(extern_weak);
503 KEYWORD(external);
504 KEYWORD(thread_local);
505 KEYWORD(zeroinitializer);
506 KEYWORD(undef);
507 KEYWORD(null);
508 KEYWORD(to);
509 KEYWORD(tail);
510 KEYWORD(target);
511 KEYWORD(triple);
512 KEYWORD(deplibs);
513 KEYWORD(datalayout);
514 KEYWORD(volatile);
515 KEYWORD(align);
516 KEYWORD(addrspace);
517 KEYWORD(section);
518 KEYWORD(alias);
519 KEYWORD(module);
520 KEYWORD(asm);
521 KEYWORD(sideeffect);
522 KEYWORD(gc);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000523
Chris Lattnerdf986172009-01-02 07:01:27 +0000524 KEYWORD(ccc);
525 KEYWORD(fastcc);
526 KEYWORD(coldcc);
527 KEYWORD(x86_stdcallcc);
528 KEYWORD(x86_fastcallcc);
529 KEYWORD(cc);
530 KEYWORD(c);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000531
Chris Lattnerdf986172009-01-02 07:01:27 +0000532 KEYWORD(signext);
533 KEYWORD(zeroext);
534 KEYWORD(inreg);
535 KEYWORD(sret);
536 KEYWORD(nounwind);
537 KEYWORD(noreturn);
538 KEYWORD(noalias);
539 KEYWORD(nocapture);
540 KEYWORD(byval);
541 KEYWORD(nest);
542 KEYWORD(readnone);
543 KEYWORD(readonly);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000544
Chris Lattnerdf986172009-01-02 07:01:27 +0000545 KEYWORD(noinline);
546 KEYWORD(alwaysinline);
547 KEYWORD(optsize);
548 KEYWORD(ssp);
549 KEYWORD(sspreq);
Devang Pateld4980812008-09-02 20:52:40 +0000550
Chris Lattnerdf986172009-01-02 07:01:27 +0000551 KEYWORD(type);
552 KEYWORD(opaque);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000553
Chris Lattnerdf986172009-01-02 07:01:27 +0000554 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
555 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
556 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
557 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
Misha Brukman9ea40342009-01-02 22:46:48 +0000558
Chris Lattnerdf986172009-01-02 07:01:27 +0000559 KEYWORD(x);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000560#undef KEYWORD
561
562 // Keywords for types.
Chris Lattnerdf986172009-01-02 07:01:27 +0000563#define TYPEKEYWORD(STR, LLVMTY) \
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000564 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
Chris Lattnerdf986172009-01-02 07:01:27 +0000565 TyVal = LLVMTY; return lltok::Type; }
566 TYPEKEYWORD("void", Type::VoidTy);
567 TYPEKEYWORD("float", Type::FloatTy);
568 TYPEKEYWORD("double", Type::DoubleTy);
569 TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty);
570 TYPEKEYWORD("fp128", Type::FP128Ty);
571 TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty);
572 TYPEKEYWORD("label", Type::LabelTy);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000573 TYPEKEYWORD("metadata", Type::MetadataTy);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000574#undef TYPEKEYWORD
575
576 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
577 // to avoid conflicting with the sext/zext instructions, below.
578 if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
579 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
580 if (JustWhitespaceNewLine(CurPtr))
Chris Lattnerdf986172009-01-02 07:01:27 +0000581 return lltok::kw_signext;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000582 } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
583 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
584 if (JustWhitespaceNewLine(CurPtr))
Chris Lattnerdf986172009-01-02 07:01:27 +0000585 return lltok::kw_zeroext;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000586 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000587
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000588 // Keywords for instructions.
Chris Lattnerdf986172009-01-02 07:01:27 +0000589#define INSTKEYWORD(STR, Enum) \
590 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
591 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000592
Chris Lattnerdf986172009-01-02 07:01:27 +0000593 INSTKEYWORD(add, Add); INSTKEYWORD(sub, Sub); INSTKEYWORD(mul, Mul);
594 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
595 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
596 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
597 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
598 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
599 INSTKEYWORD(vicmp, VICmp); INSTKEYWORD(vfcmp, VFCmp);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000600
Chris Lattnerdf986172009-01-02 07:01:27 +0000601 INSTKEYWORD(phi, PHI);
602 INSTKEYWORD(call, Call);
603 INSTKEYWORD(trunc, Trunc);
604 INSTKEYWORD(zext, ZExt);
605 INSTKEYWORD(sext, SExt);
606 INSTKEYWORD(fptrunc, FPTrunc);
607 INSTKEYWORD(fpext, FPExt);
608 INSTKEYWORD(uitofp, UIToFP);
609 INSTKEYWORD(sitofp, SIToFP);
610 INSTKEYWORD(fptoui, FPToUI);
611 INSTKEYWORD(fptosi, FPToSI);
612 INSTKEYWORD(inttoptr, IntToPtr);
613 INSTKEYWORD(ptrtoint, PtrToInt);
614 INSTKEYWORD(bitcast, BitCast);
615 INSTKEYWORD(select, Select);
616 INSTKEYWORD(va_arg, VAArg);
617 INSTKEYWORD(ret, Ret);
618 INSTKEYWORD(br, Br);
619 INSTKEYWORD(switch, Switch);
620 INSTKEYWORD(invoke, Invoke);
621 INSTKEYWORD(unwind, Unwind);
622 INSTKEYWORD(unreachable, Unreachable);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000623
Chris Lattnerdf986172009-01-02 07:01:27 +0000624 INSTKEYWORD(malloc, Malloc);
625 INSTKEYWORD(alloca, Alloca);
626 INSTKEYWORD(free, Free);
627 INSTKEYWORD(load, Load);
628 INSTKEYWORD(store, Store);
629 INSTKEYWORD(getelementptr, GetElementPtr);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000630
Chris Lattnerdf986172009-01-02 07:01:27 +0000631 INSTKEYWORD(extractelement, ExtractElement);
632 INSTKEYWORD(insertelement, InsertElement);
633 INSTKEYWORD(shufflevector, ShuffleVector);
634 INSTKEYWORD(getresult, ExtractValue);
635 INSTKEYWORD(extractvalue, ExtractValue);
636 INSTKEYWORD(insertvalue, InsertValue);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000637#undef INSTKEYWORD
638
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000639 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
640 // the CFE to avoid forcing it to deal with 64-bit numbers.
641 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
642 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
643 int len = CurPtr-TokStart-3;
644 uint32_t bits = len * 4;
645 APInt Tmp(bits, TokStart+3, len, 16);
646 uint32_t activeBits = Tmp.getActiveBits();
647 if (activeBits > 0 && activeBits < bits)
648 Tmp.trunc(activeBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000649 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
650 return lltok::APSInt;
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 is "cc1234", return this as just "cc".
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000654 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
655 CurPtr = TokStart+2;
Chris Lattnerdf986172009-01-02 07:01:27 +0000656 return lltok::kw_cc;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000657 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000658
Chris Lattner4ce0df62007-11-18 18:43:24 +0000659 // If this starts with "call", return it as CALL. This is to support old
660 // broken .ll files. FIXME: remove this with LLVM 3.0.
661 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
662 CurPtr = TokStart+4;
Chris Lattnerdf986172009-01-02 07:01:27 +0000663 UIntVal = Instruction::Call;
664 return lltok::kw_call;
Chris Lattner4ce0df62007-11-18 18:43:24 +0000665 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000666
Chris Lattnerdf986172009-01-02 07:01:27 +0000667 // Finally, if this isn't known, return an error.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000668 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000669 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000670}
671
672
673/// Lex0x: Handle productions that start with 0x, knowing that it matches and
674/// that this is not a label:
675/// HexFPConstant 0x[0-9A-Fa-f]+
676/// HexFP80Constant 0xK[0-9A-Fa-f]+
677/// HexFP128Constant 0xL[0-9A-Fa-f]+
678/// HexPPC128Constant 0xM[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000679lltok::Kind LLLexer::Lex0x() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000680 CurPtr = TokStart + 2;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000681
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000682 char Kind;
683 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
684 Kind = *CurPtr++;
685 } else {
686 Kind = 'J';
687 }
688
689 if (!isxdigit(CurPtr[0])) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000690 // Bad token, return it as an error.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000691 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000692 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000693 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000694
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000695 while (isxdigit(CurPtr[0]))
696 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000697
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000698 if (Kind == 'J') {
699 // HexFPConstant - Floating point constant represented in IEEE format as a
700 // hexadecimal number for when exponential notation is not precise enough.
701 // Float and double only.
Chris Lattnerdf986172009-01-02 07:01:27 +0000702 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
703 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000704 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000705
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000706 uint64_t Pair[2];
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000707 switch (Kind) {
708 default: assert(0 && "Unknown kind!");
709 case 'K':
710 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000711 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
Chris Lattnerdf986172009-01-02 07:01:27 +0000712 APFloatVal = APFloat(APInt(80, 2, Pair));
713 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000714 case 'L':
715 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000716 HexToIntPair(TokStart+3, CurPtr, Pair);
Chris Lattnerdf986172009-01-02 07:01:27 +0000717 APFloatVal = APFloat(APInt(128, 2, Pair), true);
718 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000719 case 'M':
720 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000721 HexToIntPair(TokStart+3, CurPtr, Pair);
Chris Lattnerdf986172009-01-02 07:01:27 +0000722 APFloatVal = APFloat(APInt(128, 2, Pair));
723 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000724 }
725}
726
727/// LexIdentifier: Handle several related productions:
728/// Label [-a-zA-Z$._0-9]+:
729/// NInteger -[0-9]+
730/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
731/// PInteger [0-9]+
732/// HexFPConstant 0x[0-9A-Fa-f]+
733/// HexFP80Constant 0xK[0-9A-Fa-f]+
734/// HexFP128Constant 0xL[0-9A-Fa-f]+
735/// HexPPC128Constant 0xM[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000736lltok::Kind LLLexer::LexDigitOrNegative() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000737 // If the letter after the negative is a number, this is probably a label.
738 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
739 // Okay, this is not a number after the -, it's probably a label.
740 if (const char *End = isLabelTail(CurPtr)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000741 StrVal.assign(TokStart, End-1);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000742 CurPtr = End;
Chris Lattnerdf986172009-01-02 07:01:27 +0000743 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000744 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000745
Chris Lattnerdf986172009-01-02 07:01:27 +0000746 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000747 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000748
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000749 // At this point, it is either a label, int or fp constant.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000750
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000751 // Skip digits, we have at least one.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000752 for (; isdigit(CurPtr[0]); ++CurPtr)
753 /*empty*/;
754
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000755 // Check to see if this really is a label afterall, e.g. "-1:".
756 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
757 if (const char *End = isLabelTail(CurPtr)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000758 StrVal.assign(TokStart, End-1);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000759 CurPtr = End;
Chris Lattnerdf986172009-01-02 07:01:27 +0000760 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000761 }
762 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000763
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000764 // If the next character is a '.', then it is a fp value, otherwise its
765 // integer.
766 if (CurPtr[0] != '.') {
767 if (TokStart[0] == '0' && TokStart[1] == 'x')
768 return Lex0x();
769 unsigned Len = CurPtr-TokStart;
770 uint32_t numBits = ((Len * 64) / 19) + 2;
771 APInt Tmp(numBits, TokStart, Len, 10);
772 if (TokStart[0] == '-') {
773 uint32_t minBits = Tmp.getMinSignedBits();
774 if (minBits > 0 && minBits < numBits)
775 Tmp.trunc(minBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000776 APSIntVal = APSInt(Tmp, false);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000777 } else {
778 uint32_t activeBits = Tmp.getActiveBits();
779 if (activeBits > 0 && activeBits < numBits)
780 Tmp.trunc(activeBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000781 APSIntVal = APSInt(Tmp, true);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000782 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000783 return lltok::APSInt;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000784 }
785
786 ++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}
802
803/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattnerdf986172009-01-02 07:01:27 +0000804lltok::Kind LLLexer::LexPositive() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000805 // If the letter after the negative is a number, this is probably not a
806 // label.
807 if (!isdigit(CurPtr[0]))
Chris Lattnerdf986172009-01-02 07:01:27 +0000808 return lltok::Error;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000809
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000810 // Skip digits.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000811 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
812 /*empty*/;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000813
814 // At this point, we need a '.'.
815 if (CurPtr[0] != '.') {
816 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000817 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000818 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000819
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000820 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000821
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000822 // Skip over [0-9]*([eE][-+]?[0-9]+)?
823 while (isdigit(CurPtr[0])) ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000824
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000825 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000826 if (isdigit(CurPtr[1]) ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000827 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
828 CurPtr += 2;
829 while (isdigit(CurPtr[0])) ++CurPtr;
830 }
831 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000832
Chris Lattnerdf986172009-01-02 07:01:27 +0000833 APFloatVal = APFloat(atof(TokStart));
834 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000835}