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