15abdec79SChris Lattner //===--- Builtins.cpp - Builtin function implementation -------------------===//
25abdec79SChris Lattner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65abdec79SChris Lattner //
75abdec79SChris Lattner //===----------------------------------------------------------------------===//
85abdec79SChris Lattner //
95abdec79SChris Lattner // This file implements various things for builtin functions.
105abdec79SChris Lattner //
115abdec79SChris Lattner //===----------------------------------------------------------------------===//
125abdec79SChris Lattner
135abdec79SChris Lattner #include "clang/Basic/Builtins.h"
14*cefe472cSYaxun (Sam) Liu #include "BuiltinTargetFeatures.h"
155abdec79SChris Lattner #include "clang/Basic/IdentifierTable.h"
16e8473c2fSFariborz Jahanian #include "clang/Basic/LangOptions.h"
173a02247dSChandler Carruth #include "clang/Basic/TargetInfo.h"
18aefa5e2fSEli Bendersky #include "llvm/ADT/StringRef.h"
195abdec79SChris Lattner using namespace clang;
205abdec79SChris Lattner
215abdec79SChris Lattner static const Builtin::Info BuiltinInfo[] = {
2207d3b625SCraig Topper { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr},
2307d3b625SCraig Topper #define BUILTIN(ID, TYPE, ATTRS) \
2407d3b625SCraig Topper { #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr },
2535869a26SEric Christopher #define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
2607d3b625SCraig Topper { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr },
2735869a26SEric Christopher #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
2807d3b625SCraig Topper { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr },
295abdec79SChris Lattner #include "clang/Basic/Builtins.def"
305abdec79SChris Lattner };
315abdec79SChris Lattner
getRecord(unsigned ID) const3202d5d86bSEric Christopher const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
335abdec79SChris Lattner if (ID < Builtin::FirstTSBuiltin)
345abdec79SChris Lattner return BuiltinInfo[ID];
356c03a544SCraig Topper assert(((ID - Builtin::FirstTSBuiltin) <
366c03a544SCraig Topper (TSRecords.size() + AuxTSRecords.size())) &&
37b5bc923aSArtem Belevich "Invalid builtin ID!");
38b5bc923aSArtem Belevich if (isAuxBuiltinID(ID))
39b5bc923aSArtem Belevich return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
405abdec79SChris Lattner return TSRecords[ID - Builtin::FirstTSBuiltin];
415abdec79SChris Lattner }
425abdec79SChris Lattner
InitializeTarget(const TargetInfo & Target,const TargetInfo * AuxTarget)43b5bc923aSArtem Belevich void Builtin::Context::InitializeTarget(const TargetInfo &Target,
44b5bc923aSArtem Belevich const TargetInfo *AuxTarget) {
456c03a544SCraig Topper assert(TSRecords.empty() && "Already initialized target?");
466c03a544SCraig Topper TSRecords = Target.getTargetBuiltins();
47b5bc923aSArtem Belevich if (AuxTarget)
486c03a544SCraig Topper AuxTSRecords = AuxTarget->getTargetBuiltins();
494ef49c1dSChris Lattner }
504ef49c1dSChris Lattner
isBuiltinFunc(llvm::StringRef FuncName)511c85a2e8SGuillaume Chatelet bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
5272315d02SRichard Smith bool InStdNamespace = FuncName.consume_front("std-");
5372315d02SRichard Smith for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin;
5472315d02SRichard Smith ++i) {
5572315d02SRichard Smith if (FuncName.equals(BuiltinInfo[i].Name) &&
5672315d02SRichard Smith (bool)strchr(BuiltinInfo[i].Attributes, 'z') == InStdNamespace)
577dbc9cf8SChad Rosier return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
5872315d02SRichard Smith }
597dbc9cf8SChad Rosier
607dbc9cf8SChad Rosier return false;
617dbc9cf8SChad Rosier }
627dbc9cf8SChad Rosier
6372315d02SRichard Smith /// Is this builtin supported according to the given language options?
builtinIsSupported(const Builtin::Info & BuiltinInfo,const LangOptions & LangOpts)6472315d02SRichard Smith static bool builtinIsSupported(const Builtin::Info &BuiltinInfo,
65aefa5e2fSEli Bendersky const LangOptions &LangOpts) {
667dbc9cf8SChad Rosier bool BuiltinsUnsupported =
6772315d02SRichard Smith LangOpts.NoBuiltin && strchr(BuiltinInfo.Attributes, 'f') != nullptr;
68a4b61c82SZahira Ammarguellat bool CorBuiltinsUnsupported =
69a4b61c82SZahira Ammarguellat !LangOpts.Coroutines && (BuiltinInfo.Langs & COR_LANG);
70aefa5e2fSEli Bendersky bool MathBuiltinsUnsupported =
71aefa5e2fSEli Bendersky LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
72aefa5e2fSEli Bendersky llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
7335869a26SEric Christopher bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG);
7435869a26SEric Christopher bool MSModeUnsupported =
7535869a26SEric Christopher !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
76fa98390bSErik Pilkington bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG;
77bee4bd70SAnton Zabaznov bool OclCUnsupported =
78bee4bd70SAnton Zabaznov !LangOpts.OpenCL && (BuiltinInfo.Langs & ALL_OCL_LANGUAGES);
79bee4bd70SAnton Zabaznov bool OclGASUnsupported =
80bee4bd70SAnton Zabaznov !LangOpts.OpenCLGenericAddressSpace && (BuiltinInfo.Langs & OCL_GAS);
81bee4bd70SAnton Zabaznov bool OclPipeUnsupported =
82bee4bd70SAnton Zabaznov !LangOpts.OpenCLPipes && (BuiltinInfo.Langs & OCL_PIPE);
83bee4bd70SAnton Zabaznov // Device side enqueue is not supported until OpenCL 2.0. In 2.0 and higher
84bee4bd70SAnton Zabaznov // support is indicated with language option for blocks.
85bee4bd70SAnton Zabaznov bool OclDSEUnsupported =
86bee4bd70SAnton Zabaznov (LangOpts.getOpenCLCompatibleVersion() < 200 || !LangOpts.Blocks) &&
87bee4bd70SAnton Zabaznov (BuiltinInfo.Langs & OCL_DSE);
8823604a83SJonas Hahnfeld bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG;
89cc947716SYaxun (Sam) Liu bool CUDAUnsupported = !LangOpts.CUDA && BuiltinInfo.Langs == CUDA_LANG;
90add16a8dSEric Fiselier bool CPlusPlusUnsupported =
91add16a8dSEric Fiselier !LangOpts.CPlusPlus && BuiltinInfo.Langs == CXX_LANG;
92a4b61c82SZahira Ammarguellat return !BuiltinsUnsupported && !CorBuiltinsUnsupported &&
93bee4bd70SAnton Zabaznov !MathBuiltinsUnsupported && !OclCUnsupported && !OclGASUnsupported &&
94bee4bd70SAnton Zabaznov !OclPipeUnsupported && !OclDSEUnsupported && !OpenMPUnsupported &&
95bee4bd70SAnton Zabaznov !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported &&
96bee4bd70SAnton Zabaznov !CPlusPlusUnsupported && !CUDAUnsupported;
97aefa5e2fSEli Bendersky }
98aefa5e2fSEli Bendersky
99a8d3a2e9SIsmail Donmez /// initializeBuiltins - Mark the identifiers for all the builtins with their
1005abdec79SChris Lattner /// appropriate builtin ID # and mark any non-portable builtin identifiers as
1015abdec79SChris Lattner /// such.
initializeBuiltins(IdentifierTable & Table,const LangOptions & LangOpts)10202d5d86bSEric Christopher void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
103e8473c2fSFariborz Jahanian const LangOptions& LangOpts) {
1045abdec79SChris Lattner // Step #1: mark all target-independent builtins with their ID's.
1055abdec79SChris Lattner for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
10602d5d86bSEric Christopher if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
1075abdec79SChris Lattner Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
108aefa5e2fSEli Bendersky }
1095abdec79SChris Lattner
1105abdec79SChris Lattner // Step #2: Register target-specific builtins.
1116c03a544SCraig Topper for (unsigned i = 0, e = TSRecords.size(); i != e; ++i)
11202d5d86bSEric Christopher if (builtinIsSupported(TSRecords[i], LangOpts))
1135abdec79SChris Lattner Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
114b5bc923aSArtem Belevich
115b5bc923aSArtem Belevich // Step #3: Register target-specific builtins for AuxTarget.
1166c03a544SCraig Topper for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i)
117b5bc923aSArtem Belevich Table.get(AuxTSRecords[i].Name)
1186c03a544SCraig Topper .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
11972315d02SRichard Smith
12072315d02SRichard Smith // Step #4: Unregister any builtins specified by -fno-builtin-foo.
12172315d02SRichard Smith for (llvm::StringRef Name : LangOpts.NoBuiltinFuncs) {
12272315d02SRichard Smith bool InStdNamespace = Name.consume_front("std-");
12372315d02SRichard Smith auto NameIt = Table.find(Name);
12472315d02SRichard Smith if (NameIt != Table.end()) {
12572315d02SRichard Smith unsigned ID = NameIt->second->getBuiltinID();
12672315d02SRichard Smith if (ID != Builtin::NotBuiltin && isPredefinedLibFunction(ID) &&
12772315d02SRichard Smith isInStdNamespace(ID) == InStdNamespace) {
12872315d02SRichard Smith Table.get(Name).setBuiltinID(Builtin::NotBuiltin);
12972315d02SRichard Smith }
13072315d02SRichard Smith }
13172315d02SRichard Smith }
1325abdec79SChris Lattner }
1335abdec79SChris Lattner
getRequiredVectorWidth(unsigned ID) const13474c10e32SCraig Topper unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
13574c10e32SCraig Topper const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
13674c10e32SCraig Topper if (!WidthPos)
13774c10e32SCraig Topper return 0;
13874c10e32SCraig Topper
13974c10e32SCraig Topper ++WidthPos;
14074c10e32SCraig Topper assert(*WidthPos == ':' &&
14174c10e32SCraig Topper "Vector width specifier must be followed by a ':'");
14274c10e32SCraig Topper ++WidthPos;
14374c10e32SCraig Topper
14474c10e32SCraig Topper char *EndPos;
14574c10e32SCraig Topper unsigned Width = ::strtol(WidthPos, &EndPos, 10);
14674c10e32SCraig Topper assert(*EndPos == ':' && "Vector width specific must end with a ':'");
14774c10e32SCraig Topper return Width;
14874c10e32SCraig Topper }
14974c10e32SCraig Topper
isLike(unsigned ID,unsigned & FormatIdx,bool & HasVAListArg,const char * Fmt) const15069714e78SAaron Ballman bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
15169714e78SAaron Ballman bool &HasVAListArg, const char *Fmt) const {
15269714e78SAaron Ballman assert(Fmt && "Not passed a format string");
15369714e78SAaron Ballman assert(::strlen(Fmt) == 2 &&
15469714e78SAaron Ballman "Format string needs to be two characters long");
15569714e78SAaron Ballman assert(::toupper(Fmt[0]) == Fmt[1] &&
15669714e78SAaron Ballman "Format string is not in the form \"xX\"");
15769714e78SAaron Ballman
15802d5d86bSEric Christopher const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
15969714e78SAaron Ballman if (!Like)
1605abdec79SChris Lattner return false;
1615abdec79SChris Lattner
16269714e78SAaron Ballman HasVAListArg = (*Like == Fmt[1]);
1635abdec79SChris Lattner
16469714e78SAaron Ballman ++Like;
16569714e78SAaron Ballman assert(*Like == ':' && "Format specifier must be followed by a ':'");
16669714e78SAaron Ballman ++Like;
1675abdec79SChris Lattner
16869714e78SAaron Ballman assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
169f1186c5aSCraig Topper FormatIdx = ::strtol(Like, nullptr, 10);
1705abdec79SChris Lattner return true;
1715abdec79SChris Lattner }
1725abdec79SChris Lattner
isPrintfLike(unsigned ID,unsigned & FormatIdx,bool & HasVAListArg)17369714e78SAaron Ballman bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
1745932c351STed Kremenek bool &HasVAListArg) {
17569714e78SAaron Ballman return isLike(ID, FormatIdx, HasVAListArg, "pP");
1765932c351STed Kremenek }
1775932c351STed Kremenek
isScanfLike(unsigned ID,unsigned & FormatIdx,bool & HasVAListArg)17869714e78SAaron Ballman bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
17969714e78SAaron Ballman bool &HasVAListArg) {
18069714e78SAaron Ballman return isLike(ID, FormatIdx, HasVAListArg, "sS");
18169714e78SAaron Ballman }
18241af9713SErich Keane
performsCallback(unsigned ID,SmallVectorImpl<int> & Encoding) const183ac991bbbSJohannes Doerfert bool Builtin::Context::performsCallback(unsigned ID,
184ac991bbbSJohannes Doerfert SmallVectorImpl<int> &Encoding) const {
185ac991bbbSJohannes Doerfert const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
186ac991bbbSJohannes Doerfert if (!CalleePos)
187ac991bbbSJohannes Doerfert return false;
188ac991bbbSJohannes Doerfert
189ac991bbbSJohannes Doerfert ++CalleePos;
190ac991bbbSJohannes Doerfert assert(*CalleePos == '<' &&
191ac991bbbSJohannes Doerfert "Callback callee specifier must be followed by a '<'");
192ac991bbbSJohannes Doerfert ++CalleePos;
193ac991bbbSJohannes Doerfert
194ac991bbbSJohannes Doerfert char *EndPos;
195ac991bbbSJohannes Doerfert int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
196ac991bbbSJohannes Doerfert assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
197ac991bbbSJohannes Doerfert Encoding.push_back(CalleeIdx);
198ac991bbbSJohannes Doerfert
199ac991bbbSJohannes Doerfert while (*EndPos == ',') {
200ac991bbbSJohannes Doerfert const char *PayloadPos = EndPos + 1;
201ac991bbbSJohannes Doerfert
202ac991bbbSJohannes Doerfert int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
203ac991bbbSJohannes Doerfert Encoding.push_back(PayloadIdx);
204ac991bbbSJohannes Doerfert }
205ac991bbbSJohannes Doerfert
206ac991bbbSJohannes Doerfert assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
207ac991bbbSJohannes Doerfert return true;
208ac991bbbSJohannes Doerfert }
209ac991bbbSJohannes Doerfert
canBeRedeclared(unsigned ID) const21041af9713SErich Keane bool Builtin::Context::canBeRedeclared(unsigned ID) const {
21172315d02SRichard Smith return ID == Builtin::NotBuiltin || ID == Builtin::BI__va_start ||
21272315d02SRichard Smith (!hasReferenceArgsOrResult(ID) && !hasCustomTypechecking(ID)) ||
21372315d02SRichard Smith isInStdNamespace(ID);
21441af9713SErich Keane }
215*cefe472cSYaxun (Sam) Liu
evaluateRequiredTargetFeatures(StringRef RequiredFeatures,const llvm::StringMap<bool> & TargetFetureMap)216*cefe472cSYaxun (Sam) Liu bool Builtin::evaluateRequiredTargetFeatures(
217*cefe472cSYaxun (Sam) Liu StringRef RequiredFeatures, const llvm::StringMap<bool> &TargetFetureMap) {
218*cefe472cSYaxun (Sam) Liu // Return true if the builtin doesn't have any required features.
219*cefe472cSYaxun (Sam) Liu if (RequiredFeatures.empty())
220*cefe472cSYaxun (Sam) Liu return true;
221*cefe472cSYaxun (Sam) Liu assert(!RequiredFeatures.contains(' ') && "Space in feature list");
222*cefe472cSYaxun (Sam) Liu
223*cefe472cSYaxun (Sam) Liu TargetFeatures TF(TargetFetureMap);
224*cefe472cSYaxun (Sam) Liu return TF.hasRequiredFeatures(RequiredFeatures);
225*cefe472cSYaxun (Sam) Liu }
226