Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 1 | //===--- Builtins.cpp - Builtin function implementation -------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements various things for builtin functions. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Chris Lattner | 5abdec7 | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 13 | #include "clang/Basic/Builtins.h" |
Chris Lattner | ef6b136 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 14 | #include "clang/Basic/IdentifierTable.h" |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 15 | #include "clang/Basic/LangOptions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
Eli Bendersky | aefa5e2 | 2013-07-23 00:13:01 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringRef.h" |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
| 20 | static const Builtin::Info BuiltinInfo[] = { |
Craig Topper | 07d3b62 | 2015-08-07 05:14:44 +0000 | [diff] [blame] | 21 | { "not a builtin function", nullptr, nullptr, nullptr, ALL_LANGUAGES,nullptr}, |
| 22 | #define BUILTIN(ID, TYPE, ATTRS) \ |
| 23 | { #ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr }, |
Eric Christopher | 35869a2 | 2015-08-05 21:04:28 +0000 | [diff] [blame] | 24 | #define LANGBUILTIN(ID, TYPE, ATTRS, LANGS) \ |
Craig Topper | 07d3b62 | 2015-08-07 05:14:44 +0000 | [diff] [blame] | 25 | { #ID, TYPE, ATTRS, nullptr, LANGS, nullptr }, |
Eric Christopher | 35869a2 | 2015-08-05 21:04:28 +0000 | [diff] [blame] | 26 | #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, LANGS) \ |
Craig Topper | 07d3b62 | 2015-08-07 05:14:44 +0000 | [diff] [blame] | 27 | { #ID, TYPE, ATTRS, HEADER, LANGS, nullptr }, |
Chris Lattner | 5abdec7 | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 28 | #include "clang/Basic/Builtins.def" |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 31 | const Builtin::Info &Builtin::Context::getRecord(unsigned ID) const { |
Chris Lattner | 10a5b38 | 2007-01-29 05:24:35 +0000 | [diff] [blame] | 32 | if (ID < Builtin::FirstTSBuiltin) |
| 33 | return BuiltinInfo[ID]; |
Craig Topper | 6c03a54 | 2015-10-19 04:51:35 +0000 | [diff] [blame] | 34 | assert(((ID - Builtin::FirstTSBuiltin) < |
| 35 | (TSRecords.size() + AuxTSRecords.size())) && |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 36 | "Invalid builtin ID!"); |
| 37 | if (isAuxBuiltinID(ID)) |
| 38 | return AuxTSRecords[getAuxBuiltinID(ID) - Builtin::FirstTSBuiltin]; |
Chris Lattner | 10a5b38 | 2007-01-29 05:24:35 +0000 | [diff] [blame] | 39 | return TSRecords[ID - Builtin::FirstTSBuiltin]; |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 42 | void Builtin::Context::InitializeTarget(const TargetInfo &Target, |
| 43 | const TargetInfo *AuxTarget) { |
Craig Topper | 6c03a54 | 2015-10-19 04:51:35 +0000 | [diff] [blame] | 44 | assert(TSRecords.empty() && "Already initialized target?"); |
| 45 | TSRecords = Target.getTargetBuiltins(); |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 46 | if (AuxTarget) |
Craig Topper | 6c03a54 | 2015-10-19 04:51:35 +0000 | [diff] [blame] | 47 | AuxTSRecords = AuxTarget->getTargetBuiltins(); |
Chris Lattner | 4ef49c1 | 2009-06-16 16:18:48 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Chad Rosier | 7dbc9cf | 2016-01-06 14:35:46 +0000 | [diff] [blame] | 50 | bool Builtin::Context::isBuiltinFunc(const char *Name) { |
| 51 | StringRef FuncName(Name); |
| 52 | for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin; ++i) |
| 53 | if (FuncName.equals(BuiltinInfo[i].Name)) |
| 54 | return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr; |
| 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 59 | bool Builtin::Context::builtinIsSupported(const Builtin::Info &BuiltinInfo, |
Eli Bendersky | aefa5e2 | 2013-07-23 00:13:01 +0000 | [diff] [blame] | 60 | const LangOptions &LangOpts) { |
Chad Rosier | 7dbc9cf | 2016-01-06 14:35:46 +0000 | [diff] [blame] | 61 | bool BuiltinsUnsupported = |
| 62 | (LangOpts.NoBuiltin || LangOpts.isNoBuiltinFunc(BuiltinInfo.Name)) && |
| 63 | strchr(BuiltinInfo.Attributes, 'f'); |
Eli Bendersky | aefa5e2 | 2013-07-23 00:13:01 +0000 | [diff] [blame] | 64 | bool MathBuiltinsUnsupported = |
Rachel Craik | f55d058 | 2015-09-14 14:08:18 +0000 | [diff] [blame] | 65 | LangOpts.NoMathBuiltin && BuiltinInfo.HeaderName && |
Eli Bendersky | aefa5e2 | 2013-07-23 00:13:01 +0000 | [diff] [blame] | 66 | llvm::StringRef(BuiltinInfo.HeaderName).equals("math.h"); |
Eric Christopher | 35869a2 | 2015-08-05 21:04:28 +0000 | [diff] [blame] | 67 | bool GnuModeUnsupported = !LangOpts.GNUMode && (BuiltinInfo.Langs & GNU_LANG); |
| 68 | bool MSModeUnsupported = |
| 69 | !LangOpts.MicrosoftExt && (BuiltinInfo.Langs & MS_LANG); |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 70 | bool ObjCUnsupported = !LangOpts.ObjC && BuiltinInfo.Langs == OBJC_LANG; |
Jan Vesely | 31ecb4b | 2017-09-07 19:39:10 +0000 | [diff] [blame] | 71 | bool OclC1Unsupported = (LangOpts.OpenCLVersion / 100) != 1 && |
| 72 | (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES ) == OCLC1X_LANG; |
| 73 | bool OclC2Unsupported = LangOpts.OpenCLVersion != 200 && |
| 74 | (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES) == OCLC20_LANG; |
| 75 | bool OclCUnsupported = !LangOpts.OpenCL && |
| 76 | (BuiltinInfo.Langs & ALL_OCLC_LANGUAGES); |
Jonas Hahnfeld | 23604a8 | 2017-10-17 14:28:14 +0000 | [diff] [blame] | 77 | bool OpenMPUnsupported = !LangOpts.OpenMP && BuiltinInfo.Langs == OMP_LANG; |
Xiuli Pan | bb4d8d3 | 2016-01-26 04:03:48 +0000 | [diff] [blame] | 78 | return !BuiltinsUnsupported && !MathBuiltinsUnsupported && !OclCUnsupported && |
Jonas Hahnfeld | 23604a8 | 2017-10-17 14:28:14 +0000 | [diff] [blame] | 79 | !OclC1Unsupported && !OclC2Unsupported && !OpenMPUnsupported && |
Reid Kleckner | cf8933d | 2013-11-13 22:47:22 +0000 | [diff] [blame] | 80 | !GnuModeUnsupported && !MSModeUnsupported && !ObjCUnsupported; |
Eli Bendersky | aefa5e2 | 2013-07-23 00:13:01 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Ismail Donmez | a8d3a2e | 2015-09-15 09:53:14 +0000 | [diff] [blame] | 83 | /// initializeBuiltins - Mark the identifiers for all the builtins with their |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 84 | /// appropriate builtin ID # and mark any non-portable builtin identifiers as |
| 85 | /// such. |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 86 | void Builtin::Context::initializeBuiltins(IdentifierTable &Table, |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 87 | const LangOptions& LangOpts) { |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 88 | // Step #1: mark all target-independent builtins with their ID's. |
Chris Lattner | 10a5b38 | 2007-01-29 05:24:35 +0000 | [diff] [blame] | 89 | for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 90 | if (builtinIsSupported(BuiltinInfo[i], LangOpts)) { |
Benjamin Kramer | 82598ec | 2013-05-31 16:29:28 +0000 | [diff] [blame] | 91 | Table.get(BuiltinInfo[i].Name).setBuiltinID(i); |
Eli Bendersky | aefa5e2 | 2013-07-23 00:13:01 +0000 | [diff] [blame] | 92 | } |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | f89771c | 2009-04-22 04:56:28 +0000 | [diff] [blame] | 94 | // Step #2: Register target-specific builtins. |
Craig Topper | 6c03a54 | 2015-10-19 04:51:35 +0000 | [diff] [blame] | 95 | for (unsigned i = 0, e = TSRecords.size(); i != e; ++i) |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 96 | if (builtinIsSupported(TSRecords[i], LangOpts)) |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 97 | Table.get(TSRecords[i].Name).setBuiltinID(i + Builtin::FirstTSBuiltin); |
| 98 | |
| 99 | // Step #3: Register target-specific builtins for AuxTarget. |
Craig Topper | 6c03a54 | 2015-10-19 04:51:35 +0000 | [diff] [blame] | 100 | for (unsigned i = 0, e = AuxTSRecords.size(); i != e; ++i) |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 101 | Table.get(AuxTSRecords[i].Name) |
Craig Topper | 6c03a54 | 2015-10-19 04:51:35 +0000 | [diff] [blame] | 102 | .setBuiltinID(i + Builtin::FirstTSBuiltin + TSRecords.size()); |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 105 | void Builtin::Context::forgetBuiltin(unsigned ID, IdentifierTable &Table) { |
| 106 | Table.get(getRecord(ID).Name).setBuiltinID(0); |
Douglas Gregor | 9246b68 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Craig Topper | 74c10e3 | 2018-07-09 19:00:16 +0000 | [diff] [blame] | 109 | unsigned Builtin::Context::getRequiredVectorWidth(unsigned ID) const { |
| 110 | const char *WidthPos = ::strchr(getRecord(ID).Attributes, 'V'); |
| 111 | if (!WidthPos) |
| 112 | return 0; |
| 113 | |
| 114 | ++WidthPos; |
| 115 | assert(*WidthPos == ':' && |
| 116 | "Vector width specifier must be followed by a ':'"); |
| 117 | ++WidthPos; |
| 118 | |
| 119 | char *EndPos; |
| 120 | unsigned Width = ::strtol(WidthPos, &EndPos, 10); |
| 121 | assert(*EndPos == ':' && "Vector width specific must end with a ':'"); |
| 122 | return Width; |
| 123 | } |
| 124 | |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 125 | bool Builtin::Context::isLike(unsigned ID, unsigned &FormatIdx, |
| 126 | bool &HasVAListArg, const char *Fmt) const { |
| 127 | assert(Fmt && "Not passed a format string"); |
| 128 | assert(::strlen(Fmt) == 2 && |
| 129 | "Format string needs to be two characters long"); |
| 130 | assert(::toupper(Fmt[0]) == Fmt[1] && |
| 131 | "Format string is not in the form \"xX\""); |
| 132 | |
Eric Christopher | 02d5d86 | 2015-08-06 01:01:12 +0000 | [diff] [blame] | 133 | const char *Like = ::strpbrk(getRecord(ID).Attributes, Fmt); |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 134 | if (!Like) |
Douglas Gregor | ac5d4c5 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 135 | return false; |
| 136 | |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 137 | HasVAListArg = (*Like == Fmt[1]); |
Douglas Gregor | ac5d4c5 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 138 | |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 139 | ++Like; |
| 140 | assert(*Like == ':' && "Format specifier must be followed by a ':'"); |
| 141 | ++Like; |
Douglas Gregor | ac5d4c5 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 142 | |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 143 | assert(::strchr(Like, ':') && "Format specifier must end with a ':'"); |
Craig Topper | f1186c5 | 2014-05-08 06:41:40 +0000 | [diff] [blame] | 144 | FormatIdx = ::strtol(Like, nullptr, 10); |
Douglas Gregor | ac5d4c5 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 148 | bool Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx, |
| 149 | bool &HasVAListArg) { |
| 150 | return isLike(ID, FormatIdx, HasVAListArg, "pP"); |
Ted Kremenek | 5932c35 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Aaron Ballman | 69714e7 | 2014-01-03 20:10:54 +0000 | [diff] [blame] | 153 | bool Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx, |
| 154 | bool &HasVAListArg) { |
| 155 | return isLike(ID, FormatIdx, HasVAListArg, "sS"); |
| 156 | } |
Erich Keane | 41af971 | 2018-04-16 21:30:08 +0000 | [diff] [blame] | 157 | |
Johannes Doerfert | ac991bb | 2019-01-19 05:36:54 +0000 | [diff] [blame] | 158 | bool Builtin::Context::performsCallback(unsigned ID, |
| 159 | SmallVectorImpl<int> &Encoding) const { |
| 160 | const char *CalleePos = ::strchr(getRecord(ID).Attributes, 'C'); |
| 161 | if (!CalleePos) |
| 162 | return false; |
| 163 | |
| 164 | ++CalleePos; |
| 165 | assert(*CalleePos == '<' && |
| 166 | "Callback callee specifier must be followed by a '<'"); |
| 167 | ++CalleePos; |
| 168 | |
| 169 | char *EndPos; |
| 170 | int CalleeIdx = ::strtol(CalleePos, &EndPos, 10); |
| 171 | assert(CalleeIdx >= 0 && "Callee index is supposed to be positive!"); |
| 172 | Encoding.push_back(CalleeIdx); |
| 173 | |
| 174 | while (*EndPos == ',') { |
| 175 | const char *PayloadPos = EndPos + 1; |
| 176 | |
| 177 | int PayloadIdx = ::strtol(PayloadPos, &EndPos, 10); |
| 178 | Encoding.push_back(PayloadIdx); |
| 179 | } |
| 180 | |
| 181 | assert(*EndPos == '>' && "Callback callee specifier must end with a '>'"); |
| 182 | return true; |
| 183 | } |
| 184 | |
Erich Keane | 41af971 | 2018-04-16 21:30:08 +0000 | [diff] [blame] | 185 | bool Builtin::Context::canBeRedeclared(unsigned ID) const { |
| 186 | return ID == Builtin::NotBuiltin || |
| 187 | ID == Builtin::BI__va_start || |
| 188 | (!hasReferenceArgsOrResult(ID) && |
| 189 | !hasCustomTypechecking(ID)); |
| 190 | } |