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"
15 #include "clang/Basic/Builtins.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/TargetBuiltins.h"
18 #include "llvm/ADT/StringSwitch.h"
19 
20 using namespace clang;
21 using namespace clang::targets;
22 
23 static 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 
33 static constexpr llvm::StringLiteral ValidCPUNames[] = {
34     {"mvp"}, {"bleeding-edge"}, {"generic"}};
35 
36 StringRef WebAssemblyTargetInfo::getABI() const { return ABI; }
37 
38 bool 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 
46 bool 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       .Default(false);
61 }
62 
63 bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
64   return llvm::is_contained(ValidCPUNames, Name);
65 }
66 
67 void WebAssemblyTargetInfo::fillValidCPUList(
68     SmallVectorImpl<StringRef> &Values) const {
69   Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
70 }
71 
72 void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
73                                              MacroBuilder &Builder) const {
74   defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
75   if (SIMDLevel >= SIMD128)
76     Builder.defineMacro("__wasm_simd128__");
77   if (SIMDLevel >= RelaxedSIMD)
78     Builder.defineMacro("__wasm_relaxed_simd__");
79   if (HasNontrappingFPToInt)
80     Builder.defineMacro("__wasm_nontrapping_fptoint__");
81   if (HasSignExt)
82     Builder.defineMacro("__wasm_sign_ext__");
83   if (HasExceptionHandling)
84     Builder.defineMacro("__wasm_exception_handling__");
85   if (HasBulkMemory)
86     Builder.defineMacro("__wasm_bulk_memory__");
87   if (HasAtomics)
88     Builder.defineMacro("__wasm_atomics__");
89   if (HasMutableGlobals)
90     Builder.defineMacro("__wasm_mutable_globals__");
91   if (HasMultivalue)
92     Builder.defineMacro("__wasm_multivalue__");
93   if (HasTailCall)
94     Builder.defineMacro("__wasm_tail_call__");
95   if (HasReferenceTypes)
96     Builder.defineMacro("__wasm_reference_types__");
97   if (HasExtendedConst)
98     Builder.defineMacro("__wasm_extended_const__");
99 
100   Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1");
101   Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2");
102   Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4");
103   Builder.defineMacro("__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8");
104 }
105 
106 void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
107                                          SIMDEnum Level, bool Enabled) {
108   if (Enabled) {
109     switch (Level) {
110     case RelaxedSIMD:
111       Features["relaxed-simd"] = true;
112       [[fallthrough]];
113     case SIMD128:
114       Features["simd128"] = true;
115       [[fallthrough]];
116     case NoSIMD:
117       break;
118     }
119     return;
120   }
121 
122   switch (Level) {
123   case NoSIMD:
124   case SIMD128:
125     Features["simd128"] = false;
126     [[fallthrough]];
127   case RelaxedSIMD:
128     Features["relaxed-simd"] = false;
129     break;
130   }
131 }
132 
133 void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
134                                               StringRef Name,
135                                               bool Enabled) const {
136   if (Name == "simd128")
137     setSIMDLevel(Features, SIMD128, Enabled);
138   else if (Name == "relaxed-simd")
139     setSIMDLevel(Features, RelaxedSIMD, Enabled);
140   else
141     Features[Name] = Enabled;
142 }
143 
144 bool WebAssemblyTargetInfo::initFeatureMap(
145     llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
146     const std::vector<std::string> &FeaturesVec) const {
147   if (CPU == "bleeding-edge") {
148     Features["nontrapping-fptoint"] = true;
149     Features["sign-ext"] = true;
150     Features["bulk-memory"] = true;
151     Features["atomics"] = true;
152     Features["mutable-globals"] = true;
153     Features["tail-call"] = true;
154     Features["reference-types"] = true;
155     setSIMDLevel(Features, SIMD128, true);
156   } else if (CPU == "generic") {
157     Features["sign-ext"] = true;
158     Features["mutable-globals"] = true;
159   }
160 
161   return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
162 }
163 
164 bool WebAssemblyTargetInfo::handleTargetFeatures(
165     std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
166   for (const auto &Feature : Features) {
167     if (Feature == "+simd128") {
168       SIMDLevel = std::max(SIMDLevel, SIMD128);
169       continue;
170     }
171     if (Feature == "-simd128") {
172       SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
173       continue;
174     }
175     if (Feature == "+relaxed-simd") {
176       SIMDLevel = std::max(SIMDLevel, RelaxedSIMD);
177       continue;
178     }
179     if (Feature == "-relaxed-simd") {
180       SIMDLevel = std::min(SIMDLevel, SIMDEnum(RelaxedSIMD - 1));
181       continue;
182     }
183     if (Feature == "+nontrapping-fptoint") {
184       HasNontrappingFPToInt = true;
185       continue;
186     }
187     if (Feature == "-nontrapping-fptoint") {
188       HasNontrappingFPToInt = false;
189       continue;
190     }
191     if (Feature == "+sign-ext") {
192       HasSignExt = true;
193       continue;
194     }
195     if (Feature == "-sign-ext") {
196       HasSignExt = false;
197       continue;
198     }
199     if (Feature == "+exception-handling") {
200       HasExceptionHandling = true;
201       continue;
202     }
203     if (Feature == "-exception-handling") {
204       HasExceptionHandling = false;
205       continue;
206     }
207     if (Feature == "+bulk-memory") {
208       HasBulkMemory = true;
209       continue;
210     }
211     if (Feature == "-bulk-memory") {
212       HasBulkMemory = false;
213       continue;
214     }
215     if (Feature == "+atomics") {
216       HasAtomics = true;
217       continue;
218     }
219     if (Feature == "-atomics") {
220       HasAtomics = false;
221       continue;
222     }
223     if (Feature == "+mutable-globals") {
224       HasMutableGlobals = true;
225       continue;
226     }
227     if (Feature == "-mutable-globals") {
228       HasMutableGlobals = false;
229       continue;
230     }
231     if (Feature == "+multivalue") {
232       HasMultivalue = true;
233       continue;
234     }
235     if (Feature == "-multivalue") {
236       HasMultivalue = false;
237       continue;
238     }
239     if (Feature == "+tail-call") {
240       HasTailCall = true;
241       continue;
242     }
243     if (Feature == "-tail-call") {
244       HasTailCall = false;
245       continue;
246     }
247     if (Feature == "+reference-types") {
248       HasReferenceTypes = true;
249       continue;
250     }
251     if (Feature == "-reference-types") {
252       HasReferenceTypes = false;
253       continue;
254     }
255     if (Feature == "+extended-const") {
256       HasExtendedConst = true;
257       continue;
258     }
259     if (Feature == "-extended-const") {
260       HasExtendedConst = false;
261       continue;
262     }
263 
264     Diags.Report(diag::err_opt_not_valid_with_opt)
265         << Feature << "-target-feature";
266     return false;
267   }
268   return true;
269 }
270 
271 ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
272   return llvm::ArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
273                                          Builtin::FirstTSBuiltin);
274 }
275 
276 void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags,
277                                    LangOptions &Opts) {
278   TargetInfo::adjust(Diags, Opts);
279   // Turn off POSIXThreads and ThreadModel so that we don't predefine _REENTRANT
280   // or __STDCPP_THREADS__ if we will eventually end up stripping atomics
281   // because they are unsupported.
282   if (!HasAtomics || !HasBulkMemory) {
283     Opts.POSIXThreads = false;
284     Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
285     Opts.ThreadsafeStatics = false;
286   }
287 }
288 
289 void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
290                                                MacroBuilder &Builder) const {
291   WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
292   defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
293 }
294 
295 void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
296                                                MacroBuilder &Builder) const {
297   WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
298   defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
299 }
300