clang 19.0.0git
WebAssembly.cpp
Go to the documentation of this file.
1//===--- WebAssembly.cpp - Implement WebAssembly target feature support ---===//
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 WebAssembly TargetInfo objects.
10//
11//===----------------------------------------------------------------------===//
12
13#include "WebAssembly.h"
14#include "Targets.h"
18#include "llvm/ADT/StringSwitch.h"
19
20using namespace clang;
21using namespace clang::targets;
22
23static constexpr Builtin::Info BuiltinInfo[] = {
24#define BUILTIN(ID, TYPE, ATTRS) \
25 {#ID, TYPE, ATTRS, nullptr, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
26#define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
27 {#ID, TYPE, ATTRS, FEATURE, HeaderDesc::NO_HEADER, ALL_LANGUAGES},
28#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
29 {#ID, TYPE, ATTRS, nullptr, HeaderDesc::HEADER, ALL_LANGUAGES},
30#include "clang/Basic/BuiltinsWebAssembly.def"
31};
32
33static constexpr llvm::StringLiteral ValidCPUNames[] = {
34 {"mvp"}, {"bleeding-edge"}, {"generic"}};
35
36StringRef WebAssemblyTargetInfo::getABI() const { return ABI; }
37
38bool WebAssemblyTargetInfo::setABI(const std::string &Name) {
39 if (Name != "mvp" && Name != "experimental-mv")
40 return false;
41
42 ABI = Name;
43 return true;
44}
45
46bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
47 return llvm::StringSwitch<bool>(Feature)
48 .Case("simd128", SIMDLevel >= SIMD128)
49 .Case("relaxed-simd", SIMDLevel >= RelaxedSIMD)
50 .Case("nontrapping-fptoint", HasNontrappingFPToInt)
51 .Case("sign-ext", HasSignExt)
52 .Case("exception-handling", HasExceptionHandling)
53 .Case("bulk-memory", HasBulkMemory)
54 .Case("atomics", HasAtomics)
55 .Case("mutable-globals", HasMutableGlobals)
56 .Case("multivalue", HasMultivalue)
57 .Case("tail-call", HasTailCall)
58 .Case("reference-types", HasReferenceTypes)
59 .Case("extended-const", HasExtendedConst)
60 .Case("multimemory", HasMultiMemory)
61 .Default(false);
62}
63
64bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
65 return llvm::is_contained(ValidCPUNames, Name);
66}
67
68void WebAssemblyTargetInfo::fillValidCPUList(
69 SmallVectorImpl<StringRef> &Values) const {
70 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
71}
72
74 MacroBuilder &Builder) const {
75 defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
76 if (SIMDLevel >= SIMD128)
77 Builder.defineMacro("__wasm_simd128__");
78 if (SIMDLevel >= RelaxedSIMD)
79 Builder.defineMacro("__wasm_relaxed_simd__");
80 if (HasNontrappingFPToInt)
81 Builder.defineMacro("__wasm_nontrapping_fptoint__");
82 if (HasSignExt)
83 Builder.defineMacro("__wasm_sign_ext__");
84 if (HasExceptionHandling)
85 Builder.defineMacro("__wasm_exception_handling__");
86 if (HasBulkMemory)
87 Builder.defineMacro("__wasm_bulk_memory__");
88 if (HasAtomics)
89 Builder.defineMacro("__wasm_atomics__");
90 if (HasMutableGlobals)
91 Builder.defineMacro("__wasm_mutable_globals__");
92 if (HasMultivalue)
93 Builder.defineMacro("__wasm_multivalue__");
94 if (HasTailCall)
95 Builder.defineMacro("__wasm_tail_call__");
96 if (HasReferenceTypes)
97 Builder.defineMacro("__wasm_reference_types__");
98 if (HasExtendedConst)
99 Builder.defineMacro("__wasm_extended_const__");
100 if (HasMultiMemory)
101 Builder.defineMacro("__wasm_multimemory__");
102
103 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
104 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
105 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
106 Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
107}
108
109void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
110 SIMDEnum Level, bool Enabled) {
111 if (Enabled) {
112 switch (Level) {
113 case RelaxedSIMD:
114 Features["relaxed-simd"] = true;
115 [[fallthrough]];
116 case SIMD128:
117 Features["simd128"] = true;
118 [[fallthrough]];
119 case NoSIMD:
120 break;
121 }
122 return;
123 }
124
125 switch (Level) {
126 case NoSIMD:
127 case SIMD128:
128 Features["simd128"] = false;
129 [[fallthrough]];
130 case RelaxedSIMD:
131 Features["relaxed-simd"] = false;
132 break;
133 }
134}
135
136void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
137 StringRef Name,
138 bool Enabled) const {
139 if (Name == "simd128")
140 setSIMDLevel(Features, SIMD128, Enabled);
141 else if (Name == "relaxed-simd")
142 setSIMDLevel(Features, RelaxedSIMD, Enabled);
143 else
144 Features[Name] = Enabled;
145}
146
147bool WebAssemblyTargetInfo::initFeatureMap(
148 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
149 const std::vector<std::string> &FeaturesVec) const {
150 if (CPU == "bleeding-edge") {
151 Features["nontrapping-fptoint"] = true;
152 Features["sign-ext"] = true;
153 Features["bulk-memory"] = true;
154 Features["atomics"] = true;
155 Features["mutable-globals"] = true;
156 Features["tail-call"] = true;
157 Features["reference-types"] = true;
158 Features["multimemory"] = true;
159 setSIMDLevel(Features, SIMD128, true);
160 } else if (CPU == "generic") {
161 Features["sign-ext"] = true;
162 Features["mutable-globals"] = true;
163 }
164
165 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
166}
167
168bool WebAssemblyTargetInfo::handleTargetFeatures(
169 std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
170 for (const auto &Feature : Features) {
171 if (Feature == "+simd128") {
172 SIMDLevel = std::max(SIMDLevel, SIMD128);
173 continue;
174 }
175 if (Feature == "-simd128") {
176 SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
177 continue;
178 }
179 if (Feature == "+relaxed-simd") {
180 SIMDLevel = std::max(SIMDLevel, RelaxedSIMD);
181 continue;
182 }
183 if (Feature == "-relaxed-simd") {
184 SIMDLevel = std::min(SIMDLevel, SIMDEnum(RelaxedSIMD - 1));
185 continue;
186 }
187 if (Feature == "+nontrapping-fptoint") {
188 HasNontrappingFPToInt = true;
189 continue;
190 }
191 if (Feature == "-nontrapping-fptoint") {
192 HasNontrappingFPToInt = false;
193 continue;
194 }
195 if (Feature == "+sign-ext") {
196 HasSignExt = true;
197 continue;
198 }
199 if (Feature == "-sign-ext") {
200 HasSignExt = false;
201 continue;
202 }
203 if (Feature == "+exception-handling") {
204 HasExceptionHandling = true;
205 continue;
206 }
207 if (Feature == "-exception-handling") {
208 HasExceptionHandling = false;
209 continue;
210 }
211 if (Feature == "+bulk-memory") {
212 HasBulkMemory = true;
213 continue;
214 }
215 if (Feature == "-bulk-memory") {
216 HasBulkMemory = false;
217 continue;
218 }
219 if (Feature == "+atomics") {
220 HasAtomics = true;
221 continue;
222 }
223 if (Feature == "-atomics") {
224 HasAtomics = false;
225 continue;
226 }
227 if (Feature == "+mutable-globals") {
228 HasMutableGlobals = true;
229 continue;
230 }
231 if (Feature == "-mutable-globals") {
232 HasMutableGlobals = false;
233 continue;
234 }
235 if (Feature == "+multivalue") {
236 HasMultivalue = true;
237 continue;
238 }
239 if (Feature == "-multivalue") {
240 HasMultivalue = false;
241 continue;
242 }
243 if (Feature == "+tail-call") {
244 HasTailCall = true;
245 continue;
246 }
247 if (Feature == "-tail-call") {
248 HasTailCall = false;
249 continue;
250 }
251 if (Feature == "+reference-types") {
252 HasReferenceTypes = true;
253 continue;
254 }
255 if (Feature == "-reference-types") {
256 HasReferenceTypes = false;
257 continue;
258 }
259 if (Feature == "+extended-const") {
260 HasExtendedConst = true;
261 continue;
262 }
263 if (Feature == "-extended-const") {
264 HasExtendedConst = false;
265 continue;
266 }
267 if (Feature == "+multimemory") {
268 HasMultiMemory = true;
269 continue;
270 }
271 if (Feature == "-multimemory") {
272 HasMultiMemory = false;
273 continue;
274 }
275
276 Diags.Report(diag::err_opt_not_valid_with_opt)
277 << Feature << "-target-feature";
278 return false;
279 }
280 return true;
281}
282
283ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
286}
287
288void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags,
289 LangOptions &Opts) {
290 TargetInfo::adjust(Diags, Opts);
291 // Turn off POSIXThreads and ThreadModel so that we don't predefine _REENTRANT
292 // or __STDCPP_THREADS__ if we will eventually end up stripping atomics
293 // because they are unsupported.
294 if (!HasAtomics || !HasBulkMemory) {
295 Opts.POSIXThreads = false;
296 Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
297 Opts.ThreadsafeStatics = false;
298 }
299}
300
302 MacroBuilder &Builder) const {
304 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
305}
306
308 MacroBuilder &Builder) const {
310 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
311}
Defines the Diagnostic-related interfaces.
static constexpr llvm::StringLiteral ValidCPUNames[]
Definition: BPF.cpp:69
static constexpr Builtin::Info BuiltinInfo[]
Definition: WebAssembly.cpp:23
static constexpr llvm::StringLiteral ValidCPUNames[]
Definition: WebAssembly.cpp:33
static constexpr Builtin::Info BuiltinInfo[]
Definition: Builtins.cpp:32
Defines enum values for all the target-independent builtin functions.
Enumerates target-specific builtins in their own namespaces within namespace clang.
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1547
@ Single
Single Threaded Environment.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:449
virtual void adjust(DiagnosticsEngine &Diags, LangOptions &Opts)
Set forced language options.
Definition: TargetInfo.cpp:392
virtual bool initFeatureMap(llvm::StringMap< bool > &Features, DiagnosticsEngine &Diags, StringRef CPU, const std::vector< std::string > &FeatureVec) const
Initialize the map with the default set of target features for the CPU this should include all legal ...
Definition: TargetInfo.cpp:522
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition: WebAssembly.cpp:73
StringRef getABI() const override
Get the ABI currently in use.
Definition: WebAssembly.cpp:36
bool setABI(const std::string &Name) override
Use the specified ABI.
Definition: WebAssembly.cpp:38
void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning)
Definition: Targets.cpp:76
The JSON file list parser is used to communicate input to InstallAPI.