LLVM  6.0.0svn
LLLexer.cpp
Go to the documentation of this file.
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.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "LLLexer.h"
15 #include "llvm/ADT/APInt.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/IR/DerivedTypes.h"
20 #include "llvm/IR/Instruction.h"
22 #include "llvm/Support/SourceMgr.h"
23 #include <cassert>
24 #include <cctype>
25 #include <cstdio>
26 
27 using namespace llvm;
28 
29 bool LLLexer::Error(LocTy ErrorLoc, const Twine &Msg) const {
30  ErrorInfo = SM.GetMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
31  return true;
32 }
33 
34 void LLLexer::Warning(LocTy WarningLoc, const Twine &Msg) const {
35  SM.PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
36 }
37 
38 //===----------------------------------------------------------------------===//
39 // Helper functions.
40 //===----------------------------------------------------------------------===//
41 
42 // atoull - Convert an ascii string of decimal digits into the unsigned long
43 // long representation... this does not have to do input error checking,
44 // because we know that the input will be matched by a suitable regex...
45 //
46 uint64_t LLLexer::atoull(const char *Buffer, const char *End) {
47  uint64_t Result = 0;
48  for (; Buffer != End; Buffer++) {
49  uint64_t OldRes = Result;
50  Result *= 10;
51  Result += *Buffer-'0';
52  if (Result < OldRes) { // Uh, oh, overflow detected!!!
53  Error("constant bigger than 64 bits detected!");
54  return 0;
55  }
56  }
57  return Result;
58 }
59 
60 uint64_t LLLexer::HexIntToVal(const char *Buffer, const char *End) {
61  uint64_t Result = 0;
62  for (; Buffer != End; ++Buffer) {
63  uint64_t OldRes = Result;
64  Result *= 16;
65  Result += hexDigitValue(*Buffer);
66 
67  if (Result < OldRes) { // Uh, oh, overflow detected!!!
68  Error("constant bigger than 64 bits detected!");
69  return 0;
70  }
71  }
72  return Result;
73 }
74 
75 void LLLexer::HexToIntPair(const char *Buffer, const char *End,
76  uint64_t Pair[2]) {
77  Pair[0] = 0;
78  if (End - Buffer >= 16) {
79  for (int i = 0; i < 16; i++, Buffer++) {
80  assert(Buffer != End);
81  Pair[0] *= 16;
82  Pair[0] += hexDigitValue(*Buffer);
83  }
84  }
85  Pair[1] = 0;
86  for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
87  Pair[1] *= 16;
88  Pair[1] += hexDigitValue(*Buffer);
89  }
90  if (Buffer != End)
91  Error("constant bigger than 128 bits detected!");
92 }
93 
94 /// FP80HexToIntPair - translate an 80 bit FP80 number (20 hexits) into
95 /// { low64, high16 } as usual for an APInt.
96 void LLLexer::FP80HexToIntPair(const char *Buffer, const char *End,
97  uint64_t Pair[2]) {
98  Pair[1] = 0;
99  for (int i=0; i<4 && Buffer != End; i++, Buffer++) {
100  assert(Buffer != End);
101  Pair[1] *= 16;
102  Pair[1] += hexDigitValue(*Buffer);
103  }
104  Pair[0] = 0;
105  for (int i = 0; i < 16 && Buffer != End; i++, Buffer++) {
106  Pair[0] *= 16;
107  Pair[0] += hexDigitValue(*Buffer);
108  }
109  if (Buffer != End)
110  Error("constant bigger than 128 bits detected!");
111 }
112 
113 // UnEscapeLexed - Run through the specified buffer and change \xx codes to the
114 // appropriate character.
115 static void UnEscapeLexed(std::string &Str) {
116  if (Str.empty()) return;
117 
118  char *Buffer = &Str[0], *EndBuffer = Buffer+Str.size();
119  char *BOut = Buffer;
120  for (char *BIn = Buffer; BIn != EndBuffer; ) {
121  if (BIn[0] == '\\') {
122  if (BIn < EndBuffer-1 && BIn[1] == '\\') {
123  *BOut++ = '\\'; // Two \ becomes one
124  BIn += 2;
125  } else if (BIn < EndBuffer-2 &&
126  isxdigit(static_cast<unsigned char>(BIn[1])) &&
127  isxdigit(static_cast<unsigned char>(BIn[2]))) {
128  *BOut = hexDigitValue(BIn[1]) * 16 + hexDigitValue(BIn[2]);
129  BIn += 3; // Skip over handled chars
130  ++BOut;
131  } else {
132  *BOut++ = *BIn++;
133  }
134  } else {
135  *BOut++ = *BIn++;
136  }
137  }
138  Str.resize(BOut-Buffer);
139 }
140 
141 /// isLabelChar - Return true for [-a-zA-Z$._0-9].
142 static bool isLabelChar(char C) {
143  return isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
144  C == '.' || C == '_';
145 }
146 
147 /// isLabelTail - Return true if this pointer points to a valid end of a label.
148 static const char *isLabelTail(const char *CurPtr) {
149  while (true) {
150  if (CurPtr[0] == ':') return CurPtr+1;
151  if (!isLabelChar(CurPtr[0])) return nullptr;
152  ++CurPtr;
153  }
154 }
155 
156 //===----------------------------------------------------------------------===//
157 // Lexer definition.
158 //===----------------------------------------------------------------------===//
159 
161  LLVMContext &C)
162  : CurBuf(StartBuf), ErrorInfo(Err), SM(sm), Context(C), APFloatVal(0.0) {
163  CurPtr = CurBuf.begin();
164 }
165 
166 int LLLexer::getNextChar() {
167  char CurChar = *CurPtr++;
168  switch (CurChar) {
169  default: return (unsigned char)CurChar;
170  case 0:
171  // A nul character in the stream is either the end of the current buffer or
172  // a random nul in the file. Disambiguate that here.
173  if (CurPtr-1 != CurBuf.end())
174  return 0; // Just whitespace.
175 
176  // Otherwise, return end of file.
177  --CurPtr; // Another call to lex will return EOF again.
178  return EOF;
179  }
180 }
181 
182 lltok::Kind LLLexer::LexToken() {
183  while (true) {
184  TokStart = CurPtr;
185 
186  int CurChar = getNextChar();
187  switch (CurChar) {
188  default:
189  // Handle letters: [a-zA-Z_]
190  if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
191  return LexIdentifier();
192 
193  return lltok::Error;
194  case EOF: return lltok::Eof;
195  case 0:
196  case ' ':
197  case '\t':
198  case '\n':
199  case '\r':
200  // Ignore whitespace.
201  continue;
202  case '+': return LexPositive();
203  case '@': return LexAt();
204  case '$': return LexDollar();
205  case '%': return LexPercent();
206  case '"': return LexQuote();
207  case '.':
208  if (const char *Ptr = isLabelTail(CurPtr)) {
209  CurPtr = Ptr;
210  StrVal.assign(TokStart, CurPtr-1);
211  return lltok::LabelStr;
212  }
213  if (CurPtr[0] == '.' && CurPtr[1] == '.') {
214  CurPtr += 2;
215  return lltok::dotdotdot;
216  }
217  return lltok::Error;
218  case ';':
219  SkipLineComment();
220  continue;
221  case '!': return LexExclaim();
222  case '#': return LexHash();
223  case '0': case '1': case '2': case '3': case '4':
224  case '5': case '6': case '7': case '8': case '9':
225  case '-':
226  return LexDigitOrNegative();
227  case '=': return lltok::equal;
228  case '[': return lltok::lsquare;
229  case ']': return lltok::rsquare;
230  case '{': return lltok::lbrace;
231  case '}': return lltok::rbrace;
232  case '<': return lltok::less;
233  case '>': return lltok::greater;
234  case '(': return lltok::lparen;
235  case ')': return lltok::rparen;
236  case ',': return lltok::comma;
237  case '*': return lltok::star;
238  case '|': return lltok::bar;
239  }
240  }
241 }
242 
243 void LLLexer::SkipLineComment() {
244  while (true) {
245  if (CurPtr[0] == '\n' || CurPtr[0] == '\r' || getNextChar() == EOF)
246  return;
247  }
248 }
249 
250 /// Lex all tokens that start with an @ character.
251 /// GlobalVar @\"[^\"]*\"
252 /// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
253 /// GlobalVarID @[0-9]+
254 lltok::Kind LLLexer::LexAt() {
255  return LexVar(lltok::GlobalVar, lltok::GlobalID);
256 }
257 
258 lltok::Kind LLLexer::LexDollar() {
259  if (const char *Ptr = isLabelTail(TokStart)) {
260  CurPtr = Ptr;
261  StrVal.assign(TokStart, CurPtr - 1);
262  return lltok::LabelStr;
263  }
264 
265  // Handle DollarStringConstant: $\"[^\"]*\"
266  if (CurPtr[0] == '"') {
267  ++CurPtr;
268 
269  while (true) {
270  int CurChar = getNextChar();
271 
272  if (CurChar == EOF) {
273  Error("end of file in COMDAT variable name");
274  return lltok::Error;
275  }
276  if (CurChar == '"') {
277  StrVal.assign(TokStart + 2, CurPtr - 1);
278  UnEscapeLexed(StrVal);
279  if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
280  Error("Null bytes are not allowed in names");
281  return lltok::Error;
282  }
283  return lltok::ComdatVar;
284  }
285  }
286  }
287 
288  // Handle ComdatVarName: $[-a-zA-Z$._][-a-zA-Z$._0-9]*
289  if (ReadVarName())
290  return lltok::ComdatVar;
291 
292  return lltok::Error;
293 }
294 
295 /// ReadString - Read a string until the closing quote.
296 lltok::Kind LLLexer::ReadString(lltok::Kind kind) {
297  const char *Start = CurPtr;
298  while (true) {
299  int CurChar = getNextChar();
300 
301  if (CurChar == EOF) {
302  Error("end of file in string constant");
303  return lltok::Error;
304  }
305  if (CurChar == '"') {
306  StrVal.assign(Start, CurPtr-1);
307  UnEscapeLexed(StrVal);
308  return kind;
309  }
310  }
311 }
312 
313 /// ReadVarName - Read the rest of a token containing a variable name.
314 bool LLLexer::ReadVarName() {
315  const char *NameStart = CurPtr;
316  if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
317  CurPtr[0] == '-' || CurPtr[0] == '$' ||
318  CurPtr[0] == '.' || CurPtr[0] == '_') {
319  ++CurPtr;
320  while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
321  CurPtr[0] == '-' || CurPtr[0] == '$' ||
322  CurPtr[0] == '.' || CurPtr[0] == '_')
323  ++CurPtr;
324 
325  StrVal.assign(NameStart, CurPtr);
326  return true;
327  }
328  return false;
329 }
330 
331 lltok::Kind LLLexer::LexVar(lltok::Kind Var, lltok::Kind VarID) {
332  // Handle StringConstant: \"[^\"]*\"
333  if (CurPtr[0] == '"') {
334  ++CurPtr;
335 
336  while (true) {
337  int CurChar = getNextChar();
338 
339  if (CurChar == EOF) {
340  Error("end of file in global variable name");
341  return lltok::Error;
342  }
343  if (CurChar == '"') {
344  StrVal.assign(TokStart+2, CurPtr-1);
345  UnEscapeLexed(StrVal);
346  if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
347  Error("Null bytes are not allowed in names");
348  return lltok::Error;
349  }
350  return Var;
351  }
352  }
353  }
354 
355  // Handle VarName: [-a-zA-Z$._][-a-zA-Z$._0-9]*
356  if (ReadVarName())
357  return Var;
358 
359  // Handle VarID: [0-9]+
360  if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
361  for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
362  /*empty*/;
363 
364  uint64_t Val = atoull(TokStart+1, CurPtr);
365  if ((unsigned)Val != Val)
366  Error("invalid value number (too large)!");
367  UIntVal = unsigned(Val);
368  return VarID;
369  }
370  return lltok::Error;
371 }
372 
373 /// Lex all tokens that start with a % character.
374 /// LocalVar ::= %\"[^\"]*\"
375 /// LocalVar ::= %[-a-zA-Z$._][-a-zA-Z$._0-9]*
376 /// LocalVarID ::= %[0-9]+
377 lltok::Kind LLLexer::LexPercent() {
378  return LexVar(lltok::LocalVar, lltok::LocalVarID);
379 }
380 
381 /// Lex all tokens that start with a " character.
382 /// QuoteLabel "[^"]+":
383 /// StringConstant "[^"]*"
384 lltok::Kind LLLexer::LexQuote() {
385  lltok::Kind kind = ReadString(lltok::StringConstant);
386  if (kind == lltok::Error || kind == lltok::Eof)
387  return kind;
388 
389  if (CurPtr[0] == ':') {
390  ++CurPtr;
391  if (StringRef(StrVal).find_first_of(0) != StringRef::npos) {
392  Error("Null bytes are not allowed in names");
393  kind = lltok::Error;
394  } else {
395  kind = lltok::LabelStr;
396  }
397  }
398 
399  return kind;
400 }
401 
402 /// Lex all tokens that start with a ! character.
403 /// !foo
404 /// !
405 lltok::Kind LLLexer::LexExclaim() {
406  // Lex a metadata name as a MetadataVar.
407  if (isalpha(static_cast<unsigned char>(CurPtr[0])) ||
408  CurPtr[0] == '-' || CurPtr[0] == '$' ||
409  CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\') {
410  ++CurPtr;
411  while (isalnum(static_cast<unsigned char>(CurPtr[0])) ||
412  CurPtr[0] == '-' || CurPtr[0] == '$' ||
413  CurPtr[0] == '.' || CurPtr[0] == '_' || CurPtr[0] == '\\')
414  ++CurPtr;
415 
416  StrVal.assign(TokStart+1, CurPtr); // Skip !
417  UnEscapeLexed(StrVal);
418  return lltok::MetadataVar;
419  }
420  return lltok::exclaim;
421 }
422 
423 /// Lex all tokens that start with a # character.
424 /// AttrGrpID ::= #[0-9]+
425 lltok::Kind LLLexer::LexHash() {
426  // Handle AttrGrpID: #[0-9]+
427  if (isdigit(static_cast<unsigned char>(CurPtr[0]))) {
428  for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
429  /*empty*/;
430 
431  uint64_t Val = atoull(TokStart+1, CurPtr);
432  if ((unsigned)Val != Val)
433  Error("invalid value number (too large)!");
434  UIntVal = unsigned(Val);
435  return lltok::AttrGrpID;
436  }
437 
438  return lltok::Error;
439 }
440 
441 /// Lex a label, integer type, keyword, or hexadecimal integer constant.
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' ? nullptr : StartChar;
449  const char *KeywordEnd = nullptr;
450 
451  for (; isLabelChar(*CurPtr); ++CurPtr) {
452  // If we decide this is an integer, remember the end of the sequence.
453  if (!IntEnd && !isdigit(static_cast<unsigned char>(*CurPtr)))
454  IntEnd = CurPtr;
455  if (!KeywordEnd && !isalnum(static_cast<unsigned char>(*CurPtr)) &&
456  *CurPtr != '_')
457  KeywordEnd = CurPtr;
458  }
459 
460  // If we stopped due to a colon, this really is a label.
461  if (*CurPtr == ':') {
462  StrVal.assign(StartChar-1, CurPtr++);
463  return lltok::LabelStr;
464  }
465 
466  // Otherwise, this wasn't a label. If this was valid as an integer type,
467  // return it.
468  if (!IntEnd) IntEnd = CurPtr;
469  if (IntEnd != StartChar) {
470  CurPtr = IntEnd;
471  uint64_t NumBits = atoull(StartChar, CurPtr);
472  if (NumBits < IntegerType::MIN_INT_BITS ||
473  NumBits > IntegerType::MAX_INT_BITS) {
474  Error("bitwidth for integer type out of range!");
475  return lltok::Error;
476  }
477  TyVal = IntegerType::get(Context, NumBits);
478  return lltok::Type;
479  }
480 
481  // Otherwise, this was a letter sequence. See which keyword this is.
482  if (!KeywordEnd) KeywordEnd = CurPtr;
483  CurPtr = KeywordEnd;
484  --StartChar;
485  StringRef Keyword(StartChar, CurPtr - StartChar);
486 
487 #define KEYWORD(STR) \
488  do { \
489  if (Keyword == #STR) \
490  return lltok::kw_##STR; \
491  } while (false)
492 
493  KEYWORD(true); KEYWORD(false);
494  KEYWORD(declare); KEYWORD(define);
495  KEYWORD(global); KEYWORD(constant);
496 
497  KEYWORD(dso_local);
498  KEYWORD(dso_preemptable);
499 
500  KEYWORD(private);
501  KEYWORD(internal);
502  KEYWORD(available_externally);
503  KEYWORD(linkonce);
504  KEYWORD(linkonce_odr);
505  KEYWORD(weak); // Use as a linkage, and a modifier for "cmpxchg".
506  KEYWORD(weak_odr);
507  KEYWORD(appending);
508  KEYWORD(dllimport);
509  KEYWORD(dllexport);
510  KEYWORD(common);
511  KEYWORD(default);
512  KEYWORD(hidden);
513  KEYWORD(protected);
514  KEYWORD(unnamed_addr);
515  KEYWORD(local_unnamed_addr);
516  KEYWORD(externally_initialized);
517  KEYWORD(extern_weak);
518  KEYWORD(external);
519  KEYWORD(thread_local);
520  KEYWORD(localdynamic);
521  KEYWORD(initialexec);
522  KEYWORD(localexec);
523  KEYWORD(zeroinitializer);
524  KEYWORD(undef);
525  KEYWORD(null);
526  KEYWORD(none);
527  KEYWORD(to);
528  KEYWORD(caller);
529  KEYWORD(within);
530  KEYWORD(from);
531  KEYWORD(tail);
532  KEYWORD(musttail);
533  KEYWORD(notail);
534  KEYWORD(target);
535  KEYWORD(triple);
536  KEYWORD(source_filename);
537  KEYWORD(unwind);
538  KEYWORD(deplibs); // FIXME: Remove in 4.0.
539  KEYWORD(datalayout);
540  KEYWORD(volatile);
541  KEYWORD(atomic);
542  KEYWORD(unordered);
543  KEYWORD(monotonic);
544  KEYWORD(acquire);
545  KEYWORD(release);
546  KEYWORD(acq_rel);
547  KEYWORD(seq_cst);
548  KEYWORD(syncscope);
549 
550  KEYWORD(nnan);
551  KEYWORD(ninf);
552  KEYWORD(nsz);
553  KEYWORD(arcp);
554  KEYWORD(contract);
555  KEYWORD(reassoc);
556  KEYWORD(afn);
557  KEYWORD(fast);
558  KEYWORD(nuw);
559  KEYWORD(nsw);
560  KEYWORD(exact);
561  KEYWORD(inbounds);
562  KEYWORD(inrange);
563  KEYWORD(align);
564  KEYWORD(addrspace);
565  KEYWORD(section);
566  KEYWORD(alias);
567  KEYWORD(ifunc);
568  KEYWORD(module);
569  KEYWORD(asm);
570  KEYWORD(sideeffect);
571  KEYWORD(alignstack);
572  KEYWORD(inteldialect);
573  KEYWORD(gc);
574  KEYWORD(prefix);
575  KEYWORD(prologue);
576 
577  KEYWORD(ccc);
578  KEYWORD(fastcc);
579  KEYWORD(coldcc);
580  KEYWORD(x86_stdcallcc);
581  KEYWORD(x86_fastcallcc);
582  KEYWORD(x86_thiscallcc);
583  KEYWORD(x86_vectorcallcc);
584  KEYWORD(arm_apcscc);
585  KEYWORD(arm_aapcscc);
586  KEYWORD(arm_aapcs_vfpcc);
587  KEYWORD(msp430_intrcc);
588  KEYWORD(avr_intrcc);
589  KEYWORD(avr_signalcc);
590  KEYWORD(ptx_kernel);
591  KEYWORD(ptx_device);
592  KEYWORD(spir_kernel);
593  KEYWORD(spir_func);
594  KEYWORD(intel_ocl_bicc);
595  KEYWORD(x86_64_sysvcc);
596  KEYWORD(win64cc);
597  KEYWORD(x86_regcallcc);
598  KEYWORD(webkit_jscc);
599  KEYWORD(swiftcc);
600  KEYWORD(anyregcc);
601  KEYWORD(preserve_mostcc);
602  KEYWORD(preserve_allcc);
603  KEYWORD(ghccc);
604  KEYWORD(x86_intrcc);
605  KEYWORD(hhvmcc);
606  KEYWORD(hhvm_ccc);
607  KEYWORD(cxx_fast_tlscc);
608  KEYWORD(amdgpu_vs);
609  KEYWORD(amdgpu_ls);
610  KEYWORD(amdgpu_hs);
611  KEYWORD(amdgpu_es);
612  KEYWORD(amdgpu_gs);
613  KEYWORD(amdgpu_ps);
614  KEYWORD(amdgpu_cs);
615  KEYWORD(amdgpu_kernel);
616 
617  KEYWORD(cc);
618  KEYWORD(c);
619 
621 
622  KEYWORD(alwaysinline);
623  KEYWORD(allocsize);
624  KEYWORD(argmemonly);
625  KEYWORD(builtin);
626  KEYWORD(byval);
627  KEYWORD(inalloca);
628  KEYWORD(cold);
629  KEYWORD(convergent);
630  KEYWORD(dereferenceable);
631  KEYWORD(dereferenceable_or_null);
632  KEYWORD(inaccessiblememonly);
633  KEYWORD(inaccessiblemem_or_argmemonly);
634  KEYWORD(inlinehint);
635  KEYWORD(inreg);
636  KEYWORD(jumptable);
637  KEYWORD(minsize);
638  KEYWORD(naked);
639  KEYWORD(nest);
640  KEYWORD(noalias);
641  KEYWORD(nobuiltin);
642  KEYWORD(nocapture);
643  KEYWORD(noduplicate);
644  KEYWORD(noimplicitfloat);
645  KEYWORD(noinline);
646  KEYWORD(norecurse);
647  KEYWORD(nonlazybind);
648  KEYWORD(nonnull);
649  KEYWORD(noredzone);
650  KEYWORD(noreturn);
651  KEYWORD(nounwind);
652  KEYWORD(optnone);
653  KEYWORD(optsize);
654  KEYWORD(readnone);
655  KEYWORD(readonly);
656  KEYWORD(returned);
657  KEYWORD(returns_twice);
658  KEYWORD(signext);
659  KEYWORD(speculatable);
660  KEYWORD(sret);
661  KEYWORD(ssp);
662  KEYWORD(sspreq);
663  KEYWORD(sspstrong);
664  KEYWORD(strictfp);
665  KEYWORD(safestack);
666  KEYWORD(sanitize_address);
667  KEYWORD(sanitize_thread);
668  KEYWORD(sanitize_memory);
669  KEYWORD(swifterror);
670  KEYWORD(swiftself);
671  KEYWORD(uwtable);
672  KEYWORD(writeonly);
673  KEYWORD(zeroext);
674 
675  KEYWORD(type);
676  KEYWORD(opaque);
677 
678  KEYWORD(comdat);
679 
680  // Comdat types
681  KEYWORD(any);
682  KEYWORD(exactmatch);
683  KEYWORD(largest);
684  KEYWORD(noduplicates);
685  KEYWORD(samesize);
686 
687  KEYWORD(eq); KEYWORD(ne); KEYWORD(slt); KEYWORD(sgt); KEYWORD(sle);
688  KEYWORD(sge); KEYWORD(ult); KEYWORD(ugt); KEYWORD(ule); KEYWORD(uge);
689  KEYWORD(oeq); KEYWORD(one); KEYWORD(olt); KEYWORD(ogt); KEYWORD(ole);
690  KEYWORD(oge); KEYWORD(ord); KEYWORD(uno); KEYWORD(ueq); KEYWORD(une);
691 
692  KEYWORD(xchg); KEYWORD(nand); KEYWORD(max); KEYWORD(min); KEYWORD(umax);
693  KEYWORD(umin);
694 
695  KEYWORD(x);
696  KEYWORD(blockaddress);
697 
698  // Metadata types.
699  KEYWORD(distinct);
700 
701  // Use-list order directives.
702  KEYWORD(uselistorder);
703  KEYWORD(uselistorder_bb);
704 
705  KEYWORD(personality);
706  KEYWORD(cleanup);
707  KEYWORD(catch);
708  KEYWORD(filter);
709 
710 #undef KEYWORD
711 
712  // Keywords for types.
713 #define TYPEKEYWORD(STR, LLVMTY) \
714  do { \
715  if (Keyword == STR) { \
716  TyVal = LLVMTY; \
717  return lltok::Type; \
718  } \
719  } while (false)
720 
721  TYPEKEYWORD("void", Type::getVoidTy(Context));
722  TYPEKEYWORD("half", Type::getHalfTy(Context));
723  TYPEKEYWORD("float", Type::getFloatTy(Context));
724  TYPEKEYWORD("double", Type::getDoubleTy(Context));
725  TYPEKEYWORD("x86_fp80", Type::getX86_FP80Ty(Context));
726  TYPEKEYWORD("fp128", Type::getFP128Ty(Context));
727  TYPEKEYWORD("ppc_fp128", Type::getPPC_FP128Ty(Context));
728  TYPEKEYWORD("label", Type::getLabelTy(Context));
729  TYPEKEYWORD("metadata", Type::getMetadataTy(Context));
730  TYPEKEYWORD("x86_mmx", Type::getX86_MMXTy(Context));
731  TYPEKEYWORD("token", Type::getTokenTy(Context));
732 
733 #undef TYPEKEYWORD
734 
735  // Keywords for instructions.
736 #define INSTKEYWORD(STR, Enum) \
737  do { \
738  if (Keyword == #STR) { \
739  UIntVal = Instruction::Enum; \
740  return lltok::kw_##STR; \
741  } \
742  } while (false)
743 
744  INSTKEYWORD(add, Add); INSTKEYWORD(fadd, FAdd);
745  INSTKEYWORD(sub, Sub); INSTKEYWORD(fsub, FSub);
746  INSTKEYWORD(mul, Mul); INSTKEYWORD(fmul, FMul);
747  INSTKEYWORD(udiv, UDiv); INSTKEYWORD(sdiv, SDiv); INSTKEYWORD(fdiv, FDiv);
748  INSTKEYWORD(urem, URem); INSTKEYWORD(srem, SRem); INSTKEYWORD(frem, FRem);
749  INSTKEYWORD(shl, Shl); INSTKEYWORD(lshr, LShr); INSTKEYWORD(ashr, AShr);
750  INSTKEYWORD(and, And); INSTKEYWORD(or, Or); INSTKEYWORD(xor, Xor);
751  INSTKEYWORD(icmp, ICmp); INSTKEYWORD(fcmp, FCmp);
752 
753  INSTKEYWORD(phi, PHI);
754  INSTKEYWORD(call, Call);
755  INSTKEYWORD(trunc, Trunc);
756  INSTKEYWORD(zext, ZExt);
757  INSTKEYWORD(sext, SExt);
758  INSTKEYWORD(fptrunc, FPTrunc);
759  INSTKEYWORD(fpext, FPExt);
760  INSTKEYWORD(uitofp, UIToFP);
761  INSTKEYWORD(sitofp, SIToFP);
762  INSTKEYWORD(fptoui, FPToUI);
763  INSTKEYWORD(fptosi, FPToSI);
764  INSTKEYWORD(inttoptr, IntToPtr);
765  INSTKEYWORD(ptrtoint, PtrToInt);
766  INSTKEYWORD(bitcast, BitCast);
767  INSTKEYWORD(addrspacecast, AddrSpaceCast);
768  INSTKEYWORD(select, Select);
769  INSTKEYWORD(va_arg, VAArg);
770  INSTKEYWORD(ret, Ret);
771  INSTKEYWORD(br, Br);
772  INSTKEYWORD(switch, Switch);
773  INSTKEYWORD(indirectbr, IndirectBr);
774  INSTKEYWORD(invoke, Invoke);
775  INSTKEYWORD(resume, Resume);
776  INSTKEYWORD(unreachable, Unreachable);
777 
778  INSTKEYWORD(alloca, Alloca);
779  INSTKEYWORD(load, Load);
780  INSTKEYWORD(store, Store);
781  INSTKEYWORD(cmpxchg, AtomicCmpXchg);
782  INSTKEYWORD(atomicrmw, AtomicRMW);
783  INSTKEYWORD(fence, Fence);
784  INSTKEYWORD(getelementptr, GetElementPtr);
785 
786  INSTKEYWORD(extractelement, ExtractElement);
787  INSTKEYWORD(insertelement, InsertElement);
788  INSTKEYWORD(shufflevector, ShuffleVector);
789  INSTKEYWORD(extractvalue, ExtractValue);
790  INSTKEYWORD(insertvalue, InsertValue);
791  INSTKEYWORD(landingpad, LandingPad);
792  INSTKEYWORD(cleanupret, CleanupRet);
793  INSTKEYWORD(catchret, CatchRet);
794  INSTKEYWORD(catchswitch, CatchSwitch);
795  INSTKEYWORD(catchpad, CatchPad);
796  INSTKEYWORD(cleanuppad, CleanupPad);
797 
798 #undef INSTKEYWORD
799 
800 #define DWKEYWORD(TYPE, TOKEN) \
801  do { \
802  if (Keyword.startswith("DW_" #TYPE "_")) { \
803  StrVal.assign(Keyword.begin(), Keyword.end()); \
804  return lltok::TOKEN; \
805  } \
806  } while (false)
807 
808  DWKEYWORD(TAG, DwarfTag);
810  DWKEYWORD(VIRTUALITY, DwarfVirtuality);
811  DWKEYWORD(LANG, DwarfLang);
812  DWKEYWORD(CC, DwarfCC);
813  DWKEYWORD(OP, DwarfOp);
814  DWKEYWORD(MACINFO, DwarfMacinfo);
815 
816 #undef DWKEYWORD
817 
818  if (Keyword.startswith("DIFlag")) {
819  StrVal.assign(Keyword.begin(), Keyword.end());
820  return lltok::DIFlag;
821  }
822 
823  if (Keyword.startswith("CSK_")) {
824  StrVal.assign(Keyword.begin(), Keyword.end());
825  return lltok::ChecksumKind;
826  }
827 
828  if (Keyword == "NoDebug" || Keyword == "FullDebug" ||
829  Keyword == "LineTablesOnly") {
830  StrVal.assign(Keyword.begin(), Keyword.end());
831  return lltok::EmissionKind;
832  }
833 
834  // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
835  // the CFE to avoid forcing it to deal with 64-bit numbers.
836  if ((TokStart[0] == 'u' || TokStart[0] == 's') &&
837  TokStart[1] == '0' && TokStart[2] == 'x' &&
838  isxdigit(static_cast<unsigned char>(TokStart[3]))) {
839  int len = CurPtr-TokStart-3;
840  uint32_t bits = len * 4;
841  StringRef HexStr(TokStart + 3, len);
842  if (!all_of(HexStr, isxdigit)) {
843  // Bad token, return it as an error.
844  CurPtr = TokStart+3;
845  return lltok::Error;
846  }
847  APInt Tmp(bits, HexStr, 16);
848  uint32_t activeBits = Tmp.getActiveBits();
849  if (activeBits > 0 && activeBits < bits)
850  Tmp = Tmp.trunc(activeBits);
851  APSIntVal = APSInt(Tmp, TokStart[0] == 'u');
852  return lltok::APSInt;
853  }
854 
855  // If this is "cc1234", return this as just "cc".
856  if (TokStart[0] == 'c' && TokStart[1] == 'c') {
857  CurPtr = TokStart+2;
858  return lltok::kw_cc;
859  }
860 
861  // Finally, if this isn't known, return an error.
862  CurPtr = TokStart+1;
863  return lltok::Error;
864 }
865 
866 /// Lex all tokens that start with a 0x prefix, knowing they match and are not
867 /// labels.
868 /// HexFPConstant 0x[0-9A-Fa-f]+
869 /// HexFP80Constant 0xK[0-9A-Fa-f]+
870 /// HexFP128Constant 0xL[0-9A-Fa-f]+
871 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
872 /// HexHalfConstant 0xH[0-9A-Fa-f]+
873 lltok::Kind LLLexer::Lex0x() {
874  CurPtr = TokStart + 2;
875 
876  char Kind;
877  if ((CurPtr[0] >= 'K' && CurPtr[0] <= 'M') || CurPtr[0] == 'H') {
878  Kind = *CurPtr++;
879  } else {
880  Kind = 'J';
881  }
882 
883  if (!isxdigit(static_cast<unsigned char>(CurPtr[0]))) {
884  // Bad token, return it as an error.
885  CurPtr = TokStart+1;
886  return lltok::Error;
887  }
888 
889  while (isxdigit(static_cast<unsigned char>(CurPtr[0])))
890  ++CurPtr;
891 
892  if (Kind == 'J') {
893  // HexFPConstant - Floating point constant represented in IEEE format as a
894  // hexadecimal number for when exponential notation is not precise enough.
895  // Half, Float, and double only.
896  APFloatVal = APFloat(APFloat::IEEEdouble(),
897  APInt(64, HexIntToVal(TokStart + 2, CurPtr)));
898  return lltok::APFloat;
899  }
900 
901  uint64_t Pair[2];
902  switch (Kind) {
903  default: llvm_unreachable("Unknown kind!");
904  case 'K':
905  // F80HexFPConstant - x87 long double in hexadecimal format (10 bytes)
906  FP80HexToIntPair(TokStart+3, CurPtr, Pair);
907  APFloatVal = APFloat(APFloat::x87DoubleExtended(), APInt(80, Pair));
908  return lltok::APFloat;
909  case 'L':
910  // F128HexFPConstant - IEEE 128-bit in hexadecimal format (16 bytes)
911  HexToIntPair(TokStart+3, CurPtr, Pair);
912  APFloatVal = APFloat(APFloat::IEEEquad(), APInt(128, Pair));
913  return lltok::APFloat;
914  case 'M':
915  // PPC128HexFPConstant - PowerPC 128-bit in hexadecimal format (16 bytes)
916  HexToIntPair(TokStart+3, CurPtr, Pair);
917  APFloatVal = APFloat(APFloat::PPCDoubleDouble(), APInt(128, Pair));
918  return lltok::APFloat;
919  case 'H':
920  APFloatVal = APFloat(APFloat::IEEEhalf(),
921  APInt(16,HexIntToVal(TokStart+3, CurPtr)));
922  return lltok::APFloat;
923  }
924 }
925 
926 /// Lex tokens for a label or a numeric constant, possibly starting with -.
927 /// Label [-a-zA-Z$._0-9]+:
928 /// NInteger -[0-9]+
929 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
930 /// PInteger [0-9]+
931 /// HexFPConstant 0x[0-9A-Fa-f]+
932 /// HexFP80Constant 0xK[0-9A-Fa-f]+
933 /// HexFP128Constant 0xL[0-9A-Fa-f]+
934 /// HexPPC128Constant 0xM[0-9A-Fa-f]+
935 lltok::Kind LLLexer::LexDigitOrNegative() {
936  // If the letter after the negative is not a number, this is probably a label.
937  if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
938  !isdigit(static_cast<unsigned char>(CurPtr[0]))) {
939  // Okay, this is not a number after the -, it's probably a label.
940  if (const char *End = isLabelTail(CurPtr)) {
941  StrVal.assign(TokStart, End-1);
942  CurPtr = End;
943  return lltok::LabelStr;
944  }
945 
946  return lltok::Error;
947  }
948 
949  // At this point, it is either a label, int or fp constant.
950 
951  // Skip digits, we have at least one.
952  for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
953  /*empty*/;
954 
955  // Check to see if this really is a label afterall, e.g. "-1:".
956  if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
957  if (const char *End = isLabelTail(CurPtr)) {
958  StrVal.assign(TokStart, End-1);
959  CurPtr = End;
960  return lltok::LabelStr;
961  }
962  }
963 
964  // If the next character is a '.', then it is a fp value, otherwise its
965  // integer.
966  if (CurPtr[0] != '.') {
967  if (TokStart[0] == '0' && TokStart[1] == 'x')
968  return Lex0x();
969  APSIntVal = APSInt(StringRef(TokStart, CurPtr - TokStart));
970  return lltok::APSInt;
971  }
972 
973  ++CurPtr;
974 
975  // Skip over [0-9]*([eE][-+]?[0-9]+)?
976  while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
977 
978  if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
979  if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
980  ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
981  isdigit(static_cast<unsigned char>(CurPtr[2])))) {
982  CurPtr += 2;
983  while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
984  }
985  }
986 
987  APFloatVal = APFloat(APFloat::IEEEdouble(),
988  StringRef(TokStart, CurPtr - TokStart));
989  return lltok::APFloat;
990 }
991 
992 /// Lex a floating point constant starting with +.
993 /// FPConstant [-+]?[0-9]+[.][0-9]*([eE][-+]?[0-9]+)?
994 lltok::Kind LLLexer::LexPositive() {
995  // If the letter after the negative is a number, this is probably not a
996  // label.
997  if (!isdigit(static_cast<unsigned char>(CurPtr[0])))
998  return lltok::Error;
999 
1000  // Skip digits.
1001  for (++CurPtr; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
1002  /*empty*/;
1003 
1004  // At this point, we need a '.'.
1005  if (CurPtr[0] != '.') {
1006  CurPtr = TokStart+1;
1007  return lltok::Error;
1008  }
1009 
1010  ++CurPtr;
1011 
1012  // Skip over [0-9]*([eE][-+]?[0-9]+)?
1013  while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1014 
1015  if (CurPtr[0] == 'e' || CurPtr[0] == 'E') {
1016  if (isdigit(static_cast<unsigned char>(CurPtr[1])) ||
1017  ((CurPtr[1] == '-' || CurPtr[1] == '+') &&
1018  isdigit(static_cast<unsigned char>(CurPtr[2])))) {
1019  CurPtr += 2;
1020  while (isdigit(static_cast<unsigned char>(CurPtr[0]))) ++CurPtr;
1021  }
1022  }
1023 
1024  APFloatVal = APFloat(APFloat::IEEEdouble(),
1025  StringRef(TokStart, CurPtr - TokStart));
1026  return lltok::APFloat;
1027 }
objc arc contract
uint64_t CallInst * C
static const fltSemantics & IEEEquad() LLVM_READNONE
Definition: APFloat.cpp:125
static Type * getDoubleTy(LLVMContext &C)
Definition: Type.cpp:165
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void Warning(LocTy WarningLoc, const Twine &Msg) const
Definition: LLLexer.cpp:34
static const char * isLabelTail(const char *CurPtr)
isLabelTail - Return true if this pointer points to a valid end of a label.
Definition: LLLexer.cpp:148
bool Error(LocTy L, const Twine &Msg) const
Definition: LLLexer.cpp:29
demanded bits
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:813
APInt trunc(unsigned width) const
Truncate to new width.
Definition: APInt.cpp:818
#define DWKEYWORD(TYPE, TOKEN)
static void cleanup(BlockFrequencyInfoImplBase &BFI)
Clear all memory not needed downstream.
static Type * getMetadataTy(LLVMContext &C)
Definition: Type.cpp:166
static Type * getX86_MMXTy(LLVMContext &C)
Definition: Type.cpp:171
static Type * getX86_FP80Ty(LLVMContext &C)
Definition: Type.cpp:168
void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges=None, ArrayRef< SMFixIt > FixIts=None, bool ShowColors=true) const
Emit a message about the specified location with the specified string.
Definition: SourceMgr.cpp:225
static Type * getTokenTy(LLVMContext &C)
Definition: Type.cpp:167
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:164
#define TYPEKEYWORD(STR, LLVMTY)
static Type * getPPC_FP128Ty(LLVMContext &C)
Definition: Type.cpp:170
LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &, LLVMContext &C)
Definition: LLLexer.cpp:160
static bool isLabelChar(char C)
isLabelChar - Return true for [-a-zA-Z$._0-9].
Definition: LLLexer.cpp:142
This file implements a class to represent arbitrary precision integral constant values and operations...
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition: APInt.h:1512
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
static Type * getLabelTy(LLVMContext &C)
Definition: Type.cpp:162
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:122
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:161
static const unsigned End
Minimum number of bits that can be specified.
Definition: DerivedTypes.h:51
static const fltSemantics & x87DoubleExtended() LLVM_READNONE
Definition: APFloat.cpp:128
rewrite statepoints for gc
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:41
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges=None, ArrayRef< SMFixIt > FixIts=None) const
Return an SMDiagnostic at the specified location with the specified string.
Definition: SourceMgr.cpp:146
static Type * getFP128Ty(LLVMContext &C)
Definition: Type.cpp:169
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition: APInt.h:2094
static Type * getHalfTy(LLVMContext &C)
Definition: Type.cpp:163
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:240
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:116
Class for arbitrary precision integers.
Definition: APInt.h:69
Base class for user error types.
Definition: Error.h:331
unsigned hexDigitValue(char C)
Interpret the given character C as a hexadecimal digit and return its value.
Definition: StringExtras.h:54
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition: APInt.h:2099
iterator begin() const
Definition: StringRef.h:106
static const fltSemantics & PPCDoubleDouble() LLVM_READNONE
Definition: APFloat.cpp:134
static const size_t npos
Definition: StringRef.h:51
Deduce function attributes
static void UnEscapeLexed(std::string &Str)
Definition: LLLexer.cpp:115
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
#define KEYWORD(STR)
Maximum number of bits that can be specified.
Definition: DerivedTypes.h:52
Represents a location in source code.
Definition: SMLoc.h:24
iterator end() const
Definition: StringRef.h:108
#define OP(n)
Definition: regex2.h:73
#define INSTKEYWORD(STR, Enum)
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:233