Fix part 1 of pr4682. PICADD is a 16-bit instruction even in thumb2 mode.
[llvm/avr.git] / lib / AsmParser / LLLexer.cpp
bloba6e63448b2e83a64752cc266e229994df6a95cb0
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/Support/ErrorHandling.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Assembly/Parser.h"
24 #include <cstdlib>
25 #include <cstring>
26 using namespace llvm;
28 bool LLLexer::Error(LocTy ErrorLoc, const std::string &Msg) const {
29 ErrorInfo = SM.GetMessage(ErrorLoc, Msg, "error");
30 return true;
33 //===----------------------------------------------------------------------===//
34 // Helper functions.
35 //===----------------------------------------------------------------------===//
37 // atoull - Convert an ascii string of decimal digits into the unsigned long
38 // long representation... this does not have to do input error checking,
39 // because we know that the input will be matched by a suitable regex...
41 uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
42 uint64_t Result = 0;
43 for (; Buffer != End; Buffer++) {
44 uint64_t OldRes = Result;
45 Result *= 10;
46 Result += *Buffer-'0';
47 if (Result < OldRes) { // Uh, oh, overflow detected!!!
48 Error("constant bigger than 64 bits detected!");
49 return 0;
52 return Result;
55 uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
56 uint64_t Result = 0;
57 for (; Buffer != End; ++Buffer) {
58 uint64_t OldRes = Result;
59 Result *= 16;
60 char C = *Buffer;
61 if (C >= '0' && C <= '9')
62 Result += C-'0';
63 else if (C >= 'A' && C <= 'F')
64 Result += C-'A'+10;
65 else if (C >= 'a' && C <= 'f')
66 Result += C-'a'+10;
68 if (Result < OldRes) { // Uh, oh, overflow detected!!!
69 Error("constant bigger than 64 bits detected!");
70 return 0;
73 return Result;
76 void LLLexer::HexToIntPair(const char *Buffer, const char *End,
77 uint64_t Pair[2]) {
78 Pair[0] = 0;
79 for (int i=0; i<16; i++, Buffer++) {
80 assert(Buffer != End);
81 Pair[0] *= 16;
82 char C = *Buffer;
83 if (C >= '0' && C <= '9')
84 Pair[0] += C-'0';
85 else if (C >= 'A' && C <= 'F')
86 Pair[0] += C-'A'+10;
87 else if (C >= 'a' && C <= 'f')
88 Pair[0] += C-'a'+10;
90 Pair[1] = 0;
91 for (int i=0; i<16 && Buffer != End; i++, Buffer++) {
92 Pair[1] *= 16;
93 char C = *Buffer;
94 if (C >= '0' && C <= '9')
95 Pair[1] += C-'0';
96 else if (C >= 'A' && C <= 'F')
97 Pair[1] += C-'A'+10;
98 else if (C >= 'a' && C <= 'f')
99 Pair[1] += C-'a'+10;
101 if (Buffer != End)
102 Error("constant bigger than 128 bits detected!");
105 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
106 /// { low64, high16 } as usual for an APInt.
107 void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
108 uint64_t Pair[2]) {
109 Pair[1] = 0;
110 for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
111 assert(Buffer != End);
112 Pair[1] *= 16;
113 char C = *Buffer;
114 if (C >= '0' && C <= '9')
115 Pair[1] += C-'0';
116 else if (C >= 'A' && C <= 'F')
117 Pair[1] += C-'A'+10;
118 else if (C >= 'a' && C <= 'f')
119 Pair[1] += C-'a'+10;
121 Pair[0] = 0;
122 for (int i=0; i<16; i++, Buffer++) {
123 Pair[0] *= 16;
124 char C = *Buffer;
125 if (C >= '0' && C <= '9')
126 Pair[0] += C-'0';
127 else if (C >= 'A' && C <= 'F')
128 Pair[0] += C-'A'+10;
129 else if (C >= 'a' && C <= 'f')
130 Pair[0] += C-'a'+10;
132 if (Buffer != End)
133 Error("constant bigger than 128 bits detected!");
136 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
137 // appropriate character.
138 static void UnEscapeLexed(std::string &Str) {
139 if (Str.empty()) return;
141 char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
142 char *BOut = Buffer;
143 for (char *BIn = Buffer; BIn != EndBuffer; ) {
144 if (BIn[0] == '\\') {
145 if (BIn < EndBuffer-1 && BIn[1] == '\\') {
146 *BOut++ = '\\'; // Two \ becomes one
147 BIn += 2;
148 } else if (BIn < EndBuffer-2 && isxdigit(BIn[1]) && isxdigit(BIn[2])) {
149 char Tmp = BIn[3]; BIn[3] = 0; // Terminate string
150 *BOut = (char)strtol(BIn+1, 0, 16); // Convert to number
151 BIn[3] = Tmp; // Restore character
152 BIn += 3; // Skip over handled chars
153 ++BOut;
154 } else {
155 *BOut++ = *BIn++;
157 } else {
158 *BOut++ = *BIn++;
161 Str.resize(BOut-Buffer);
164 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
165 static bool isLabelChar(char C) {
166 return isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_';
170 /// isLabelTail - Return true if this pointer points to a valid end of a label.
171 static const char *isLabelTail(const char *CurPtr) {
172 while (1) {
173 if (CurPtr[0] == ':') return CurPtr+1;
174 if (!isLabelChar(CurPtr[0])) return 0;
175 ++CurPtr;
181 //===----------------------------------------------------------------------===//
182 // Lexer definition.
183 //===----------------------------------------------------------------------===//
185 LLLexer::LLLexer(MemoryBuffer *StartBuf, SourceMgr &sm, SMDiagnostic &Err,
186 LLVMContext &C)
187 : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
188 CurPtr = CurBuf->getBufferStart();
191 std::string LLLexer::getFilename() const {
192 return CurBuf->getBufferIdentifier();
195 int LLLexer::getNextChar() {
196 char CurChar = *CurPtr++;
197 switch (CurChar) {
198 default: return (unsigned char)CurChar;
199 case 0:
200 // A nul character in the stream is either the end of the current buffer or
201 // a random nul in the file. Disambiguate that here.
202 if (CurPtr-1 != CurBuf->getBufferEnd())
203 return 0; // Just whitespace.
205 // Otherwise, return end of file.
206 --CurPtr; // Another call to lex will return EOF again.
207 return EOF;
212 lltok::Kind LLLexer::LexToken() {
213 TokStart = CurPtr;
215 int CurChar = getNextChar();
216 switch (CurChar) {
217 default:
218 // Handle letters: [a-zA-Z_]
219 if (isalpha(CurChar) || CurChar == '_')
220 return LexIdentifier();
222 return lltok::Error;
223 case EOF: return lltok::Eof;
224 case 0:
225 case ' ':
226 case '\t':
227 case '\n':
228 case '\r':
229 // Ignore whitespace.
230 return LexToken();
231 case '+': return LexPositive();
232 case '@': return LexAt();
233 case '%': return LexPercent();
234 case '"': return LexQuote();
235 case '.':
236 if (const char *Ptr = isLabelTail(CurPtr)) {
237 CurPtr = Ptr;
238 StrVal.assign(TokStart, CurPtr-1);
239 return lltok::LabelStr;
241 if (CurPtr[0] == '.' && CurPtr[1] == '.') {
242 CurPtr += 2;
243 return lltok::dotdotdot;
245 return lltok::Error;
246 case '$':
247 if (const char *Ptr = isLabelTail(CurPtr)) {
248 CurPtr = Ptr;
249 StrVal.assign(TokStart, CurPtr-1);
250 return lltok::LabelStr;
252 return lltok::Error;
253 case ';':
254 SkipLineComment();
255 return LexToken();
256 case '!': return LexMetadata();
257 case '0': case '1': case '2': case '3': case '4':
258 case '5': case '6': case '7': case '8': case '9':
259 case '-':
260 return LexDigitOrNegative();
261 case '=': return lltok::equal;
262 case '[': return lltok::lsquare;
263 case ']': return lltok::rsquare;
264 case '{': return lltok::lbrace;
265 case '}': return lltok::rbrace;
266 case '<': return lltok::less;
267 case '>': return lltok::greater;
268 case '(': return lltok::lparen;
269 case ')': return lltok::rparen;
270 case ',': return lltok::comma;
271 case '*': return lltok::star;
272 case '\\': return lltok::backslash;
276 void LLLexer::SkipLineComment() {
277 while (1) {
278 if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
279 return;
283 /// LexAt - Lex all tokens that start with an @ character:
284 /// GlobalVar @\"[^\"]*\"
285 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
286 /// GlobalVarID @[0-9]+
287 lltok::Kind LLLexer::LexAt() {
288 // Handle AtStringConstant: @\"[^\"]*\"
289 if (CurPtr[0] == '"') {
290 ++CurPtr;
292 while (1) {
293 int CurChar = getNextChar();
295 if (CurChar == EOF) {
296 Error("end of file in global variable name");
297 return lltok::Error;
299 if (CurChar == '"') {
300 StrVal.assign(TokStart+2, CurPtr-1);
301 UnEscapeLexed(StrVal);
302 return lltok::GlobalVar;
307 // Handle GlobalVarName: @[-a-zA-Z$._][-a-zA-Z$._0-9]*
308 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
309 CurPtr[0] == '.' || CurPtr[0] == '_') {
310 ++CurPtr;
311 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
312 CurPtr[0] == '.' || CurPtr[0] == '_')
313 ++CurPtr;
315 StrVal.assign(TokStart+1, CurPtr); // Skip @
316 return lltok::GlobalVar;
319 // Handle GlobalVarID: @[0-9]+
320 if (isdigit(CurPtr[0])) {
321 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
322 /*empty*/;
324 uint64_t Val = atoull(TokStart+1, CurPtr);
325 if ((unsigned)Val != Val)
326 Error("invalid value number (too large)!");
327 UIntVal = unsigned(Val);
328 return lltok::GlobalID;
331 return lltok::Error;
335 /// LexPercent - Lex all tokens that start with a % character:
336 /// LocalVar ::= %\"[^\"]*\"
337 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
338 /// LocalVarID ::= %[0-9]+
339 lltok::Kind LLLexer::LexPercent() {
340 // Handle LocalVarName: %\"[^\"]*\"
341 if (CurPtr[0] == '"') {
342 ++CurPtr;
344 while (1) {
345 int CurChar = getNextChar();
347 if (CurChar == EOF) {
348 Error("end of file in string constant");
349 return lltok::Error;
351 if (CurChar == '"') {
352 StrVal.assign(TokStart+2, CurPtr-1);
353 UnEscapeLexed(StrVal);
354 return lltok::LocalVar;
359 // Handle LocalVarName: %[-a-zA-Z$._][-a-zA-Z$._0-9]*
360 if (isalpha(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
361 CurPtr[0] == '.' || CurPtr[0] == '_') {
362 ++CurPtr;
363 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
364 CurPtr[0] == '.' || CurPtr[0] == '_')
365 ++CurPtr;
367 StrVal.assign(TokStart+1, CurPtr); // Skip %
368 return lltok::LocalVar;
371 // Handle LocalVarID: %[0-9]+
372 if (isdigit(CurPtr[0])) {
373 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
374 /*empty*/;
376 uint64_t Val = atoull(TokStart+1, CurPtr);
377 if ((unsigned)Val != Val)
378 Error("invalid value number (too large)!");
379 UIntVal = unsigned(Val);
380 return lltok::LocalVarID;
383 return lltok::Error;
386 /// LexQuote - Lex all tokens that start with a " character:
387 /// QuoteLabel "[^"]+":
388 /// StringConstant "[^"]*"
389 lltok::Kind LLLexer::LexQuote() {
390 while (1) {
391 int CurChar = getNextChar();
393 if (CurChar == EOF) {
394 Error("end of file in quoted string");
395 return lltok::Error;
398 if (CurChar != '"') continue;
400 if (CurPtr[0] != ':') {
401 StrVal.assign(TokStart+1, CurPtr-1);
402 UnEscapeLexed(StrVal);
403 return lltok::StringConstant;
406 ++CurPtr;
407 StrVal.assign(TokStart+1, CurPtr-2);
408 UnEscapeLexed(StrVal);
409 return lltok::LabelStr;
413 static bool JustWhitespaceNewLine(const char *&Ptr) {
414 const char *ThisPtr = Ptr;
415 while (*ThisPtr == ' ' || *ThisPtr == '\t')
416 ++ThisPtr;
417 if (*ThisPtr == '\n' || *ThisPtr == '\r') {
418 Ptr = ThisPtr;
419 return true;
421 return false;
424 /// LexMetadata:
425 /// !{...}
426 /// !42
427 /// !foo
428 lltok::Kind LLLexer::LexMetadata() {
429 if (isalpha(CurPtr[0])) {
430 ++CurPtr;
431 while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' ||
432 CurPtr[0] == '.' || CurPtr[0] == '_')
433 ++CurPtr;
435 StrVal.assign(TokStart+1, CurPtr); // Skip !
436 return lltok::NamedMD;
438 return lltok::Metadata;
441 /// LexIdentifier: Handle several related productions:
442 /// Label [-a-zA-Z$._0-9]+:
443 /// IntegerType i[0-9]+
444 /// Keyword sdiv, float, ...
445 /// HexIntConstant [us]0x[0-9A-Fa-f]+
446 lltok::Kind LLLexer::LexIdentifier() {
447 const char *StartChar = CurPtr;
448 const char *IntEnd = CurPtr[-1] == 'i' ? 0 : StartChar;
449 const char *KeywordEnd = 0;
451 for (; isLabelChar(*CurPtr); ++CurPtr) {
452 // If we decide this is an integer, remember the end of the sequence.
453 if (!IntEnd && !isdigit(*CurPtr)) IntEnd = CurPtr;
454 if (!KeywordEnd && !isalnum(*CurPtr) && *CurPtr != '_') KeywordEnd = CurPtr;
457 // If we stopped due to a colon, this really is a label.
458 if (*CurPtr == ':') {
459 StrVal.assign(StartChar-1, CurPtr++);
460 return lltok::LabelStr;
463 // Otherwise, this wasn't a label. If this was valid as an integer type,
464 // return it.
465 if (IntEnd == 0) IntEnd = CurPtr;
466 if (IntEnd != StartChar) {
467 CurPtr = IntEnd;
468 uint64_t NumBits = atoull(StartChar, CurPtr);
469 if (NumBits < IntegerType::MIN_INT_BITS ||
470 NumBits > IntegerType::MAX_INT_BITS) {
471 Error("bitwidth for integer type out of range!");
472 return lltok::Error;
474 TyVal = IntegerType::get(NumBits);
475 return lltok::Type;
478 // Otherwise, this was a letter sequence. See which keyword this is.
479 if (KeywordEnd == 0) KeywordEnd = CurPtr;
480 CurPtr = KeywordEnd;
481 --StartChar;
482 unsigned Len = CurPtr-StartChar;
483 #define KEYWORD(STR) \
484 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) \
485 return lltok::kw_##STR;
487 KEYWORD(begin); KEYWORD(end);
488 KEYWORD(true); KEYWORD(false);
489 KEYWORD(declare); KEYWORD(define);
490 KEYWORD(global); KEYWORD(constant);
492 KEYWORD(private);
493 KEYWORD(linker_private);
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(extern_weak);
508 KEYWORD(external);
509 KEYWORD(thread_local);
510 KEYWORD(zeroinitializer);
511 KEYWORD(undef);
512 KEYWORD(null);
513 KEYWORD(to);
514 KEYWORD(tail);
515 KEYWORD(target);
516 KEYWORD(triple);
517 KEYWORD(deplibs);
518 KEYWORD(datalayout);
519 KEYWORD(volatile);
520 KEYWORD(nuw);
521 KEYWORD(nsw);
522 KEYWORD(exact);
523 KEYWORD(inbounds);
524 KEYWORD(align);
525 KEYWORD(addrspace);
526 KEYWORD(section);
527 KEYWORD(alias);
528 KEYWORD(module);
529 KEYWORD(asm);
530 KEYWORD(sideeffect);
531 KEYWORD(gc);
533 KEYWORD(ccc);
534 KEYWORD(fastcc);
535 KEYWORD(coldcc);
536 KEYWORD(x86_stdcallcc);
537 KEYWORD(x86_fastcallcc);
538 KEYWORD(arm_apcscc);
539 KEYWORD(arm_aapcscc);
540 KEYWORD(arm_aapcs_vfpcc);
542 KEYWORD(cc);
543 KEYWORD(c);
545 KEYWORD(signext);
546 KEYWORD(zeroext);
547 KEYWORD(inreg);
548 KEYWORD(sret);
549 KEYWORD(nounwind);
550 KEYWORD(noreturn);
551 KEYWORD(noalias);
552 KEYWORD(nocapture);
553 KEYWORD(byval);
554 KEYWORD(nest);
555 KEYWORD(readnone);
556 KEYWORD(readonly);
558 KEYWORD(noinline);
559 KEYWORD(alwaysinline);
560 KEYWORD(optsize);
561 KEYWORD(ssp);
562 KEYWORD(sspreq);
563 KEYWORD(noredzone);
564 KEYWORD(noimplicitfloat);
565 KEYWORD(naked);
567 KEYWORD(type);
568 KEYWORD(opaque);
570 KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
571 KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
572 KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
573 KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
575 KEYWORD(x);
576 #undef KEYWORD
578 // Keywords for types.
579 #define TYPEKEYWORD(STR, LLVMTY) \
580 if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) { \
581 TyVal = LLVMTY; return lltok::Type; }
582 TYPEKEYWORD("void", Type::VoidTy);
583 TYPEKEYWORD("float", Type::FloatTy);
584 TYPEKEYWORD("double", Type::DoubleTy);
585 TYPEKEYWORD("x86_fp80", Type::X86_FP80Ty);
586 TYPEKEYWORD("fp128", Type::FP128Ty);
587 TYPEKEYWORD("ppc_fp128", Type::PPC_FP128Ty);
588 TYPEKEYWORD("label", Type::LabelTy);
589 TYPEKEYWORD("metadata", Type::MetadataTy);
590 #undef TYPEKEYWORD
592 // Handle special forms for autoupgrading. Drop these in LLVM 3.0. This is
593 // to avoid conflicting with the sext/zext instructions, below.
594 if (Len == 4 && !memcmp(StartChar, "sext", 4)) {
595 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
596 if (JustWhitespaceNewLine(CurPtr))
597 return lltok::kw_signext;
598 } else if (Len == 4 && !memcmp(StartChar, "zext", 4)) {
599 // Scan CurPtr ahead, seeing if there is just whitespace before the newline.
600 if (JustWhitespaceNewLine(CurPtr))
601 return lltok::kw_zeroext;
604 // Keywords for instructions.
605 #define INSTKEYWORD(STR, Enum) \
606 if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) { \
607 UIntVal = Instruction::Enum; return lltok::kw_##STR; }
609 INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
610 INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
611 INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
612 INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
613 INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
614 INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
615 INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
616 INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
618 INSTKEYWORD(phi, PHI);
619 INSTKEYWORD(call, Call);
620 INSTKEYWORD(trunc, Trunc);
621 INSTKEYWORD(zext, ZExt);
622 INSTKEYWORD(sext, SExt);
623 INSTKEYWORD(fptrunc, FPTrunc);
624 INSTKEYWORD(fpext, FPExt);
625 INSTKEYWORD(uitofp, UIToFP);
626 INSTKEYWORD(sitofp, SIToFP);
627 INSTKEYWORD(fptoui, FPToUI);
628 INSTKEYWORD(fptosi, FPToSI);
629 INSTKEYWORD(inttoptr, IntToPtr);
630 INSTKEYWORD(ptrtoint, PtrToInt);
631 INSTKEYWORD(bitcast, BitCast);
632 INSTKEYWORD(select, Select);
633 INSTKEYWORD(va_arg, VAArg);
634 INSTKEYWORD(ret, Ret);
635 INSTKEYWORD(br, Br);
636 INSTKEYWORD(switch, Switch);
637 INSTKEYWORD(invoke, Invoke);
638 INSTKEYWORD(unwind, Unwind);
639 INSTKEYWORD(unreachable, Unreachable);
641 INSTKEYWORD(malloc, Malloc);
642 INSTKEYWORD(alloca, Alloca);
643 INSTKEYWORD(free, Free);
644 INSTKEYWORD(load, Load);
645 INSTKEYWORD(store, Store);
646 INSTKEYWORD(getelementptr, GetElementPtr);
648 INSTKEYWORD(extractelement, ExtractElement);
649 INSTKEYWORD(insertelement, InsertElement);
650 INSTKEYWORD(shufflevector, ShuffleVector);
651 INSTKEYWORD(getresult, ExtractValue);
652 INSTKEYWORD(extractvalue, ExtractValue);
653 INSTKEYWORD(insertvalue, InsertValue);
654 #undef INSTKEYWORD
656 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
657 // the CFE to avoid forcing it to deal with 64-bit numbers.
658 if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
659 TokStart[1] == '0' && TokStart[2] == 'x' && isxdigit(TokStart[3])) {
660 int len = CurPtr-TokStart-3;
661 uint32_t bits = len * 4;
662 APInt Tmp(bits, TokStart+3, len, 16);
663 uint32_t activeBits = Tmp.getActiveBits();
664 if (activeBits > 0 && activeBits < bits)
665 Tmp.trunc(activeBits);
666 APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
667 return lltok::APSInt;
670 // If this is "cc1234", return this as just "cc".
671 if (TokStart[0] == 'c' && TokStart[1] == 'c') {
672 CurPtr = TokStart+2;
673 return lltok::kw_cc;
676 // If this starts with "call", return it as CALL. This is to support old
677 // broken .ll files. FIXME: remove this with LLVM 3.0.
678 if (CurPtr-TokStart > 4 && !memcmp(TokStart, "call", 4)) {
679 CurPtr = TokStart+4;
680 UIntVal = Instruction::Call;
681 return lltok::kw_call;
684 // Finally, if this isn't known, return an error.
685 CurPtr = TokStart+1;
686 return lltok::Error;
690 /// Lex0x: Handle productions that start with 0x, knowing that it matches and
691 /// that this is not a label:
692 /// HexFPConstant 0x[0-9A-Fa-f]+
693 /// HexFP80Constant 0xK[0-9A-Fa-f]+
694 /// HexFP128Constant 0xL[0-9A-Fa-f]+
695 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
696 lltok::Kind LLLexer::Lex0x() {
697 CurPtr = TokStart + 2;
699 char Kind;
700 if (CurPtr[0] >= 'K' && CurPtr[0] <= 'M') {
701 Kind = *CurPtr++;
702 } else {
703 Kind = 'J';
706 if (!isxdigit(CurPtr[0])) {
707 // Bad token, return it as an error.
708 CurPtr = TokStart+1;
709 return lltok::Error;
712 while (isxdigit(CurPtr[0]))
713 ++CurPtr;
715 if (Kind == 'J') {
716 // HexFPConstant - Floating point constant represented in IEEE format as a
717 // hexadecimal number for when exponential notation is not precise enough.
718 // Float and double only.
719 APFloatVal = APFloat(BitsToDouble(HexIntToVal(TokStart+2, CurPtr)));
720 return lltok::APFloat;
723 uint64_t Pair[2];
724 switch (Kind) {
725 default: llvm_unreachable("Unknown kind!");
726 case 'K':
727 // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
728 FP80HexToIntPair(TokStart+3, CurPtr, Pair);
729 APFloatVal = APFloat(APInt(80, 2, Pair));
730 return lltok::APFloat;
731 case 'L':
732 // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
733 HexToIntPair(TokStart+3, CurPtr, Pair);
734 APFloatVal = APFloat(APInt(128, 2, Pair), true);
735 return lltok::APFloat;
736 case 'M':
737 // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
738 HexToIntPair(TokStart+3, CurPtr, Pair);
739 APFloatVal = APFloat(APInt(128, 2, Pair));
740 return lltok::APFloat;
744 /// LexIdentifier: Handle several related productions:
745 /// Label [-a-zA-Z$._0-9]+:
746 /// NInteger -[0-9]+
747 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
748 /// PInteger [0-9]+
749 /// HexFPConstant 0x[0-9A-Fa-f]+
750 /// HexFP80Constant 0xK[0-9A-Fa-f]+
751 /// HexFP128Constant 0xL[0-9A-Fa-f]+
752 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
753 lltok::Kind LLLexer::LexDigitOrNegative() {
754 // If the letter after the negative is a number, this is probably a label.
755 if (!isdigit(TokStart[0]) && !isdigit(CurPtr[0])) {
756 // Okay, this is not a number after the -, it's probably a label.
757 if (const char *End = isLabelTail(CurPtr)) {
758 StrVal.assign(TokStart, End-1);
759 CurPtr = End;
760 return lltok::LabelStr;
763 return lltok::Error;
766 // At this point, it is either a label, int or fp constant.
768 // Skip digits, we have at least one.
769 for (; isdigit(CurPtr[0]); ++CurPtr)
770 /*empty*/;
772 // Check to see if this really is a label afterall, e.g. "-1:".
773 if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
774 if (const char *End = isLabelTail(CurPtr)) {
775 StrVal.assign(TokStart, End-1);
776 CurPtr = End;
777 return lltok::LabelStr;
781 // If the next character is a '.', then it is a fp value, otherwise its
782 // integer.
783 if (CurPtr[0] != '.') {
784 if (TokStart[0] == '0' && TokStart[1] == 'x')
785 return Lex0x();
786 unsigned Len = CurPtr-TokStart;
787 uint32_t numBits = ((Len * 64) / 19) + 2;
788 APInt Tmp(numBits, TokStart, Len, 10);
789 if (TokStart[0] == '-') {
790 uint32_t minBits = Tmp.getMinSignedBits();
791 if (minBits > 0 && minBits < numBits)
792 Tmp.trunc(minBits);
793 APSIntVal = APSInt(Tmp, false);
794 } else {
795 uint32_t activeBits = Tmp.getActiveBits();
796 if (activeBits > 0 && activeBits < numBits)
797 Tmp.trunc(activeBits);
798 APSIntVal = APSInt(Tmp, true);
800 return lltok::APSInt;
803 ++CurPtr;
805 // Skip over [0-9]*([eE][-+]?[0-9]+)?
806 while (isdigit(CurPtr[0])) ++CurPtr;
808 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
809 if (isdigit(CurPtr[1]) ||
810 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
811 CurPtr += 2;
812 while (isdigit(CurPtr[0])) ++CurPtr;
816 APFloatVal = APFloat(atof(TokStart));
817 return lltok::APFloat;
820 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
821 lltok::Kind LLLexer::LexPositive() {
822 // If the letter after the negative is a number, this is probably not a
823 // label.
824 if (!isdigit(CurPtr[0]))
825 return lltok::Error;
827 // Skip digits.
828 for (++CurPtr; isdigit(CurPtr[0]); ++CurPtr)
829 /*empty*/;
831 // At this point, we need a '.'.
832 if (CurPtr[0] != '.') {
833 CurPtr = TokStart+1;
834 return lltok::Error;
837 ++CurPtr;
839 // Skip over [0-9]*([eE][-+]?[0-9]+)?
840 while (isdigit(CurPtr[0])) ++CurPtr;
842 if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
843 if (isdigit(CurPtr[1]) ||
844 ((CurPtr[1] == '-' || CurPtr[1] == '+') && isdigit(CurPtr[2]))) {
845 CurPtr += 2;
846 while (isdigit(CurPtr[0])) ++CurPtr;
850 APFloatVal = APFloat(atof(TokStart));
851 return lltok::APFloat;