blob: 2fdc84bb8cc8cad4a75c1f0c2dc8b27b782bedc0 [file] [log] [blame]
Erich Keaneebba5922017-07-21 22:37:03 +00001//===--- 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
21using namespace clang;
22using namespace clang::targets;
23
24const Builtin::Info WebAssemblyTargetInfo::BuiltinInfo[] = {
25#define BUILTIN(ID, TYPE, ATTRS) \
26 {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000027#define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
28 {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
Erich Keaneebba5922017-07-21 22:37:03 +000029#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
30 {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
31#include "clang/Basic/BuiltinsWebAssembly.def"
32};
33
Erich Keanee44bdb32018-02-08 23:16:55 +000034static constexpr llvm::StringLiteral ValidCPUNames[] = {
35 {"mvp"}, {"bleeding-edge"}, {"generic"}};
36
Erich Keaneebba5922017-07-21 22:37:03 +000037bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
38 return llvm::StringSwitch<bool>(Feature)
39 .Case("simd128", SIMDLevel >= SIMD128)
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000040 .Case("unimplemented-simd128", SIMDLevel >= UnimplementedSIMD128)
Dan Gohman0811cd12017-11-28 01:13:45 +000041 .Case("nontrapping-fptoint", HasNontrappingFPToInt)
Dan Gohmand0c4e1e2018-01-19 17:16:32 +000042 .Case("sign-ext", HasSignExt)
Heejin Ahn8b6af222018-03-02 00:39:16 +000043 .Case("exception-handling", HasExceptionHandling)
Erich Keaneebba5922017-07-21 22:37:03 +000044 .Default(false);
45}
46
47bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
Erich Keanee44bdb32018-02-08 23:16:55 +000048 return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
49}
50
51void WebAssemblyTargetInfo::fillValidCPUList(
52 SmallVectorImpl<StringRef> &Values) const {
53 Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
Erich Keaneebba5922017-07-21 22:37:03 +000054}
55
56void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
57 MacroBuilder &Builder) const {
58 defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
59 if (SIMDLevel >= SIMD128)
60 Builder.defineMacro("__wasm_simd128__");
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000061 if (SIMDLevel >= UnimplementedSIMD128)
62 Builder.defineMacro("__wasm_unimplemented_simd128__");
63}
64
65void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
66 SIMDEnum Level) {
67 switch (Level) {
68 case UnimplementedSIMD128:
69 Features["unimplemented-simd128"] = true;
70 LLVM_FALLTHROUGH;
71 case SIMD128:
72 Features["simd128"] = true;
73 LLVM_FALLTHROUGH;
74 case NoSIMD:
75 break;
76 }
77}
78
79bool WebAssemblyTargetInfo::initFeatureMap(
80 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
81 const std::vector<std::string> &FeaturesVec) const {
82 if (CPU == "bleeding-edge") {
83 Features["nontrapping-fptoint"] = true;
84 Features["sign-ext"] = true;
85 setSIMDLevel(Features, SIMD128);
86 }
87 // Other targets do not consider user-configured features here, but while we
88 // are actively developing new features it is useful to let user-configured
89 // features control availability of builtins
90 setSIMDLevel(Features, SIMDLevel);
91 if (HasNontrappingFPToInt)
92 Features["nontrapping-fptoint"] = true;
93 if (HasSignExt)
94 Features["sign-ext"] = true;
95 if (HasExceptionHandling)
96 Features["exception-handling"] = true;
97
98 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
Erich Keaneebba5922017-07-21 22:37:03 +000099}
100
101bool WebAssemblyTargetInfo::handleTargetFeatures(
102 std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
103 for (const auto &Feature : Features) {
104 if (Feature == "+simd128") {
105 SIMDLevel = std::max(SIMDLevel, SIMD128);
106 continue;
107 }
108 if (Feature == "-simd128") {
109 SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
110 continue;
111 }
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +0000112 if (Feature == "+unimplemented-simd128") {
113 SIMDLevel = std::max(SIMDLevel, SIMDEnum(UnimplementedSIMD128));
114 continue;
115 }
116 if (Feature == "-unimplemented-simd128") {
117 SIMDLevel = std::min(SIMDLevel, SIMDEnum(UnimplementedSIMD128 - 1));
118 continue;
119 }
Dan Gohman0811cd12017-11-28 01:13:45 +0000120 if (Feature == "+nontrapping-fptoint") {
121 HasNontrappingFPToInt = true;
122 continue;
123 }
124 if (Feature == "-nontrapping-fptoint") {
125 HasNontrappingFPToInt = false;
126 continue;
127 }
Dan Gohmand0c4e1e2018-01-19 17:16:32 +0000128 if (Feature == "+sign-ext") {
129 HasSignExt = true;
130 continue;
131 }
132 if (Feature == "-sign-ext") {
133 HasSignExt = false;
134 continue;
135 }
Heejin Ahn8b6af222018-03-02 00:39:16 +0000136 if (Feature == "+exception-handling") {
137 HasExceptionHandling = true;
138 continue;
139 }
140 if (Feature == "-exception-handling") {
141 HasExceptionHandling = false;
142 continue;
143 }
Erich Keaneebba5922017-07-21 22:37:03 +0000144
145 Diags.Report(diag::err_opt_not_valid_with_opt)
146 << Feature << "-target-feature";
147 return false;
148 }
149 return true;
150}
151
152ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
153 return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
154 Builtin::FirstTSBuiltin);
155}
156
157void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
158 MacroBuilder &Builder) const {
159 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
160 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
161}
162
163void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
164 MacroBuilder &Builder) const {
165 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
166 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
167}