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