Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 1 | //===--- WebAssembly.cpp - Implement WebAssembly target feature support ---===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 6 | // |
| 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 | |
| 20 | using namespace clang; |
| 21 | using namespace clang::targets; |
| 22 | |
| 23 | const Builtin::Info WebAssemblyTargetInfo::BuiltinInfo[] = { |
| 24 | #define BUILTIN(ID, TYPE, ATTRS) \ |
| 25 | {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr}, |
Thomas Lively | b7b9fdc | 2019-01-10 23:49:00 +0000 | [diff] [blame] | 26 | #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \ |
| 27 | {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE}, |
Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 28 | #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \ |
| 29 | {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr}, |
| 30 | #include "clang/Basic/BuiltinsWebAssembly.def" |
| 31 | }; |
| 32 | |
Erich Keane | e44bdb3 | 2018-02-08 23:16:55 +0000 | [diff] [blame] | 33 | static constexpr llvm::StringLiteral ValidCPUNames[] = { |
| 34 | {"mvp"}, {"bleeding-edge"}, {"generic"}}; |
| 35 | |
Thomas Lively | 8c3e6af | 2020-01-17 18:28:43 -0800 | [diff] [blame] | 36 | StringRef WebAssemblyTargetInfo::getABI() const { return ABI; } |
| 37 | |
| 38 | bool WebAssemblyTargetInfo::setABI(const std::string &Name) { |
| 39 | if (Name != "mvp" && Name != "experimental-mv") |
| 40 | return false; |
| 41 | |
| 42 | ABI = Name; |
| 43 | return true; |
| 44 | } |
| 45 | |
Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 46 | bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const { |
| 47 | return llvm::StringSwitch<bool>(Feature) |
| 48 | .Case("simd128", SIMDLevel >= SIMD128) |
Thomas Lively | b7b9fdc | 2019-01-10 23:49:00 +0000 | [diff] [blame] | 49 | .Case("unimplemented-simd128", SIMDLevel >= UnimplementedSIMD128) |
Dan Gohman | 0811cd1 | 2017-11-28 01:13:45 +0000 | [diff] [blame] | 50 | .Case("nontrapping-fptoint", HasNontrappingFPToInt) |
Dan Gohman | d0c4e1e | 2018-01-19 17:16:32 +0000 | [diff] [blame] | 51 | .Case("sign-ext", HasSignExt) |
Heejin Ahn | 8b6af22 | 2018-03-02 00:39:16 +0000 | [diff] [blame] | 52 | .Case("exception-handling", HasExceptionHandling) |
Thomas Lively | 88058d4 | 2019-01-31 21:02:19 +0000 | [diff] [blame] | 53 | .Case("bulk-memory", HasBulkMemory) |
Heejin Ahn | bab8597 | 2019-02-06 01:41:26 +0000 | [diff] [blame] | 54 | .Case("atomics", HasAtomics) |
Thomas Lively | 5f0c4c6 | 2019-03-29 22:00:18 +0000 | [diff] [blame] | 55 | .Case("mutable-globals", HasMutableGlobals) |
Thomas Lively | eafe8ef | 2019-05-23 17:26:47 +0000 | [diff] [blame] | 56 | .Case("multivalue", HasMultivalue) |
| 57 | .Case("tail-call", HasTailCall) |
Heejin Ahn | 764f408 | 2020-01-23 19:22:51 -0800 | [diff] [blame] | 58 | .Case("reference-types", HasReferenceTypes) |
Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 59 | .Default(false); |
| 60 | } |
| 61 | |
| 62 | bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const { |
Erich Keane | e44bdb3 | 2018-02-08 23:16:55 +0000 | [diff] [blame] | 63 | return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames); |
| 64 | } |
| 65 | |
| 66 | void WebAssemblyTargetInfo::fillValidCPUList( |
| 67 | SmallVectorImpl<StringRef> &Values) const { |
| 68 | Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames)); |
Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 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__"); |
Thomas Lively | b7b9fdc | 2019-01-10 23:49:00 +0000 | [diff] [blame] | 76 | if (SIMDLevel >= UnimplementedSIMD128) |
| 77 | Builder.defineMacro("__wasm_unimplemented_simd128__"); |
Thomas Lively | 88058d4 | 2019-01-31 21:02:19 +0000 | [diff] [blame] | 78 | if (HasNontrappingFPToInt) |
| 79 | Builder.defineMacro("__wasm_nontrapping_fptoint__"); |
| 80 | if (HasSignExt) |
| 81 | Builder.defineMacro("__wasm_sign_ext__"); |
| 82 | if (HasExceptionHandling) |
| 83 | Builder.defineMacro("__wasm_exception_handling__"); |
| 84 | if (HasBulkMemory) |
| 85 | Builder.defineMacro("__wasm_bulk_memory__"); |
Heejin Ahn | bab8597 | 2019-02-06 01:41:26 +0000 | [diff] [blame] | 86 | if (HasAtomics) |
| 87 | Builder.defineMacro("__wasm_atomics__"); |
Thomas Lively | 5f0c4c6 | 2019-03-29 22:00:18 +0000 | [diff] [blame] | 88 | if (HasMutableGlobals) |
| 89 | Builder.defineMacro("__wasm_mutable_globals__"); |
Thomas Lively | eafe8ef | 2019-05-23 17:26:47 +0000 | [diff] [blame] | 90 | if (HasMultivalue) |
| 91 | Builder.defineMacro("__wasm_multivalue__"); |
| 92 | if (HasTailCall) |
| 93 | Builder.defineMacro("__wasm_tail_call__"); |
Heejin Ahn | 764f408 | 2020-01-23 19:22:51 -0800 | [diff] [blame] | 94 | if (HasReferenceTypes) |
| 95 | Builder.defineMacro("__wasm_reference_types__"); |
Thomas Lively | b7b9fdc | 2019-01-10 23:49:00 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features, |
| 99 | SIMDEnum Level) { |
| 100 | switch (Level) { |
| 101 | case UnimplementedSIMD128: |
| 102 | Features["unimplemented-simd128"] = true; |
| 103 | LLVM_FALLTHROUGH; |
| 104 | case SIMD128: |
| 105 | Features["simd128"] = true; |
| 106 | LLVM_FALLTHROUGH; |
| 107 | case NoSIMD: |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | bool WebAssemblyTargetInfo::initFeatureMap( |
| 113 | llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU, |
| 114 | const std::vector<std::string> &FeaturesVec) const { |
| 115 | if (CPU == "bleeding-edge") { |
| 116 | Features["nontrapping-fptoint"] = true; |
| 117 | Features["sign-ext"] = true; |
Heejin Ahn | 65eb113 | 2020-01-23 19:32:32 -0800 | [diff] [blame] | 118 | Features["bulk-memory"] = true; |
Heejin Ahn | bab8597 | 2019-02-06 01:41:26 +0000 | [diff] [blame] | 119 | Features["atomics"] = true; |
Thomas Lively | 5f0c4c6 | 2019-03-29 22:00:18 +0000 | [diff] [blame] | 120 | Features["mutable-globals"] = true; |
Heejin Ahn | 65eb113 | 2020-01-23 19:32:32 -0800 | [diff] [blame] | 121 | Features["tail-call"] = true; |
Thomas Lively | b7b9fdc | 2019-01-10 23:49:00 +0000 | [diff] [blame] | 122 | setSIMDLevel(Features, SIMD128); |
| 123 | } |
| 124 | // Other targets do not consider user-configured features here, but while we |
| 125 | // are actively developing new features it is useful to let user-configured |
| 126 | // features control availability of builtins |
| 127 | setSIMDLevel(Features, SIMDLevel); |
| 128 | if (HasNontrappingFPToInt) |
| 129 | Features["nontrapping-fptoint"] = true; |
| 130 | if (HasSignExt) |
| 131 | Features["sign-ext"] = true; |
| 132 | if (HasExceptionHandling) |
| 133 | Features["exception-handling"] = true; |
Thomas Lively | 88058d4 | 2019-01-31 21:02:19 +0000 | [diff] [blame] | 134 | if (HasBulkMemory) |
| 135 | Features["bulk-memory"] = true; |
Heejin Ahn | bab8597 | 2019-02-06 01:41:26 +0000 | [diff] [blame] | 136 | if (HasAtomics) |
| 137 | Features["atomics"] = true; |
Thomas Lively | 5f0c4c6 | 2019-03-29 22:00:18 +0000 | [diff] [blame] | 138 | if (HasMutableGlobals) |
| 139 | Features["mutable-globals"] = true; |
Thomas Lively | eafe8ef | 2019-05-23 17:26:47 +0000 | [diff] [blame] | 140 | if (HasMultivalue) |
| 141 | Features["multivalue"] = true; |
| 142 | if (HasTailCall) |
| 143 | Features["tail-call"] = true; |
Heejin Ahn | 764f408 | 2020-01-23 19:22:51 -0800 | [diff] [blame] | 144 | if (HasReferenceTypes) |
| 145 | Features["reference-types"] = true; |
Thomas Lively | b7b9fdc | 2019-01-10 23:49:00 +0000 | [diff] [blame] | 146 | |
| 147 | return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec); |
Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | bool WebAssemblyTargetInfo::handleTargetFeatures( |
| 151 | std::vector<std::string> &Features, DiagnosticsEngine &Diags) { |
| 152 | for (const auto &Feature : Features) { |
| 153 | if (Feature == "+simd128") { |
| 154 | SIMDLevel = std::max(SIMDLevel, SIMD128); |
| 155 | continue; |
| 156 | } |
| 157 | if (Feature == "-simd128") { |
| 158 | SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1)); |
| 159 | continue; |
| 160 | } |
Thomas Lively | b7b9fdc | 2019-01-10 23:49:00 +0000 | [diff] [blame] | 161 | if (Feature == "+unimplemented-simd128") { |
| 162 | SIMDLevel = std::max(SIMDLevel, SIMDEnum(UnimplementedSIMD128)); |
| 163 | continue; |
| 164 | } |
| 165 | if (Feature == "-unimplemented-simd128") { |
| 166 | SIMDLevel = std::min(SIMDLevel, SIMDEnum(UnimplementedSIMD128 - 1)); |
| 167 | continue; |
| 168 | } |
Dan Gohman | 0811cd1 | 2017-11-28 01:13:45 +0000 | [diff] [blame] | 169 | if (Feature == "+nontrapping-fptoint") { |
| 170 | HasNontrappingFPToInt = true; |
| 171 | continue; |
| 172 | } |
| 173 | if (Feature == "-nontrapping-fptoint") { |
| 174 | HasNontrappingFPToInt = false; |
| 175 | continue; |
| 176 | } |
Dan Gohman | d0c4e1e | 2018-01-19 17:16:32 +0000 | [diff] [blame] | 177 | if (Feature == "+sign-ext") { |
| 178 | HasSignExt = true; |
| 179 | continue; |
| 180 | } |
| 181 | if (Feature == "-sign-ext") { |
| 182 | HasSignExt = false; |
| 183 | continue; |
| 184 | } |
Heejin Ahn | 8b6af22 | 2018-03-02 00:39:16 +0000 | [diff] [blame] | 185 | if (Feature == "+exception-handling") { |
| 186 | HasExceptionHandling = true; |
| 187 | continue; |
| 188 | } |
| 189 | if (Feature == "-exception-handling") { |
| 190 | HasExceptionHandling = false; |
| 191 | continue; |
| 192 | } |
Thomas Lively | 88058d4 | 2019-01-31 21:02:19 +0000 | [diff] [blame] | 193 | if (Feature == "+bulk-memory") { |
| 194 | HasBulkMemory = true; |
| 195 | continue; |
| 196 | } |
| 197 | if (Feature == "-bulk-memory") { |
| 198 | HasBulkMemory = false; |
| 199 | continue; |
| 200 | } |
Heejin Ahn | bab8597 | 2019-02-06 01:41:26 +0000 | [diff] [blame] | 201 | if (Feature == "+atomics") { |
| 202 | HasAtomics = true; |
| 203 | continue; |
| 204 | } |
| 205 | if (Feature == "-atomics") { |
| 206 | HasAtomics = false; |
| 207 | continue; |
| 208 | } |
Thomas Lively | 5f0c4c6 | 2019-03-29 22:00:18 +0000 | [diff] [blame] | 209 | if (Feature == "+mutable-globals") { |
| 210 | HasMutableGlobals = true; |
| 211 | continue; |
| 212 | } |
| 213 | if (Feature == "-mutable-globals") { |
| 214 | HasMutableGlobals = false; |
| 215 | continue; |
| 216 | } |
Thomas Lively | eafe8ef | 2019-05-23 17:26:47 +0000 | [diff] [blame] | 217 | if (Feature == "+multivalue") { |
| 218 | HasMultivalue = true; |
| 219 | continue; |
| 220 | } |
| 221 | if (Feature == "-multivalue") { |
| 222 | HasMultivalue = false; |
| 223 | continue; |
| 224 | } |
| 225 | if (Feature == "+tail-call") { |
| 226 | HasTailCall = true; |
| 227 | continue; |
| 228 | } |
| 229 | if (Feature == "-tail-call") { |
| 230 | HasTailCall = false; |
| 231 | continue; |
| 232 | } |
Heejin Ahn | 764f408 | 2020-01-23 19:22:51 -0800 | [diff] [blame] | 233 | if (Feature == "+reference-types") { |
| 234 | HasReferenceTypes = true; |
| 235 | continue; |
| 236 | } |
| 237 | if (Feature == "-reference-types") { |
| 238 | HasReferenceTypes = false; |
| 239 | continue; |
| 240 | } |
Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 241 | |
| 242 | Diags.Report(diag::err_opt_not_valid_with_opt) |
| 243 | << Feature << "-target-feature"; |
| 244 | return false; |
| 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const { |
| 250 | return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin - |
| 251 | Builtin::FirstTSBuiltin); |
| 252 | } |
| 253 | |
| 254 | void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts, |
| 255 | MacroBuilder &Builder) const { |
| 256 | WebAssemblyTargetInfo::getTargetDefines(Opts, Builder); |
| 257 | defineCPUMacros(Builder, "wasm32", /*Tuning=*/false); |
| 258 | } |
| 259 | |
| 260 | void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts, |
| 261 | MacroBuilder &Builder) const { |
| 262 | WebAssemblyTargetInfo::getTargetDefines(Opts, Builder); |
| 263 | defineCPUMacros(Builder, "wasm64", /*Tuning=*/false); |
| 264 | } |