1 //===--- WebAssembly.cpp - Implement WebAssembly target feature support ---===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements WebAssembly TargetInfo objects.
11 //===----------------------------------------------------------------------===//
13 #include "WebAssembly.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"
20 using namespace clang;
21 using namespace clang::targets;
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"
33 static constexpr llvm::StringLiteral ValidCPUNames[] = {
34 {"mvp"}, {"bleeding-edge"}, {"generic"}};
36 StringRef WebAssemblyTargetInfo::getABI() const { return ABI; }
38 bool WebAssemblyTargetInfo::setABI(const std::string &Name) {
39 if (Name != "mvp" && Name != "experimental-mv")
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)
62 bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
63 return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
66 void WebAssemblyTargetInfo::fillValidCPUList(
67 SmallVectorImpl<StringRef> &Values) const {
68 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
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__");
81 Builder.defineMacro("__wasm_sign_ext__");
82 if (HasExceptionHandling)
83 Builder.defineMacro("__wasm_exception_handling__");
85 Builder.defineMacro("__wasm_bulk_memory__");
87 Builder.defineMacro("__wasm_atomics__");
88 if (HasMutableGlobals)
89 Builder.defineMacro("__wasm_mutable_globals__");
91 Builder.defineMacro("__wasm_multivalue__");
93 Builder.defineMacro("__wasm_tail_call__");
94 if (HasReferenceTypes)
95 Builder.defineMacro("__wasm_reference_types__");
98 void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
99 SIMDEnum Level, bool Enabled) {
102 case UnimplementedSIMD128:
103 Features["unimplemented-simd128"] = true;
106 Features["simd128"] = true;
117 Features["simd128"] = false;
119 case UnimplementedSIMD128:
120 Features["unimplemented-simd128"] = false;
125 void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
127 bool Enabled) const {
128 if (Name == "simd128")
129 setSIMDLevel(Features, SIMD128, Enabled);
130 else if (Name == "unimplemented-simd128")
131 setSIMDLevel(Features, UnimplementedSIMD128, Enabled);
133 Features[Name] = Enabled;
136 bool WebAssemblyTargetInfo::initFeatureMap(
137 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
138 const std::vector<std::string> &FeaturesVec) const {
139 if (CPU == "bleeding-edge") {
140 Features["nontrapping-fptoint"] = true;
141 Features["sign-ext"] = true;
142 Features["bulk-memory"] = true;
143 Features["atomics"] = true;
144 Features["mutable-globals"] = true;
145 Features["tail-call"] = true;
146 setSIMDLevel(Features, SIMD128, true);
149 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
152 bool WebAssemblyTargetInfo::handleTargetFeatures(
153 std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
154 for (const auto &Feature : Features) {
155 if (Feature == "+simd128") {
156 SIMDLevel = std::max(SIMDLevel, SIMD128);
159 if (Feature == "-simd128") {
160 SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
163 if (Feature == "+unimplemented-simd128") {
164 SIMDLevel = std::max(SIMDLevel, SIMDEnum(UnimplementedSIMD128));
167 if (Feature == "-unimplemented-simd128") {
168 SIMDLevel = std::min(SIMDLevel, SIMDEnum(UnimplementedSIMD128 - 1));
171 if (Feature == "+nontrapping-fptoint") {
172 HasNontrappingFPToInt = true;
175 if (Feature == "-nontrapping-fptoint") {
176 HasNontrappingFPToInt = false;
179 if (Feature == "+sign-ext") {
183 if (Feature == "-sign-ext") {
187 if (Feature == "+exception-handling") {
188 HasExceptionHandling = true;
191 if (Feature == "-exception-handling") {
192 HasExceptionHandling = false;
195 if (Feature == "+bulk-memory") {
196 HasBulkMemory = true;
199 if (Feature == "-bulk-memory") {
200 HasBulkMemory = false;
203 if (Feature == "+atomics") {
207 if (Feature == "-atomics") {
211 if (Feature == "+mutable-globals") {
212 HasMutableGlobals = true;
215 if (Feature == "-mutable-globals") {
216 HasMutableGlobals = false;
219 if (Feature == "+multivalue") {
220 HasMultivalue = true;
223 if (Feature == "-multivalue") {
224 HasMultivalue = false;
227 if (Feature == "+tail-call") {
231 if (Feature == "-tail-call") {
235 if (Feature == "+reference-types") {
236 HasReferenceTypes = true;
239 if (Feature == "-reference-types") {
240 HasReferenceTypes = false;
244 Diags.Report(diag::err_opt_not_valid_with_opt)
245 << Feature << "-target-feature";
251 ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
252 return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
253 Builtin::FirstTSBuiltin);
256 void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
257 MacroBuilder &Builder) const {
258 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
259 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
262 void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
263 MacroBuilder &Builder) const {
264 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
265 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);