Builtins.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //===--- Builtins.cpp - Builtin function implementation -------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements various things for builtin functions.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Basic/Builtins.h"
  13. #include "clang/Basic/IdentifierTable.h"
  14. #include "clang/Basic/LangOptions.h"
  15. #include "clang/Basic/TargetInfo.h"
  16. #include "llvm/ADT/StringRef.h"
  17. using namespace clang;
  18. static const Builtin::Info BuiltinInfo[] = {
  19. { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr},
  20. #define BUILTIN(ID, TYPE, ATTRS) \
  21. { #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr },
  22. #define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
  23. { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr },
  24. #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
  25. { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr },
  26. #include "clang/Basic/Builtins.def"
  27. };
  28. const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
  29. if (ID < Builtin::FirstTSBuiltin)
  30. return BuiltinInfo[ID];
  31. assert(((ID - Builtin::FirstTSBuiltin) <
  32. (TSRecords.size() + AuxTSRecords.size())) &&
  33. "Invalid builtin ID!");
  34. if (isAuxBuiltinID(ID))
  35. return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
  36. return TSRecords[ID - Builtin::FirstTSBuiltin];
  37. }
  38. void Builtin::Context::InitializeTarget(const TargetInfo &Target,
  39. const TargetInfo *AuxTarget) {
  40. assert(TSRecords.empty() && "Already initialized target?");
  41. TSRecords = Target.getTargetBuiltins();
  42. if (AuxTarget)
  43. AuxTSRecords = AuxTarget->getTargetBuiltins();
  44. }
  45. bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
  46. for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; ++i)
  47. if (FuncName.equals(BuiltinInfo[i].Name))
  48. return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
  49. return false;
  50. }
  51. bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
  52. const LangOptions &LangOpts) {
  53. bool BuiltinsUnsupported =
  54. (LangOpts.NoBuiltin || LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)) &&
  55. strchr(BuiltinInfo.Attributes, 'f');
  56. bool CorBuiltinsUnsupported =
  57. !LangOpts.Coroutines && (BuiltinInfo.Langs & COR_LANG);
  58. bool MathBuiltinsUnsupported =
  59. LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
  60. llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
  61. bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG);
  62. bool MSModeUnsupported =
  63. !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
  64. bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG;
  65. bool OclCUnsupported =
  66. !LangOpts.OpenCL && (BuiltinInfo.Langs & ALL_OCL_LANGUAGES);
  67. bool OclGASUnsupported =
  68. !LangOpts.OpenCLGenericAddressSpace && (BuiltinInfo.Langs & OCL_GAS);
  69. bool OclPipeUnsupported =
  70. !LangOpts.OpenCLPipes && (BuiltinInfo.Langs & OCL_PIPE);
  71. // Device side enqueue is not supported until OpenCL 2.0. In 2.0 and higher
  72. // support is indicated with language option for blocks.
  73. bool OclDSEUnsupported =
  74. (LangOpts.getOpenCLCompatibleVersion() < 200 || !LangOpts.Blocks) &&
  75. (BuiltinInfo.Langs & OCL_DSE);
  76. bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG;
  77. bool CUDAUnsupported = !LangOpts.CUDA && BuiltinInfo.Langs == CUDA_LANG;
  78. bool CPlusPlusUnsupported =
  79. !LangOpts.CPlusPlus && BuiltinInfo.Langs == CXX_LANG;
  80. return !BuiltinsUnsupported && !CorBuiltinsUnsupported &&
  81. !MathBuiltinsUnsupported && !OclCUnsupported && !OclGASUnsupported &&
  82. !OclPipeUnsupported && !OclDSEUnsupported && !OpenMPUnsupported &&
  83. !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported &&
  84. !CPlusPlusUnsupported && !CUDAUnsupported;
  85. }
  86. /// initializeBuiltins - Mark the identifiers for all the builtins with their
  87. /// appropriate builtin ID # and mark any non-portable builtin identifiers as
  88. /// such.
  89. void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
  90. const LangOptions& LangOpts) {
  91. // Step #1: mark all target-independent builtins with their ID's.
  92. for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
  93. if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
  94. Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
  95. }
  96. // Step #2: Register target-specific builtins.
  97. for (unsigned i = 0, e = TSRecords.size(); i != e; ++i)
  98. if (builtinIsSupported(TSRecords[i], LangOpts))
  99. Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
  100. // Step #3: Register target-specific builtins for AuxTarget.
  101. for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i)
  102. Table.get(AuxTSRecords[i].Name)
  103. .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
  104. }
  105. unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
  106. const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
  107. if (!WidthPos)
  108. return 0;
  109. ++WidthPos;
  110. assert(*WidthPos == ':' &&
  111. "Vector width specifier must be followed by a ':'");
  112. ++WidthPos;
  113. char *EndPos;
  114. unsigned Width = ::strtol(WidthPos, &EndPos, 10);
  115. assert(*EndPos == ':' && "Vector width specific must end with a ':'");
  116. return Width;
  117. }
  118. bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
  119. bool &HasVAListArg, const char *Fmt) const {
  120. assert(Fmt && "Not passed a format string");
  121. assert(::strlen(Fmt) == 2 &&
  122. "Format string needs to be two characters long");
  123. assert(::toupper(Fmt[0]) == Fmt[1] &&
  124. "Format string is not in the form \"xX\"");
  125. const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
  126. if (!Like)
  127. return false;
  128. HasVAListArg = (*Like == Fmt[1]);
  129. ++Like;
  130. assert(*Like == ':' && "Format specifier must be followed by a ':'");
  131. ++Like;
  132. assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
  133. FormatIdx = ::strtol(Like, nullptr, 10);
  134. return true;
  135. }
  136. bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
  137. bool &HasVAListArg) {
  138. return isLike(ID, FormatIdx, HasVAListArg, "pP");
  139. }
  140. bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
  141. bool &HasVAListArg) {
  142. return isLike(ID, FormatIdx, HasVAListArg, "sS");
  143. }
  144. bool Builtin::Context::performsCallback(unsigned ID,
  145. SmallVectorImpl<int> &Encoding) const {
  146. const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
  147. if (!CalleePos)
  148. return false;
  149. ++CalleePos;
  150. assert(*CalleePos == '<' &&
  151. "Callback callee specifier must be followed by a '<'");
  152. ++CalleePos;
  153. char *EndPos;
  154. int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
  155. assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
  156. Encoding.push_back(CalleeIdx);
  157. while (*EndPos == ',') {
  158. const char *PayloadPos = EndPos + 1;
  159. int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
  160. Encoding.push_back(PayloadIdx);
  161. }
  162. assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
  163. return true;
  164. }
  165. bool Builtin::Context::canBeRedeclared(unsigned ID) const {
  166. return ID == Builtin::NotBuiltin ||
  167. ID == Builtin::BI__va_start ||
  168. (!hasReferenceArgsOrResult(ID) &&
  169. !hasCustomTypechecking(ID));
  170. }