blob: a2e5b76fd3af883f55f845165a72e3affbac4a1c [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"
Torok Edwinc25e7582009-07-11 20:10:48 +000018#include "llvm/Support/ErrorHandling.h"
Chris Lattner8e3a8e02007-11-18 08:46:26 +000019#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerd185f642007-12-08 19:03:30 +000020#include "llvm/Support/MathExtras.h"
Chris Lattner92bcb422009-07-02 22:46:18 +000021#include "llvm/Support/SourceMgr.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000022#include "llvm/Support/raw_ostream.h"
23#include "llvm/Assembly/Parser.h"
Chris Lattnera8961762009-01-02 07:18:46 +000024#include <cstdlib>
Misha Brukman5679d182009-01-02 22:49:28 +000025#include <cstring>
Chris Lattner8e3a8e02007-11-18 08:46:26 +000026using namespace llvm;
27
Chris Lattnerdf986172009-01-02 07:01:27 +000028bool LLLexer::Error(LocTy ErrorLoc, const std::string &Msg) const {
Chris Lattnereeb4a842009-07-02 23:08:13 +000029 ErrorInfo = SM.GetMessage(ErrorLoc, Msg, "error");
Chris Lattnerdf986172009-01-02 07:01:27 +000030 return true;
31}
32
Chris Lattner8e3a8e02007-11-18 08:46:26 +000033//===----------------------------------------------------------------------===//
34// Helper functions.
35//===----------------------------------------------------------------------===//
36
37// atoull - Convert an ascii string of decimal digits into the unsigned long
38// long representation... this does not have to do input error checking,
39// because we know that the input will be matched by a suitable regex...
40//
Chris Lattnerdf986172009-01-02 07:01:27 +000041uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000042 uint64_t Result = 0;
43 for (; Buffer != End; Buffer++) {
44 uint64_t OldRes = Result;
45 Result *= 10;
46 Result += *Buffer-'0';
47 if (Result < OldRes) { // Uh, oh, overflow detected!!!
Chris Lattnerdf986172009-01-02 07:01:27 +000048 Error("constant bigger than 64 bits detected!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +000049 return 0;
50 }
51 }
52 return Result;
53}
54
Chris Lattnerdf986172009-01-02 07:01:27 +000055uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000056 uint64_t Result = 0;
57 for (; Buffer != End; ++Buffer) {
58 uint64_t OldRes = Result;
59 Result *= 16;
60 char C = *Buffer;
61 if (C >= '0' && C <= '9')
62 Result += C-'0';
63 else if (C >= 'A' && C <= 'F')
64 Result += C-'A'+10;
65 else if (C >= 'a' && C <= 'f')
66 Result += C-'a'+10;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +000067
Chris Lattner8e3a8e02007-11-18 08:46:26 +000068 if (Result < OldRes) { // Uh, oh, overflow detected!!!
Chris Lattnerdf986172009-01-02 07:01:27 +000069 Error("constant bigger than 64 bits detected!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +000070 return 0;
71 }
72 }
73 return Result;
74}
75
Chris Lattnerdf986172009-01-02 07:01:27 +000076void LLLexer::HexToIntPair(const char *Buffer, const char *End,
77 uint64_t Pair[2]) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000078 Pair[0] = 0;
79 for (int i=0; i<16; i++, Buffer++) {
80 assert(Buffer != End);
81 Pair[0] *= 16;
82 char C = *Buffer;
83 if (C >= '0' && C <= '9')
84 Pair[0] += C-'0';
85 else if (C >= 'A' && C <= 'F')
86 Pair[0] += C-'A'+10;
87 else if (C >= 'a' && C <= 'f')
88 Pair[0] += C-'a'+10;
89 }
90 Pair[1] = 0;
91 for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
92 Pair[1] *= 16;
93 char C = *Buffer;
94 if (C >= '0' && C <= '9')
95 Pair[1] += C-'0';
96 else if (C >= 'A' && C <= 'F')
97 Pair[1] += C-'A'+10;
98 else if (C >= 'a' && C <= 'f')
99 Pair[1] += C-'a'+10;
100 }
Chris Lattnerd343c6b2007-11-18 18:25:18 +0000101 if (Buffer != End)
Chris Lattnerdf986172009-01-02 07:01:27 +0000102 Error("constant bigger than 128 bits detected!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000103}
104
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000105/// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
106/// { low64, high16 } as usual for an APInt.
107void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
108 uint64_t Pair[2]) {
109 Pair[1] = 0;
110 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
111 assert(Buffer != End);
112 Pair[1] *= 16;
113 char C = *Buffer;
114 if (C >= '0' && C <= '9')
115 Pair[1] += C-'0';
116 else if (C >= 'A' && C <= 'F')
117 Pair[1] += C-'A'+10;
118 else if (C >= 'a' && C <= 'f')
119 Pair[1] += C-'a'+10;
120 }
121 Pair[0] = 0;
122 for (int i=0; i<16; i++, Buffer++) {
123 Pair[0] *= 16;
124 char C = *Buffer;
125 if (C >= '0' && C <= '9')
126 Pair[0] += C-'0';
127 else if (C >= 'A' && C <= 'F')
128 Pair[0] += C-'A'+10;
129 else if (C >= 'a' && C <= 'f')
130 Pair[0] += C-'a'+10;
131 }
132 if (Buffer != End)
133 Error("constant bigger than 128 bits detected!");
134}
135
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000136// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
137// appropriate character.
138static void UnEscapeLexed(std::string &Str) {
139 if (Str.empty()) return;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000140
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000141 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
142 char *BOut = Buffer;
143 for (char *BIn = Buffer; BIn != EndBuffer; ) {
144 if (BIn[0] == '\\') {
145 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
146 *BOut++ = '\\'; // Two \ becomes one
147 BIn += 2;
148 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
149 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
150 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
151 BIn[3] = Tmp; // Restore character
152 BIn += 3; // Skip over handled chars
153 ++BOut;
154 } else {
155 *BOut++ = *BIn++;
156 }
157 } else {
158 *BOut++ = *BIn++;
159 }
160 }
161 Str.resize(BOut-Buffer);
162}
163
164/// isLabelChar - Return true for [-a-zA-Z$._0-9].
165static bool isLabelChar(char C) {
166 return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
167}
168
169
170/// isLabelTail - Return true if this pointer points to a valid end of a label.
171static const char *isLabelTail(const char *CurPtr) {
172 while (1) {
173 if (CurPtr[0] == ':') return CurPtr+1;
174 if (!isLabelChar(CurPtr[0])) return 0;
175 ++CurPtr;
176 }
177}
178
179
180
181//===----------------------------------------------------------------------===//
182// Lexer definition.
183//===----------------------------------------------------------------------===//
184
Owen Andersonff6c91e2009-07-07 18:44:11 +0000185LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err,
186 LLVMContext &C)
187 : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000188 CurPtr = CurBuf->getBufferStart();
189}
190
191std::string LLLexer::getFilename() const {
192 return CurBuf->getBufferIdentifier();
193}
194
195int LLLexer::getNextChar() {
196 char CurChar = *CurPtr++;
197 switch (CurChar) {
198 default: return (unsigned char)CurChar;
199 case 0:
200 // A nul character in the stream is either the end of the current buffer or
201 // a random nul in the file. Disambiguate that here.
202 if (CurPtr-1 != CurBuf->getBufferEnd())
203 return 0; // Just whitespace.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000204
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000205 // Otherwise, return end of file.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000206 --CurPtr; // Another call to lex will return EOF again.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000207 return EOF;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000208 }
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000209}
210
211
Chris Lattnerdf986172009-01-02 07:01:27 +0000212lltok::Kind LLLexer::LexToken() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000213 TokStart = CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000214
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000215 int CurChar = getNextChar();
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000216 switch (CurChar) {
217 default:
218 // Handle letters: [a-zA-Z_]
219 if (isalpha(CurChar) || CurChar == '_')
220 return LexIdentifier();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000221
Chris Lattnerdf986172009-01-02 07:01:27 +0000222 return lltok::Error;
223 case EOF: return lltok::Eof;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000224 case 0:
225 case ' ':
226 case '\t':
227 case '\n':
228 case '\r':
229 // Ignore whitespace.
230 return LexToken();
231 case '+': return LexPositive();
232 case '@': return LexAt();
233 case '%': return LexPercent();
234 case '"': return LexQuote();
235 case '.':
236 if (const char *Ptr = isLabelTail(CurPtr)) {
237 CurPtr = Ptr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000238 StrVal.assign(TokStart, CurPtr-1);
239 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000240 }
241 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
242 CurPtr += 2;
Chris Lattnerdf986172009-01-02 07:01:27 +0000243 return lltok::dotdotdot;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000244 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000245 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000246 case '$':
247 if (const char *Ptr = isLabelTail(CurPtr)) {
248 CurPtr = Ptr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000249 StrVal.assign(TokStart, CurPtr-1);
250 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000251 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000252 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000253 case ';':
254 SkipLineComment();
255 return LexToken();
Nick Lewycky21cc4462009-04-04 07:22:01 +0000256 case '!': return lltok::Metadata;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000257 case '0': case '1': case '2': case '3': case '4':
258 case '5': case '6': case '7': case '8': case '9':
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000259 case '-':
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000260 return LexDigitOrNegative();
Chris Lattnerdf986172009-01-02 07:01:27 +0000261 case '=': return lltok::equal;
262 case '[': return lltok::lsquare;
263 case ']': return lltok::rsquare;
264 case '{': return lltok::lbrace;
265 case '}': return lltok::rbrace;
266 case '<': return lltok::less;
267 case '>': return lltok::greater;
268 case '(': return lltok::lparen;
269 case ')': return lltok::rparen;
270 case ',': return lltok::comma;
271 case '*': return lltok::star;
272 case '\\': return lltok::backslash;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000273 }
274}
275
276void LLLexer::SkipLineComment() {
277 while (1) {
278 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
279 return;
280 }
281}
282
283/// LexAt - Lex all tokens that start with an @ character:
Chris Lattnerdf986172009-01-02 07:01:27 +0000284/// GlobalVar @\"[^\"]*\"
285/// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
286/// GlobalVarID @[0-9]+
287lltok::Kind LLLexer::LexAt() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000288 // Handle AtStringConstant: @\"[^\"]*\"
289 if (CurPtr[0] == '"') {
290 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000291
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000292 while (1) {
293 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000294
295 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000296 Error("end of file in global variable name");
297 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000298 }
299 if (CurChar == '"') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000300 StrVal.assign(TokStart+2, CurPtr-1);
301 UnEscapeLexed(StrVal);
302 return lltok::GlobalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000303 }
304 }
305 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000306
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000307 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000308 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000309 CurPtr[0] == '.' || CurPtr[0] == '_') {
310 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000311 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000312 CurPtr[0] == '.' || CurPtr[0] == '_')
313 ++CurPtr;
314
Chris Lattnerdf986172009-01-02 07:01:27 +0000315 StrVal.assign(TokStart+1, CurPtr); // Skip @
316 return lltok::GlobalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000317 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000318
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000319 // Handle GlobalVarID: @[0-9]+
320 if (isdigit(CurPtr[0])) {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000321 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
322 /*empty*/;
323
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000324 uint64_t Val = atoull(TokStart+1, CurPtr);
325 if ((unsigned)Val != Val)
Chris Lattnerdf986172009-01-02 07:01:27 +0000326 Error("invalid value number (too large)!");
327 UIntVal = unsigned(Val);
328 return lltok::GlobalID;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000329 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000330
Chris Lattnerdf986172009-01-02 07:01:27 +0000331 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000332}
333
334
335/// LexPercent - Lex all tokens that start with a % character:
Chris Lattnerdf986172009-01-02 07:01:27 +0000336/// LocalVar ::= %\"[^\"]*\"
337/// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
338/// LocalVarID ::= %[0-9]+
339lltok::Kind LLLexer::LexPercent() {
340 // Handle LocalVarName: %\"[^\"]*\"
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000341 if (CurPtr[0] == '"') {
342 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000343
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000344 while (1) {
345 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000346
347 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000348 Error("end of file in string constant");
349 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000350 }
351 if (CurChar == '"') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000352 StrVal.assign(TokStart+2, CurPtr-1);
353 UnEscapeLexed(StrVal);
354 return lltok::LocalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000355 }
356 }
357 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000358
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000359 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000360 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000361 CurPtr[0] == '.' || CurPtr[0] == '_') {
362 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000363 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000364 CurPtr[0] == '.' || CurPtr[0] == '_')
365 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000366
Chris Lattnerdf986172009-01-02 07:01:27 +0000367 StrVal.assign(TokStart+1, CurPtr); // Skip %
368 return lltok::LocalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000369 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000370
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000371 // Handle LocalVarID: %[0-9]+
372 if (isdigit(CurPtr[0])) {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000373 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
374 /*empty*/;
375
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000376 uint64_t Val = atoull(TokStart+1, CurPtr);
377 if ((unsigned)Val != Val)
Chris Lattnerdf986172009-01-02 07:01:27 +0000378 Error("invalid value number (too large)!");
379 UIntVal = unsigned(Val);
380 return lltok::LocalVarID;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000381 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000382
Chris Lattnerdf986172009-01-02 07:01:27 +0000383 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000384}
385
386/// LexQuote - Lex all tokens that start with a " character:
387/// QuoteLabel "[^"]+":
388/// StringConstant "[^"]*"
Chris Lattnerdf986172009-01-02 07:01:27 +0000389lltok::Kind LLLexer::LexQuote() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000390 while (1) {
391 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000392
393 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000394 Error("end of file in quoted string");
395 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000396 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000397
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000398 if (CurChar != '"') continue;
399
400 if (CurPtr[0] != ':') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000401 StrVal.assign(TokStart+1, CurPtr-1);
402 UnEscapeLexed(StrVal);
403 return lltok::StringConstant;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000404 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000405
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000406 ++CurPtr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000407 StrVal.assign(TokStart+1, CurPtr-2);
408 UnEscapeLexed(StrVal);
409 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000410 }
411}
412
413static bool JustWhitespaceNewLine(const char *&Ptr) {
414 const char *ThisPtr = Ptr;
415 while (*ThisPtr == ' ' || *ThisPtr == '\t')
416 ++ThisPtr;
417 if (*ThisPtr == '\n' || *ThisPtr == '\r') {
418 Ptr = ThisPtr;
419 return true;
420 }
421 return false;
422}
423
424
425/// LexIdentifier: Handle several related productions:
426/// Label [-a-zA-Z$._0-9]+:
427/// IntegerType i[0-9]+
428/// Keyword sdiv, float, ...
429/// HexIntConstant [us]0x[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000430lltok::Kind LLLexer::LexIdentifier() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000431 const char *StartChar = CurPtr;
432 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
433 const char *KeywordEnd = 0;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000434
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000435 for (; isLabelChar(*CurPtr); ++CurPtr) {
436 // If we decide this is an integer, remember the end of the sequence.
437 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
438 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
439 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000440
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000441 // If we stopped due to a colon, this really is a label.
442 if (*CurPtr == ':') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000443 StrVal.assign(StartChar-1, CurPtr++);
444 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000445 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000446
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000447 // Otherwise, this wasn't a label. If this was valid as an integer type,
448 // return it.
449 if (IntEnd == 0) IntEnd = CurPtr;
450 if (IntEnd != StartChar) {
451 CurPtr = IntEnd;
452 uint64_t NumBits = atoull(StartChar, CurPtr);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000453 if (NumBits < IntegerType::MIN_INT_BITS ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000454 NumBits > IntegerType::MAX_INT_BITS) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000455 Error("bitwidth for integer type out of range!");
456 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000457 }
Owen Andersonff6c91e2009-07-07 18:44:11 +0000458 TyVal = Context.getIntegerType(NumBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000459 return lltok::Type;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000460 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000461
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000462 // Otherwise, this was a letter sequence. See which keyword this is.
463 if (KeywordEnd == 0) KeywordEnd = CurPtr;
464 CurPtr = KeywordEnd;
465 --StartChar;
466 unsigned Len = CurPtr-StartChar;
Chris Lattnerdf986172009-01-02 07:01:27 +0000467#define KEYWORD(STR) \
468 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
469 return lltok::kw_##STR;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000470
Chris Lattnerdf986172009-01-02 07:01:27 +0000471 KEYWORD(begin); KEYWORD(end);
472 KEYWORD(true); KEYWORD(false);
473 KEYWORD(declare); KEYWORD(define);
474 KEYWORD(global); KEYWORD(constant);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000475
Rafael Espindolabb46f522009-01-15 20:18:42 +0000476 KEYWORD(private);
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000477 KEYWORD(linker_private);
Chris Lattnerdf986172009-01-02 07:01:27 +0000478 KEYWORD(internal);
Chris Lattner266c7bb2009-04-13 05:44:34 +0000479 KEYWORD(available_externally);
Chris Lattnerdf986172009-01-02 07:01:27 +0000480 KEYWORD(linkonce);
Duncan Sands667d4b82009-03-07 15:45:40 +0000481 KEYWORD(linkonce_odr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000482 KEYWORD(weak);
Duncan Sands667d4b82009-03-07 15:45:40 +0000483 KEYWORD(weak_odr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000484 KEYWORD(appending);
485 KEYWORD(dllimport);
486 KEYWORD(dllexport);
487 KEYWORD(common);
488 KEYWORD(default);
489 KEYWORD(hidden);
490 KEYWORD(protected);
491 KEYWORD(extern_weak);
492 KEYWORD(external);
493 KEYWORD(thread_local);
494 KEYWORD(zeroinitializer);
495 KEYWORD(undef);
496 KEYWORD(null);
497 KEYWORD(to);
498 KEYWORD(tail);
499 KEYWORD(target);
500 KEYWORD(triple);
501 KEYWORD(deplibs);
502 KEYWORD(datalayout);
503 KEYWORD(volatile);
504 KEYWORD(align);
505 KEYWORD(addrspace);
506 KEYWORD(section);
507 KEYWORD(alias);
508 KEYWORD(module);
509 KEYWORD(asm);
510 KEYWORD(sideeffect);
511 KEYWORD(gc);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000512
Chris Lattnerdf986172009-01-02 07:01:27 +0000513 KEYWORD(ccc);
514 KEYWORD(fastcc);
515 KEYWORD(coldcc);
516 KEYWORD(x86_stdcallcc);
517 KEYWORD(x86_fastcallcc);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000518 KEYWORD(arm_apcscc);
519 KEYWORD(arm_aapcscc);
520 KEYWORD(arm_aapcs_vfpcc);
521
Chris Lattnerdf986172009-01-02 07:01:27 +0000522 KEYWORD(cc);
523 KEYWORD(c);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000524
Chris Lattnerdf986172009-01-02 07:01:27 +0000525 KEYWORD(signext);
526 KEYWORD(zeroext);
527 KEYWORD(inreg);
528 KEYWORD(sret);
529 KEYWORD(nounwind);
530 KEYWORD(noreturn);
531 KEYWORD(noalias);
532 KEYWORD(nocapture);
533 KEYWORD(byval);
534 KEYWORD(nest);
535 KEYWORD(readnone);
536 KEYWORD(readonly);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000537
Chris Lattnerdf986172009-01-02 07:01:27 +0000538 KEYWORD(noinline);
539 KEYWORD(alwaysinline);
540 KEYWORD(optsize);
541 KEYWORD(ssp);
542 KEYWORD(sspreq);
Devang Pateld18e31a2009-06-04 22:05:33 +0000543 KEYWORD(noredzone);
Devang Patel578efa92009-06-05 21:57:13 +0000544 KEYWORD(noimplicitfloat);
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000545 KEYWORD(naked);
Devang Pateld4980812008-09-02 20:52:40 +0000546
Chris Lattnerdf986172009-01-02 07:01:27 +0000547 KEYWORD(type);
548 KEYWORD(opaque);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000549
Chris Lattnerdf986172009-01-02 07:01:27 +0000550 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
551 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
552 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
553 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
Misha Brukman9ea40342009-01-02 22:46:48 +0000554
Chris Lattnerdf986172009-01-02 07:01:27 +0000555 KEYWORD(x);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000556#undef KEYWORD
557
558 // Keywords for types.
Chris Lattnerdf986172009-01-02 07:01:27 +0000559#define TYPEKEYWORD(STR, LLVMTY) \
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000560 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
Chris Lattnerdf986172009-01-02 07:01:27 +0000561 TyVal = LLVMTY; return lltok::Type; }
562 TYPEKEYWORD("void", Type::VoidTy);
563 TYPEKEYWORD("float", Type::FloatTy);
564 TYPEKEYWORD("double", Type::DoubleTy);
565 TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty);
566 TYPEKEYWORD("fp128", Type::FP128Ty);
567 TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty);
568 TYPEKEYWORD("label", Type::LabelTy);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000569 TYPEKEYWORD("metadata", Type::MetadataTy);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000570#undef TYPEKEYWORD
571
572 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
573 // to avoid conflicting with the sext/zext instructions, below.
574 if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
575 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
576 if (JustWhitespaceNewLine(CurPtr))
Chris Lattnerdf986172009-01-02 07:01:27 +0000577 return lltok::kw_signext;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000578 } else if (Len == 4 && !memcmp(StartChar, "zext", 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_zeroext;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000582 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000583
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000584 // Keywords for instructions.
Chris Lattnerdf986172009-01-02 07:01:27 +0000585#define INSTKEYWORD(STR, Enum) \
586 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
587 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000588
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000589 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
590 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
591 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
Chris Lattnerdf986172009-01-02 07:01:27 +0000592 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
593 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
594 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
595 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
596 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000597
Chris Lattnerdf986172009-01-02 07:01:27 +0000598 INSTKEYWORD(phi, PHI);
599 INSTKEYWORD(call, Call);
600 INSTKEYWORD(trunc, Trunc);
601 INSTKEYWORD(zext, ZExt);
602 INSTKEYWORD(sext, SExt);
603 INSTKEYWORD(fptrunc, FPTrunc);
604 INSTKEYWORD(fpext, FPExt);
605 INSTKEYWORD(uitofp, UIToFP);
606 INSTKEYWORD(sitofp, SIToFP);
607 INSTKEYWORD(fptoui, FPToUI);
608 INSTKEYWORD(fptosi, FPToSI);
609 INSTKEYWORD(inttoptr, IntToPtr);
610 INSTKEYWORD(ptrtoint, PtrToInt);
611 INSTKEYWORD(bitcast, BitCast);
612 INSTKEYWORD(select, Select);
613 INSTKEYWORD(va_arg, VAArg);
614 INSTKEYWORD(ret, Ret);
615 INSTKEYWORD(br, Br);
616 INSTKEYWORD(switch, Switch);
617 INSTKEYWORD(invoke, Invoke);
618 INSTKEYWORD(unwind, Unwind);
619 INSTKEYWORD(unreachable, Unreachable);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000620
Chris Lattnerdf986172009-01-02 07:01:27 +0000621 INSTKEYWORD(malloc, Malloc);
622 INSTKEYWORD(alloca, Alloca);
623 INSTKEYWORD(free, Free);
624 INSTKEYWORD(load, Load);
625 INSTKEYWORD(store, Store);
626 INSTKEYWORD(getelementptr, GetElementPtr);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000627
Chris Lattnerdf986172009-01-02 07:01:27 +0000628 INSTKEYWORD(extractelement, ExtractElement);
629 INSTKEYWORD(insertelement, InsertElement);
630 INSTKEYWORD(shufflevector, ShuffleVector);
631 INSTKEYWORD(getresult, ExtractValue);
632 INSTKEYWORD(extractvalue, ExtractValue);
633 INSTKEYWORD(insertvalue, InsertValue);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000634#undef INSTKEYWORD
635
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000636 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
637 // the CFE to avoid forcing it to deal with 64-bit numbers.
638 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
639 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
640 int len = CurPtr-TokStart-3;
641 uint32_t bits = len * 4;
642 APInt Tmp(bits, TokStart+3, len, 16);
643 uint32_t activeBits = Tmp.getActiveBits();
644 if (activeBits > 0 && activeBits < bits)
645 Tmp.trunc(activeBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000646 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
647 return lltok::APSInt;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000648 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000649
Chris Lattner4ce0df62007-11-18 18:43:24 +0000650 // If this is "cc1234", return this as just "cc".
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000651 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
652 CurPtr = TokStart+2;
Chris Lattnerdf986172009-01-02 07:01:27 +0000653 return lltok::kw_cc;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000654 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000655
Chris Lattner4ce0df62007-11-18 18:43:24 +0000656 // If this starts with "call", return it as CALL. This is to support old
657 // broken .ll files. FIXME: remove this with LLVM 3.0.
658 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
659 CurPtr = TokStart+4;
Chris Lattnerdf986172009-01-02 07:01:27 +0000660 UIntVal = Instruction::Call;
661 return lltok::kw_call;
Chris Lattner4ce0df62007-11-18 18:43:24 +0000662 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000663
Chris Lattnerdf986172009-01-02 07:01:27 +0000664 // Finally, if this isn't known, return an error.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000665 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000666 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000667}
668
669
670/// Lex0x: Handle productions that start with 0x, knowing that it matches and
671/// that this is not a label:
672/// HexFPConstant 0x[0-9A-Fa-f]+
673/// HexFP80Constant 0xK[0-9A-Fa-f]+
674/// HexFP128Constant 0xL[0-9A-Fa-f]+
675/// HexPPC128Constant 0xM[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000676lltok::Kind LLLexer::Lex0x() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000677 CurPtr = TokStart + 2;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000678
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000679 char Kind;
680 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
681 Kind = *CurPtr++;
682 } else {
683 Kind = 'J';
684 }
685
686 if (!isxdigit(CurPtr[0])) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000687 // Bad token, return it as an error.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000688 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000689 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000690 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000691
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000692 while (isxdigit(CurPtr[0]))
693 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000694
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000695 if (Kind == 'J') {
696 // HexFPConstant - Floating point constant represented in IEEE format as a
697 // hexadecimal number for when exponential notation is not precise enough.
698 // Float and double only.
Chris Lattnerdf986172009-01-02 07:01:27 +0000699 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
700 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000701 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000702
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000703 uint64_t Pair[2];
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000704 switch (Kind) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000705 default: llvm_unreachable("Unknown kind!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000706 case 'K':
707 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000708 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
Chris Lattnerdf986172009-01-02 07:01:27 +0000709 APFloatVal = APFloat(APInt(80, 2, Pair));
710 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000711 case 'L':
712 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000713 HexToIntPair(TokStart+3, CurPtr, Pair);
Chris Lattnerdf986172009-01-02 07:01:27 +0000714 APFloatVal = APFloat(APInt(128, 2, Pair), true);
715 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000716 case 'M':
717 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000718 HexToIntPair(TokStart+3, CurPtr, Pair);
Chris Lattnerdf986172009-01-02 07:01:27 +0000719 APFloatVal = APFloat(APInt(128, 2, Pair));
720 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000721 }
722}
723
724/// LexIdentifier: Handle several related productions:
725/// Label [-a-zA-Z$._0-9]+:
726/// NInteger -[0-9]+
727/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
728/// PInteger [0-9]+
729/// HexFPConstant 0x[0-9A-Fa-f]+
730/// HexFP80Constant 0xK[0-9A-Fa-f]+
731/// HexFP128Constant 0xL[0-9A-Fa-f]+
732/// HexPPC128Constant 0xM[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000733lltok::Kind LLLexer::LexDigitOrNegative() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000734 // If the letter after the negative is a number, this is probably a label.
735 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
736 // Okay, this is not a number after the -, it's probably a label.
737 if (const char *End = isLabelTail(CurPtr)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000738 StrVal.assign(TokStart, End-1);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000739 CurPtr = End;
Chris Lattnerdf986172009-01-02 07:01:27 +0000740 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000741 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000742
Chris Lattnerdf986172009-01-02 07:01:27 +0000743 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000744 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000745
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000746 // At this point, it is either a label, int or fp constant.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000747
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000748 // Skip digits, we have at least one.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000749 for (; isdigit(CurPtr[0]); ++CurPtr)
750 /*empty*/;
751
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000752 // Check to see if this really is a label afterall, e.g. "-1:".
753 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
754 if (const char *End = isLabelTail(CurPtr)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000755 StrVal.assign(TokStart, End-1);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000756 CurPtr = End;
Chris Lattnerdf986172009-01-02 07:01:27 +0000757 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000758 }
759 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000760
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000761 // If the next character is a '.', then it is a fp value, otherwise its
762 // integer.
763 if (CurPtr[0] != '.') {
764 if (TokStart[0] == '0' && TokStart[1] == 'x')
765 return Lex0x();
766 unsigned Len = CurPtr-TokStart;
767 uint32_t numBits = ((Len * 64) / 19) + 2;
768 APInt Tmp(numBits, TokStart, Len, 10);
769 if (TokStart[0] == '-') {
770 uint32_t minBits = Tmp.getMinSignedBits();
771 if (minBits > 0 && minBits < numBits)
772 Tmp.trunc(minBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000773 APSIntVal = APSInt(Tmp, false);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000774 } else {
775 uint32_t activeBits = Tmp.getActiveBits();
776 if (activeBits > 0 && activeBits < numBits)
777 Tmp.trunc(activeBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000778 APSIntVal = APSInt(Tmp, true);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000779 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000780 return lltok::APSInt;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000781 }
782
783 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000784
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000785 // Skip over [0-9]*([eE][-+]?[0-9]+)?
786 while (isdigit(CurPtr[0])) ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000787
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000788 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000789 if (isdigit(CurPtr[1]) ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000790 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
791 CurPtr += 2;
792 while (isdigit(CurPtr[0])) ++CurPtr;
793 }
794 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000795
Chris Lattnerdf986172009-01-02 07:01:27 +0000796 APFloatVal = APFloat(atof(TokStart));
797 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000798}
799
800/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattnerdf986172009-01-02 07:01:27 +0000801lltok::Kind LLLexer::LexPositive() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000802 // If the letter after the negative is a number, this is probably not a
803 // label.
804 if (!isdigit(CurPtr[0]))
Chris Lattnerdf986172009-01-02 07:01:27 +0000805 return lltok::Error;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000806
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000807 // Skip digits.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000808 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
809 /*empty*/;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000810
811 // At this point, we need a '.'.
812 if (CurPtr[0] != '.') {
813 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000814 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000815 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000816
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000817 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000818
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000819 // Skip over [0-9]*([eE][-+]?[0-9]+)?
820 while (isdigit(CurPtr[0])) ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000821
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000822 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000823 if (isdigit(CurPtr[1]) ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000824 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
825 CurPtr += 2;
826 while (isdigit(CurPtr[0])) ++CurPtr;
827 }
828 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000829
Chris Lattnerdf986172009-01-02 07:01:27 +0000830 APFloatVal = APFloat(atof(TokStart));
831 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000832}