xref: /freebsd/contrib/llvm-project/clang/lib/Basic/Targets/WebAssembly.cpp (revision da477bcdc0c335171bb0ed3813f570026de6df85)
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 const Builtin::Info WebAssemblyTargetInfo::BuiltinInfo[] = {
24 #define BUILTIN(ID, TYPE, ATTRS)                                               \
25   {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
26 #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE)                               \
27   {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
28 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER)                                    \
29   {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
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("unimplemented-simd128", SIMDLevel >= UnimplementedSIMD128)
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       .Default(false);
60 }
61 
62 bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
63   return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
64 }
65 
66 void WebAssemblyTargetInfo::fillValidCPUList(
67     SmallVectorImpl<StringRef> &Values) const {
68   Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
69 }
70 
71 void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
72                                              MacroBuilder &Builder) const {
73   defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
74   if (SIMDLevel >= SIMD128)
75     Builder.defineMacro("__wasm_simd128__");
76   if (SIMDLevel >= UnimplementedSIMD128)
77     Builder.defineMacro("__wasm_unimplemented_simd128__");
78   if (HasNontrappingFPToInt)
79     Builder.defineMacro("__wasm_nontrapping_fptoint__");
80   if (HasSignExt)
81     Builder.defineMacro("__wasm_sign_ext__");
82   if (HasExceptionHandling)
83     Builder.defineMacro("__wasm_exception_handling__");
84   if (HasBulkMemory)
85     Builder.defineMacro("__wasm_bulk_memory__");
86   if (HasAtomics)
87     Builder.defineMacro("__wasm_atomics__");
88   if (HasMutableGlobals)
89     Builder.defineMacro("__wasm_mutable_globals__");
90   if (HasMultivalue)
91     Builder.defineMacro("__wasm_multivalue__");
92   if (HasTailCall)
93     Builder.defineMacro("__wasm_tail_call__");
94   if (HasReferenceTypes)
95     Builder.defineMacro("__wasm_reference_types__");
96 }
97 
98 void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
99                                          SIMDEnum Level) {
100   switch (Level) {
101   case UnimplementedSIMD128:
102     Features["unimplemented-simd128"] = true;
103     LLVM_FALLTHROUGH;
104   case SIMD128:
105     Features["simd128"] = true;
106     LLVM_FALLTHROUGH;
107   case NoSIMD:
108     break;
109   }
110 }
111 
112 bool WebAssemblyTargetInfo::initFeatureMap(
113     llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
114     const std::vector<std::string> &FeaturesVec) const {
115   if (CPU == "bleeding-edge") {
116     Features["nontrapping-fptoint"] = true;
117     Features["sign-ext"] = true;
118     Features["bulk-memory"] = true;
119     Features["atomics"] = true;
120     Features["mutable-globals"] = true;
121     Features["tail-call"] = true;
122     setSIMDLevel(Features, SIMD128);
123   }
124   // Other targets do not consider user-configured features here, but while we
125   // are actively developing new features it is useful to let user-configured
126   // features control availability of builtins
127   setSIMDLevel(Features, SIMDLevel);
128   if (HasNontrappingFPToInt)
129     Features["nontrapping-fptoint"] = true;
130   if (HasSignExt)
131     Features["sign-ext"] = true;
132   if (HasExceptionHandling)
133     Features["exception-handling"] = true;
134   if (HasBulkMemory)
135     Features["bulk-memory"] = true;
136   if (HasAtomics)
137     Features["atomics"] = true;
138   if (HasMutableGlobals)
139     Features["mutable-globals"] = true;
140   if (HasMultivalue)
141     Features["multivalue"] = true;
142   if (HasTailCall)
143     Features["tail-call"] = true;
144   if (HasReferenceTypes)
145     Features["reference-types"] = true;
146 
147   return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
148 }
149 
150 bool WebAssemblyTargetInfo::handleTargetFeatures(
151     std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
152   for (const auto &Feature : Features) {
153     if (Feature == "+simd128") {
154       SIMDLevel = std::max(SIMDLevel, SIMD128);
155       continue;
156     }
157     if (Feature == "-simd128") {
158       SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
159       continue;
160     }
161     if (Feature == "+unimplemented-simd128") {
162       SIMDLevel = std::max(SIMDLevel, SIMDEnum(UnimplementedSIMD128));
163       continue;
164     }
165     if (Feature == "-unimplemented-simd128") {
166       SIMDLevel = std::min(SIMDLevel, SIMDEnum(UnimplementedSIMD128 - 1));
167       continue;
168     }
169     if (Feature == "+nontrapping-fptoint") {
170       HasNontrappingFPToInt = true;
171       continue;
172     }
173     if (Feature == "-nontrapping-fptoint") {
174       HasNontrappingFPToInt = false;
175       continue;
176     }
177     if (Feature == "+sign-ext") {
178       HasSignExt = true;
179       continue;
180     }
181     if (Feature == "-sign-ext") {
182       HasSignExt = false;
183       continue;
184     }
185     if (Feature == "+exception-handling") {
186       HasExceptionHandling = true;
187       continue;
188     }
189     if (Feature == "-exception-handling") {
190       HasExceptionHandling = false;
191       continue;
192     }
193     if (Feature == "+bulk-memory") {
194       HasBulkMemory = true;
195       continue;
196     }
197     if (Feature == "-bulk-memory") {
198       HasBulkMemory = false;
199       continue;
200     }
201     if (Feature == "+atomics") {
202       HasAtomics = true;
203       continue;
204     }
205     if (Feature == "-atomics") {
206       HasAtomics = false;
207       continue;
208     }
209     if (Feature == "+mutable-globals") {
210       HasMutableGlobals = true;
211       continue;
212     }
213     if (Feature == "-mutable-globals") {
214       HasMutableGlobals = false;
215       continue;
216     }
217     if (Feature == "+multivalue") {
218       HasMultivalue = true;
219       continue;
220     }
221     if (Feature == "-multivalue") {
222       HasMultivalue = false;
223       continue;
224     }
225     if (Feature == "+tail-call") {
226       HasTailCall = true;
227       continue;
228     }
229     if (Feature == "-tail-call") {
230       HasTailCall = false;
231       continue;
232     }
233     if (Feature == "+reference-types") {
234       HasReferenceTypes = true;
235       continue;
236     }
237     if (Feature == "-reference-types") {
238       HasReferenceTypes = false;
239       continue;
240     }
241 
242     Diags.Report(diag::err_opt_not_valid_with_opt)
243         << Feature << "-target-feature";
244     return false;
245   }
246   return true;
247 }
248 
249 ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
250   return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
251                                              Builtin::FirstTSBuiltin);
252 }
253 
254 void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
255                                                MacroBuilder &Builder) const {
256   WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
257   defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
258 }
259 
260 void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
261                                                MacroBuilder &Builder) const {
262   WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
263   defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
264 }
265