blob: a363a653b21deffe2680dd34d0ee32d0d196ade4 [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"
Benjamin Kramerd1e17032010-09-27 17:42:11 +000018#include "llvm/ADT/Twine.h"
19#include "llvm/Assembly/Parser.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000020#include "llvm/Support/ErrorHandling.h"
Chris Lattner8e3a8e02007-11-18 08:46:26 +000021#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerd185f642007-12-08 19:03:30 +000022#include "llvm/Support/MathExtras.h"
Chris Lattner92bcb422009-07-02 22:46:18 +000023#include "llvm/Support/SourceMgr.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000024#include "llvm/Support/raw_ostream.h"
Nick Lewycky476b2422010-12-19 20:43:38 +000025#include <cctype>
Duncan Sandsbcef7df2009-08-24 10:34:41 +000026#include <cstdio>
Chris Lattnera8961762009-01-02 07:18:46 +000027#include <cstdlib>
Misha Brukman5679d182009-01-02 22:49:28 +000028#include <cstring>
Chris Lattner8e3a8e02007-11-18 08:46:26 +000029using namespace llvm;
30
Benjamin Kramerd1e17032010-09-27 17:42:11 +000031bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
Chris Lattnereeb4a842009-07-02 23:08:13 +000032 ErrorInfo = SM.GetMessage(ErrorLoc, Msg, "error");
Chris Lattnerdf986172009-01-02 07:01:27 +000033 return true;
34}
35
Chris Lattner8e3a8e02007-11-18 08:46:26 +000036//===----------------------------------------------------------------------===//
37// Helper functions.
38//===----------------------------------------------------------------------===//
39
40// atoull - Convert an ascii string of decimal digits into the unsigned long
41// long representation... this does not have to do input error checking,
42// because we know that the input will be matched by a suitable regex...
43//
Chris Lattnerdf986172009-01-02 07:01:27 +000044uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000045 uint64_t Result = 0;
46 for (; Buffer != End; Buffer++) {
47 uint64_t OldRes = Result;
48 Result *= 10;
49 Result += *Buffer-'0';
50 if (Result < OldRes) { // Uh, oh, overflow detected!!!
Chris Lattnerdf986172009-01-02 07:01:27 +000051 Error("constant bigger than 64 bits detected!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +000052 return 0;
53 }
54 }
55 return Result;
56}
57
Chris Lattnerdf986172009-01-02 07:01:27 +000058uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000059 uint64_t Result = 0;
60 for (; Buffer != End; ++Buffer) {
61 uint64_t OldRes = Result;
62 Result *= 16;
63 char C = *Buffer;
64 if (C >= '0' && C <= '9')
65 Result += C-'0';
66 else if (C >= 'A' && C <= 'F')
67 Result += C-'A'+10;
68 else if (C >= 'a' && C <= 'f')
69 Result += C-'a'+10;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +000070
Chris Lattner8e3a8e02007-11-18 08:46:26 +000071 if (Result < OldRes) { // Uh, oh, overflow detected!!!
Chris Lattnerdf986172009-01-02 07:01:27 +000072 Error("constant bigger than 64 bits detected!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +000073 return 0;
74 }
75 }
76 return Result;
77}
78
Chris Lattnerdf986172009-01-02 07:01:27 +000079void LLLexer::HexToIntPair(const char *Buffer, const char *End,
80 uint64_t Pair[2]) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000081 Pair[0] = 0;
82 for (int i=0; i<16; i++, Buffer++) {
83 assert(Buffer != End);
84 Pair[0] *= 16;
85 char C = *Buffer;
86 if (C >= '0' && C <= '9')
87 Pair[0] += C-'0';
88 else if (C >= 'A' && C <= 'F')
89 Pair[0] += C-'A'+10;
90 else if (C >= 'a' && C <= 'f')
91 Pair[0] += C-'a'+10;
92 }
93 Pair[1] = 0;
94 for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
95 Pair[1] *= 16;
96 char C = *Buffer;
97 if (C >= '0' && C <= '9')
98 Pair[1] += C-'0';
99 else if (C >= 'A' && C <= 'F')
100 Pair[1] += C-'A'+10;
101 else if (C >= 'a' && C <= 'f')
102 Pair[1] += C-'a'+10;
103 }
Chris Lattnerd343c6b2007-11-18 18:25:18 +0000104 if (Buffer != End)
Chris Lattnerdf986172009-01-02 07:01:27 +0000105 Error("constant bigger than 128 bits detected!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000106}
107
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000108/// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
109/// { low64, high16 } as usual for an APInt.
110void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
111 uint64_t Pair[2]) {
112 Pair[1] = 0;
113 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
114 assert(Buffer != End);
115 Pair[1] *= 16;
116 char C = *Buffer;
117 if (C >= '0' && C <= '9')
118 Pair[1] += C-'0';
119 else if (C >= 'A' && C <= 'F')
120 Pair[1] += C-'A'+10;
121 else if (C >= 'a' && C <= 'f')
122 Pair[1] += C-'a'+10;
123 }
124 Pair[0] = 0;
125 for (int i=0; i<16; i++, Buffer++) {
126 Pair[0] *= 16;
127 char C = *Buffer;
128 if (C >= '0' && C <= '9')
129 Pair[0] += C-'0';
130 else if (C >= 'A' && C <= 'F')
131 Pair[0] += C-'A'+10;
132 else if (C >= 'a' && C <= 'f')
133 Pair[0] += C-'a'+10;
134 }
135 if (Buffer != End)
136 Error("constant bigger than 128 bits detected!");
137}
138
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000139// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
140// appropriate character.
141static void UnEscapeLexed(std::string &Str) {
142 if (Str.empty()) return;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000143
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000144 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
145 char *BOut = Buffer;
146 for (char *BIn = Buffer; BIn != EndBuffer; ) {
147 if (BIn[0] == '\\') {
148 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
149 *BOut++ = '\\'; // Two \ becomes one
150 BIn += 2;
151 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
152 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
153 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
154 BIn[3] = Tmp; // Restore character
155 BIn += 3; // Skip over handled chars
156 ++BOut;
157 } else {
158 *BOut++ = *BIn++;
159 }
160 } else {
161 *BOut++ = *BIn++;
162 }
163 }
164 Str.resize(BOut-Buffer);
165}
166
167/// isLabelChar - Return true for [-a-zA-Z$._0-9].
168static bool isLabelChar(char C) {
169 return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
170}
171
172
173/// isLabelTail - Return true if this pointer points to a valid end of a label.
174static const char *isLabelTail(const char *CurPtr) {
175 while (1) {
176 if (CurPtr[0] == ':') return CurPtr+1;
177 if (!isLabelChar(CurPtr[0])) return 0;
178 ++CurPtr;
179 }
180}
181
182
183
184//===----------------------------------------------------------------------===//
185// Lexer definition.
186//===----------------------------------------------------------------------===//
187
Owen Andersonff6c91e2009-07-07 18:44:11 +0000188LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err,
189 LLVMContext &C)
190 : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000191 CurPtr = CurBuf->getBufferStart();
192}
193
194std::string LLLexer::getFilename() const {
195 return CurBuf->getBufferIdentifier();
196}
197
198int LLLexer::getNextChar() {
199 char CurChar = *CurPtr++;
200 switch (CurChar) {
201 default: return (unsigned char)CurChar;
202 case 0:
203 // A nul character in the stream is either the end of the current buffer or
204 // a random nul in the file. Disambiguate that here.
205 if (CurPtr-1 != CurBuf->getBufferEnd())
206 return 0; // Just whitespace.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000207
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000208 // Otherwise, return end of file.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000209 --CurPtr; // Another call to lex will return EOF again.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000210 return EOF;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000211 }
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000212}
213
214
Chris Lattnerdf986172009-01-02 07:01:27 +0000215lltok::Kind LLLexer::LexToken() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000216 TokStart = CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000217
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000218 int CurChar = getNextChar();
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000219 switch (CurChar) {
220 default:
221 // Handle letters: [a-zA-Z_]
222 if (isalpha(CurChar) || CurChar == '_')
223 return LexIdentifier();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000224
Chris Lattnerdf986172009-01-02 07:01:27 +0000225 return lltok::Error;
226 case EOF: return lltok::Eof;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000227 case 0:
228 case ' ':
229 case '\t':
230 case '\n':
231 case '\r':
232 // Ignore whitespace.
233 return LexToken();
234 case '+': return LexPositive();
235 case '@': return LexAt();
236 case '%': return LexPercent();
237 case '"': return LexQuote();
238 case '.':
239 if (const char *Ptr = isLabelTail(CurPtr)) {
240 CurPtr = Ptr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000241 StrVal.assign(TokStart, CurPtr-1);
242 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000243 }
244 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
245 CurPtr += 2;
Chris Lattnerdf986172009-01-02 07:01:27 +0000246 return lltok::dotdotdot;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000247 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000248 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000249 case '$':
250 if (const char *Ptr = isLabelTail(CurPtr)) {
251 CurPtr = Ptr;
Chris Lattnerdf986172009-01-02 07:01:27 +0000252 StrVal.assign(TokStart, CurPtr-1);
253 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000254 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000255 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000256 case ';':
257 SkipLineComment();
258 return LexToken();
Chris Lattnere434d272009-12-30 04:56:59 +0000259 case '!': return LexExclaim();
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000260 case '0': case '1': case '2': case '3': case '4':
261 case '5': case '6': case '7': case '8': case '9':
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000262 case '-':
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000263 return LexDigitOrNegative();
Chris Lattnerdf986172009-01-02 07:01:27 +0000264 case '=': return lltok::equal;
265 case '[': return lltok::lsquare;
266 case ']': return lltok::rsquare;
267 case '{': return lltok::lbrace;
268 case '}': return lltok::rbrace;
269 case '<': return lltok::less;
270 case '>': return lltok::greater;
271 case '(': return lltok::lparen;
272 case ')': return lltok::rparen;
273 case ',': return lltok::comma;
274 case '*': return lltok::star;
275 case '\\': return lltok::backslash;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000276 }
277}
278
279void LLLexer::SkipLineComment() {
280 while (1) {
281 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
282 return;
283 }
284}
285
286/// LexAt - Lex all tokens that start with an @ character:
Chris Lattnerdf986172009-01-02 07:01:27 +0000287/// GlobalVar @\"[^\"]*\"
288/// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
289/// GlobalVarID @[0-9]+
290lltok::Kind LLLexer::LexAt() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000291 // Handle AtStringConstant: @\"[^\"]*\"
292 if (CurPtr[0] == '"') {
293 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000294
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000295 while (1) {
296 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000297
298 if (CurChar == EOF) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000299 Error("end of file in global variable name");
300 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000301 }
302 if (CurChar == '"') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000303 StrVal.assign(TokStart+2, CurPtr-1);
304 UnEscapeLexed(StrVal);
305 return lltok::GlobalVar;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000306 }
307 }
308 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000309
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000310 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
Nick Lewycky9fa89332011-06-04 18:16:26 +0000311 if (ReadVarName())
Chris Lattnerdf986172009-01-02 07:01:27 +0000312 return lltok::GlobalVar;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000313
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000314 // Handle GlobalVarID: @[0-9]+
315 if (isdigit(CurPtr[0])) {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000316 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
317 /*empty*/;
318
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000319 uint64_t Val = atoull(TokStart+1, CurPtr);
320 if ((unsigned)Val != Val)
Chris Lattnerdf986172009-01-02 07:01:27 +0000321 Error("invalid value number (too large)!");
322 UIntVal = unsigned(Val);
323 return lltok::GlobalID;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000324 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000325
Chris Lattnerdf986172009-01-02 07:01:27 +0000326 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000327}
328
Nick Lewycky9fa89332011-06-04 18:16:26 +0000329/// ReadString - Read a string until the closing quote.
330lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
331 const char *Start = CurPtr;
332 while (1) {
333 int CurChar = getNextChar();
334
335 if (CurChar == EOF) {
336 Error("end of file in string constant");
337 return lltok::Error;
338 }
339 if (CurChar == '"') {
340 StrVal.assign(Start, CurPtr-1);
341 UnEscapeLexed(StrVal);
342 return kind;
343 }
344 }
345}
346
347/// ReadVarName - Read the rest of a token containing a variable name.
348bool LLLexer::ReadVarName() {
349 const char *NameStart = CurPtr;
350 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
351 CurPtr[0] == '.' || CurPtr[0] == '_') {
352 ++CurPtr;
353 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
354 CurPtr[0] == '.' || CurPtr[0] == '_')
355 ++CurPtr;
356
357 StrVal.assign(NameStart, CurPtr);
358 return true;
359 }
360 return false;
361}
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000362
363/// LexPercent - Lex all tokens that start with a % character:
Chris Lattnerdf986172009-01-02 07:01:27 +0000364/// LocalVar ::= %\"[^\"]*\"
365/// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
366/// LocalVarID ::= %[0-9]+
367lltok::Kind LLLexer::LexPercent() {
368 // Handle LocalVarName: %\"[^\"]*\"
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000369 if (CurPtr[0] == '"') {
370 ++CurPtr;
Nick Lewycky9fa89332011-06-04 18:16:26 +0000371 return ReadString(lltok::LocalVar);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000372 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000373
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000374 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Nick Lewycky9fa89332011-06-04 18:16:26 +0000375 if (ReadVarName())
Chris Lattnerdf986172009-01-02 07:01:27 +0000376 return lltok::LocalVar;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000377
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000378 // Handle LocalVarID: %[0-9]+
379 if (isdigit(CurPtr[0])) {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000380 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
381 /*empty*/;
382
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000383 uint64_t Val = atoull(TokStart+1, CurPtr);
384 if ((unsigned)Val != Val)
Chris Lattnerdf986172009-01-02 07:01:27 +0000385 Error("invalid value number (too large)!");
386 UIntVal = unsigned(Val);
387 return lltok::LocalVarID;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000388 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000389
Chris Lattnerdf986172009-01-02 07:01:27 +0000390 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000391}
392
393/// LexQuote - Lex all tokens that start with a " character:
394/// QuoteLabel "[^"]+":
395/// StringConstant "[^"]*"
Chris Lattnerdf986172009-01-02 07:01:27 +0000396lltok::Kind LLLexer::LexQuote() {
Nick Lewycky9fa89332011-06-04 18:16:26 +0000397 lltok::Kind kind = ReadString(lltok::StringConstant);
398 if (kind == lltok::Error || kind == lltok::Eof)
399 return kind;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000400
Nick Lewycky9fa89332011-06-04 18:16:26 +0000401 if (CurPtr[0] == ':') {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000402 ++CurPtr;
Nick Lewycky9fa89332011-06-04 18:16:26 +0000403 kind = lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000404 }
Nick Lewycky9fa89332011-06-04 18:16:26 +0000405
406 return kind;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000407}
408
409static bool JustWhitespaceNewLine(const char *&Ptr) {
410 const char *ThisPtr = Ptr;
411 while (*ThisPtr == ' ' || *ThisPtr == '\t')
412 ++ThisPtr;
413 if (*ThisPtr == '\n' || *ThisPtr == '\r') {
414 Ptr = ThisPtr;
415 return true;
416 }
417 return false;
418}
419
Chris Lattnere434d272009-12-30 04:56:59 +0000420/// LexExclaim:
Devang Pateleff2ab62009-07-29 00:34:02 +0000421/// !foo
Chris Lattner1d928312009-12-30 05:02:06 +0000422/// !
Chris Lattnere434d272009-12-30 04:56:59 +0000423lltok::Kind LLLexer::LexExclaim() {
Chris Lattner1d928312009-12-30 05:02:06 +0000424 // Lex a metadata name as a MetadataVar.
Nick Lewycky9100a782011-06-15 06:37:58 +0000425 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
426 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
Devang Pateleff2ab62009-07-29 00:34:02 +0000427 ++CurPtr;
428 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Nick Lewycky9100a782011-06-15 06:37:58 +0000429 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
Devang Pateleff2ab62009-07-29 00:34:02 +0000430 ++CurPtr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000431
Devang Pateleff2ab62009-07-29 00:34:02 +0000432 StrVal.assign(TokStart+1, CurPtr); // Skip !
Nick Lewycky9100a782011-06-15 06:37:58 +0000433 UnEscapeLexed(StrVal);
Chris Lattner1d928312009-12-30 05:02:06 +0000434 return lltok::MetadataVar;
Devang Pateleff2ab62009-07-29 00:34:02 +0000435 }
Chris Lattnere434d272009-12-30 04:56:59 +0000436 return lltok::exclaim;
Devang Pateleff2ab62009-07-29 00:34:02 +0000437}
438
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000439/// LexIdentifier: Handle several related productions:
440/// Label [-a-zA-Z$._0-9]+:
441/// IntegerType i[0-9]+
442/// Keyword sdiv, float, ...
443/// HexIntConstant [us]0x[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000444lltok::Kind LLLexer::LexIdentifier() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000445 const char *StartChar = CurPtr;
446 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
447 const char *KeywordEnd = 0;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000448
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000449 for (; isLabelChar(*CurPtr); ++CurPtr) {
450 // If we decide this is an integer, remember the end of the sequence.
451 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
452 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
453 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000454
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000455 // If we stopped due to a colon, this really is a label.
456 if (*CurPtr == ':') {
Chris Lattnerdf986172009-01-02 07:01:27 +0000457 StrVal.assign(StartChar-1, CurPtr++);
458 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000459 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000460
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000461 // Otherwise, this wasn't a label. If this was valid as an integer type,
462 // return it.
463 if (IntEnd == 0) IntEnd = CurPtr;
464 if (IntEnd != StartChar) {
465 CurPtr = IntEnd;
466 uint64_t NumBits = atoull(StartChar, CurPtr);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000467 if (NumBits < IntegerType::MIN_INT_BITS ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000468 NumBits > IntegerType::MAX_INT_BITS) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000469 Error("bitwidth for integer type out of range!");
470 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000471 }
Owen Anderson1d0be152009-08-13 21:58:54 +0000472 TyVal = IntegerType::get(Context, NumBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000473 return lltok::Type;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000474 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000475
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000476 // Otherwise, this was a letter sequence. See which keyword this is.
477 if (KeywordEnd == 0) KeywordEnd = CurPtr;
478 CurPtr = KeywordEnd;
479 --StartChar;
480 unsigned Len = CurPtr-StartChar;
Chris Lattnerdf986172009-01-02 07:01:27 +0000481#define KEYWORD(STR) \
482 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
483 return lltok::kw_##STR;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000484
Chris Lattnerdf986172009-01-02 07:01:27 +0000485 KEYWORD(begin); KEYWORD(end);
486 KEYWORD(true); KEYWORD(false);
487 KEYWORD(declare); KEYWORD(define);
488 KEYWORD(global); KEYWORD(constant);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000489
Rafael Espindolabb46f522009-01-15 20:18:42 +0000490 KEYWORD(private);
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000491 KEYWORD(linker_private);
Bill Wendling5e721d72010-07-01 21:55:59 +0000492 KEYWORD(linker_private_weak);
Bill Wendling55ae5152010-08-20 22:05:50 +0000493 KEYWORD(linker_private_weak_def_auto);
Chris Lattnerdf986172009-01-02 07:01:27 +0000494 KEYWORD(internal);
Chris Lattner266c7bb2009-04-13 05:44:34 +0000495 KEYWORD(available_externally);
Chris Lattnerdf986172009-01-02 07:01:27 +0000496 KEYWORD(linkonce);
Duncan Sands667d4b82009-03-07 15:45:40 +0000497 KEYWORD(linkonce_odr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000498 KEYWORD(weak);
Duncan Sands667d4b82009-03-07 15:45:40 +0000499 KEYWORD(weak_odr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000500 KEYWORD(appending);
501 KEYWORD(dllimport);
502 KEYWORD(dllexport);
503 KEYWORD(common);
504 KEYWORD(default);
505 KEYWORD(hidden);
506 KEYWORD(protected);
Rafael Espindolabea46262011-01-08 16:42:36 +0000507 KEYWORD(unnamed_addr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000508 KEYWORD(extern_weak);
509 KEYWORD(external);
510 KEYWORD(thread_local);
511 KEYWORD(zeroinitializer);
512 KEYWORD(undef);
513 KEYWORD(null);
514 KEYWORD(to);
515 KEYWORD(tail);
516 KEYWORD(target);
517 KEYWORD(triple);
518 KEYWORD(deplibs);
519 KEYWORD(datalayout);
520 KEYWORD(volatile);
Dan Gohman08d012e2009-07-22 22:44:56 +0000521 KEYWORD(nuw);
522 KEYWORD(nsw);
Dan Gohman1224c382009-07-20 21:19:07 +0000523 KEYWORD(exact);
Dan Gohmandd8004d2009-07-27 21:53:46 +0000524 KEYWORD(inbounds);
Chris Lattnerdf986172009-01-02 07:01:27 +0000525 KEYWORD(align);
526 KEYWORD(addrspace);
527 KEYWORD(section);
528 KEYWORD(alias);
529 KEYWORD(module);
530 KEYWORD(asm);
531 KEYWORD(sideeffect);
Dale Johannesen8ba2d5b2009-10-21 23:28:00 +0000532 KEYWORD(alignstack);
Chris Lattnerdf986172009-01-02 07:01:27 +0000533 KEYWORD(gc);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000534
Chris Lattnerdf986172009-01-02 07:01:27 +0000535 KEYWORD(ccc);
536 KEYWORD(fastcc);
537 KEYWORD(coldcc);
538 KEYWORD(x86_stdcallcc);
539 KEYWORD(x86_fastcallcc);
Anton Korobeynikovded05e32010-05-16 09:08:45 +0000540 KEYWORD(x86_thiscallcc);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000541 KEYWORD(arm_apcscc);
542 KEYWORD(arm_aapcscc);
543 KEYWORD(arm_aapcs_vfpcc);
Anton Korobeynikov211a14e2009-12-07 02:27:35 +0000544 KEYWORD(msp430_intrcc);
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000545 KEYWORD(ptx_kernel);
546 KEYWORD(ptx_device);
Anton Korobeynikov385f5a92009-06-16 18:50:49 +0000547
Chris Lattnerdf986172009-01-02 07:01:27 +0000548 KEYWORD(cc);
549 KEYWORD(c);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000550
Chris Lattnerdf986172009-01-02 07:01:27 +0000551 KEYWORD(signext);
552 KEYWORD(zeroext);
553 KEYWORD(inreg);
554 KEYWORD(sret);
555 KEYWORD(nounwind);
556 KEYWORD(noreturn);
557 KEYWORD(noalias);
558 KEYWORD(nocapture);
559 KEYWORD(byval);
560 KEYWORD(nest);
561 KEYWORD(readnone);
562 KEYWORD(readonly);
Rafael Espindolafc2bb8c2011-05-25 03:44:17 +0000563 KEYWORD(uwtable);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000564
Jakob Stoklund Olesen570a4a52010-02-06 01:16:28 +0000565 KEYWORD(inlinehint);
Chris Lattnerdf986172009-01-02 07:01:27 +0000566 KEYWORD(noinline);
567 KEYWORD(alwaysinline);
568 KEYWORD(optsize);
569 KEYWORD(ssp);
570 KEYWORD(sspreq);
Devang Pateld18e31a2009-06-04 22:05:33 +0000571 KEYWORD(noredzone);
Devang Patel578efa92009-06-05 21:57:13 +0000572 KEYWORD(noimplicitfloat);
Anton Korobeynikovc5ec8a72009-07-17 18:07:26 +0000573 KEYWORD(naked);
Charles Davis970bfcc2010-10-25 15:37:09 +0000574 KEYWORD(hotpatch);
Devang Pateld4980812008-09-02 20:52:40 +0000575
Chris Lattnerdf986172009-01-02 07:01:27 +0000576 KEYWORD(type);
577 KEYWORD(opaque);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000578
Chris Lattnerdf986172009-01-02 07:01:27 +0000579 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
580 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
581 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
582 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
Misha Brukman9ea40342009-01-02 22:46:48 +0000583
Chris Lattnerdf986172009-01-02 07:01:27 +0000584 KEYWORD(x);
Chris Lattner09d9ef42009-10-28 03:39:23 +0000585 KEYWORD(blockaddress);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000586#undef KEYWORD
587
588 // Keywords for types.
Chris Lattnerdf986172009-01-02 07:01:27 +0000589#define TYPEKEYWORD(STR, LLVMTY) \
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000590 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
Chris Lattnerdf986172009-01-02 07:01:27 +0000591 TyVal = LLVMTY; return lltok::Type; }
Owen Anderson1d0be152009-08-13 21:58:54 +0000592 TYPEKEYWORD("void", Type::getVoidTy(Context));
593 TYPEKEYWORD("float", Type::getFloatTy(Context));
594 TYPEKEYWORD("double", Type::getDoubleTy(Context));
595 TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
596 TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
597 TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
598 TYPEKEYWORD("label", Type::getLabelTy(Context));
599 TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
Dale Johannesenbb811a22010-09-10 20:55:01 +0000600 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000601#undef TYPEKEYWORD
602
603 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
604 // to avoid conflicting with the sext/zext instructions, below.
605 if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
606 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
607 if (JustWhitespaceNewLine(CurPtr))
Chris Lattnerdf986172009-01-02 07:01:27 +0000608 return lltok::kw_signext;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000609 } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
610 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
611 if (JustWhitespaceNewLine(CurPtr))
Chris Lattnerdf986172009-01-02 07:01:27 +0000612 return lltok::kw_zeroext;
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000613 } else if (Len == 6 && !memcmp(StartChar, "malloc", 6)) {
Chris Lattnercf4d2f12009-10-18 05:09:15 +0000614 // FIXME: Remove in LLVM 3.0.
615 // Autoupgrade malloc instruction.
Victor Hernandez13ad5aa2009-10-17 00:00:19 +0000616 return lltok::kw_malloc;
Victor Hernandez046e78c2009-10-26 23:43:48 +0000617 } else if (Len == 4 && !memcmp(StartChar, "free", 4)) {
618 // FIXME: Remove in LLVM 3.0.
619 // Autoupgrade malloc instruction.
620 return lltok::kw_free;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000621 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000622
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000623 // Keywords for instructions.
Chris Lattnerdf986172009-01-02 07:01:27 +0000624#define INSTKEYWORD(STR, Enum) \
625 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
626 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000627
Dan Gohmanae3a0be2009-06-04 22:49:04 +0000628 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
629 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
630 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
Chris Lattnerdf986172009-01-02 07:01:27 +0000631 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
632 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
633 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
634 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
635 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000636
Chris Lattnerdf986172009-01-02 07:01:27 +0000637 INSTKEYWORD(phi, PHI);
638 INSTKEYWORD(call, Call);
639 INSTKEYWORD(trunc, Trunc);
640 INSTKEYWORD(zext, ZExt);
641 INSTKEYWORD(sext, SExt);
642 INSTKEYWORD(fptrunc, FPTrunc);
643 INSTKEYWORD(fpext, FPExt);
644 INSTKEYWORD(uitofp, UIToFP);
645 INSTKEYWORD(sitofp, SIToFP);
646 INSTKEYWORD(fptoui, FPToUI);
647 INSTKEYWORD(fptosi, FPToSI);
648 INSTKEYWORD(inttoptr, IntToPtr);
649 INSTKEYWORD(ptrtoint, PtrToInt);
650 INSTKEYWORD(bitcast, BitCast);
651 INSTKEYWORD(select, Select);
652 INSTKEYWORD(va_arg, VAArg);
653 INSTKEYWORD(ret, Ret);
654 INSTKEYWORD(br, Br);
655 INSTKEYWORD(switch, Switch);
Chris Lattnerab21db72009-10-28 00:19:10 +0000656 INSTKEYWORD(indirectbr, IndirectBr);
Chris Lattnerdf986172009-01-02 07:01:27 +0000657 INSTKEYWORD(invoke, Invoke);
658 INSTKEYWORD(unwind, Unwind);
659 INSTKEYWORD(unreachable, Unreachable);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000660
Chris Lattnerdf986172009-01-02 07:01:27 +0000661 INSTKEYWORD(alloca, Alloca);
Chris Lattnerdf986172009-01-02 07:01:27 +0000662 INSTKEYWORD(load, Load);
663 INSTKEYWORD(store, Store);
664 INSTKEYWORD(getelementptr, GetElementPtr);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000665
Chris Lattnerdf986172009-01-02 07:01:27 +0000666 INSTKEYWORD(extractelement, ExtractElement);
667 INSTKEYWORD(insertelement, InsertElement);
668 INSTKEYWORD(shufflevector, ShuffleVector);
669 INSTKEYWORD(getresult, ExtractValue);
670 INSTKEYWORD(extractvalue, ExtractValue);
671 INSTKEYWORD(insertvalue, InsertValue);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000672#undef INSTKEYWORD
673
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000674 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
675 // the CFE to avoid forcing it to deal with 64-bit numbers.
676 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
677 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
678 int len = CurPtr-TokStart-3;
679 uint32_t bits = len * 4;
Daniel Dunbar689ad6e2009-08-13 02:33:34 +0000680 APInt Tmp(bits, StringRef(TokStart+3, len), 16);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000681 uint32_t activeBits = Tmp.getActiveBits();
682 if (activeBits > 0 && activeBits < bits)
Jay Foad40f8f622010-12-07 08:25:19 +0000683 Tmp = Tmp.trunc(activeBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000684 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
685 return lltok::APSInt;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000686 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000687
Chris Lattner4ce0df62007-11-18 18:43:24 +0000688 // If this is "cc1234", return this as just "cc".
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000689 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
690 CurPtr = TokStart+2;
Chris Lattnerdf986172009-01-02 07:01:27 +0000691 return lltok::kw_cc;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000692 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000693
Chris Lattner4ce0df62007-11-18 18:43:24 +0000694 // If this starts with "call", return it as CALL. This is to support old
695 // broken .ll files. FIXME: remove this with LLVM 3.0.
696 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
697 CurPtr = TokStart+4;
Chris Lattnerdf986172009-01-02 07:01:27 +0000698 UIntVal = Instruction::Call;
699 return lltok::kw_call;
Chris Lattner4ce0df62007-11-18 18:43:24 +0000700 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000701
Chris Lattnerdf986172009-01-02 07:01:27 +0000702 // Finally, if this isn't known, return an error.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000703 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000704 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000705}
706
707
708/// Lex0x: Handle productions that start with 0x, knowing that it matches and
709/// that this is not a label:
710/// HexFPConstant 0x[0-9A-Fa-f]+
711/// HexFP80Constant 0xK[0-9A-Fa-f]+
712/// HexFP128Constant 0xL[0-9A-Fa-f]+
713/// HexPPC128Constant 0xM[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000714lltok::Kind LLLexer::Lex0x() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000715 CurPtr = TokStart + 2;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000716
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000717 char Kind;
718 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
719 Kind = *CurPtr++;
720 } else {
721 Kind = 'J';
722 }
723
724 if (!isxdigit(CurPtr[0])) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000725 // Bad token, return it as an error.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000726 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000727 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000728 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000729
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000730 while (isxdigit(CurPtr[0]))
731 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000732
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000733 if (Kind == 'J') {
734 // HexFPConstant - Floating point constant represented in IEEE format as a
735 // hexadecimal number for when exponential notation is not precise enough.
736 // Float and double only.
Chris Lattnerdf986172009-01-02 07:01:27 +0000737 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
738 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000739 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000740
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000741 uint64_t Pair[2];
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000742 switch (Kind) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000743 default: llvm_unreachable("Unknown kind!");
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000744 case 'K':
745 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000746 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
Chris Lattnerdf986172009-01-02 07:01:27 +0000747 APFloatVal = APFloat(APInt(80, 2, Pair));
748 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000749 case 'L':
750 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000751 HexToIntPair(TokStart+3, CurPtr, Pair);
Chris Lattnerdf986172009-01-02 07:01:27 +0000752 APFloatVal = APFloat(APInt(128, 2, Pair), true);
753 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000754 case 'M':
755 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
Dale Johannesen1b25cb22009-03-23 21:16:53 +0000756 HexToIntPair(TokStart+3, CurPtr, Pair);
Chris Lattnerdf986172009-01-02 07:01:27 +0000757 APFloatVal = APFloat(APInt(128, 2, Pair));
758 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000759 }
760}
761
762/// LexIdentifier: Handle several related productions:
763/// Label [-a-zA-Z$._0-9]+:
764/// NInteger -[0-9]+
765/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
766/// PInteger [0-9]+
767/// HexFPConstant 0x[0-9A-Fa-f]+
768/// HexFP80Constant 0xK[0-9A-Fa-f]+
769/// HexFP128Constant 0xL[0-9A-Fa-f]+
770/// HexPPC128Constant 0xM[0-9A-Fa-f]+
Chris Lattnerdf986172009-01-02 07:01:27 +0000771lltok::Kind LLLexer::LexDigitOrNegative() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000772 // If the letter after the negative is a number, this is probably a label.
773 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
774 // Okay, this is not a number after the -, it's probably a label.
775 if (const char *End = isLabelTail(CurPtr)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000776 StrVal.assign(TokStart, End-1);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000777 CurPtr = End;
Chris Lattnerdf986172009-01-02 07:01:27 +0000778 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000779 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000780
Chris Lattnerdf986172009-01-02 07:01:27 +0000781 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000782 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000783
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000784 // At this point, it is either a label, int or fp constant.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000785
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000786 // Skip digits, we have at least one.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000787 for (; isdigit(CurPtr[0]); ++CurPtr)
788 /*empty*/;
789
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000790 // Check to see if this really is a label afterall, e.g. "-1:".
791 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
792 if (const char *End = isLabelTail(CurPtr)) {
Chris Lattnerdf986172009-01-02 07:01:27 +0000793 StrVal.assign(TokStart, End-1);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000794 CurPtr = End;
Chris Lattnerdf986172009-01-02 07:01:27 +0000795 return lltok::LabelStr;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000796 }
797 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000798
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000799 // If the next character is a '.', then it is a fp value, otherwise its
800 // integer.
801 if (CurPtr[0] != '.') {
802 if (TokStart[0] == '0' && TokStart[1] == 'x')
803 return Lex0x();
804 unsigned Len = CurPtr-TokStart;
805 uint32_t numBits = ((Len * 64) / 19) + 2;
Daniel Dunbar689ad6e2009-08-13 02:33:34 +0000806 APInt Tmp(numBits, StringRef(TokStart, Len), 10);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000807 if (TokStart[0] == '-') {
808 uint32_t minBits = Tmp.getMinSignedBits();
809 if (minBits > 0 && minBits < numBits)
Jay Foad40f8f622010-12-07 08:25:19 +0000810 Tmp = Tmp.trunc(minBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000811 APSIntVal = APSInt(Tmp, false);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000812 } else {
813 uint32_t activeBits = Tmp.getActiveBits();
814 if (activeBits > 0 && activeBits < numBits)
Jay Foad40f8f622010-12-07 08:25:19 +0000815 Tmp = Tmp.trunc(activeBits);
Chris Lattnerdf986172009-01-02 07:01:27 +0000816 APSIntVal = APSInt(Tmp, true);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000817 }
Chris Lattnerdf986172009-01-02 07:01:27 +0000818 return lltok::APSInt;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000819 }
820
821 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000822
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000823 // Skip over [0-9]*([eE][-+]?[0-9]+)?
824 while (isdigit(CurPtr[0])) ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000825
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000826 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000827 if (isdigit(CurPtr[1]) ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000828 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
829 CurPtr += 2;
830 while (isdigit(CurPtr[0])) ++CurPtr;
831 }
832 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000833
Nick Lewycky24021232010-12-19 20:42:43 +0000834 APFloatVal = APFloat(std::atof(TokStart));
Chris Lattnerdf986172009-01-02 07:01:27 +0000835 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000836}
837
838/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
Chris Lattnerdf986172009-01-02 07:01:27 +0000839lltok::Kind LLLexer::LexPositive() {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000840 // If the letter after the negative is a number, this is probably not a
841 // label.
842 if (!isdigit(CurPtr[0]))
Chris Lattnerdf986172009-01-02 07:01:27 +0000843 return lltok::Error;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000844
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000845 // Skip digits.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000846 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
847 /*empty*/;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000848
849 // At this point, we need a '.'.
850 if (CurPtr[0] != '.') {
851 CurPtr = TokStart+1;
Chris Lattnerdf986172009-01-02 07:01:27 +0000852 return lltok::Error;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000853 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000854
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000855 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000856
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000857 // Skip over [0-9]*([eE][-+]?[0-9]+)?
858 while (isdigit(CurPtr[0])) ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000859
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000860 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000861 if (isdigit(CurPtr[1]) ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000862 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
863 CurPtr += 2;
864 while (isdigit(CurPtr[0])) ++CurPtr;
865 }
866 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000867
Nick Lewycky24021232010-12-19 20:42:43 +0000868 APFloatVal = APFloat(std::atof(TokStart));
Chris Lattnerdf986172009-01-02 07:01:27 +0000869 return lltok::APFloat;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000870}