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