Teach the .ll parser to handle named metadata with non-simple names.
[llvm/stm8.git] / lib / AsmParser / LLLexer.cpp
bloba363a653b21deffe2680dd34d0ee32d0d196ade4
1 //===- LLLexer.cpp - Lexer for .ll Files ----------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implement the Lexer for .ll files.
12 //===----------------------------------------------------------------------===//
14 #include "LLLexer.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Instruction.h"
17 #include "llvm/LLVMContext.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Assembly/Parser.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include <cctype>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29 using namespace llvm;
31 bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
32 ErrorInfo = SM.GetMessage(ErrorLoc, Msg, "error");
33 return true;
36 //===----------------------------------------------------------------------===//
37 // Helper functions.
38 //===----------------------------------------------------------------------===//
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...
44 uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
45 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!!!
51 Error("constant bigger than 64 bits detected!");
52 return 0;
55 return Result;
58 uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
59 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;
71 if (Result < OldRes) { // Uh, oh, overflow detected!!!
72 Error("constant bigger than 64 bits detected!");
73 return 0;
76 return Result;
79 void LLLexer::HexToIntPair(const char *Buffer, const char *End,
80 uint64_t Pair[2]) {
81 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;
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;
104 if (Buffer != End)
105 Error("constant bigger than 128 bits detected!");
108 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
109 /// { low64, high16 } as usual for an APInt.
110 void 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;
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;
135 if (Buffer != End)
136 Error("constant bigger than 128 bits detected!");
139 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
140 // appropriate character.
141 static void UnEscapeLexed(std::string &Str) {
142 if (Str.empty()) return;
144 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++;
160 } else {
161 *BOut++ = *BIn++;
164 Str.resize(BOut-Buffer);
167 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
168 static bool isLabelChar(char C) {
169 return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
173 /// isLabelTail - Return true if this pointer points to a valid end of a label.
174 static 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;
184 //===----------------------------------------------------------------------===//
185 // Lexer definition.
186 //===----------------------------------------------------------------------===//
188 LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err,
189 LLVMContext &C)
190 : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
191 CurPtr = CurBuf->getBufferStart();
194 std::string LLLexer::getFilename() const {
195 return CurBuf->getBufferIdentifier();
198 int 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.
208 // Otherwise, return end of file.
209 --CurPtr; // Another call to lex will return EOF again.
210 return EOF;
215 lltok::Kind LLLexer::LexToken() {
216 TokStart = CurPtr;
218 int CurChar = getNextChar();
219 switch (CurChar) {
220 default:
221 // Handle letters: [a-zA-Z_]
222 if (isalpha(CurChar) || CurChar == '_')
223 return LexIdentifier();
225 return lltok::Error;
226 case EOF: return lltok::Eof;
227 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;
241 StrVal.assign(TokStart, CurPtr-1);
242 return lltok::LabelStr;
244 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
245 CurPtr += 2;
246 return lltok::dotdotdot;
248 return lltok::Error;
249 case '$':
250 if (const char *Ptr = isLabelTail(CurPtr)) {
251 CurPtr = Ptr;
252 StrVal.assign(TokStart, CurPtr-1);
253 return lltok::LabelStr;
255 return lltok::Error;
256 case ';':
257 SkipLineComment();
258 return LexToken();
259 case '!': return LexExclaim();
260 case '0': case '1': case '2': case '3': case '4':
261 case '5': case '6': case '7': case '8': case '9':
262 case '-':
263 return LexDigitOrNegative();
264 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;
279 void LLLexer::SkipLineComment() {
280 while (1) {
281 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
282 return;
286 /// LexAt - Lex all tokens that start with an @ character:
287 /// GlobalVar @\"[^\"]*\"
288 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
289 /// GlobalVarID @[0-9]+
290 lltok::Kind LLLexer::LexAt() {
291 // Handle AtStringConstant: @\"[^\"]*\"
292 if (CurPtr[0] == '"') {
293 ++CurPtr;
295 while (1) {
296 int CurChar = getNextChar();
298 if (CurChar == EOF) {
299 Error("end of file in global variable name");
300 return lltok::Error;
302 if (CurChar == '"') {
303 StrVal.assign(TokStart+2, CurPtr-1);
304 UnEscapeLexed(StrVal);
305 return lltok::GlobalVar;
310 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
311 if (ReadVarName())
312 return lltok::GlobalVar;
314 // Handle GlobalVarID: @[0-9]+
315 if (isdigit(CurPtr[0])) {
316 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
317 /*empty*/;
319 uint64_t Val = atoull(TokStart+1, CurPtr);
320 if ((unsigned)Val != Val)
321 Error("invalid value number (too large)!");
322 UIntVal = unsigned(Val);
323 return lltok::GlobalID;
326 return lltok::Error;
329 /// ReadString - Read a string until the closing quote.
330 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
331 const char *Start = CurPtr;
332 while (1) {
333 int CurChar = getNextChar();
335 if (CurChar == EOF) {
336 Error("end of file in string constant");
337 return lltok::Error;
339 if (CurChar == '"') {
340 StrVal.assign(Start, CurPtr-1);
341 UnEscapeLexed(StrVal);
342 return kind;
347 /// ReadVarName - Read the rest of a token containing a variable name.
348 bool 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;
357 StrVal.assign(NameStart, CurPtr);
358 return true;
360 return false;
363 /// LexPercent - Lex all tokens that start with a % character:
364 /// LocalVar ::= %\"[^\"]*\"
365 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
366 /// LocalVarID ::= %[0-9]+
367 lltok::Kind LLLexer::LexPercent() {
368 // Handle LocalVarName: %\"[^\"]*\"
369 if (CurPtr[0] == '"') {
370 ++CurPtr;
371 return ReadString(lltok::LocalVar);
374 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
375 if (ReadVarName())
376 return lltok::LocalVar;
378 // Handle LocalVarID: %[0-9]+
379 if (isdigit(CurPtr[0])) {
380 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
381 /*empty*/;
383 uint64_t Val = atoull(TokStart+1, CurPtr);
384 if ((unsigned)Val != Val)
385 Error("invalid value number (too large)!");
386 UIntVal = unsigned(Val);
387 return lltok::LocalVarID;
390 return lltok::Error;
393 /// LexQuote - Lex all tokens that start with a " character:
394 /// QuoteLabel "[^"]+":
395 /// StringConstant "[^"]*"
396 lltok::Kind LLLexer::LexQuote() {
397 lltok::Kind kind = ReadString(lltok::StringConstant);
398 if (kind == lltok::Error || kind == lltok::Eof)
399 return kind;
401 if (CurPtr[0] == ':') {
402 ++CurPtr;
403 kind = lltok::LabelStr;
406 return kind;
409 static 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;
417 return false;
420 /// LexExclaim:
421 /// !foo
422 /// !
423 lltok::Kind LLLexer::LexExclaim() {
424 // Lex a metadata name as a MetadataVar.
425 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
426 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
427 ++CurPtr;
428 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
429 CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
430 ++CurPtr;
432 StrVal.assign(TokStart+1, CurPtr); // Skip !
433 UnEscapeLexed(StrVal);
434 return lltok::MetadataVar;
436 return lltok::exclaim;
439 /// 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]+
444 lltok::Kind LLLexer::LexIdentifier() {
445 const char *StartChar = CurPtr;
446 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
447 const char *KeywordEnd = 0;
449 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;
455 // If we stopped due to a colon, this really is a label.
456 if (*CurPtr == ':') {
457 StrVal.assign(StartChar-1, CurPtr++);
458 return lltok::LabelStr;
461 // 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);
467 if (NumBits < IntegerType::MIN_INT_BITS ||
468 NumBits > IntegerType::MAX_INT_BITS) {
469 Error("bitwidth for integer type out of range!");
470 return lltok::Error;
472 TyVal = IntegerType::get(Context, NumBits);
473 return lltok::Type;
476 // 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;
481 #define KEYWORD(STR) \
482 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
483 return lltok::kw_##STR;
485 KEYWORD(begin); KEYWORD(end);
486 KEYWORD(true); KEYWORD(false);
487 KEYWORD(declare); KEYWORD(define);
488 KEYWORD(global); KEYWORD(constant);
490 KEYWORD(private);
491 KEYWORD(linker_private);
492 KEYWORD(linker_private_weak);
493 KEYWORD(linker_private_weak_def_auto);
494 KEYWORD(internal);
495 KEYWORD(available_externally);
496 KEYWORD(linkonce);
497 KEYWORD(linkonce_odr);
498 KEYWORD(weak);
499 KEYWORD(weak_odr);
500 KEYWORD(appending);
501 KEYWORD(dllimport);
502 KEYWORD(dllexport);
503 KEYWORD(common);
504 KEYWORD(default);
505 KEYWORD(hidden);
506 KEYWORD(protected);
507 KEYWORD(unnamed_addr);
508 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);
521 KEYWORD(nuw);
522 KEYWORD(nsw);
523 KEYWORD(exact);
524 KEYWORD(inbounds);
525 KEYWORD(align);
526 KEYWORD(addrspace);
527 KEYWORD(section);
528 KEYWORD(alias);
529 KEYWORD(module);
530 KEYWORD(asm);
531 KEYWORD(sideeffect);
532 KEYWORD(alignstack);
533 KEYWORD(gc);
535 KEYWORD(ccc);
536 KEYWORD(fastcc);
537 KEYWORD(coldcc);
538 KEYWORD(x86_stdcallcc);
539 KEYWORD(x86_fastcallcc);
540 KEYWORD(x86_thiscallcc);
541 KEYWORD(arm_apcscc);
542 KEYWORD(arm_aapcscc);
543 KEYWORD(arm_aapcs_vfpcc);
544 KEYWORD(msp430_intrcc);
545 KEYWORD(ptx_kernel);
546 KEYWORD(ptx_device);
548 KEYWORD(cc);
549 KEYWORD(c);
551 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);
563 KEYWORD(uwtable);
565 KEYWORD(inlinehint);
566 KEYWORD(noinline);
567 KEYWORD(alwaysinline);
568 KEYWORD(optsize);
569 KEYWORD(ssp);
570 KEYWORD(sspreq);
571 KEYWORD(noredzone);
572 KEYWORD(noimplicitfloat);
573 KEYWORD(naked);
574 KEYWORD(hotpatch);
576 KEYWORD(type);
577 KEYWORD(opaque);
579 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);
584 KEYWORD(x);
585 KEYWORD(blockaddress);
586 #undef KEYWORD
588 // Keywords for types.
589 #define TYPEKEYWORD(STR, LLVMTY) \
590 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
591 TyVal = LLVMTY; return lltok::Type; }
592 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));
600 TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
601 #undef TYPEKEYWORD
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))
608 return lltok::kw_signext;
609 } 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))
612 return lltok::kw_zeroext;
613 } else if (Len == 6 && !memcmp(StartChar, "malloc", 6)) {
614 // FIXME: Remove in LLVM 3.0.
615 // Autoupgrade malloc instruction.
616 return lltok::kw_malloc;
617 } else if (Len == 4 && !memcmp(StartChar, "free", 4)) {
618 // FIXME: Remove in LLVM 3.0.
619 // Autoupgrade malloc instruction.
620 return lltok::kw_free;
623 // Keywords for instructions.
624 #define INSTKEYWORD(STR, Enum) \
625 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
626 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
628 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
629 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
630 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
631 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);
637 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);
656 INSTKEYWORD(indirectbr, IndirectBr);
657 INSTKEYWORD(invoke, Invoke);
658 INSTKEYWORD(unwind, Unwind);
659 INSTKEYWORD(unreachable, Unreachable);
661 INSTKEYWORD(alloca, Alloca);
662 INSTKEYWORD(load, Load);
663 INSTKEYWORD(store, Store);
664 INSTKEYWORD(getelementptr, GetElementPtr);
666 INSTKEYWORD(extractelement, ExtractElement);
667 INSTKEYWORD(insertelement, InsertElement);
668 INSTKEYWORD(shufflevector, ShuffleVector);
669 INSTKEYWORD(getresult, ExtractValue);
670 INSTKEYWORD(extractvalue, ExtractValue);
671 INSTKEYWORD(insertvalue, InsertValue);
672 #undef INSTKEYWORD
674 // 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;
680 APInt Tmp(bits, StringRef(TokStart+3, len), 16);
681 uint32_t activeBits = Tmp.getActiveBits();
682 if (activeBits > 0 && activeBits < bits)
683 Tmp = Tmp.trunc(activeBits);
684 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
685 return lltok::APSInt;
688 // If this is "cc1234", return this as just "cc".
689 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
690 CurPtr = TokStart+2;
691 return lltok::kw_cc;
694 // 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;
698 UIntVal = Instruction::Call;
699 return lltok::kw_call;
702 // Finally, if this isn't known, return an error.
703 CurPtr = TokStart+1;
704 return lltok::Error;
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]+
714 lltok::Kind LLLexer::Lex0x() {
715 CurPtr = TokStart + 2;
717 char Kind;
718 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
719 Kind = *CurPtr++;
720 } else {
721 Kind = 'J';
724 if (!isxdigit(CurPtr[0])) {
725 // Bad token, return it as an error.
726 CurPtr = TokStart+1;
727 return lltok::Error;
730 while (isxdigit(CurPtr[0]))
731 ++CurPtr;
733 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.
737 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
738 return lltok::APFloat;
741 uint64_t Pair[2];
742 switch (Kind) {
743 default: llvm_unreachable("Unknown kind!");
744 case 'K':
745 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
746 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
747 APFloatVal = APFloat(APInt(80, 2, Pair));
748 return lltok::APFloat;
749 case 'L':
750 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
751 HexToIntPair(TokStart+3, CurPtr, Pair);
752 APFloatVal = APFloat(APInt(128, 2, Pair), true);
753 return lltok::APFloat;
754 case 'M':
755 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
756 HexToIntPair(TokStart+3, CurPtr, Pair);
757 APFloatVal = APFloat(APInt(128, 2, Pair));
758 return lltok::APFloat;
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]+
771 lltok::Kind LLLexer::LexDigitOrNegative() {
772 // 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)) {
776 StrVal.assign(TokStart, End-1);
777 CurPtr = End;
778 return lltok::LabelStr;
781 return lltok::Error;
784 // At this point, it is either a label, int or fp constant.
786 // Skip digits, we have at least one.
787 for (; isdigit(CurPtr[0]); ++CurPtr)
788 /*empty*/;
790 // 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)) {
793 StrVal.assign(TokStart, End-1);
794 CurPtr = End;
795 return lltok::LabelStr;
799 // 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;
806 APInt Tmp(numBits, StringRef(TokStart, Len), 10);
807 if (TokStart[0] == '-') {
808 uint32_t minBits = Tmp.getMinSignedBits();
809 if (minBits > 0 && minBits < numBits)
810 Tmp = Tmp.trunc(minBits);
811 APSIntVal = APSInt(Tmp, false);
812 } else {
813 uint32_t activeBits = Tmp.getActiveBits();
814 if (activeBits > 0 && activeBits < numBits)
815 Tmp = Tmp.trunc(activeBits);
816 APSIntVal = APSInt(Tmp, true);
818 return lltok::APSInt;
821 ++CurPtr;
823 // Skip over [0-9]*([eE][-+]?[0-9]+)?
824 while (isdigit(CurPtr[0])) ++CurPtr;
826 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
827 if (isdigit(CurPtr[1]) ||
828 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
829 CurPtr += 2;
830 while (isdigit(CurPtr[0])) ++CurPtr;
834 APFloatVal = APFloat(std::atof(TokStart));
835 return lltok::APFloat;
838 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
839 lltok::Kind LLLexer::LexPositive() {
840 // If the letter after the negative is a number, this is probably not a
841 // label.
842 if (!isdigit(CurPtr[0]))
843 return lltok::Error;
845 // Skip digits.
846 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
847 /*empty*/;
849 // At this point, we need a '.'.
850 if (CurPtr[0] != '.') {
851 CurPtr = TokStart+1;
852 return lltok::Error;
855 ++CurPtr;
857 // Skip over [0-9]*([eE][-+]?[0-9]+)?
858 while (isdigit(CurPtr[0])) ++CurPtr;
860 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
861 if (isdigit(CurPtr[1]) ||
862 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
863 CurPtr += 2;
864 while (isdigit(CurPtr[0])) ++CurPtr;
868 APFloatVal = APFloat(std::atof(TokStart));
869 return lltok::APFloat;