1 //===--- WebAssembly.cpp - Implement WebAssembly target feature support ---===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements WebAssembly TargetInfo objects.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "WebAssembly.h"
15 #include "Targets.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"
20
21 using namespace clang;
22 using namespace clang::targets;
23
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"
32 };
33
34 static constexpr llvm::StringLiteral ValidCPUNames[] = {
35 {"mvp"}, {"bleeding-edge"}, {"generic"}};
36
hasFeature(StringRef Feature) const37 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)
44 .Default(false);
45 }
46
isValidCPUName(StringRef Name) const47 bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
48 return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
49 }
50
fillValidCPUList(SmallVectorImpl<StringRef> & Values) const51 void WebAssemblyTargetInfo::fillValidCPUList(
52 SmallVectorImpl<StringRef> &Values) const {
53 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
54 }
55
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const56 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__");
63 }
64
setSIMDLevel(llvm::StringMap<bool> & Features,SIMDEnum Level)65 void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
66 SIMDEnum Level) {
67 switch (Level) {
68 case UnimplementedSIMD128:
69 Features["unimplemented-simd128"] = true;
70 LLVM_FALLTHROUGH;
71 case SIMD128:
72 Features["simd128"] = true;
73 LLVM_FALLTHROUGH;
74 case NoSIMD:
75 break;
76 }
77 }
78
initFeatureMap(llvm::StringMap<bool> & Features,DiagnosticsEngine & Diags,StringRef CPU,const std::vector<std::string> & FeaturesVec) const79 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);
86 }
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;
93 if (HasSignExt)
94 Features["sign-ext"] = true;
95 if (HasExceptionHandling)
96 Features["exception-handling"] = true;
97
98 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
99 }
100
handleTargetFeatures(std::vector<std::string> & Features,DiagnosticsEngine & Diags)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);
106 continue;
107 }
108 if (Feature == "-simd128") {
109 SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
110 continue;
111 }
112 if (Feature == "+unimplemented-simd128") {
113 SIMDLevel = std::max(SIMDLevel, SIMDEnum(UnimplementedSIMD128));
114 continue;
115 }
116 if (Feature == "-unimplemented-simd128") {
117 SIMDLevel = std::min(SIMDLevel, SIMDEnum(UnimplementedSIMD128 - 1));
118 continue;
119 }
120 if (Feature == "+nontrapping-fptoint") {
121 HasNontrappingFPToInt = true;
122 continue;
123 }
124 if (Feature == "-nontrapping-fptoint") {
125 HasNontrappingFPToInt = false;
126 continue;
127 }
128 if (Feature == "+sign-ext") {
129 HasSignExt = true;
130 continue;
131 }
132 if (Feature == "-sign-ext") {
133 HasSignExt = false;
134 continue;
135 }
136 if (Feature == "+exception-handling") {
137 HasExceptionHandling = true;
138 continue;
139 }
140 if (Feature == "-exception-handling") {
141 HasExceptionHandling = false;
142 continue;
143 }
144
145 Diags.Report(diag::err_opt_not_valid_with_opt)
146 << Feature << "-target-feature";
147 return false;
148 }
149 return true;
150 }
151
getTargetBuiltins() const152 ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
153 return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
154 Builtin::FirstTSBuiltin);
155 }
156
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const157 void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
158 MacroBuilder &Builder) const {
159 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
160 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
161 }
162
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const163 void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
164 MacroBuilder &Builder) const {
165 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
166 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
167 }
168