blob: 750529451a6a8ef5cdba644e2e5694ee71328aa3 [file] [log] [blame]
Chris Lattner8e3a8e02007-11-18 08:46:26 +00001//===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
21using namespace llvm;
22
23//===----------------------------------------------------------------------===//
24// Helper functions.
25//===----------------------------------------------------------------------===//
26
27// atoull - Convert an ascii string of decimal digits into the unsigned long
28// long representation... this does not have to do input error checking,
29// because we know that the input will be matched by a suitable regex...
30//
31static uint64_t atoull(const char *Buffer, const char *End) {
32 uint64_t Result = 0;
33 for (; Buffer != End; Buffer++) {
34 uint64_t OldRes = Result;
35 Result *= 10;
36 Result += *Buffer-'0';
37 if (Result < OldRes) { // Uh, oh, overflow detected!!!
38 GenerateError("constant bigger than 64 bits detected!");
39 return 0;
40 }
41 }
42 return Result;
43}
44
45static uint64_t HexIntToVal(const char *Buffer, const char *End) {
46 uint64_t Result = 0;
47 for (; Buffer != End; ++Buffer) {
48 uint64_t OldRes = Result;
49 Result *= 16;
50 char C = *Buffer;
51 if (C >= '0' && C <= '9')
52 Result += C-'0';
53 else if (C >= 'A' && C <= 'F')
54 Result += C-'A'+10;
55 else if (C >= 'a' && C <= 'f')
56 Result += C-'a'+10;
57
58 if (Result < OldRes) { // Uh, oh, overflow detected!!!
59 GenerateError("constant bigger than 64 bits detected!");
60 return 0;
61 }
62 }
63 return Result;
64}
65
66// HexToFP - Convert the ascii string in hexadecimal format to the floating
67// point representation of it.
68//
69static double HexToFP(const char *Buffer, const char *End) {
70 return BitsToDouble(HexIntToVal(Buffer, End)); // Cast Hex constant to double
71}
72
73static void HexToIntPair(const char *Buffer, const char *End, uint64_t Pair[2]){
74 Pair[0] = 0;
75 for (int i=0; i<16; i++, Buffer++) {
76 assert(Buffer != End);
77 Pair[0] *= 16;
78 char C = *Buffer;
79 if (C >= '0' && C <= '9')
80 Pair[0] += C-'0';
81 else if (C >= 'A' && C <= 'F')
82 Pair[0] += C-'A'+10;
83 else if (C >= 'a' && C <= 'f')
84 Pair[0] += C-'a'+10;
85 }
86 Pair[1] = 0;
87 for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
88 Pair[1] *= 16;
89 char C = *Buffer;
90 if (C >= '0' && C <= '9')
91 Pair[1] += C-'0';
92 else if (C >= 'A' && C <= 'F')
93 Pair[1] += C-'A'+10;
94 else if (C >= 'a' && C <= 'f')
95 Pair[1] += C-'a'+10;
96 }
Chris Lattnerd343c6b2007-11-18 18:25:18 +000097 if (Buffer != End)
Chris Lattner8e3a8e02007-11-18 08:46:26 +000098 GenerateError("constant bigger than 128 bits detected!");
99}
100
101// UnEscapeLexed - Run through the specified buffer and change \xx codes to the
102// appropriate character.
103static void UnEscapeLexed(std::string &Str) {
104 if (Str.empty()) return;
105
106 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
107 char *BOut = Buffer;
108 for (char *BIn = Buffer; BIn != EndBuffer; ) {
109 if (BIn[0] == '\\') {
110 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
111 *BOut++ = '\\'; // Two \ becomes one
112 BIn += 2;
113 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
114 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
115 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
116 BIn[3] = Tmp; // Restore character
117 BIn += 3; // Skip over handled chars
118 ++BOut;
119 } else {
120 *BOut++ = *BIn++;
121 }
122 } else {
123 *BOut++ = *BIn++;
124 }
125 }
126 Str.resize(BOut-Buffer);
127}
128
129/// isLabelChar - Return true for [-a-zA-Z$._0-9].
130static bool isLabelChar(char C) {
131 return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
132}
133
134
135/// isLabelTail - Return true if this pointer points to a valid end of a label.
136static const char *isLabelTail(const char *CurPtr) {
137 while (1) {
138 if (CurPtr[0] == ':') return CurPtr+1;
139 if (!isLabelChar(CurPtr[0])) return 0;
140 ++CurPtr;
141 }
142}
143
144
145
146//===----------------------------------------------------------------------===//
147// Lexer definition.
148//===----------------------------------------------------------------------===//
149
150// FIXME: REMOVE THIS.
151#define YYEOF 0
152#define YYERROR -2
153
154LLLexer::LLLexer(MemoryBuffer *StartBuf) : CurLineNo(1), CurBuf(StartBuf) {
155 CurPtr = CurBuf->getBufferStart();
156}
157
158std::string LLLexer::getFilename() const {
159 return CurBuf->getBufferIdentifier();
160}
161
162int LLLexer::getNextChar() {
163 char CurChar = *CurPtr++;
164 switch (CurChar) {
165 default: return (unsigned char)CurChar;
166 case 0:
167 // A nul character in the stream is either the end of the current buffer or
168 // a random nul in the file. Disambiguate that here.
169 if (CurPtr-1 != CurBuf->getBufferEnd())
170 return 0; // Just whitespace.
171
172 // Otherwise, return end of file.
173 --CurPtr; // Another call to lex will return EOF again.
174 return EOF;
175 case '\n':
176 case '\r':
177 // Handle the newline character by ignoring it and incrementing the line
178 // count. However, be careful about 'dos style' files with \n\r in them.
179 // Only treat a \n\r or \r\n as a single line.
180 if ((*CurPtr == '\n' || (*CurPtr == '\r')) &&
181 *CurPtr != CurChar)
182 ++CurPtr; // Eat the two char newline sequence.
183
184 ++CurLineNo;
185 return '\n';
186 }
187}
188
189
190int LLLexer::LexToken() {
191 TokStart = CurPtr;
192
193 int CurChar = getNextChar();
194
195 switch (CurChar) {
196 default:
197 // Handle letters: [a-zA-Z_]
198 if (isalpha(CurChar) || CurChar == '_')
199 return LexIdentifier();
200
201 return CurChar;
202 case EOF: return YYEOF;
203 case 0:
204 case ' ':
205 case '\t':
206 case '\n':
207 case '\r':
208 // Ignore whitespace.
209 return LexToken();
210 case '+': return LexPositive();
211 case '@': return LexAt();
212 case '%': return LexPercent();
213 case '"': return LexQuote();
214 case '.':
215 if (const char *Ptr = isLabelTail(CurPtr)) {
216 CurPtr = Ptr;
217 llvmAsmlval.StrVal = new std::string(TokStart, CurPtr-1);
218 return LABELSTR;
219 }
220 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
221 CurPtr += 2;
222 return DOTDOTDOT;
223 }
224 return '.';
225 case '$':
226 if (const char *Ptr = isLabelTail(CurPtr)) {
227 CurPtr = Ptr;
228 llvmAsmlval.StrVal = new std::string(TokStart, CurPtr-1);
229 return LABELSTR;
230 }
231 return '$';
232 case ';':
233 SkipLineComment();
234 return LexToken();
235 case '0': case '1': case '2': case '3': case '4':
236 case '5': case '6': case '7': case '8': case '9':
237 case '-':
238 return LexDigitOrNegative();
239 }
240}
241
242void LLLexer::SkipLineComment() {
243 while (1) {
244 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
245 return;
246 }
247}
248
249/// LexAt - Lex all tokens that start with an @ character:
250/// AtStringConstant @\"[^\"]*\"
251/// GlobalVarName @[-a-zA-Z$._][-a-zA-Z$._0-9]*
252/// GlobalVarID @[0-9]+
253int LLLexer::LexAt() {
254 // Handle AtStringConstant: @\"[^\"]*\"
255 if (CurPtr[0] == '"') {
256 ++CurPtr;
257
258 while (1) {
259 int CurChar = getNextChar();
260
261 if (CurChar == EOF) {
262 GenerateError("End of file in global variable name");
263 return YYERROR;
264 }
265 if (CurChar == '"') {
266 llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
267 UnEscapeLexed(*llvmAsmlval.StrVal);
268 return ATSTRINGCONSTANT;
269 }
270 }
271 }
272
273 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
274 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
275 CurPtr[0] == '.' || CurPtr[0] == '_') {
276 ++CurPtr;
277 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
278 CurPtr[0] == '.' || CurPtr[0] == '_')
279 ++CurPtr;
280
281 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr); // Skip @
282 return GLOBALVAR;
283 }
284
285 // Handle GlobalVarID: @[0-9]+
286 if (isdigit(CurPtr[0])) {
287 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr);
288
289 uint64_t Val = atoull(TokStart+1, CurPtr);
290 if ((unsigned)Val != Val)
291 GenerateError("Invalid value number (too large)!");
292 llvmAsmlval.UIntVal = unsigned(Val);
293 return GLOBALVAL_ID;
294 }
295
296 return '@';
297}
298
299
300/// LexPercent - Lex all tokens that start with a % character:
301/// PctStringConstant %\"[^\"]*\"
302/// LocalVarName %[-a-zA-Z$._][-a-zA-Z$._0-9]*
303/// LocalVarID %[0-9]+
304int LLLexer::LexPercent() {
305 // Handle PctStringConstant: %\"[^\"]*\"
306 if (CurPtr[0] == '"') {
307 ++CurPtr;
308
309 while (1) {
310 int CurChar = getNextChar();
311
312 if (CurChar == EOF) {
313 GenerateError("End of file in local variable name");
314 return YYERROR;
315 }
316 if (CurChar == '"') {
317 llvmAsmlval.StrVal = new std::string(TokStart+2, CurPtr-1);
318 UnEscapeLexed(*llvmAsmlval.StrVal);
319 return PCTSTRINGCONSTANT;
320 }
321 }
322 }
323
324 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
325 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
326 CurPtr[0] == '.' || CurPtr[0] == '_') {
327 ++CurPtr;
328 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
329 CurPtr[0] == '.' || CurPtr[0] == '_')
330 ++CurPtr;
331
332 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr); // Skip %
333 return LOCALVAR;
334 }
335
336 // Handle LocalVarID: %[0-9]+
337 if (isdigit(CurPtr[0])) {
338 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr);
339
340 uint64_t Val = atoull(TokStart+1, CurPtr);
341 if ((unsigned)Val != Val)
342 GenerateError("Invalid value number (too large)!");
343 llvmAsmlval.UIntVal = unsigned(Val);
344 return LOCALVAL_ID;
345 }
346
347 return '%';
348}
349
350/// LexQuote - Lex all tokens that start with a " character:
351/// QuoteLabel "[^"]+":
352/// StringConstant "[^"]*"
353int LLLexer::LexQuote() {
354 while (1) {
355 int CurChar = getNextChar();
356
357 if (CurChar == EOF) {
358 GenerateError("End of file in quoted string");
359 return YYERROR;
360 }
361
362 if (CurChar != '"') continue;
363
364 if (CurPtr[0] != ':') {
365 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-1);
366 UnEscapeLexed(*llvmAsmlval.StrVal);
367 return STRINGCONSTANT;
368 }
369
370 ++CurPtr;
371 llvmAsmlval.StrVal = new std::string(TokStart+1, CurPtr-2);
372 UnEscapeLexed(*llvmAsmlval.StrVal);
373 return LABELSTR;
374 }
375}
376
377static bool JustWhitespaceNewLine(const char *&Ptr) {
378 const char *ThisPtr = Ptr;
379 while (*ThisPtr == ' ' || *ThisPtr == '\t')
380 ++ThisPtr;
381 if (*ThisPtr == '\n' || *ThisPtr == '\r') {
382 Ptr = ThisPtr;
383 return true;
384 }
385 return false;
386}
387
388
389/// LexIdentifier: Handle several related productions:
390/// Label [-a-zA-Z$._0-9]+:
391/// IntegerType i[0-9]+
392/// Keyword sdiv, float, ...
393/// HexIntConstant [us]0x[0-9A-Fa-f]+
394int LLLexer::LexIdentifier() {
395 const char *StartChar = CurPtr;
396 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
397 const char *KeywordEnd = 0;
398
399 for (; isLabelChar(*CurPtr); ++CurPtr) {
400 // If we decide this is an integer, remember the end of the sequence.
401 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
402 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
403 }
404
405 // If we stopped due to a colon, this really is a label.
406 if (*CurPtr == ':') {
407 llvmAsmlval.StrVal = new std::string(StartChar-1, CurPtr++);
408 return LABELSTR;
409 }
410
411 // Otherwise, this wasn't a label. If this was valid as an integer type,
412 // return it.
413 if (IntEnd == 0) IntEnd = CurPtr;
414 if (IntEnd != StartChar) {
415 CurPtr = IntEnd;
416 uint64_t NumBits = atoull(StartChar, CurPtr);
417 if (NumBits < IntegerType::MIN_INT_BITS ||
418 NumBits > IntegerType::MAX_INT_BITS) {
419 GenerateError("Bitwidth for integer type out of range!");
420 return YYERROR;
421 }
422 const Type* Ty = IntegerType::get(NumBits);
423 llvmAsmlval.PrimType = Ty;
424 return INTTYPE;
425 }
426
427 // Otherwise, this was a letter sequence. See which keyword this is.
428 if (KeywordEnd == 0) KeywordEnd = CurPtr;
429 CurPtr = KeywordEnd;
430 --StartChar;
431 unsigned Len = CurPtr-StartChar;
432#define KEYWORD(STR, TOK) \
433 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) return TOK;
434
435 KEYWORD("begin", BEGINTOK);
436 KEYWORD("end", ENDTOK);
437 KEYWORD("true", TRUETOK);
438 KEYWORD("false", FALSETOK);
439 KEYWORD("declare", DECLARE);
440 KEYWORD("define", DEFINE);
441 KEYWORD("global", GLOBAL);
442 KEYWORD("constant", CONSTANT);
443
444 KEYWORD("internal", INTERNAL);
445 KEYWORD("linkonce", LINKONCE);
446 KEYWORD("weak", WEAK);
447 KEYWORD("appending", APPENDING);
448 KEYWORD("dllimport", DLLIMPORT);
449 KEYWORD("dllexport", DLLEXPORT);
450 KEYWORD("hidden", HIDDEN);
451 KEYWORD("protected", PROTECTED);
452 KEYWORD("extern_weak", EXTERN_WEAK);
453 KEYWORD("external", EXTERNAL);
454 KEYWORD("thread_local", THREAD_LOCAL);
455 KEYWORD("zeroinitializer", ZEROINITIALIZER);
456 KEYWORD("undef", UNDEF);
457 KEYWORD("null", NULL_TOK);
458 KEYWORD("to", TO);
459 KEYWORD("tail", TAIL);
460 KEYWORD("target", TARGET);
461 KEYWORD("triple", TRIPLE);
462 KEYWORD("deplibs", DEPLIBS);
463 KEYWORD("datalayout", DATALAYOUT);
464 KEYWORD("volatile", VOLATILE);
465 KEYWORD("align", ALIGN);
Christopher Lambfe63fb92007-12-11 08:59:05 +0000466 KEYWORD("addrspace", ADDRSPACE);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000467 KEYWORD("section", SECTION);
468 KEYWORD("alias", ALIAS);
469 KEYWORD("module", MODULE);
470 KEYWORD("asm", ASM_TOK);
471 KEYWORD("sideeffect", SIDEEFFECT);
Gordon Henriksen80a75bf2007-12-10 03:18:06 +0000472 KEYWORD("gc", GC);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000473
474 KEYWORD("cc", CC_TOK);
475 KEYWORD("ccc", CCC_TOK);
476 KEYWORD("fastcc", FASTCC_TOK);
477 KEYWORD("coldcc", COLDCC_TOK);
478 KEYWORD("x86_stdcallcc", X86_STDCALLCC_TOK);
479 KEYWORD("x86_fastcallcc", X86_FASTCALLCC_TOK);
480
481 KEYWORD("signext", SIGNEXT);
482 KEYWORD("zeroext", ZEROEXT);
483 KEYWORD("inreg", INREG);
484 KEYWORD("sret", SRET);
485 KEYWORD("nounwind", NOUNWIND);
486 KEYWORD("noreturn", NORETURN);
487 KEYWORD("noalias", NOALIAS);
488 KEYWORD("byval", BYVAL);
489 KEYWORD("nest", NEST);
Duncan Sandsed4a2f12007-11-22 20:23:04 +0000490 KEYWORD("readnone", READNONE);
491 KEYWORD("readonly", READONLY);
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000492
493 KEYWORD("type", TYPE);
494 KEYWORD("opaque", OPAQUE);
495
496 KEYWORD("eq" , EQ);
497 KEYWORD("ne" , NE);
498 KEYWORD("slt", SLT);
499 KEYWORD("sgt", SGT);
500 KEYWORD("sle", SLE);
501 KEYWORD("sge", SGE);
502 KEYWORD("ult", ULT);
503 KEYWORD("ugt", UGT);
504 KEYWORD("ule", ULE);
505 KEYWORD("uge", UGE);
506 KEYWORD("oeq", OEQ);
507 KEYWORD("one", ONE);
508 KEYWORD("olt", OLT);
509 KEYWORD("ogt", OGT);
510 KEYWORD("ole", OLE);
511 KEYWORD("oge", OGE);
512 KEYWORD("ord", ORD);
513 KEYWORD("uno", UNO);
514 KEYWORD("ueq", UEQ);
515 KEYWORD("une", UNE);
516#undef KEYWORD
517
518 // Keywords for types.
519#define TYPEKEYWORD(STR, LLVMTY, TOK) \
520 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
521 llvmAsmlval.PrimType = LLVMTY; return TOK; }
522 TYPEKEYWORD("void", Type::VoidTy, VOID);
523 TYPEKEYWORD("float", Type::FloatTy, FLOAT);
524 TYPEKEYWORD("double", Type::DoubleTy, DOUBLE);
525 TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty, X86_FP80);
526 TYPEKEYWORD("fp128", Type::FP128Ty, FP128);
527 TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty, PPC_FP128);
528 TYPEKEYWORD("label", Type::LabelTy, LABEL);
529#undef TYPEKEYWORD
530
531 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
532 // to avoid conflicting with the sext/zext instructions, below.
533 if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
534 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
535 if (JustWhitespaceNewLine(CurPtr))
536 return SIGNEXT;
537 } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
538 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
539 if (JustWhitespaceNewLine(CurPtr))
540 return ZEROEXT;
541 }
542
543 // Keywords for instructions.
544#define INSTKEYWORD(STR, type, Enum, TOK) \
545 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
546 llvmAsmlval.type = Instruction::Enum; return TOK; }
547
548 INSTKEYWORD("add", BinaryOpVal, Add, ADD);
549 INSTKEYWORD("sub", BinaryOpVal, Sub, SUB);
550 INSTKEYWORD("mul", BinaryOpVal, Mul, MUL);
551 INSTKEYWORD("udiv", BinaryOpVal, UDiv, UDIV);
552 INSTKEYWORD("sdiv", BinaryOpVal, SDiv, SDIV);
553 INSTKEYWORD("fdiv", BinaryOpVal, FDiv, FDIV);
554 INSTKEYWORD("urem", BinaryOpVal, URem, UREM);
555 INSTKEYWORD("srem", BinaryOpVal, SRem, SREM);
556 INSTKEYWORD("frem", BinaryOpVal, FRem, FREM);
557 INSTKEYWORD("shl", BinaryOpVal, Shl, SHL);
558 INSTKEYWORD("lshr", BinaryOpVal, LShr, LSHR);
559 INSTKEYWORD("ashr", BinaryOpVal, AShr, ASHR);
560 INSTKEYWORD("and", BinaryOpVal, And, AND);
561 INSTKEYWORD("or", BinaryOpVal, Or , OR );
562 INSTKEYWORD("xor", BinaryOpVal, Xor, XOR);
563 INSTKEYWORD("icmp", OtherOpVal, ICmp, ICMP);
564 INSTKEYWORD("fcmp", OtherOpVal, FCmp, FCMP);
565
566 INSTKEYWORD("phi", OtherOpVal, PHI, PHI_TOK);
567 INSTKEYWORD("call", OtherOpVal, Call, CALL);
568 INSTKEYWORD("trunc", CastOpVal, Trunc, TRUNC);
569 INSTKEYWORD("zext", CastOpVal, ZExt, ZEXT);
570 INSTKEYWORD("sext", CastOpVal, SExt, SEXT);
571 INSTKEYWORD("fptrunc", CastOpVal, FPTrunc, FPTRUNC);
572 INSTKEYWORD("fpext", CastOpVal, FPExt, FPEXT);
573 INSTKEYWORD("uitofp", CastOpVal, UIToFP, UITOFP);
574 INSTKEYWORD("sitofp", CastOpVal, SIToFP, SITOFP);
575 INSTKEYWORD("fptoui", CastOpVal, FPToUI, FPTOUI);
576 INSTKEYWORD("fptosi", CastOpVal, FPToSI, FPTOSI);
577 INSTKEYWORD("inttoptr", CastOpVal, IntToPtr, INTTOPTR);
578 INSTKEYWORD("ptrtoint", CastOpVal, PtrToInt, PTRTOINT);
579 INSTKEYWORD("bitcast", CastOpVal, BitCast, BITCAST);
580 INSTKEYWORD("select", OtherOpVal, Select, SELECT);
581 INSTKEYWORD("va_arg", OtherOpVal, VAArg , VAARG);
582 INSTKEYWORD("ret", TermOpVal, Ret, RET);
583 INSTKEYWORD("br", TermOpVal, Br, BR);
584 INSTKEYWORD("switch", TermOpVal, Switch, SWITCH);
585 INSTKEYWORD("invoke", TermOpVal, Invoke, INVOKE);
586 INSTKEYWORD("unwind", TermOpVal, Unwind, UNWIND);
587 INSTKEYWORD("unreachable", TermOpVal, Unreachable, UNREACHABLE);
588
589 INSTKEYWORD("malloc", MemOpVal, Malloc, MALLOC);
590 INSTKEYWORD("alloca", MemOpVal, Alloca, ALLOCA);
591 INSTKEYWORD("free", MemOpVal, Free, FREE);
592 INSTKEYWORD("load", MemOpVal, Load, LOAD);
593 INSTKEYWORD("store", MemOpVal, Store, STORE);
594 INSTKEYWORD("getelementptr", MemOpVal, GetElementPtr, GETELEMENTPTR);
595
596 INSTKEYWORD("extractelement", OtherOpVal, ExtractElement, EXTRACTELEMENT);
597 INSTKEYWORD("insertelement", OtherOpVal, InsertElement, INSERTELEMENT);
598 INSTKEYWORD("shufflevector", OtherOpVal, ShuffleVector, SHUFFLEVECTOR);
599#undef INSTKEYWORD
600
601 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
602 // the CFE to avoid forcing it to deal with 64-bit numbers.
603 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
604 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
605 int len = CurPtr-TokStart-3;
606 uint32_t bits = len * 4;
607 APInt Tmp(bits, TokStart+3, len, 16);
608 uint32_t activeBits = Tmp.getActiveBits();
609 if (activeBits > 0 && activeBits < bits)
610 Tmp.trunc(activeBits);
611 if (Tmp.getBitWidth() > 64) {
612 llvmAsmlval.APIntVal = new APInt(Tmp);
613 return TokStart[0] == 's' ? ESAPINTVAL : EUAPINTVAL;
614 } else if (TokStart[0] == 's') {
615 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
616 return ESINT64VAL;
617 } else {
618 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
619 return EUINT64VAL;
620 }
621 }
622
Chris Lattner4ce0df62007-11-18 18:43:24 +0000623 // If this is "cc1234", return this as just "cc".
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000624 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
625 CurPtr = TokStart+2;
626 return CC_TOK;
627 }
628
Chris Lattner4ce0df62007-11-18 18:43:24 +0000629 // If this starts with "call", return it as CALL. This is to support old
630 // broken .ll files. FIXME: remove this with LLVM 3.0.
631 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
632 CurPtr = TokStart+4;
633 llvmAsmlval.OtherOpVal = Instruction::Call;
634 return CALL;
635 }
636
Chris Lattner8e3a8e02007-11-18 08:46:26 +0000637 // Finally, if this isn't known, return just a single character.
638 CurPtr = TokStart+1;
639 return TokStart[0];
640}
641
642
643/// Lex0x: Handle productions that start with 0x, knowing that it matches and
644/// that this is not a label:
645/// HexFPConstant 0x[0-9A-Fa-f]+
646/// HexFP80Constant 0xK[0-9A-Fa-f]+
647/// HexFP128Constant 0xL[0-9A-Fa-f]+
648/// HexPPC128Constant 0xM[0-9A-Fa-f]+
649int LLLexer::Lex0x() {
650 CurPtr = TokStart + 2;
651
652 char Kind;
653 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
654 Kind = *CurPtr++;
655 } else {
656 Kind = 'J';
657 }
658
659 if (!isxdigit(CurPtr[0])) {
660 // Bad token, return it as just zero.
661 CurPtr = TokStart+1;
662 return '0';
663 }
664
665 while (isxdigit(CurPtr[0]))
666 ++CurPtr;
667
668 if (Kind == 'J') {
669 // HexFPConstant - Floating point constant represented in IEEE format as a
670 // hexadecimal number for when exponential notation is not precise enough.
671 // Float and double only.
672 llvmAsmlval.FPVal = new APFloat(HexToFP(TokStart+2, CurPtr));
673 return FPVAL;
674 }
675
676 uint64_t Pair[2];
677 HexToIntPair(TokStart+3, CurPtr, Pair);
678 switch (Kind) {
679 default: assert(0 && "Unknown kind!");
680 case 'K':
681 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
682 llvmAsmlval.FPVal = new APFloat(APInt(80, 2, Pair));
683 return FPVAL;
684 case 'L':
685 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
686 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair), true);
687 return FPVAL;
688 case 'M':
689 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
690 llvmAsmlval.FPVal = new APFloat(APInt(128, 2, Pair));
691 return FPVAL;
692 }
693}
694
695/// LexIdentifier: Handle several related productions:
696/// Label [-a-zA-Z$._0-9]+:
697/// NInteger -[0-9]+
698/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
699/// PInteger [0-9]+
700/// HexFPConstant 0x[0-9A-Fa-f]+
701/// HexFP80Constant 0xK[0-9A-Fa-f]+
702/// HexFP128Constant 0xL[0-9A-Fa-f]+
703/// HexPPC128Constant 0xM[0-9A-Fa-f]+
704int LLLexer::LexDigitOrNegative() {
705 // If the letter after the negative is a number, this is probably a label.
706 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
707 // Okay, this is not a number after the -, it's probably a label.
708 if (const char *End = isLabelTail(CurPtr)) {
709 llvmAsmlval.StrVal = new std::string(TokStart, End-1);
710 CurPtr = End;
711 return LABELSTR;
712 }
713
714 return CurPtr[-1];
715 }
716
717 // At this point, it is either a label, int or fp constant.
718
719 // Skip digits, we have at least one.
720 for (; isdigit(CurPtr[0]); ++CurPtr);
721
722 // Check to see if this really is a label afterall, e.g. "-1:".
723 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
724 if (const char *End = isLabelTail(CurPtr)) {
725 llvmAsmlval.StrVal = new std::string(TokStart, End-1);
726 CurPtr = End;
727 return LABELSTR;
728 }
729 }
730
731 // If the next character is a '.', then it is a fp value, otherwise its
732 // integer.
733 if (CurPtr[0] != '.') {
734 if (TokStart[0] == '0' && TokStart[1] == 'x')
735 return Lex0x();
736 unsigned Len = CurPtr-TokStart;
737 uint32_t numBits = ((Len * 64) / 19) + 2;
738 APInt Tmp(numBits, TokStart, Len, 10);
739 if (TokStart[0] == '-') {
740 uint32_t minBits = Tmp.getMinSignedBits();
741 if (minBits > 0 && minBits < numBits)
742 Tmp.trunc(minBits);
743 if (Tmp.getBitWidth() > 64) {
744 llvmAsmlval.APIntVal = new APInt(Tmp);
745 return ESAPINTVAL;
746 } else {
747 llvmAsmlval.SInt64Val = Tmp.getSExtValue();
748 return ESINT64VAL;
749 }
750 } else {
751 uint32_t activeBits = Tmp.getActiveBits();
752 if (activeBits > 0 && activeBits < numBits)
753 Tmp.trunc(activeBits);
754 if (Tmp.getBitWidth() > 64) {
755 llvmAsmlval.APIntVal = new APInt(Tmp);
756 return EUAPINTVAL;
757 } else {
758 llvmAsmlval.UInt64Val = Tmp.getZExtValue();
759 return EUINT64VAL;
760 }
761 }
762 }
763
764 ++CurPtr;
765
766 // Skip over [0-9]*([eE][-+]?[0-9]+)?
767 while (isdigit(CurPtr[0])) ++CurPtr;
768
769 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
770 if (isdigit(CurPtr[1]) ||
771 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
772 CurPtr += 2;
773 while (isdigit(CurPtr[0])) ++CurPtr;
774 }
775 }
776
777 llvmAsmlval.FPVal = new APFloat(atof(TokStart));
778 return FPVAL;
779}
780
781/// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
782int LLLexer::LexPositive() {
783 // If the letter after the negative is a number, this is probably not a
784 // label.
785 if (!isdigit(CurPtr[0]))
786 return CurPtr[-1];
787
788 // Skip digits.
789 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr);
790
791 // At this point, we need a '.'.
792 if (CurPtr[0] != '.') {
793 CurPtr = TokStart+1;
794 return TokStart[0];
795 }
796
797 ++CurPtr;
798
799 // Skip over [0-9]*([eE][-+]?[0-9]+)?
800 while (isdigit(CurPtr[0])) ++CurPtr;
801
802 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
803 if (isdigit(CurPtr[1]) ||
804 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
805 CurPtr += 2;
806 while (isdigit(CurPtr[0])) ++CurPtr;
807 }
808 }
809
810 llvmAsmlval.FPVal = new APFloat(atof(TokStart));
811 return FPVAL;
812}
813
814
815//===----------------------------------------------------------------------===//
816// Define the interface to this file.
817//===----------------------------------------------------------------------===//
818
819static LLLexer *TheLexer;
820
821void InitLLLexer(llvm::MemoryBuffer *MB) {
822 assert(TheLexer == 0 && "LL Lexer isn't reentrant yet");
823 TheLexer = new LLLexer(MB);
824}
825
826int llvmAsmlex() {
827 return TheLexer->LexToken();
828}
829const char *LLLgetTokenStart() { return TheLexer->getTokStart(); }
830unsigned LLLgetTokenLength() { return TheLexer->getTokLength(); }
831std::string LLLgetFilename() { return TheLexer->getFilename(); }
832unsigned LLLgetLineNo() { return TheLexer->getLineNo(); }
833
834void FreeLexer() {
835 delete TheLexer;
836 TheLexer = 0;
837}