Builtins.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 "BuiltinTargetFeatures.h"
  14. #include "clang/Basic/IdentifierTable.h"
  15. #include "clang/Basic/LangOptions.h"
  16. #include "clang/Basic/TargetInfo.h"
  17. #include "llvm/ADT/StringRef.h"
  18. using namespace clang;
  19. const char *HeaderDesc::getName() const {
  20. switch (ID) {
  21. #define HEADER(ID, NAME) \
  22. case ID: \
  23. return NAME;
  24. #include "clang/Basic/BuiltinHeaders.def"
  25. #undef HEADER
  26. };
  27. llvm_unreachable("Unknown HeaderDesc::HeaderID enum");
  28. }
  29. static constexpr Builtin::Info BuiltinInfo[] = {
  30. {"not a builtin function", nullptr, nullptr, nullptr, HeaderDesc::NO_HEADER,
  31. ALL_LANGUAGES},
  32. #define BUILTIN(ID, TYPE, ATTRS) \
  33. {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
  34. #define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
  35. {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, LANGS},
  36. #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
  37. {#ID, TYPE, ATTRS, nullptr, HeaderDesc::HEADER, LANGS},
  38. #include "clang/Basic/Builtins.def"
  39. };
  40. const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
  41. if (ID < Builtin::FirstTSBuiltin)
  42. return BuiltinInfo[ID];
  43. assert(((ID - Builtin::FirstTSBuiltin) <
  44. (TSRecords.size() + AuxTSRecords.size())) &&
  45. "Invalid builtin ID!");
  46. if (isAuxBuiltinID(ID))
  47. return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
  48. return TSRecords[ID - Builtin::FirstTSBuiltin];
  49. }
  50. void Builtin::Context::InitializeTarget(const TargetInfo &Target,
  51. const TargetInfo *AuxTarget) {
  52. assert(TSRecords.empty() && "Already initialized target?");
  53. TSRecords = Target.getTargetBuiltins();
  54. if (AuxTarget)
  55. AuxTSRecords = AuxTarget->getTargetBuiltins();
  56. }
  57. bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
  58. bool InStdNamespace = FuncName.consume_front("std-");
  59. for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin;
  60. ++i) {
  61. if (FuncName.equals(BuiltinInfo[i].Name) &&
  62. (bool)strchr(BuiltinInfo[i].Attributes, 'z') == InStdNamespace)
  63. return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
  64. }
  65. return false;
  66. }
  67. /// Is this builtin supported according to the given language options?
  68. static bool builtinIsSupported(const Builtin::Info &BuiltinInfo,
  69. const LangOptions &LangOpts) {
  70. /* Builtins Unsupported */
  71. if (LangOpts.NoBuiltin && strchr(BuiltinInfo.Attributes, 'f') != nullptr)
  72. return false;
  73. /* CorBuiltins Unsupported */
  74. if (!LangOpts.Coroutines && (BuiltinInfo.Langs & COR_LANG))
  75. return false;
  76. /* MathBuiltins Unsupported */
  77. if (LangOpts.NoMathBuiltin && BuiltinInfo.Header.ID == HeaderDesc::MATH_H)
  78. return false;
  79. /* GnuMode Unsupported */
  80. if (!LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG))
  81. return false;
  82. /* MSMode Unsupported */
  83. if (!LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG))
  84. return false;
  85. /* ObjC Unsupported */
  86. if (!LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG)
  87. return false;
  88. /* OpenCLC Unsupported */
  89. if (!LangOpts.OpenCL && (BuiltinInfo.Langs & ALL_OCL_LANGUAGES))
  90. return false;
  91. /* OopenCL GAS Unsupported */
  92. if (!LangOpts.OpenCLGenericAddressSpace && (BuiltinInfo.Langs & OCL_GAS))
  93. return false;
  94. /* OpenCL Pipe Unsupported */
  95. if (!LangOpts.OpenCLPipes && (BuiltinInfo.Langs & OCL_PIPE))
  96. return false;
  97. // Device side enqueue is not supported until OpenCL 2.0. In 2.0 and higher
  98. // support is indicated with language option for blocks.
  99. /* OpenCL DSE Unsupported */
  100. if ((LangOpts.getOpenCLCompatibleVersion() < 200 || !LangOpts.Blocks) &&
  101. (BuiltinInfo.Langs & OCL_DSE))
  102. return false;
  103. /* OpenMP Unsupported */
  104. if (!LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG)
  105. return false;
  106. /* CUDA Unsupported */
  107. if (!LangOpts.CUDA && BuiltinInfo.Langs == CUDA_LANG)
  108. return false;
  109. /* CPlusPlus Unsupported */
  110. if (!LangOpts.CPlusPlus && BuiltinInfo.Langs == CXX_LANG)
  111. return false;
  112. return true;
  113. }
  114. /// initializeBuiltins - Mark the identifiers for all the builtins with their
  115. /// appropriate builtin ID # and mark any non-portable builtin identifiers as
  116. /// such.
  117. void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
  118. const LangOptions& LangOpts) {
  119. // Step #1: mark all target-independent builtins with their ID's.
  120. for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
  121. if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
  122. Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
  123. }
  124. // Step #2: Register target-specific builtins.
  125. for (unsigned i = 0, e = TSRecords.size(); i != e; ++i)
  126. if (builtinIsSupported(TSRecords[i], LangOpts))
  127. Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
  128. // Step #3: Register target-specific builtins for AuxTarget.
  129. for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i)
  130. Table.get(AuxTSRecords[i].Name)
  131. .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
  132. // Step #4: Unregister any builtins specified by -fno-builtin-foo.
  133. for (llvm::StringRef Name : LangOpts.NoBuiltinFuncs) {
  134. bool InStdNamespace = Name.consume_front("std-");
  135. auto NameIt = Table.find(Name);
  136. if (NameIt != Table.end()) {
  137. unsigned ID = NameIt->second->getBuiltinID();
  138. if (ID != Builtin::NotBuiltin && isPredefinedLibFunction(ID) &&
  139. isInStdNamespace(ID) == InStdNamespace) {
  140. Table.get(Name).setBuiltinID(Builtin::NotBuiltin);
  141. }
  142. }
  143. }
  144. }
  145. unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
  146. const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
  147. if (!WidthPos)
  148. return 0;
  149. ++WidthPos;
  150. assert(*WidthPos == ':' &&
  151. "Vector width specifier must be followed by a ':'");
  152. ++WidthPos;
  153. char *EndPos;
  154. unsigned Width = ::strtol(WidthPos, &EndPos, 10);
  155. assert(*EndPos == ':' && "Vector width specific must end with a ':'");
  156. return Width;
  157. }
  158. bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
  159. bool &HasVAListArg, const char *Fmt) const {
  160. assert(Fmt && "Not passed a format string");
  161. assert(::strlen(Fmt) == 2 &&
  162. "Format string needs to be two characters long");
  163. assert(::toupper(Fmt[0]) == Fmt[1] &&
  164. "Format string is not in the form \"xX\"");
  165. const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
  166. if (!Like)
  167. return false;
  168. HasVAListArg = (*Like == Fmt[1]);
  169. ++Like;
  170. assert(*Like == ':' && "Format specifier must be followed by a ':'");
  171. ++Like;
  172. assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
  173. FormatIdx = ::strtol(Like, nullptr, 10);
  174. return true;
  175. }
  176. bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
  177. bool &HasVAListArg) {
  178. return isLike(ID, FormatIdx, HasVAListArg, "pP");
  179. }
  180. bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
  181. bool &HasVAListArg) {
  182. return isLike(ID, FormatIdx, HasVAListArg, "sS");
  183. }
  184. bool Builtin::Context::performsCallback(unsigned ID,
  185. SmallVectorImpl<int> &Encoding) const {
  186. const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
  187. if (!CalleePos)
  188. return false;
  189. ++CalleePos;
  190. assert(*CalleePos == '<' &&
  191. "Callback callee specifier must be followed by a '<'");
  192. ++CalleePos;
  193. char *EndPos;
  194. int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
  195. assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
  196. Encoding.push_back(CalleeIdx);
  197. while (*EndPos == ',') {
  198. const char *PayloadPos = EndPos + 1;
  199. int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
  200. Encoding.push_back(PayloadIdx);
  201. }
  202. assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
  203. return true;
  204. }
  205. bool Builtin::Context::canBeRedeclared(unsigned ID) const {
  206. return ID == Builtin::NotBuiltin || ID == Builtin::BI__va_start ||
  207. ID == Builtin::BI__builtin_assume_aligned ||
  208. (!hasReferenceArgsOrResult(ID) && !hasCustomTypechecking(ID)) ||
  209. isInStdNamespace(ID);
  210. }
  211. bool Builtin::evaluateRequiredTargetFeatures(
  212. StringRef RequiredFeatures, const llvm::StringMap<bool> &TargetFetureMap) {
  213. // Return true if the builtin doesn't have any required features.
  214. if (RequiredFeatures.empty())
  215. return true;
  216. assert(!RequiredFeatures.contains(' ') && "Space in feature list");
  217. TargetFeatures TF(TargetFetureMap);
  218. return TF.hasRequiredFeatures(RequiredFeatures);
  219. }