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
getABI() const36 StringRef WebAssemblyTargetInfo::getABI() const { return ABI; }
37
setABI(const std::string & Name)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
hasFeature(StringRef Feature) const46 bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
47 return llvm::StringSwitch<bool>(Feature)
48 .Case("simd128", SIMDLevel >= SIMD128)
49 .Case("nontrapping-fptoint", HasNontrappingFPToInt)
50 .Case("sign-ext", HasSignExt)
51 .Case("exception-handling", HasExceptionHandling)
52 .Case("bulk-memory", HasBulkMemory)
53 .Case("atomics", HasAtomics)
54 .Case("mutable-globals", HasMutableGlobals)
55 .Case("multivalue", HasMultivalue)
56 .Case("tail-call", HasTailCall)
57 .Case("reference-types", HasReferenceTypes)
58 .Default(false);
59 }
60
isValidCPUName(StringRef Name) const61 bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
62 return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
63 }
64
fillValidCPUList(SmallVectorImpl<StringRef> & Values) const65 void WebAssemblyTargetInfo::fillValidCPUList(
66 SmallVectorImpl<StringRef> &Values) const {
67 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
68 }
69
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const70 void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
71 MacroBuilder &Builder) const {
72 defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
73 if (SIMDLevel >= SIMD128)
74 Builder.defineMacro("__wasm_simd128__");
75 if (HasNontrappingFPToInt)
76 Builder.defineMacro("__wasm_nontrapping_fptoint__");
77 if (HasSignExt)
78 Builder.defineMacro("__wasm_sign_ext__");
79 if (HasExceptionHandling)
80 Builder.defineMacro("__wasm_exception_handling__");
81 if (HasBulkMemory)
82 Builder.defineMacro("__wasm_bulk_memory__");
83 if (HasAtomics)
84 Builder.defineMacro("__wasm_atomics__");
85 if (HasMutableGlobals)
86 Builder.defineMacro("__wasm_mutable_globals__");
87 if (HasMultivalue)
88 Builder.defineMacro("__wasm_multivalue__");
89 if (HasTailCall)
90 Builder.defineMacro("__wasm_tail_call__");
91 if (HasReferenceTypes)
92 Builder.defineMacro("__wasm_reference_types__");
93 }
94
setSIMDLevel(llvm::StringMap<bool> & Features,SIMDEnum Level,bool Enabled)95 void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
96 SIMDEnum Level, bool Enabled) {
97 if (Enabled) {
98 switch (Level) {
99 case SIMD128:
100 Features["simd128"] = true;
101 LLVM_FALLTHROUGH;
102 case NoSIMD:
103 break;
104 }
105 return;
106 }
107
108 switch (Level) {
109 case NoSIMD:
110 case SIMD128:
111 Features["simd128"] = false;
112 break;
113 }
114 }
115
setFeatureEnabled(llvm::StringMap<bool> & Features,StringRef Name,bool Enabled) const116 void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
117 StringRef Name,
118 bool Enabled) const {
119 if (Name == "simd128")
120 setSIMDLevel(Features, SIMD128, Enabled);
121 else
122 Features[Name] = Enabled;
123 }
124
initFeatureMap(llvm::StringMap<bool> & Features,DiagnosticsEngine & Diags,StringRef CPU,const std::vector<std::string> & FeaturesVec) const125 bool WebAssemblyTargetInfo::initFeatureMap(
126 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
127 const std::vector<std::string> &FeaturesVec) const {
128 if (CPU == "bleeding-edge") {
129 Features["nontrapping-fptoint"] = true;
130 Features["sign-ext"] = true;
131 Features["bulk-memory"] = true;
132 Features["atomics"] = true;
133 Features["mutable-globals"] = true;
134 Features["tail-call"] = true;
135 setSIMDLevel(Features, SIMD128, true);
136 }
137
138 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
139 }
140
handleTargetFeatures(std::vector<std::string> & Features,DiagnosticsEngine & Diags)141 bool WebAssemblyTargetInfo::handleTargetFeatures(
142 std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
143 for (const auto &Feature : Features) {
144 if (Feature == "+simd128") {
145 SIMDLevel = std::max(SIMDLevel, SIMD128);
146 continue;
147 }
148 if (Feature == "-simd128") {
149 SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
150 continue;
151 }
152 if (Feature == "+nontrapping-fptoint") {
153 HasNontrappingFPToInt = true;
154 continue;
155 }
156 if (Feature == "-nontrapping-fptoint") {
157 HasNontrappingFPToInt = false;
158 continue;
159 }
160 if (Feature == "+sign-ext") {
161 HasSignExt = true;
162 continue;
163 }
164 if (Feature == "-sign-ext") {
165 HasSignExt = false;
166 continue;
167 }
168 if (Feature == "+exception-handling") {
169 HasExceptionHandling = true;
170 continue;
171 }
172 if (Feature == "-exception-handling") {
173 HasExceptionHandling = false;
174 continue;
175 }
176 if (Feature == "+bulk-memory") {
177 HasBulkMemory = true;
178 continue;
179 }
180 if (Feature == "-bulk-memory") {
181 HasBulkMemory = false;
182 continue;
183 }
184 if (Feature == "+atomics") {
185 HasAtomics = true;
186 continue;
187 }
188 if (Feature == "-atomics") {
189 HasAtomics = false;
190 continue;
191 }
192 if (Feature == "+mutable-globals") {
193 HasMutableGlobals = true;
194 continue;
195 }
196 if (Feature == "-mutable-globals") {
197 HasMutableGlobals = false;
198 continue;
199 }
200 if (Feature == "+multivalue") {
201 HasMultivalue = true;
202 continue;
203 }
204 if (Feature == "-multivalue") {
205 HasMultivalue = false;
206 continue;
207 }
208 if (Feature == "+tail-call") {
209 HasTailCall = true;
210 continue;
211 }
212 if (Feature == "-tail-call") {
213 HasTailCall = false;
214 continue;
215 }
216 if (Feature == "+reference-types") {
217 HasReferenceTypes = true;
218 continue;
219 }
220 if (Feature == "-reference-types") {
221 HasReferenceTypes = false;
222 continue;
223 }
224
225 Diags.Report(diag::err_opt_not_valid_with_opt)
226 << Feature << "-target-feature";
227 return false;
228 }
229 return true;
230 }
231
getTargetBuiltins() const232 ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
233 return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
234 Builtin::FirstTSBuiltin);
235 }
236
adjust(DiagnosticsEngine & Diags,LangOptions & Opts)237 void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags,
238 LangOptions &Opts) {
239 // If the Atomics feature isn't available, turn off POSIXThreads and
240 // ThreadModel, so that we don't predefine _REENTRANT or __STDCPP_THREADS__.
241 if (!HasAtomics) {
242 Opts.POSIXThreads = false;
243 Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
244 }
245 }
246
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const247 void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
248 MacroBuilder &Builder) const {
249 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
250 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
251 }
252
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const253 void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
254 MacroBuilder &Builder) const {
255 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
256 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
257 }
258