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 | |
| 32 | bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const { |
| 33 | return llvm::StringSwitch<bool>(Feature) |
| 34 | .Case("simd128", SIMDLevel >= SIMD128) |
| 35 | .Default(false); |
| 36 | } |
| 37 | |
| 38 | bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const { |
| 39 | return llvm::StringSwitch<bool>(Name) |
| 40 | .Case("mvp", true) |
| 41 | .Case("bleeding-edge", true) |
| 42 | .Case("generic", true) |
| 43 | .Default(false); |
| 44 | } |
| 45 | |
| 46 | void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts, |
| 47 | MacroBuilder &Builder) const { |
| 48 | defineCPUMacros(Builder, "wasm", /*Tuning=*/false); |
| 49 | if (SIMDLevel >= SIMD128) |
| 50 | Builder.defineMacro("__wasm_simd128__"); |
| 51 | } |
| 52 | |
| 53 | bool WebAssemblyTargetInfo::handleTargetFeatures( |
| 54 | std::vector<std::string> &Features, DiagnosticsEngine &Diags) { |
| 55 | for (const auto &Feature : Features) { |
| 56 | if (Feature == "+simd128") { |
| 57 | SIMDLevel = std::max(SIMDLevel, SIMD128); |
| 58 | continue; |
| 59 | } |
| 60 | if (Feature == "-simd128") { |
| 61 | SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1)); |
| 62 | continue; |
| 63 | } |
| 64 | |
| 65 | Diags.Report(diag::err_opt_not_valid_with_opt) |
| 66 | << Feature << "-target-feature"; |
| 67 | return false; |
| 68 | } |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const { |
| 73 | return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin - |
| 74 | Builtin::FirstTSBuiltin); |
| 75 | } |
| 76 | |
| 77 | void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts, |
| 78 | MacroBuilder &Builder) const { |
| 79 | WebAssemblyTargetInfo::getTargetDefines(Opts, Builder); |
| 80 | defineCPUMacros(Builder, "wasm32", /*Tuning=*/false); |
| 81 | } |
| 82 | |
| 83 | void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts, |
| 84 | MacroBuilder &Builder) const { |
| 85 | WebAssemblyTargetInfo::getTargetDefines(Opts, Builder); |
| 86 | defineCPUMacros(Builder, "wasm64", /*Tuning=*/false); |
| 87 | } |