2007-01-28 16:20:04 +08:00
|
|
|
//===--- Builtins.cpp - Builtin function implementation -------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2007-01-28 16:20:04 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements various things for builtin functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-14 09:05:48 +08:00
|
|
|
#include "clang/Basic/Builtins.h"
|
2007-10-07 16:58:51 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2010-12-01 01:35:24 +08:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2013-07-23 08:13:01 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2007-01-28 16:20:04 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
static const Builtin::Info BuiltinInfo[] = {
|
2015-08-07 13:14:44 +08:00
|
|
|
{ "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr},
|
|
|
|
#define BUILTIN(ID, TYPE, ATTRS) \
|
|
|
|
{ #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr },
|
2015-08-06 05:04:28 +08:00
|
|
|
#define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \
|
2015-08-07 13:14:44 +08:00
|
|
|
{ #ID, TYPE, ATTRS, nullptr, LANGS, nullptr },
|
2015-08-06 05:04:28 +08:00
|
|
|
#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \
|
2015-08-07 13:14:44 +08:00
|
|
|
{ #ID, TYPE, ATTRS, HEADER, LANGS, nullptr },
|
2009-06-14 09:05:48 +08:00
|
|
|
#include "clang/Basic/Builtins.def"
|
2007-01-28 16:20:04 +08:00
|
|
|
};
|
|
|
|
|
2015-08-06 09:01:12 +08:00
|
|
|
const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const {
|
2007-01-29 13:24:35 +08:00
|
|
|
if (ID < Builtin::FirstTSBuiltin)
|
|
|
|
return BuiltinInfo[ID];
|
2015-10-19 12:51:35 +08:00
|
|
|
assert(((ID - Builtin::FirstTSBuiltin) <
|
|
|
|
(TSRecords.size() + AuxTSRecords.size())) &&
|
2015-09-23 01:23:22 +08:00
|
|
|
"Invalid builtin ID!");
|
|
|
|
if (isAuxBuiltinID(ID))
|
|
|
|
return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin];
|
2007-01-29 13:24:35 +08:00
|
|
|
return TSRecords[ID - Builtin::FirstTSBuiltin];
|
2007-01-28 16:20:04 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 01:23:22 +08:00
|
|
|
void Builtin::Context::InitializeTarget(const TargetInfo &Target,
|
|
|
|
const TargetInfo *AuxTarget) {
|
2015-10-19 12:51:35 +08:00
|
|
|
assert(TSRecords.empty() && "Already initialized target?");
|
|
|
|
TSRecords = Target.getTargetBuiltins();
|
2015-09-23 01:23:22 +08:00
|
|
|
if (AuxTarget)
|
2015-10-19 12:51:35 +08:00
|
|
|
AuxTSRecords = AuxTarget->getTargetBuiltins();
|
2009-06-17 00:18:48 +08:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:28:34 +08:00
|
|
|
bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
|
2016-01-06 22:35:46 +08:00
|
|
|
for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; ++i)
|
|
|
|
if (FuncName.equals(BuiltinInfo[i].Name))
|
|
|
|
return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-06 09:01:12 +08:00
|
|
|
bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo,
|
2013-07-23 08:13:01 +08:00
|
|
|
const LangOptions &LangOpts) {
|
2016-01-06 22:35:46 +08:00
|
|
|
bool BuiltinsUnsupported =
|
|
|
|
(LangOpts.NoBuiltin || LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)) &&
|
|
|
|
strchr(BuiltinInfo.Attributes, 'f');
|
2013-07-23 08:13:01 +08:00
|
|
|
bool MathBuiltinsUnsupported =
|
2015-09-14 22:08:18 +08:00
|
|
|
LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName &&
|
2013-07-23 08:13:01 +08:00
|
|
|
llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h");
|
2015-08-06 05:04:28 +08:00
|
|
|
bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG);
|
|
|
|
bool MSModeUnsupported =
|
|
|
|
!LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG);
|
2018-10-31 04:31:30 +08:00
|
|
|
bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG;
|
2017-09-08 03:39:10 +08:00
|
|
|
bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 &&
|
|
|
|
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG;
|
2019-05-22 21:12:20 +08:00
|
|
|
bool OclC2Unsupported =
|
|
|
|
(LangOpts.OpenCLVersion != 200 && !LangOpts.OpenCLCPlusPlus) &&
|
|
|
|
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG;
|
2017-09-08 03:39:10 +08:00
|
|
|
bool OclCUnsupported = !LangOpts.OpenCL &&
|
|
|
|
(BuiltinInfo.Langs & ALL_OCLC_LANGUAGES);
|
2017-10-17 22:28:14 +08:00
|
|
|
bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG;
|
2019-04-24 10:23:30 +08:00
|
|
|
bool CPlusPlusUnsupported =
|
|
|
|
!LangOpts.CPlusPlus && BuiltinInfo.Langs == CXX_LANG;
|
2016-01-26 12:03:48 +08:00
|
|
|
return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported &&
|
2017-10-17 22:28:14 +08:00
|
|
|
!OclC1Unsupported && !OclC2Unsupported && !OpenMPUnsupported &&
|
2019-04-24 10:23:30 +08:00
|
|
|
!GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported &&
|
|
|
|
!CPlusPlusUnsupported;
|
2013-07-23 08:13:01 +08:00
|
|
|
}
|
|
|
|
|
2015-09-15 17:53:14 +08:00
|
|
|
/// initializeBuiltins - Mark the identifiers for all the builtins with their
|
2007-01-28 16:20:04 +08:00
|
|
|
/// appropriate builtin ID # and mark any non-portable builtin identifiers as
|
|
|
|
/// such.
|
2015-08-06 09:01:12 +08:00
|
|
|
void Builtin::Context::initializeBuiltins(IdentifierTable &Table,
|
2010-12-01 01:35:24 +08:00
|
|
|
const LangOptions& LangOpts) {
|
2007-01-28 16:20:04 +08:00
|
|
|
// Step #1: mark all target-independent builtins with their ID's.
|
2007-01-29 13:24:35 +08:00
|
|
|
for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i)
|
2015-08-06 09:01:12 +08:00
|
|
|
if (builtinIsSupported(BuiltinInfo[i], LangOpts)) {
|
2013-06-01 00:29:28 +08:00
|
|
|
Table.get(BuiltinInfo[i].Name).setBuiltinID(i);
|
2013-07-23 08:13:01 +08:00
|
|
|
}
|
2009-06-14 09:54:56 +08:00
|
|
|
|
2009-04-22 12:56:28 +08:00
|
|
|
// Step #2: Register target-specific builtins.
|
2015-10-19 12:51:35 +08:00
|
|
|
for (unsigned i = 0, e = TSRecords.size(); i != e; ++i)
|
2015-08-06 09:01:12 +08:00
|
|
|
if (builtinIsSupported(TSRecords[i], LangOpts))
|
2015-09-23 01:23:22 +08:00
|
|
|
Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin);
|
|
|
|
|
|
|
|
// Step #3: Register target-specific builtins for AuxTarget.
|
2015-10-19 12:51:35 +08:00
|
|
|
for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i)
|
2015-09-23 01:23:22 +08:00
|
|
|
Table.get(AuxTSRecords[i].Name)
|
2015-10-19 12:51:35 +08:00
|
|
|
.setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size());
|
2007-01-28 16:20:04 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 09:01:12 +08:00
|
|
|
void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) {
|
|
|
|
Table.get(getRecord(ID).Name).setBuiltinID(0);
|
2010-12-22 03:47:46 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 03:00:16 +08:00
|
|
|
unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const {
|
|
|
|
const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V');
|
|
|
|
if (!WidthPos)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
++WidthPos;
|
|
|
|
assert(*WidthPos == ':' &&
|
|
|
|
"Vector width specifier must be followed by a ':'");
|
|
|
|
++WidthPos;
|
|
|
|
|
|
|
|
char *EndPos;
|
|
|
|
unsigned Width = ::strtol(WidthPos, &EndPos, 10);
|
|
|
|
assert(*EndPos == ':' && "Vector width specific must end with a ':'");
|
|
|
|
return Width;
|
|
|
|
}
|
|
|
|
|
2014-01-04 04:10:54 +08:00
|
|
|
bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx,
|
|
|
|
bool &HasVAListArg, const char *Fmt) const {
|
|
|
|
assert(Fmt && "Not passed a format string");
|
|
|
|
assert(::strlen(Fmt) == 2 &&
|
|
|
|
"Format string needs to be two characters long");
|
|
|
|
assert(::toupper(Fmt[0]) == Fmt[1] &&
|
|
|
|
"Format string is not in the form \"xX\"");
|
|
|
|
|
2015-08-06 09:01:12 +08:00
|
|
|
const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt);
|
2014-01-04 04:10:54 +08:00
|
|
|
if (!Like)
|
2009-02-14 08:32:47 +08:00
|
|
|
return false;
|
|
|
|
|
2014-01-04 04:10:54 +08:00
|
|
|
HasVAListArg = (*Like == Fmt[1]);
|
2009-02-14 08:32:47 +08:00
|
|
|
|
2014-01-04 04:10:54 +08:00
|
|
|
++Like;
|
|
|
|
assert(*Like == ':' && "Format specifier must be followed by a ':'");
|
|
|
|
++Like;
|
2009-02-14 08:32:47 +08:00
|
|
|
|
2014-01-04 04:10:54 +08:00
|
|
|
assert(::strchr(Like, ':') && "Format specifier must end with a ':'");
|
2014-05-08 14:41:40 +08:00
|
|
|
FormatIdx = ::strtol(Like, nullptr, 10);
|
2009-02-14 08:32:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-04 04:10:54 +08:00
|
|
|
bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx,
|
|
|
|
bool &HasVAListArg) {
|
|
|
|
return isLike(ID, FormatIdx, HasVAListArg, "pP");
|
2010-07-16 10:11:15 +08:00
|
|
|
}
|
|
|
|
|
2014-01-04 04:10:54 +08:00
|
|
|
bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx,
|
|
|
|
bool &HasVAListArg) {
|
|
|
|
return isLike(ID, FormatIdx, HasVAListArg, "sS");
|
|
|
|
}
|
2018-04-17 05:30:08 +08:00
|
|
|
|
Emit !callback metadata and introduce the callback attribute
With commit r351627, LLVM gained the ability to apply (existing) IPO
optimizations on indirections through callbacks, or transitive calls.
The general idea is that we use an abstraction to hide the middle man
and represent the callback call in the context of the initial caller.
It is described in more detail in the commit message of the LLVM patch
r351627, the llvm::AbstractCallSite class description, and the
language reference section on callback-metadata.
This commit enables clang to emit !callback metadata that is
understood by LLVM. It does so in three different cases:
1) For known broker functions declarations that are directly
generated, e.g., __kmpc_fork_call for the OpenMP pragma parallel.
2) For known broker functions that are identified by their name and
source location through the builtin detection, e.g.,
pthread_create from the POSIX thread API.
3) For user annotated functions that carry the "callback(callee, ...)"
attribute. The attribute has to include the name, or index, of
the callback callee and how the passed arguments can be
identified (as many as the callback callee has). See the callback
attribute documentation for detailed information.
Differential Revision: https://reviews.llvm.org/D55483
llvm-svn: 351629
2019-01-19 13:36:54 +08:00
|
|
|
bool Builtin::Context::performsCallback(unsigned ID,
|
|
|
|
SmallVectorImpl<int> &Encoding) const {
|
|
|
|
const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C');
|
|
|
|
if (!CalleePos)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++CalleePos;
|
|
|
|
assert(*CalleePos == '<' &&
|
|
|
|
"Callback callee specifier must be followed by a '<'");
|
|
|
|
++CalleePos;
|
|
|
|
|
|
|
|
char *EndPos;
|
|
|
|
int CalleeIdx = ::strtol(CalleePos, &EndPos, 10);
|
|
|
|
assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!");
|
|
|
|
Encoding.push_back(CalleeIdx);
|
|
|
|
|
|
|
|
while (*EndPos == ',') {
|
|
|
|
const char *PayloadPos = EndPos + 1;
|
|
|
|
|
|
|
|
int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10);
|
|
|
|
Encoding.push_back(PayloadIdx);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(*EndPos == '>' && "Callback callee specifier must end with a '>'");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-17 05:30:08 +08:00
|
|
|
bool Builtin::Context::canBeRedeclared(unsigned ID) const {
|
|
|
|
return ID == Builtin::NotBuiltin ||
|
|
|
|
ID == Builtin::BI__va_start ||
|
|
|
|
(!hasReferenceArgsOrResult(ID) &&
|
|
|
|
!hasCustomTypechecking(ID));
|
|
|
|
}
|