blob: ce53f0d2a904da00f95e16de8a2a909ea7e55280 [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"
15#include "ParserInternals.h"
16#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerd185f642007-12-08 19:03:30 +000017#include "llvm/Support/MathExtras.h"
Chris Lattner8e3a8e02007-11-18 08:46:26 +000018
19#include <list>
20#include "llvmAsmParser.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000021
22#include <cstring>
Chris Lattner8e3a8e02007-11-18 08:46:26 +000023using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26// Helper functions.
27//===----------------------------------------------------------------------===//
28
29// atoull - Convert an ascii string of decimal digits into the unsigned long
30// long representation... this does not have to do input error checking,
31// because we know that the input will be matched by a suitable regex...
32//
33static uint64_t atoull(const char *Buffer, const char *End) {
34 uint64_t Result = 0;
35 for (; Buffer != End; Buffer++) {
36 uint64_t OldRes = Result;
37 Result *= 10;
38 Result += *Buffer-'0';
39 if (Result < OldRes) { // Uh, oh, overflow detected!!!
40 GenerateError("constant bigger than 64 bits detected!");
41 return 0;
42 }
43 }
44 return Result;
45}
46
47static uint64_t HexIntToVal(const char *Buffer, const char *End) {
48 uint64_t Result = 0;
49 for (; Buffer != End; ++Buffer) {
50 uint64_t OldRes = Result;
51 Result *= 16;
52 char C = *Buffer;
53 if (C >= '0' && C <= '9')
54 Result += C-'0';
55 else if (C >= 'A' && C <= 'F')
56 Result += C-'A'+10;
57 else if (C >= 'a' && C <= 'f')
58 Result += C-'a'+10;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +000059
Chris Lattner8e3a8e02007-11-18 08:46:26 +000060 if (Result < OldRes) { // Uh, oh, overflow detected!!!
61 GenerateError("constant bigger than 64 bits detected!");
62 return 0;
63 }
64 }
65 return Result;
66}
67
68// HexToFP - Convert the ascii string in hexadecimal format to the floating
69// point representation of it.
70//
71static double HexToFP(const char *Buffer, const char *End) {
72 return BitsToDouble(HexIntToVal(Buffer, End)); // Cast Hex constant to double
73}
74
75static void HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]){
76 Pair[0] = 0;
77 for (int i=0; i<16; i++, Buffer++) {
78 assert(Buffer != End);
79 Pair[0] *= 16;
80 char C = *Buffer;
81 if (C >= '0' && C <= '9')
82 Pair[0] += C-'0';
83 else if (C >= 'A' && C <= 'F')
84 Pair[0] += C-'A'+10;
85 else if (C >= 'a' && C <= 'f')
86 Pair[0] += C-'a'+10;
87 }
88 Pair[1] = 0;
89 for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
90 Pair[1] *= 16;
91 char C = *Buffer;
92 if (C >= '0' && C <= '9')
93 Pair[1] += C-'0';
94 else if (C >= 'A' && C <= 'F')
95 Pair[1] += C-'A'+10;
96 else if (C >= 'a' && C <= 'f')
97 Pair[1] += C-'a'+10;
98 }
Chris Lattnerd343c6b2007-11-18 18:25:18 +000099 if (Buffer != End)
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000100 GenerateError("constant bigger than 128 bits detected!");
101}
102
103// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
104// appropriate character.
105static void UnEscapeLexed(std::string &Str) {
106 if (Str.empty()) return;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000107
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000108 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
109 char *BOut = Buffer;
110 for (char *BIn = Buffer; BIn != EndBuffer; ) {
111 if (BIn[0] == '\\') {
112 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
113 *BOut++ = '\\'; // Two \ becomes one
114 BIn += 2;
115 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
116 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
117 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
118 BIn[3] = Tmp; // Restore character
119 BIn += 3; // Skip over handled chars
120 ++BOut;
121 } else {
122 *BOut++ = *BIn++;
123 }
124 } else {
125 *BOut++ = *BIn++;
126 }
127 }
128 Str.resize(BOut-Buffer);
129}
130
131/// isLabelChar - Return true for [-a-zA-Z$._0-9].
132static bool isLabelChar(char C) {
133 return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
134}
135
136
137/// isLabelTail - Return true if this pointer points to a valid end of a label.
138static const char *isLabelTail(const char *CurPtr) {
139 while (1) {
140 if (CurPtr[0] == ':') return CurPtr+1;
141 if (!isLabelChar(CurPtr[0])) return 0;
142 ++CurPtr;
143 }
144}
145
146
147
148//===----------------------------------------------------------------------===//
149// Lexer definition.
150//===----------------------------------------------------------------------===//
151
152// FIXME: REMOVE THIS.
153#define YYEOF 0
154#define YYERROR -2
155
156LLLexer::LLLexer(MemoryBuffer *StartBuf) : CurLineNo(1), CurBuf(StartBuf) {
157 CurPtr = CurBuf->getBufferStart();
158}
159
160std::string LLLexer::getFilename() const {
161 return CurBuf->getBufferIdentifier();
162}
163
164int LLLexer::getNextChar() {
165 char CurChar = *CurPtr++;
166 switch (CurChar) {
167 default: return (unsigned char)CurChar;
168 case 0:
169 // A nul character in the stream is either the end of the current buffer or
170 // a random nul in the file. Disambiguate that here.
171 if (CurPtr-1 != CurBuf->getBufferEnd())
172 return 0; // Just whitespace.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000173
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000174 // Otherwise, return end of file.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000175 --CurPtr; // Another call to lex will return EOF again.
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000176 return EOF;
177 case '\n':
178 case '\r':
179 // Handle the newline character by ignoring it and incrementing the line
180 // count. However, be careful about 'dos style' files with \n\r in them.
181 // Only treat a \n\r or \r\n as a single line.
182 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
183 *CurPtr != CurChar)
184 ++CurPtr; // Eat the two char newline sequence.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000185
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000186 ++CurLineNo;
187 return '\n';
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000188 }
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000189}
190
191
192int LLLexer::LexToken() {
193 TokStart = CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000194
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000195 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000196
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000197 switch (CurChar) {
198 default:
199 // Handle letters: [a-zA-Z_]
200 if (isalpha(CurChar) || CurChar == '_')
201 return LexIdentifier();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000202
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000203 return CurChar;
204 case EOF: return YYEOF;
205 case 0:
206 case ' ':
207 case '\t':
208 case '\n':
209 case '\r':
210 // Ignore whitespace.
211 return LexToken();
212 case '+': return LexPositive();
213 case '@': return LexAt();
214 case '%': return LexPercent();
215 case '"': return LexQuote();
216 case '.':
217 if (const char *Ptr = isLabelTail(CurPtr)) {
218 CurPtr = Ptr;
219 llvmAsmlval.StrVal = new std::string(TokStart, CurPtr-1);
220 return LABELSTR;
221 }
222 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
223 CurPtr += 2;
224 return DOTDOTDOT;
225 }
226 return '.';
227 case '$':
228 if (const char *Ptr = isLabelTail(CurPtr)) {
229 CurPtr = Ptr;
230 llvmAsmlval.StrVal = new std::string(TokStart, CurPtr-1);
231 return LABELSTR;
232 }
233 return '$';
234 case ';':
235 SkipLineComment();
236 return LexToken();
237 case '0': case '1': case '2': case '3': case '4':
238 case '5': case '6': case '7': case '8': case '9':
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000239 case '-':
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000240 return LexDigitOrNegative();
241 }
242}
243
244void LLLexer::SkipLineComment() {
245 while (1) {
246 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
247 return;
248 }
249}
250
251/// LexAt - Lex all tokens that start with an @ character:
252/// AtStringConstant @\"[^\"]*\"
253/// GlobalVarName @[-a-zA-Z$._][-a-zA-Z$._0-9]*
254/// GlobalVarID @[0-9]+
255int LLLexer::LexAt() {
256 // Handle AtStringConstant: @\"[^\"]*\"
257 if (CurPtr[0] == '"') {
258 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000259
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000260 while (1) {
261 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000262
263 if (CurChar == EOF) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000264 GenerateError("End of file in global variable name");
265 return YYERROR;
266 }
267 if (CurChar == '"') {
268 llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
269 UnEscapeLexed(*llvmAsmlval.StrVal);
270 return ATSTRINGCONSTANT;
271 }
272 }
273 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000274
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000275 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000276 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000277 CurPtr[0] == '.' || CurPtr[0] == '_') {
278 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000279 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000280 CurPtr[0] == '.' || CurPtr[0] == '_')
281 ++CurPtr;
282
283 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr); // Skip @
284 return GLOBALVAR;
285 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000286
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000287 // Handle GlobalVarID: @[0-9]+
288 if (isdigit(CurPtr[0])) {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000289 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
290 /*empty*/;
291
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000292 uint64_t Val = atoull(TokStart+1, CurPtr);
293 if ((unsigned)Val != Val)
294 GenerateError("Invalid value number (too large)!");
295 llvmAsmlval.UIntVal = unsigned(Val);
296 return GLOBALVAL_ID;
297 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000298
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000299 return '@';
300}
301
302
303/// LexPercent - Lex all tokens that start with a % character:
304/// PctStringConstant %\"[^\"]*\"
305/// LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
306/// LocalVarID %[0-9]+
307int LLLexer::LexPercent() {
308 // Handle PctStringConstant: %\"[^\"]*\"
309 if (CurPtr[0] == '"') {
310 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000311
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000312 while (1) {
313 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000314
315 if (CurChar == EOF) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000316 GenerateError("End of file in local variable name");
317 return YYERROR;
318 }
319 if (CurChar == '"') {
320 llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
321 UnEscapeLexed(*llvmAsmlval.StrVal);
322 return PCTSTRINGCONSTANT;
323 }
324 }
325 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000326
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000327 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000328 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000329 CurPtr[0] == '.' || CurPtr[0] == '_') {
330 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000331 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000332 CurPtr[0] == '.' || CurPtr[0] == '_')
333 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000334
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000335 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr); // Skip %
336 return LOCALVAR;
337 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000338
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000339 // Handle LocalVarID: %[0-9]+
340 if (isdigit(CurPtr[0])) {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000341 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
342 /*empty*/;
343
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000344 uint64_t Val = atoull(TokStart+1, CurPtr);
345 if ((unsigned)Val != Val)
346 GenerateError("Invalid value number (too large)!");
347 llvmAsmlval.UIntVal = unsigned(Val);
348 return LOCALVAL_ID;
349 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000350
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000351 return '%';
352}
353
354/// LexQuote - Lex all tokens that start with a " character:
355/// QuoteLabel "[^"]+":
356/// StringConstant "[^"]*"
357int LLLexer::LexQuote() {
358 while (1) {
359 int CurChar = getNextChar();
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000360
361 if (CurChar == EOF) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000362 GenerateError("End of file in quoted string");
363 return YYERROR;
364 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000365
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000366 if (CurChar != '"') continue;
367
368 if (CurPtr[0] != ':') {
369 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-1);
370 UnEscapeLexed(*llvmAsmlval.StrVal);
371 return STRINGCONSTANT;
372 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000373
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000374 ++CurPtr;
375 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-2);
376 UnEscapeLexed(*llvmAsmlval.StrVal);
377 return LABELSTR;
378 }
379}
380
381static bool JustWhitespaceNewLine(const char *&Ptr) {
382 const char *ThisPtr = Ptr;
383 while (*ThisPtr == ' ' || *ThisPtr == '\t')
384 ++ThisPtr;
385 if (*ThisPtr == '\n' || *ThisPtr == '\r') {
386 Ptr = ThisPtr;
387 return true;
388 }
389 return false;
390}
391
392
393/// LexIdentifier: Handle several related productions:
394/// Label [-a-zA-Z$._0-9]+:
395/// IntegerType i[0-9]+
396/// Keyword sdiv, float, ...
397/// HexIntConstant [us]0x[0-9A-Fa-f]+
398int LLLexer::LexIdentifier() {
399 const char *StartChar = CurPtr;
400 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
401 const char *KeywordEnd = 0;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000402
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000403 for (; isLabelChar(*CurPtr); ++CurPtr) {
404 // If we decide this is an integer, remember the end of the sequence.
405 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
406 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
407 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000408
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000409 // If we stopped due to a colon, this really is a label.
410 if (*CurPtr == ':') {
411 llvmAsmlval.StrVal = new std::string(StartChar-1, CurPtr++);
412 return LABELSTR;
413 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000414
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000415 // Otherwise, this wasn't a label. If this was valid as an integer type,
416 // return it.
417 if (IntEnd == 0) IntEnd = CurPtr;
418 if (IntEnd != StartChar) {
419 CurPtr = IntEnd;
420 uint64_t NumBits = atoull(StartChar, CurPtr);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000421 if (NumBits < IntegerType::MIN_INT_BITS ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000422 NumBits > IntegerType::MAX_INT_BITS) {
423 GenerateError("Bitwidth for integer type out of range!");
424 return YYERROR;
425 }
426 const Type* Ty = IntegerType::get(NumBits);
427 llvmAsmlval.PrimType = Ty;
428 return INTTYPE;
429 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000430
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000431 // Otherwise, this was a letter sequence. See which keyword this is.
432 if (KeywordEnd == 0) KeywordEnd = CurPtr;
433 CurPtr = KeywordEnd;
434 --StartChar;
435 unsigned Len = CurPtr-StartChar;
436#define KEYWORD(STR, TOK) \
437 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) return TOK;
438
439 KEYWORD("begin", BEGINTOK);
440 KEYWORD("end", ENDTOK);
441 KEYWORD("true", TRUETOK);
442 KEYWORD("false", FALSETOK);
443 KEYWORD("declare", DECLARE);
444 KEYWORD("define", DEFINE);
445 KEYWORD("global", GLOBAL);
446 KEYWORD("constant", CONSTANT);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000447
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000448 KEYWORD("internal", INTERNAL);
449 KEYWORD("linkonce", LINKONCE);
450 KEYWORD("weak", WEAK);
451 KEYWORD("appending", APPENDING);
452 KEYWORD("dllimport", DLLIMPORT);
453 KEYWORD("dllexport", DLLEXPORT);
Dale Johannesenaafce772008-05-14 20:12:51 +0000454 KEYWORD("common", COMMON);
Dan Gohmanfdfef0d2008-05-22 22:30:09 +0000455 KEYWORD("default", DEFAULT);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000456 KEYWORD("hidden", HIDDEN);
457 KEYWORD("protected", PROTECTED);
458 KEYWORD("extern_weak", EXTERN_WEAK);
459 KEYWORD("external", EXTERNAL);
460 KEYWORD("thread_local", THREAD_LOCAL);
461 KEYWORD("zeroinitializer", ZEROINITIALIZER);
462 KEYWORD("undef", UNDEF);
463 KEYWORD("null", NULL_TOK);
464 KEYWORD("to", TO);
465 KEYWORD("tail", TAIL);
466 KEYWORD("target", TARGET);
467 KEYWORD("triple", TRIPLE);
468 KEYWORD("deplibs", DEPLIBS);
469 KEYWORD("datalayout", DATALAYOUT);
470 KEYWORD("volatile", VOLATILE);
471 KEYWORD("align", ALIGN);
Christopher Lambfe63fb92007-12-11 08:59:05 +0000472 KEYWORD("addrspace", ADDRSPACE);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000473 KEYWORD("section", SECTION);
474 KEYWORD("alias", ALIAS);
475 KEYWORD("module", MODULE);
476 KEYWORD("asm", ASM_TOK);
477 KEYWORD("sideeffect", SIDEEFFECT);
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000478 KEYWORD("gc", GC);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000479
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000480 KEYWORD("cc", CC_TOK);
481 KEYWORD("ccc", CCC_TOK);
482 KEYWORD("fastcc", FASTCC_TOK);
483 KEYWORD("coldcc", COLDCC_TOK);
484 KEYWORD("x86_stdcallcc", X86_STDCALLCC_TOK);
485 KEYWORD("x86_fastcallcc", X86_FASTCALLCC_TOK);
Dale Johannesen7dc00ab2008-08-13 18:40:23 +0000486 KEYWORD("x86_ssecallcc", X86_SSECALLCC_TOK);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000487
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000488 KEYWORD("signext", SIGNEXT);
489 KEYWORD("zeroext", ZEROEXT);
490 KEYWORD("inreg", INREG);
491 KEYWORD("sret", SRET);
492 KEYWORD("nounwind", NOUNWIND);
493 KEYWORD("noreturn", NORETURN);
494 KEYWORD("noalias", NOALIAS);
495 KEYWORD("byval", BYVAL);
496 KEYWORD("nest", NEST);
Duncan Sandsed4a2f12007-11-22 20:23:04 +0000497 KEYWORD("readnone", READNONE);
498 KEYWORD("readonly", READONLY);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000499
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000500 KEYWORD("type", TYPE);
501 KEYWORD("opaque", OPAQUE);
502
503 KEYWORD("eq" , EQ);
504 KEYWORD("ne" , NE);
505 KEYWORD("slt", SLT);
506 KEYWORD("sgt", SGT);
507 KEYWORD("sle", SLE);
508 KEYWORD("sge", SGE);
509 KEYWORD("ult", ULT);
510 KEYWORD("ugt", UGT);
511 KEYWORD("ule", ULE);
512 KEYWORD("uge", UGE);
513 KEYWORD("oeq", OEQ);
514 KEYWORD("one", ONE);
515 KEYWORD("olt", OLT);
516 KEYWORD("ogt", OGT);
517 KEYWORD("ole", OLE);
518 KEYWORD("oge", OGE);
519 KEYWORD("ord", ORD);
520 KEYWORD("uno", UNO);
521 KEYWORD("ueq", UEQ);
522 KEYWORD("une", UNE);
523#undef KEYWORD
524
525 // Keywords for types.
526#define TYPEKEYWORD(STR, LLVMTY, TOK) \
527 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
528 llvmAsmlval.PrimType = LLVMTY; return TOK; }
529 TYPEKEYWORD("void", Type::VoidTy, VOID);
530 TYPEKEYWORD("float", Type::FloatTy, FLOAT);
531 TYPEKEYWORD("double", Type::DoubleTy, DOUBLE);
532 TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty, X86_FP80);
533 TYPEKEYWORD("fp128", Type::FP128Ty, FP128);
534 TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty, PPC_FP128);
535 TYPEKEYWORD("label", Type::LabelTy, LABEL);
536#undef TYPEKEYWORD
537
538 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
539 // to avoid conflicting with the sext/zext instructions, below.
540 if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
541 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
542 if (JustWhitespaceNewLine(CurPtr))
543 return SIGNEXT;
544 } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
545 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
546 if (JustWhitespaceNewLine(CurPtr))
547 return ZEROEXT;
548 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000549
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000550 // Keywords for instructions.
551#define INSTKEYWORD(STR, type, Enum, TOK) \
552 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
553 llvmAsmlval.type = Instruction::Enum; return TOK; }
554
555 INSTKEYWORD("add", BinaryOpVal, Add, ADD);
556 INSTKEYWORD("sub", BinaryOpVal, Sub, SUB);
557 INSTKEYWORD("mul", BinaryOpVal, Mul, MUL);
558 INSTKEYWORD("udiv", BinaryOpVal, UDiv, UDIV);
559 INSTKEYWORD("sdiv", BinaryOpVal, SDiv, SDIV);
560 INSTKEYWORD("fdiv", BinaryOpVal, FDiv, FDIV);
561 INSTKEYWORD("urem", BinaryOpVal, URem, UREM);
562 INSTKEYWORD("srem", BinaryOpVal, SRem, SREM);
563 INSTKEYWORD("frem", BinaryOpVal, FRem, FREM);
564 INSTKEYWORD("shl", BinaryOpVal, Shl, SHL);
565 INSTKEYWORD("lshr", BinaryOpVal, LShr, LSHR);
566 INSTKEYWORD("ashr", BinaryOpVal, AShr, ASHR);
567 INSTKEYWORD("and", BinaryOpVal, And, AND);
568 INSTKEYWORD("or", BinaryOpVal, Or , OR );
569 INSTKEYWORD("xor", BinaryOpVal, Xor, XOR);
570 INSTKEYWORD("icmp", OtherOpVal, ICmp, ICMP);
571 INSTKEYWORD("fcmp", OtherOpVal, FCmp, FCMP);
Nate Begemanac80ade2008-05-12 19:01:56 +0000572 INSTKEYWORD("vicmp", OtherOpVal, VICmp, VICMP);
573 INSTKEYWORD("vfcmp", OtherOpVal, VFCmp, VFCMP);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000574
575 INSTKEYWORD("phi", OtherOpVal, PHI, PHI_TOK);
576 INSTKEYWORD("call", OtherOpVal, Call, CALL);
577 INSTKEYWORD("trunc", CastOpVal, Trunc, TRUNC);
578 INSTKEYWORD("zext", CastOpVal, ZExt, ZEXT);
579 INSTKEYWORD("sext", CastOpVal, SExt, SEXT);
580 INSTKEYWORD("fptrunc", CastOpVal, FPTrunc, FPTRUNC);
581 INSTKEYWORD("fpext", CastOpVal, FPExt, FPEXT);
582 INSTKEYWORD("uitofp", CastOpVal, UIToFP, UITOFP);
583 INSTKEYWORD("sitofp", CastOpVal, SIToFP, SITOFP);
584 INSTKEYWORD("fptoui", CastOpVal, FPToUI, FPTOUI);
585 INSTKEYWORD("fptosi", CastOpVal, FPToSI, FPTOSI);
586 INSTKEYWORD("inttoptr", CastOpVal, IntToPtr, INTTOPTR);
587 INSTKEYWORD("ptrtoint", CastOpVal, PtrToInt, PTRTOINT);
588 INSTKEYWORD("bitcast", CastOpVal, BitCast, BITCAST);
589 INSTKEYWORD("select", OtherOpVal, Select, SELECT);
590 INSTKEYWORD("va_arg", OtherOpVal, VAArg , VAARG);
591 INSTKEYWORD("ret", TermOpVal, Ret, RET);
592 INSTKEYWORD("br", TermOpVal, Br, BR);
593 INSTKEYWORD("switch", TermOpVal, Switch, SWITCH);
594 INSTKEYWORD("invoke", TermOpVal, Invoke, INVOKE);
595 INSTKEYWORD("unwind", TermOpVal, Unwind, UNWIND);
596 INSTKEYWORD("unreachable", TermOpVal, Unreachable, UNREACHABLE);
597
598 INSTKEYWORD("malloc", MemOpVal, Malloc, MALLOC);
599 INSTKEYWORD("alloca", MemOpVal, Alloca, ALLOCA);
600 INSTKEYWORD("free", MemOpVal, Free, FREE);
601 INSTKEYWORD("load", MemOpVal, Load, LOAD);
602 INSTKEYWORD("store", MemOpVal, Store, STORE);
603 INSTKEYWORD("getelementptr", MemOpVal, GetElementPtr, GETELEMENTPTR);
604
605 INSTKEYWORD("extractelement", OtherOpVal, ExtractElement, EXTRACTELEMENT);
606 INSTKEYWORD("insertelement", OtherOpVal, InsertElement, INSERTELEMENT);
607 INSTKEYWORD("shufflevector", OtherOpVal, ShuffleVector, SHUFFLEVECTOR);
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000608 INSTKEYWORD("getresult", OtherOpVal, ExtractValue, GETRESULT);
Dan Gohmane4977cf2008-05-23 01:55:30 +0000609 INSTKEYWORD("extractvalue", OtherOpVal, ExtractValue, EXTRACTVALUE);
610 INSTKEYWORD("insertvalue", OtherOpVal, InsertValue, INSERTVALUE);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000611#undef INSTKEYWORD
612
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000613 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
614 // the CFE to avoid forcing it to deal with 64-bit numbers.
615 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
616 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
617 int len = CurPtr-TokStart-3;
618 uint32_t bits = len * 4;
619 APInt Tmp(bits, TokStart+3, len, 16);
620 uint32_t activeBits = Tmp.getActiveBits();
621 if (activeBits > 0 && activeBits < bits)
622 Tmp.trunc(activeBits);
623 if (Tmp.getBitWidth() > 64) {
624 llvmAsmlval.APIntVal = new APInt(Tmp);
625 return TokStart[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
626 } else if (TokStart[0] == 's') {
627 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
628 return ESINT64VAL;
629 } else {
630 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
631 return EUINT64VAL;
632 }
633 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000634
Chris Lattner4ce0df62007-11-18 18:43:24 +0000635 // If this is "cc1234", return this as just "cc".
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000636 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
637 CurPtr = TokStart+2;
638 return CC_TOK;
639 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000640
Chris Lattner4ce0df62007-11-18 18:43:24 +0000641 // If this starts with "call", return it as CALL. This is to support old
642 // broken .ll files. FIXME: remove this with LLVM 3.0.
643 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
644 CurPtr = TokStart+4;
645 llvmAsmlval.OtherOpVal = Instruction::Call;
646 return CALL;
647 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000648
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000649 // Finally, if this isn't known, return just a single character.
650 CurPtr = TokStart+1;
651 return TokStart[0];
652}
653
654
655/// Lex0x: Handle productions that start with 0x, knowing that it matches and
656/// that this is not a label:
657/// HexFPConstant 0x[0-9A-Fa-f]+
658/// HexFP80Constant 0xK[0-9A-Fa-f]+
659/// HexFP128Constant 0xL[0-9A-Fa-f]+
660/// HexPPC128Constant 0xM[0-9A-Fa-f]+
661int LLLexer::Lex0x() {
662 CurPtr = TokStart + 2;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000663
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000664 char Kind;
665 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
666 Kind = *CurPtr++;
667 } else {
668 Kind = 'J';
669 }
670
671 if (!isxdigit(CurPtr[0])) {
672 // Bad token, return it as just zero.
673 CurPtr = TokStart+1;
674 return '0';
675 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000676
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000677 while (isxdigit(CurPtr[0]))
678 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000679
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000680 if (Kind == 'J') {
681 // HexFPConstant - Floating point constant represented in IEEE format as a
682 // hexadecimal number for when exponential notation is not precise enough.
683 // Float and double only.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000684 llvmAsmlval.FPVal = new APFloat(HexToFP(TokStart+2, CurPtr));
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000685 return FPVAL;
686 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000687
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000688 uint64_t Pair[2];
689 HexToIntPair(TokStart+3, CurPtr, Pair);
690 switch (Kind) {
691 default: assert(0 && "Unknown kind!");
692 case 'K':
693 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
694 llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
695 return FPVAL;
696 case 'L':
697 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
698 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair), true);
699 return FPVAL;
700 case 'M':
701 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
702 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
703 return FPVAL;
704 }
705}
706
707/// LexIdentifier: Handle several related productions:
708/// Label [-a-zA-Z$._0-9]+:
709/// NInteger -[0-9]+
710/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
711/// PInteger [0-9]+
712/// HexFPConstant 0x[0-9A-Fa-f]+
713/// HexFP80Constant 0xK[0-9A-Fa-f]+
714/// HexFP128Constant 0xL[0-9A-Fa-f]+
715/// HexPPC128Constant 0xM[0-9A-Fa-f]+
716int LLLexer::LexDigitOrNegative() {
717 // If the letter after the negative is a number, this is probably a label.
718 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
719 // Okay, this is not a number after the -, it's probably a label.
720 if (const char *End = isLabelTail(CurPtr)) {
721 llvmAsmlval.StrVal = new std::string(TokStart, End-1);
722 CurPtr = End;
723 return LABELSTR;
724 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000725
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000726 return CurPtr[-1];
727 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000728
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000729 // At this point, it is either a label, int or fp constant.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000730
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000731 // Skip digits, we have at least one.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000732 for (; isdigit(CurPtr[0]); ++CurPtr)
733 /*empty*/;
734
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000735 // Check to see if this really is a label afterall, e.g. "-1:".
736 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
737 if (const char *End = isLabelTail(CurPtr)) {
738 llvmAsmlval.StrVal = new std::string(TokStart, End-1);
739 CurPtr = End;
740 return LABELSTR;
741 }
742 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000743
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000744 // If the next character is a '.', then it is a fp value, otherwise its
745 // integer.
746 if (CurPtr[0] != '.') {
747 if (TokStart[0] == '0' && TokStart[1] == 'x')
748 return Lex0x();
749 unsigned Len = CurPtr-TokStart;
750 uint32_t numBits = ((Len * 64) / 19) + 2;
751 APInt Tmp(numBits, TokStart, Len, 10);
752 if (TokStart[0] == '-') {
753 uint32_t minBits = Tmp.getMinSignedBits();
754 if (minBits > 0 && minBits < numBits)
755 Tmp.trunc(minBits);
756 if (Tmp.getBitWidth() > 64) {
757 llvmAsmlval.APIntVal = new APInt(Tmp);
758 return ESAPINTVAL;
759 } else {
760 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
761 return ESINT64VAL;
762 }
763 } else {
764 uint32_t activeBits = Tmp.getActiveBits();
765 if (activeBits > 0 && activeBits < numBits)
766 Tmp.trunc(activeBits);
767 if (Tmp.getBitWidth() > 64) {
768 llvmAsmlval.APIntVal = new APInt(Tmp);
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000769 return EUAPINTVAL;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000770 } else {
771 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
772 return EUINT64VAL;
773 }
774 }
775 }
776
777 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000778
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000779 // Skip over [0-9]*([eE][-+]?[0-9]+)?
780 while (isdigit(CurPtr[0])) ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000781
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000782 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000783 if (isdigit(CurPtr[1]) ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000784 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
785 CurPtr += 2;
786 while (isdigit(CurPtr[0])) ++CurPtr;
787 }
788 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000789
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000790 llvmAsmlval.FPVal = new APFloat(atof(TokStart));
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000791 return FPVAL;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000792}
793
794/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
795int LLLexer::LexPositive() {
796 // If the letter after the negative is a number, this is probably not a
797 // label.
798 if (!isdigit(CurPtr[0]))
799 return CurPtr[-1];
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000800
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000801 // Skip digits.
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000802 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
803 /*empty*/;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000804
805 // At this point, we need a '.'.
806 if (CurPtr[0] != '.') {
807 CurPtr = TokStart+1;
808 return TokStart[0];
809 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000810
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000811 ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000812
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000813 // Skip over [0-9]*([eE][-+]?[0-9]+)?
814 while (isdigit(CurPtr[0])) ++CurPtr;
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000815
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000816 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000817 if (isdigit(CurPtr[1]) ||
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000818 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
819 CurPtr += 2;
820 while (isdigit(CurPtr[0])) ++CurPtr;
821 }
822 }
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000823
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000824 llvmAsmlval.FPVal = new APFloat(atof(TokStart));
Bill Wendling2c6fd8c2007-12-16 09:16:12 +0000825 return FPVAL;
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000826}
827
828
829//===----------------------------------------------------------------------===//
830// Define the interface to this file.
831//===----------------------------------------------------------------------===//
832
833static LLLexer *TheLexer;
834
835void InitLLLexer(llvm::MemoryBuffer *MB) {
836 assert(TheLexer == 0 && "LL Lexer isn't reentrant yet");
837 TheLexer = new LLLexer(MB);
838}
839
840int llvmAsmlex() {
841 return TheLexer->LexToken();
842}
843const char *LLLgetTokenStart() { return TheLexer->getTokStart(); }
844unsigned LLLgetTokenLength() { return TheLexer->getTokLength(); }
845std::string LLLgetFilename() { return TheLexer->getFilename(); }
846unsigned LLLgetLineNo() { return TheLexer->getLineNo(); }
847
848void FreeLexer() {
849 delete TheLexer;
850 TheLexer = 0;
851}