xref: /llvm-project/clang/lib/Basic/Targets/WebAssembly.cpp (revision d0c4e1e9fcf8eb9e9bc0d8d19c777daabd8b34db)
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       .Case("nontrapping-fptoint", HasNontrappingFPToInt)
36       .Case("sign-ext", HasSignExt)
37       .Default(false);
38 }
39 
40 bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
41   return llvm::StringSwitch<bool>(Name)
42       .Case("mvp", true)
43       .Case("bleeding-edge", true)
44       .Case("generic", true)
45       .Default(false);
46 }
47 
48 void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
49                                              MacroBuilder &Builder) const {
50   defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
51   if (SIMDLevel >= SIMD128)
52     Builder.defineMacro("__wasm_simd128__");
53 }
54 
55 bool WebAssemblyTargetInfo::handleTargetFeatures(
56     std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
57   for (const auto &Feature : Features) {
58     if (Feature == "+simd128") {
59       SIMDLevel = std::max(SIMDLevel, SIMD128);
60       continue;
61     }
62     if (Feature == "-simd128") {
63       SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
64       continue;
65     }
66     if (Feature == "+nontrapping-fptoint") {
67       HasNontrappingFPToInt = true;
68       continue;
69     }
70     if (Feature == "-nontrapping-fptoint") {
71       HasNontrappingFPToInt = false;
72       continue;
73     }
74     if (Feature == "+sign-ext") {
75       HasSignExt = true;
76       continue;
77     }
78     if (Feature == "-sign-ext") {
79       HasSignExt = false;
80       continue;
81     }
82 
83     Diags.Report(diag::err_opt_not_valid_with_opt)
84         << Feature << "-target-feature";
85     return false;
86   }
87   return true;
88 }
89 
90 ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
91   return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
92                                              Builtin::FirstTSBuiltin);
93 }
94 
95 void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
96                                                MacroBuilder &Builder) const {
97   WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
98   defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
99 }
100 
101 void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
102                                                MacroBuilder &Builder) const {
103   WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
104   defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
105 }
106