Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 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 LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \ |
| 28 | {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr}, |
| 29 | #include "clang/Basic/BuiltinsWebAssembly.def" |
| 30 | }; |
| 31 | |
Erich Keane | e44bdb3 | 2018-02-08 23:16:55 +0000 | [diff] [blame] | 32 | static constexpr llvm::StringLiteral ValidCPUNames[] = { |
| 33 | {"mvp"}, {"bleeding-edge"}, {"generic"}}; |
| 34 | |
Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 35 | bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const { |
| 36 | return llvm::StringSwitch<bool>(Feature) |
| 37 | .Case("simd128", SIMDLevel >= SIMD128) |
Dan Gohman | 0811cd1 | 2017-11-28 01:13:45 +0000 | [diff] [blame] | 38 | .Case("nontrapping-fptoint", HasNontrappingFPToInt) |
Dan Gohman | d0c4e1e | 2018-01-19 17:16:32 +0000 | [diff] [blame] | 39 | .Case("sign-ext", HasSignExt) |
Heejin Ahn | 8b6af22 | 2018-03-02 00:39:16 +0000 | [diff] [blame] | 40 | .Case("exception-handling", HasExceptionHandling) |
Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 41 | .Default(false); |
| 42 | } |
| 43 | |
| 44 | bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const { |
Erich Keane | e44bdb3 | 2018-02-08 23:16:55 +0000 | [diff] [blame] | 45 | return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames); |
| 46 | } |
| 47 | |
| 48 | void WebAssemblyTargetInfo::fillValidCPUList( |
| 49 | SmallVectorImpl<StringRef> &Values) const { |
| 50 | Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames)); |
Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts, |
| 54 | MacroBuilder &Builder) const { |
| 55 | defineCPUMacros(Builder, "wasm", /*Tuning=*/false); |
| 56 | if (SIMDLevel >= SIMD128) |
| 57 | Builder.defineMacro("__wasm_simd128__"); |
| 58 | } |
| 59 | |
| 60 | bool WebAssemblyTargetInfo::handleTargetFeatures( |
| 61 | std::vector<std::string> &Features, DiagnosticsEngine &Diags) { |
| 62 | for (const auto &Feature : Features) { |
| 63 | if (Feature == "+simd128") { |
| 64 | SIMDLevel = std::max(SIMDLevel, SIMD128); |
| 65 | continue; |
| 66 | } |
| 67 | if (Feature == "-simd128") { |
| 68 | SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1)); |
| 69 | continue; |
| 70 | } |
Dan Gohman | 0811cd1 | 2017-11-28 01:13:45 +0000 | [diff] [blame] | 71 | if (Feature == "+nontrapping-fptoint") { |
| 72 | HasNontrappingFPToInt = true; |
| 73 | continue; |
| 74 | } |
| 75 | if (Feature == "-nontrapping-fptoint") { |
| 76 | HasNontrappingFPToInt = false; |
| 77 | continue; |
| 78 | } |
Dan Gohman | d0c4e1e | 2018-01-19 17:16:32 +0000 | [diff] [blame] | 79 | if (Feature == "+sign-ext") { |
| 80 | HasSignExt = true; |
| 81 | continue; |
| 82 | } |
| 83 | if (Feature == "-sign-ext") { |
| 84 | HasSignExt = false; |
| 85 | continue; |
| 86 | } |
Heejin Ahn | 8b6af22 | 2018-03-02 00:39:16 +0000 | [diff] [blame] | 87 | if (Feature == "+exception-handling") { |
| 88 | HasExceptionHandling = true; |
| 89 | continue; |
| 90 | } |
| 91 | if (Feature == "-exception-handling") { |
| 92 | HasExceptionHandling = false; |
| 93 | continue; |
| 94 | } |
Erich Keane | ebba592 | 2017-07-21 22:37:03 +0000 | [diff] [blame] | 95 | |
| 96 | Diags.Report(diag::err_opt_not_valid_with_opt) |
| 97 | << Feature << "-target-feature"; |
| 98 | return false; |
| 99 | } |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const { |
| 104 | return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin - |
| 105 | Builtin::FirstTSBuiltin); |
| 106 | } |
| 107 | |
| 108 | void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts, |
| 109 | MacroBuilder &Builder) const { |
| 110 | WebAssemblyTargetInfo::getTargetDefines(Opts, Builder); |
| 111 | defineCPUMacros(Builder, "wasm32", /*Tuning=*/false); |
| 112 | } |
| 113 | |
| 114 | void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts, |
| 115 | MacroBuilder &Builder) const { |
| 116 | WebAssemblyTargetInfo::getTargetDefines(Opts, Builder); |
| 117 | defineCPUMacros(Builder, "wasm64", /*Tuning=*/false); |
| 118 | } |