1 //===--- WebAssembly.cpp - Implement WebAssembly target feature support ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements WebAssembly TargetInfo objects.
12 //===----------------------------------------------------------------------===//
14 #include "WebAssembly.h"
16 #include "clang/Basic/Builtins.h"
17 #include "clang/Basic/Diagnostic.h"
18 #include "clang/Basic/TargetBuiltins.h"
19 #include "llvm/ADT/StringSwitch.h"
21 using namespace clang;
22 using namespace clang::targets;
24 const Builtin::Info WebAssemblyTargetInfo::BuiltinInfo[] = {
25 #define BUILTIN(ID, TYPE, ATTRS) \
26 {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
27 #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
28 {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
29 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
30 {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
31 #include "clang/Basic/BuiltinsWebAssembly.def"
34 static constexpr llvm::StringLiteral ValidCPUNames[] = {
35 {"mvp"}, {"bleeding-edge"}, {"generic"}};
37 bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
38 return llvm::StringSwitch<bool>(Feature)
39 .Case("simd128", SIMDLevel >= SIMD128)
40 .Case("unimplemented-simd128", SIMDLevel >= UnimplementedSIMD128)
41 .Case("nontrapping-fptoint", HasNontrappingFPToInt)
42 .Case("sign-ext", HasSignExt)
43 .Case("exception-handling", HasExceptionHandling)
47 bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
48 return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
51 void WebAssemblyTargetInfo::fillValidCPUList(
52 SmallVectorImpl<StringRef> &Values) const {
53 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
56 void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
57 MacroBuilder &Builder) const {
58 defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
59 if (SIMDLevel >= SIMD128)
60 Builder.defineMacro("__wasm_simd128__");
61 if (SIMDLevel >= UnimplementedSIMD128)
62 Builder.defineMacro("__wasm_unimplemented_simd128__");
65 void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
68 case UnimplementedSIMD128:
69 Features["unimplemented-simd128"] = true;
72 Features["simd128"] = true;
79 bool WebAssemblyTargetInfo::initFeatureMap(
80 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
81 const std::vector<std::string> &FeaturesVec) const {
82 if (CPU == "bleeding-edge") {
83 Features["nontrapping-fptoint"] = true;
84 Features["sign-ext"] = true;
85 setSIMDLevel(Features, SIMD128);
87 // Other targets do not consider user-configured features here, but while we
88 // are actively developing new features it is useful to let user-configured
89 // features control availability of builtins
90 setSIMDLevel(Features, SIMDLevel);
91 if (HasNontrappingFPToInt)
92 Features["nontrapping-fptoint"] = true;
94 Features["sign-ext"] = true;
95 if (HasExceptionHandling)
96 Features["exception-handling"] = true;
98 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
101 bool WebAssemblyTargetInfo::handleTargetFeatures(
102 std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
103 for (const auto &Feature : Features) {
104 if (Feature == "+simd128") {
105 SIMDLevel = std::max(SIMDLevel, SIMD128);
108 if (Feature == "-simd128") {
109 SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
112 if (Feature == "+unimplemented-simd128") {
113 SIMDLevel = std::max(SIMDLevel, SIMDEnum(UnimplementedSIMD128));
116 if (Feature == "-unimplemented-simd128") {
117 SIMDLevel = std::min(SIMDLevel, SIMDEnum(UnimplementedSIMD128 - 1));
120 if (Feature == "+nontrapping-fptoint") {
121 HasNontrappingFPToInt = true;
124 if (Feature == "-nontrapping-fptoint") {
125 HasNontrappingFPToInt = false;
128 if (Feature == "+sign-ext") {
132 if (Feature == "-sign-ext") {
136 if (Feature == "+exception-handling") {
137 HasExceptionHandling = true;
140 if (Feature == "-exception-handling") {
141 HasExceptionHandling = false;
145 Diags.Report(diag::err_opt_not_valid_with_opt)
146 << Feature << "-target-feature";
152 ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
153 return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
154 Builtin::FirstTSBuiltin);
157 void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
158 MacroBuilder &Builder) const {
159 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
160 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
163 void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
164 MacroBuilder &Builder) const {
165 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
166 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);