1ebba5926SErich Keane //===--- WebAssembly.cpp - Implement WebAssembly target feature support ---===//
2ebba5926SErich Keane //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ebba5926SErich Keane //
7ebba5926SErich Keane //===----------------------------------------------------------------------===//
8ebba5926SErich Keane //
9ebba5926SErich Keane // This file implements WebAssembly TargetInfo objects.
10ebba5926SErich Keane //
11ebba5926SErich Keane //===----------------------------------------------------------------------===//
12ebba5926SErich Keane 
13ebba5926SErich Keane #include "WebAssembly.h"
14ebba5926SErich Keane #include "Targets.h"
15ebba5926SErich Keane #include "clang/Basic/Builtins.h"
16ebba5926SErich Keane #include "clang/Basic/Diagnostic.h"
17ebba5926SErich Keane #include "clang/Basic/TargetBuiltins.h"
18ebba5926SErich Keane #include "llvm/ADT/StringSwitch.h"
19ebba5926SErich Keane 
20ebba5926SErich Keane using namespace clang;
21ebba5926SErich Keane using namespace clang::targets;
22ebba5926SErich Keane 
23ebba5926SErich Keane const Builtin::Info WebAssemblyTargetInfo::BuiltinInfo[] = {
24ebba5926SErich Keane #define BUILTIN(ID, TYPE, ATTRS)                                               \
25ebba5926SErich Keane   {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
26b7b9fdc1SThomas Lively #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE)                               \
27b7b9fdc1SThomas Lively   {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
28ebba5926SErich Keane #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER)                                    \
29ebba5926SErich Keane   {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
30ebba5926SErich Keane #include "clang/Basic/BuiltinsWebAssembly.def"
31ebba5926SErich Keane };
32ebba5926SErich Keane 
33e44bdb3fSErich Keane static constexpr llvm::StringLiteral ValidCPUNames[] = {
34e44bdb3fSErich Keane     {"mvp"}, {"bleeding-edge"}, {"generic"}};
35e44bdb3fSErich Keane 
getABI() const368c3e6af7SThomas Lively StringRef WebAssemblyTargetInfo::getABI() const { return ABI; }
378c3e6af7SThomas Lively 
setABI(const std::string & Name)388c3e6af7SThomas Lively bool WebAssemblyTargetInfo::setABI(const std::string &Name) {
398c3e6af7SThomas Lively   if (Name != "mvp" && Name != "experimental-mv")
408c3e6af7SThomas Lively     return false;
418c3e6af7SThomas Lively 
428c3e6af7SThomas Lively   ABI = Name;
438c3e6af7SThomas Lively   return true;
448c3e6af7SThomas Lively }
458c3e6af7SThomas Lively 
hasFeature(StringRef Feature) const46ebba5926SErich Keane bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
47ebba5926SErich Keane   return llvm::StringSwitch<bool>(Feature)
48ebba5926SErich Keane       .Case("simd128", SIMDLevel >= SIMD128)
491552179aSZhi An Ng       .Case("relaxed-simd", SIMDLevel >= RelaxedSIMD)
500811cd1dSDan Gohman       .Case("nontrapping-fptoint", HasNontrappingFPToInt)
51d0c4e1e9SDan Gohman       .Case("sign-ext", HasSignExt)
528b6af22eSHeejin Ahn       .Case("exception-handling", HasExceptionHandling)
5388058d4eSThomas Lively       .Case("bulk-memory", HasBulkMemory)
54bab85979SHeejin Ahn       .Case("atomics", HasAtomics)
555f0c4c67SThomas Lively       .Case("mutable-globals", HasMutableGlobals)
56eafe8ef6SThomas Lively       .Case("multivalue", HasMultivalue)
57eafe8ef6SThomas Lively       .Case("tail-call", HasTailCall)
58764f4089SHeejin Ahn       .Case("reference-types", HasReferenceTypes)
59*c832edfdSSam Clegg       .Case("extended-const", HasExtendedConst)
60ebba5926SErich Keane       .Default(false);
61ebba5926SErich Keane }
62ebba5926SErich Keane 
isValidCPUName(StringRef Name) const63ebba5926SErich Keane bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
640e9373a6SKazu Hirata   return llvm::is_contained(ValidCPUNames, Name);
65e44bdb3fSErich Keane }
66e44bdb3fSErich Keane 
fillValidCPUList(SmallVectorImpl<StringRef> & Values) const67e44bdb3fSErich Keane void WebAssemblyTargetInfo::fillValidCPUList(
68e44bdb3fSErich Keane     SmallVectorImpl<StringRef> &Values) const {
69e44bdb3fSErich Keane   Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
70ebba5926SErich Keane }
71ebba5926SErich Keane 
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const72ebba5926SErich Keane void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
73ebba5926SErich Keane                                              MacroBuilder &Builder) const {
74ebba5926SErich Keane   defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
75ebba5926SErich Keane   if (SIMDLevel >= SIMD128)
76ebba5926SErich Keane     Builder.defineMacro("__wasm_simd128__");
771552179aSZhi An Ng   if (SIMDLevel >= RelaxedSIMD)
781552179aSZhi An Ng     Builder.defineMacro("__wasm_relaxed_simd__");
7988058d4eSThomas Lively   if (HasNontrappingFPToInt)
8088058d4eSThomas Lively     Builder.defineMacro("__wasm_nontrapping_fptoint__");
8188058d4eSThomas Lively   if (HasSignExt)
8288058d4eSThomas Lively     Builder.defineMacro("__wasm_sign_ext__");
8388058d4eSThomas Lively   if (HasExceptionHandling)
8488058d4eSThomas Lively     Builder.defineMacro("__wasm_exception_handling__");
8588058d4eSThomas Lively   if (HasBulkMemory)
8688058d4eSThomas Lively     Builder.defineMacro("__wasm_bulk_memory__");
87bab85979SHeejin Ahn   if (HasAtomics)
88bab85979SHeejin Ahn     Builder.defineMacro("__wasm_atomics__");
895f0c4c67SThomas Lively   if (HasMutableGlobals)
905f0c4c67SThomas Lively     Builder.defineMacro("__wasm_mutable_globals__");
91eafe8ef6SThomas Lively   if (HasMultivalue)
92eafe8ef6SThomas Lively     Builder.defineMacro("__wasm_multivalue__");
93eafe8ef6SThomas Lively   if (HasTailCall)
94eafe8ef6SThomas Lively     Builder.defineMacro("__wasm_tail_call__");
95764f4089SHeejin Ahn   if (HasReferenceTypes)
96764f4089SHeejin Ahn     Builder.defineMacro("__wasm_reference_types__");
97*c832edfdSSam Clegg   if (HasExtendedConst)
98*c832edfdSSam Clegg     Builder.defineMacro("__wasm_extended_const__");
99b7b9fdc1SThomas Lively }
100b7b9fdc1SThomas Lively 
setSIMDLevel(llvm::StringMap<bool> & Features,SIMDEnum Level,bool Enabled)101b7b9fdc1SThomas Lively void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
1022b8ad6b6SCraig Topper                                          SIMDEnum Level, bool Enabled) {
1032b8ad6b6SCraig Topper   if (Enabled) {
104b7b9fdc1SThomas Lively     switch (Level) {
1051552179aSZhi An Ng     case RelaxedSIMD:
1061552179aSZhi An Ng       Features["relaxed-simd"] = true;
1071552179aSZhi An Ng       LLVM_FALLTHROUGH;
108b7b9fdc1SThomas Lively     case SIMD128:
109b7b9fdc1SThomas Lively       Features["simd128"] = true;
110b7b9fdc1SThomas Lively       LLVM_FALLTHROUGH;
111b7b9fdc1SThomas Lively     case NoSIMD:
112b7b9fdc1SThomas Lively       break;
113b7b9fdc1SThomas Lively     }
1142b8ad6b6SCraig Topper     return;
1152b8ad6b6SCraig Topper   }
1162b8ad6b6SCraig Topper 
1172b8ad6b6SCraig Topper   switch (Level) {
1182b8ad6b6SCraig Topper   case NoSIMD:
1192b8ad6b6SCraig Topper   case SIMD128:
1202b8ad6b6SCraig Topper     Features["simd128"] = false;
1211552179aSZhi An Ng     LLVM_FALLTHROUGH;
1221552179aSZhi An Ng   case RelaxedSIMD:
1231552179aSZhi An Ng     Features["relaxed-simd"] = false;
1242b8ad6b6SCraig Topper     break;
1252b8ad6b6SCraig Topper   }
1262b8ad6b6SCraig Topper }
1272b8ad6b6SCraig Topper 
setFeatureEnabled(llvm::StringMap<bool> & Features,StringRef Name,bool Enabled) const1282b8ad6b6SCraig Topper void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
1292b8ad6b6SCraig Topper                                               StringRef Name,
1302b8ad6b6SCraig Topper                                               bool Enabled) const {
1312b8ad6b6SCraig Topper   if (Name == "simd128")
1322b8ad6b6SCraig Topper     setSIMDLevel(Features, SIMD128, Enabled);
1331552179aSZhi An Ng   else if (Name == "relaxed-simd")
1341552179aSZhi An Ng     setSIMDLevel(Features, RelaxedSIMD, Enabled);
1352b8ad6b6SCraig Topper   else
1362b8ad6b6SCraig Topper     Features[Name] = Enabled;
137b7b9fdc1SThomas Lively }
138b7b9fdc1SThomas Lively 
initFeatureMap(llvm::StringMap<bool> & Features,DiagnosticsEngine & Diags,StringRef CPU,const std::vector<std::string> & FeaturesVec) const139b7b9fdc1SThomas Lively bool WebAssemblyTargetInfo::initFeatureMap(
140b7b9fdc1SThomas Lively     llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
141b7b9fdc1SThomas Lively     const std::vector<std::string> &FeaturesVec) const {
142b7b9fdc1SThomas Lively   if (CPU == "bleeding-edge") {
143b7b9fdc1SThomas Lively     Features["nontrapping-fptoint"] = true;
144b7b9fdc1SThomas Lively     Features["sign-ext"] = true;
14565eb1130SHeejin Ahn     Features["bulk-memory"] = true;
146bab85979SHeejin Ahn     Features["atomics"] = true;
1475f0c4c67SThomas Lively     Features["mutable-globals"] = true;
14865eb1130SHeejin Ahn     Features["tail-call"] = true;
1492b8ad6b6SCraig Topper     setSIMDLevel(Features, SIMD128, true);
150b7b9fdc1SThomas Lively   }
151b7b9fdc1SThomas Lively 
152b7b9fdc1SThomas Lively   return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
153ebba5926SErich Keane }
154ebba5926SErich Keane 
handleTargetFeatures(std::vector<std::string> & Features,DiagnosticsEngine & Diags)155ebba5926SErich Keane bool WebAssemblyTargetInfo::handleTargetFeatures(
156ebba5926SErich Keane     std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
157ebba5926SErich Keane   for (const auto &Feature : Features) {
158ebba5926SErich Keane     if (Feature == "+simd128") {
159ebba5926SErich Keane       SIMDLevel = std::max(SIMDLevel, SIMD128);
160ebba5926SErich Keane       continue;
161ebba5926SErich Keane     }
162ebba5926SErich Keane     if (Feature == "-simd128") {
163ebba5926SErich Keane       SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
164ebba5926SErich Keane       continue;
165ebba5926SErich Keane     }
1661552179aSZhi An Ng     if (Feature == "+relaxed-simd") {
1671552179aSZhi An Ng       SIMDLevel = std::max(SIMDLevel, RelaxedSIMD);
1681552179aSZhi An Ng       continue;
1691552179aSZhi An Ng     }
1701552179aSZhi An Ng     if (Feature == "-relaxed-simd") {
1711552179aSZhi An Ng       SIMDLevel = std::min(SIMDLevel, SIMDEnum(RelaxedSIMD - 1));
1721552179aSZhi An Ng       continue;
1731552179aSZhi An Ng     }
1740811cd1dSDan Gohman     if (Feature == "+nontrapping-fptoint") {
1750811cd1dSDan Gohman       HasNontrappingFPToInt = true;
1760811cd1dSDan Gohman       continue;
1770811cd1dSDan Gohman     }
1780811cd1dSDan Gohman     if (Feature == "-nontrapping-fptoint") {
1790811cd1dSDan Gohman       HasNontrappingFPToInt = false;
1800811cd1dSDan Gohman       continue;
1810811cd1dSDan Gohman     }
182d0c4e1e9SDan Gohman     if (Feature == "+sign-ext") {
183d0c4e1e9SDan Gohman       HasSignExt = true;
184d0c4e1e9SDan Gohman       continue;
185d0c4e1e9SDan Gohman     }
186d0c4e1e9SDan Gohman     if (Feature == "-sign-ext") {
187d0c4e1e9SDan Gohman       HasSignExt = false;
188d0c4e1e9SDan Gohman       continue;
189d0c4e1e9SDan Gohman     }
1908b6af22eSHeejin Ahn     if (Feature == "+exception-handling") {
1918b6af22eSHeejin Ahn       HasExceptionHandling = true;
1928b6af22eSHeejin Ahn       continue;
1938b6af22eSHeejin Ahn     }
1948b6af22eSHeejin Ahn     if (Feature == "-exception-handling") {
1958b6af22eSHeejin Ahn       HasExceptionHandling = false;
1968b6af22eSHeejin Ahn       continue;
1978b6af22eSHeejin Ahn     }
19888058d4eSThomas Lively     if (Feature == "+bulk-memory") {
19988058d4eSThomas Lively       HasBulkMemory = true;
20088058d4eSThomas Lively       continue;
20188058d4eSThomas Lively     }
20288058d4eSThomas Lively     if (Feature == "-bulk-memory") {
20388058d4eSThomas Lively       HasBulkMemory = false;
20488058d4eSThomas Lively       continue;
20588058d4eSThomas Lively     }
206bab85979SHeejin Ahn     if (Feature == "+atomics") {
207bab85979SHeejin Ahn       HasAtomics = true;
208bab85979SHeejin Ahn       continue;
209bab85979SHeejin Ahn     }
210bab85979SHeejin Ahn     if (Feature == "-atomics") {
211bab85979SHeejin Ahn       HasAtomics = false;
212bab85979SHeejin Ahn       continue;
213bab85979SHeejin Ahn     }
2145f0c4c67SThomas Lively     if (Feature == "+mutable-globals") {
2155f0c4c67SThomas Lively       HasMutableGlobals = true;
2165f0c4c67SThomas Lively       continue;
2175f0c4c67SThomas Lively     }
2185f0c4c67SThomas Lively     if (Feature == "-mutable-globals") {
2195f0c4c67SThomas Lively       HasMutableGlobals = false;
2205f0c4c67SThomas Lively       continue;
2215f0c4c67SThomas Lively     }
222eafe8ef6SThomas Lively     if (Feature == "+multivalue") {
223eafe8ef6SThomas Lively       HasMultivalue = true;
224eafe8ef6SThomas Lively       continue;
225eafe8ef6SThomas Lively     }
226eafe8ef6SThomas Lively     if (Feature == "-multivalue") {
227eafe8ef6SThomas Lively       HasMultivalue = false;
228eafe8ef6SThomas Lively       continue;
229eafe8ef6SThomas Lively     }
230eafe8ef6SThomas Lively     if (Feature == "+tail-call") {
231eafe8ef6SThomas Lively       HasTailCall = true;
232eafe8ef6SThomas Lively       continue;
233eafe8ef6SThomas Lively     }
234eafe8ef6SThomas Lively     if (Feature == "-tail-call") {
235eafe8ef6SThomas Lively       HasTailCall = false;
236eafe8ef6SThomas Lively       continue;
237eafe8ef6SThomas Lively     }
238764f4089SHeejin Ahn     if (Feature == "+reference-types") {
239764f4089SHeejin Ahn       HasReferenceTypes = true;
240764f4089SHeejin Ahn       continue;
241764f4089SHeejin Ahn     }
242764f4089SHeejin Ahn     if (Feature == "-reference-types") {
243764f4089SHeejin Ahn       HasReferenceTypes = false;
244764f4089SHeejin Ahn       continue;
245764f4089SHeejin Ahn     }
246*c832edfdSSam Clegg     if (Feature == "+extended-const") {
247*c832edfdSSam Clegg       HasExtendedConst = true;
248*c832edfdSSam Clegg       continue;
249*c832edfdSSam Clegg     }
250*c832edfdSSam Clegg     if (Feature == "-extended-const") {
251*c832edfdSSam Clegg       HasExtendedConst = false;
252*c832edfdSSam Clegg       continue;
253*c832edfdSSam Clegg     }
254ebba5926SErich Keane 
255ebba5926SErich Keane     Diags.Report(diag::err_opt_not_valid_with_opt)
256ebba5926SErich Keane         << Feature << "-target-feature";
257ebba5926SErich Keane     return false;
258ebba5926SErich Keane   }
259ebba5926SErich Keane   return true;
260ebba5926SErich Keane }
261ebba5926SErich Keane 
getTargetBuiltins() const262ebba5926SErich Keane ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
263ebba5926SErich Keane   return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
264ebba5926SErich Keane                                              Builtin::FirstTSBuiltin);
265ebba5926SErich Keane }
266ebba5926SErich Keane 
adjust(DiagnosticsEngine & Diags,LangOptions & Opts)267aaba3718SMelanie Blower void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags,
268aaba3718SMelanie Blower                                    LangOptions &Opts) {
269ecbcefd6SSam Clegg   TargetInfo::adjust(Diags, Opts);
2703be9e0baSThomas Lively   // Turn off POSIXThreads and ThreadModel so that we don't predefine _REENTRANT
2713be9e0baSThomas Lively   // or __STDCPP_THREADS__ if we will eventually end up stripping atomics
2723be9e0baSThomas Lively   // because they are unsupported.
2733be9e0baSThomas Lively   if (!HasAtomics || !HasBulkMemory) {
27495da64daSDan Gohman     Opts.POSIXThreads = false;
27595da64daSDan Gohman     Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
27645ad3467SSam Clegg     Opts.ThreadsafeStatics = false;
27795da64daSDan Gohman   }
27895da64daSDan Gohman }
27995da64daSDan Gohman 
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const280ebba5926SErich Keane void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
281ebba5926SErich Keane                                                MacroBuilder &Builder) const {
282ebba5926SErich Keane   WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
283ebba5926SErich Keane   defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
284ebba5926SErich Keane }
285ebba5926SErich Keane 
getTargetDefines(const LangOptions & Opts,MacroBuilder & Builder) const286ebba5926SErich Keane void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
287ebba5926SErich Keane                                                MacroBuilder &Builder) const {
288ebba5926SErich Keane   WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
289ebba5926SErich Keane   defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
290ebba5926SErich Keane }
291