blob: 6746768090f564cc63c1fbb6abc500ef748b8d41 [file] [log] [blame]
Erich Keaneebba5922017-07-21 22:37:03 +00001//===--- WebAssembly.cpp - Implement WebAssembly target feature support ---===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Erich Keaneebba5922017-07-21 22:37:03 +00006//
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
20using namespace clang;
21using namespace clang::targets;
22
23const Builtin::Info WebAssemblyTargetInfo::BuiltinInfo[] = {
24#define BUILTIN(ID, TYPE, ATTRS) \
25 {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000026#define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
27 {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
Erich Keaneebba5922017-07-21 22:37:03 +000028#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
29 {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
30#include "clang/Basic/BuiltinsWebAssembly.def"
31};
32
Erich Keanee44bdb32018-02-08 23:16:55 +000033static constexpr llvm::StringLiteral ValidCPUNames[] = {
34 {"mvp"}, {"bleeding-edge"}, {"generic"}};
35
Thomas Lively8c3e6af2020-01-17 18:28:43 -080036StringRef 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
Erich Keaneebba5922017-07-21 22:37:03 +000046bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
47 return llvm::StringSwitch<bool>(Feature)
48 .Case("simd128", SIMDLevel >= SIMD128)
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000049 .Case("unimplemented-simd128", SIMDLevel >= UnimplementedSIMD128)
Dan Gohman0811cd12017-11-28 01:13:45 +000050 .Case("nontrapping-fptoint", HasNontrappingFPToInt)
Dan Gohmand0c4e1e2018-01-19 17:16:32 +000051 .Case("sign-ext", HasSignExt)
Heejin Ahn8b6af222018-03-02 00:39:16 +000052 .Case("exception-handling", HasExceptionHandling)
Thomas Lively88058d42019-01-31 21:02:19 +000053 .Case("bulk-memory", HasBulkMemory)
Heejin Ahnbab85972019-02-06 01:41:26 +000054 .Case("atomics", HasAtomics)
Thomas Lively5f0c4c62019-03-29 22:00:18 +000055 .Case("mutable-globals", HasMutableGlobals)
Thomas Livelyeafe8ef2019-05-23 17:26:47 +000056 .Case("multivalue", HasMultivalue)
57 .Case("tail-call", HasTailCall)
Heejin Ahn764f4082020-01-23 19:22:51 -080058 .Case("reference-types", HasReferenceTypes)
Erich Keaneebba5922017-07-21 22:37:03 +000059 .Default(false);
60}
61
62bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
Erich Keanee44bdb32018-02-08 23:16:55 +000063 return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
64}
65
66void WebAssemblyTargetInfo::fillValidCPUList(
67 SmallVectorImpl<StringRef> &Values) const {
68 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
Erich Keaneebba5922017-07-21 22:37:03 +000069}
70
71void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
72 MacroBuilder &Builder) const {
73 defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
74 if (SIMDLevel >= SIMD128)
75 Builder.defineMacro("__wasm_simd128__");
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000076 if (SIMDLevel >= UnimplementedSIMD128)
77 Builder.defineMacro("__wasm_unimplemented_simd128__");
Thomas Lively88058d42019-01-31 21:02:19 +000078 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__");
Heejin Ahnbab85972019-02-06 01:41:26 +000086 if (HasAtomics)
87 Builder.defineMacro("__wasm_atomics__");
Thomas Lively5f0c4c62019-03-29 22:00:18 +000088 if (HasMutableGlobals)
89 Builder.defineMacro("__wasm_mutable_globals__");
Thomas Livelyeafe8ef2019-05-23 17:26:47 +000090 if (HasMultivalue)
91 Builder.defineMacro("__wasm_multivalue__");
92 if (HasTailCall)
93 Builder.defineMacro("__wasm_tail_call__");
Heejin Ahn764f4082020-01-23 19:22:51 -080094 if (HasReferenceTypes)
95 Builder.defineMacro("__wasm_reference_types__");
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000096}
97
98void 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
112bool 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;
Heejin Ahn65eb1132020-01-23 19:32:32 -0800118 Features["bulk-memory"] = true;
Heejin Ahnbab85972019-02-06 01:41:26 +0000119 Features["atomics"] = true;
Thomas Lively5f0c4c62019-03-29 22:00:18 +0000120 Features["mutable-globals"] = true;
Heejin Ahn65eb1132020-01-23 19:32:32 -0800121 Features["tail-call"] = true;
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +0000122 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;
Thomas Lively88058d42019-01-31 21:02:19 +0000134 if (HasBulkMemory)
135 Features["bulk-memory"] = true;
Heejin Ahnbab85972019-02-06 01:41:26 +0000136 if (HasAtomics)
137 Features["atomics"] = true;
Thomas Lively5f0c4c62019-03-29 22:00:18 +0000138 if (HasMutableGlobals)
139 Features["mutable-globals"] = true;
Thomas Livelyeafe8ef2019-05-23 17:26:47 +0000140 if (HasMultivalue)
141 Features["multivalue"] = true;
142 if (HasTailCall)
143 Features["tail-call"] = true;
Heejin Ahn764f4082020-01-23 19:22:51 -0800144 if (HasReferenceTypes)
145 Features["reference-types"] = true;
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +0000146
147 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
Erich Keaneebba5922017-07-21 22:37:03 +0000148}
149
150bool 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 }
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +0000161 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 }
Dan Gohman0811cd12017-11-28 01:13:45 +0000169 if (Feature == "+nontrapping-fptoint") {
170 HasNontrappingFPToInt = true;
171 continue;
172 }
173 if (Feature == "-nontrapping-fptoint") {
174 HasNontrappingFPToInt = false;
175 continue;
176 }
Dan Gohmand0c4e1e2018-01-19 17:16:32 +0000177 if (Feature == "+sign-ext") {
178 HasSignExt = true;
179 continue;
180 }
181 if (Feature == "-sign-ext") {
182 HasSignExt = false;
183 continue;
184 }
Heejin Ahn8b6af222018-03-02 00:39:16 +0000185 if (Feature == "+exception-handling") {
186 HasExceptionHandling = true;
187 continue;
188 }
189 if (Feature == "-exception-handling") {
190 HasExceptionHandling = false;
191 continue;
192 }
Thomas Lively88058d42019-01-31 21:02:19 +0000193 if (Feature == "+bulk-memory") {
194 HasBulkMemory = true;
195 continue;
196 }
197 if (Feature == "-bulk-memory") {
198 HasBulkMemory = false;
199 continue;
200 }
Heejin Ahnbab85972019-02-06 01:41:26 +0000201 if (Feature == "+atomics") {
202 HasAtomics = true;
203 continue;
204 }
205 if (Feature == "-atomics") {
206 HasAtomics = false;
207 continue;
208 }
Thomas Lively5f0c4c62019-03-29 22:00:18 +0000209 if (Feature == "+mutable-globals") {
210 HasMutableGlobals = true;
211 continue;
212 }
213 if (Feature == "-mutable-globals") {
214 HasMutableGlobals = false;
215 continue;
216 }
Thomas Livelyeafe8ef2019-05-23 17:26:47 +0000217 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 }
Heejin Ahn764f4082020-01-23 19:22:51 -0800233 if (Feature == "+reference-types") {
234 HasReferenceTypes = true;
235 continue;
236 }
237 if (Feature == "-reference-types") {
238 HasReferenceTypes = false;
239 continue;
240 }
Erich Keaneebba5922017-07-21 22:37:03 +0000241
242 Diags.Report(diag::err_opt_not_valid_with_opt)
243 << Feature << "-target-feature";
244 return false;
245 }
246 return true;
247}
248
249ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
250 return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
251 Builtin::FirstTSBuiltin);
252}
253
254void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
255 MacroBuilder &Builder) const {
256 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
257 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
258}
259
260void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
261 MacroBuilder &Builder) const {
262 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
263 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
264}