blob: ca07697f6ac8abdc02fef31000082e7a995c1bb3 [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
Erich Keaneebba5922017-07-21 22:37:03 +000036bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
37 return llvm::StringSwitch<bool>(Feature)
38 .Case("simd128", SIMDLevel >= SIMD128)
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000039 .Case("unimplemented-simd128", SIMDLevel >= UnimplementedSIMD128)
Dan Gohman0811cd12017-11-28 01:13:45 +000040 .Case("nontrapping-fptoint", HasNontrappingFPToInt)
Dan Gohmand0c4e1e2018-01-19 17:16:32 +000041 .Case("sign-ext", HasSignExt)
Heejin Ahn8b6af222018-03-02 00:39:16 +000042 .Case("exception-handling", HasExceptionHandling)
Thomas Lively88058d42019-01-31 21:02:19 +000043 .Case("bulk-memory", HasBulkMemory)
Heejin Ahnbab85972019-02-06 01:41:26 +000044 .Case("atomics", HasAtomics)
Thomas Lively5f0c4c62019-03-29 22:00:18 +000045 .Case("mutable-globals", HasMutableGlobals)
Thomas Livelyeafe8ef2019-05-23 17:26:47 +000046 .Case("multivalue", HasMultivalue)
47 .Case("tail-call", HasTailCall)
Heejin Ahn764f4082020-01-23 19:22:51 -080048 .Case("reference-types", HasReferenceTypes)
Erich Keaneebba5922017-07-21 22:37:03 +000049 .Default(false);
50}
51
52bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
Erich Keanee44bdb32018-02-08 23:16:55 +000053 return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
54}
55
56void WebAssemblyTargetInfo::fillValidCPUList(
57 SmallVectorImpl<StringRef> &Values) const {
58 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
Erich Keaneebba5922017-07-21 22:37:03 +000059}
60
61void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
62 MacroBuilder &Builder) const {
63 defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
64 if (SIMDLevel >= SIMD128)
65 Builder.defineMacro("__wasm_simd128__");
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000066 if (SIMDLevel >= UnimplementedSIMD128)
67 Builder.defineMacro("__wasm_unimplemented_simd128__");
Thomas Lively88058d42019-01-31 21:02:19 +000068 if (HasNontrappingFPToInt)
69 Builder.defineMacro("__wasm_nontrapping_fptoint__");
70 if (HasSignExt)
71 Builder.defineMacro("__wasm_sign_ext__");
72 if (HasExceptionHandling)
73 Builder.defineMacro("__wasm_exception_handling__");
74 if (HasBulkMemory)
75 Builder.defineMacro("__wasm_bulk_memory__");
Heejin Ahnbab85972019-02-06 01:41:26 +000076 if (HasAtomics)
77 Builder.defineMacro("__wasm_atomics__");
Thomas Lively5f0c4c62019-03-29 22:00:18 +000078 if (HasMutableGlobals)
79 Builder.defineMacro("__wasm_mutable_globals__");
Thomas Livelyeafe8ef2019-05-23 17:26:47 +000080 if (HasMultivalue)
81 Builder.defineMacro("__wasm_multivalue__");
82 if (HasTailCall)
83 Builder.defineMacro("__wasm_tail_call__");
Heejin Ahn764f4082020-01-23 19:22:51 -080084 if (HasReferenceTypes)
85 Builder.defineMacro("__wasm_reference_types__");
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000086}
87
88void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
89 SIMDEnum Level) {
90 switch (Level) {
91 case UnimplementedSIMD128:
92 Features["unimplemented-simd128"] = true;
93 LLVM_FALLTHROUGH;
94 case SIMD128:
95 Features["simd128"] = true;
96 LLVM_FALLTHROUGH;
97 case NoSIMD:
98 break;
99 }
100}
101
102bool WebAssemblyTargetInfo::initFeatureMap(
103 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
104 const std::vector<std::string> &FeaturesVec) const {
105 if (CPU == "bleeding-edge") {
106 Features["nontrapping-fptoint"] = true;
107 Features["sign-ext"] = true;
Heejin Ahnbab85972019-02-06 01:41:26 +0000108 Features["atomics"] = true;
Thomas Lively5f0c4c62019-03-29 22:00:18 +0000109 Features["mutable-globals"] = true;
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +0000110 setSIMDLevel(Features, SIMD128);
111 }
112 // Other targets do not consider user-configured features here, but while we
113 // are actively developing new features it is useful to let user-configured
114 // features control availability of builtins
115 setSIMDLevel(Features, SIMDLevel);
116 if (HasNontrappingFPToInt)
117 Features["nontrapping-fptoint"] = true;
118 if (HasSignExt)
119 Features["sign-ext"] = true;
120 if (HasExceptionHandling)
121 Features["exception-handling"] = true;
Thomas Lively88058d42019-01-31 21:02:19 +0000122 if (HasBulkMemory)
123 Features["bulk-memory"] = true;
Heejin Ahnbab85972019-02-06 01:41:26 +0000124 if (HasAtomics)
125 Features["atomics"] = true;
Thomas Lively5f0c4c62019-03-29 22:00:18 +0000126 if (HasMutableGlobals)
127 Features["mutable-globals"] = true;
Thomas Livelyeafe8ef2019-05-23 17:26:47 +0000128 if (HasMultivalue)
129 Features["multivalue"] = true;
130 if (HasTailCall)
131 Features["tail-call"] = true;
Heejin Ahn764f4082020-01-23 19:22:51 -0800132 if (HasReferenceTypes)
133 Features["reference-types"] = true;
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +0000134
135 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
Erich Keaneebba5922017-07-21 22:37:03 +0000136}
137
138bool WebAssemblyTargetInfo::handleTargetFeatures(
139 std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
140 for (const auto &Feature : Features) {
141 if (Feature == "+simd128") {
142 SIMDLevel = std::max(SIMDLevel, SIMD128);
143 continue;
144 }
145 if (Feature == "-simd128") {
146 SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
147 continue;
148 }
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +0000149 if (Feature == "+unimplemented-simd128") {
150 SIMDLevel = std::max(SIMDLevel, SIMDEnum(UnimplementedSIMD128));
151 continue;
152 }
153 if (Feature == "-unimplemented-simd128") {
154 SIMDLevel = std::min(SIMDLevel, SIMDEnum(UnimplementedSIMD128 - 1));
155 continue;
156 }
Dan Gohman0811cd12017-11-28 01:13:45 +0000157 if (Feature == "+nontrapping-fptoint") {
158 HasNontrappingFPToInt = true;
159 continue;
160 }
161 if (Feature == "-nontrapping-fptoint") {
162 HasNontrappingFPToInt = false;
163 continue;
164 }
Dan Gohmand0c4e1e2018-01-19 17:16:32 +0000165 if (Feature == "+sign-ext") {
166 HasSignExt = true;
167 continue;
168 }
169 if (Feature == "-sign-ext") {
170 HasSignExt = false;
171 continue;
172 }
Heejin Ahn8b6af222018-03-02 00:39:16 +0000173 if (Feature == "+exception-handling") {
174 HasExceptionHandling = true;
175 continue;
176 }
177 if (Feature == "-exception-handling") {
178 HasExceptionHandling = false;
179 continue;
180 }
Thomas Lively88058d42019-01-31 21:02:19 +0000181 if (Feature == "+bulk-memory") {
182 HasBulkMemory = true;
183 continue;
184 }
185 if (Feature == "-bulk-memory") {
186 HasBulkMemory = false;
187 continue;
188 }
Heejin Ahnbab85972019-02-06 01:41:26 +0000189 if (Feature == "+atomics") {
190 HasAtomics = true;
191 continue;
192 }
193 if (Feature == "-atomics") {
194 HasAtomics = false;
195 continue;
196 }
Thomas Lively5f0c4c62019-03-29 22:00:18 +0000197 if (Feature == "+mutable-globals") {
198 HasMutableGlobals = true;
199 continue;
200 }
201 if (Feature == "-mutable-globals") {
202 HasMutableGlobals = false;
203 continue;
204 }
Thomas Livelyeafe8ef2019-05-23 17:26:47 +0000205 if (Feature == "+multivalue") {
206 HasMultivalue = true;
207 continue;
208 }
209 if (Feature == "-multivalue") {
210 HasMultivalue = false;
211 continue;
212 }
213 if (Feature == "+tail-call") {
214 HasTailCall = true;
215 continue;
216 }
217 if (Feature == "-tail-call") {
218 HasTailCall = false;
219 continue;
220 }
Heejin Ahn764f4082020-01-23 19:22:51 -0800221 if (Feature == "+reference-types") {
222 HasReferenceTypes = true;
223 continue;
224 }
225 if (Feature == "-reference-types") {
226 HasReferenceTypes = false;
227 continue;
228 }
Erich Keaneebba5922017-07-21 22:37:03 +0000229
230 Diags.Report(diag::err_opt_not_valid_with_opt)
231 << Feature << "-target-feature";
232 return false;
233 }
234 return true;
235}
236
237ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
238 return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
239 Builtin::FirstTSBuiltin);
240}
241
242void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
243 MacroBuilder &Builder) const {
244 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
245 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
246}
247
248void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
249 MacroBuilder &Builder) const {
250 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
251 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
252}