blob: 48edaded5125707ca51d5f15bd3649c7454ae18e [file] [log] [blame]
Erich Keaneebba5922017-07-21 22:37:03 +00001//===--- WebAssembly.cpp - Implement WebAssembly target feature support ---===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Keaneebba5922017-07-21 22:37:03 +00006//
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
20using namespace clang;
21using namespace clang::targets;
22
23const Builtin::Info WebAssemblyTargetInfo::BuiltinInfo[] = {
24#define BUILTIN(ID, TYPE, ATTRS) \
25 {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000026#define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
27 {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
Erich Keaneebba5922017-07-21 22:37:03 +000028#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
29 {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
30#include "clang/Basic/BuiltinsWebAssembly.def"
31};
32
Erich Keanee44bdb32018-02-08 23:16:55 +000033static constexpr llvm::StringLiteral ValidCPUNames[] = {
34 {"mvp"}, {"bleeding-edge"}, {"generic"}};
35
Erich Keaneebba5922017-07-21 22:37:03 +000036bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
37 return llvm::StringSwitch<bool>(Feature)
38 .Case("simd128", SIMDLevel >= SIMD128)
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000039 .Case("unimplemented-simd128", SIMDLevel >= UnimplementedSIMD128)
Dan Gohman0811cd12017-11-28 01:13:45 +000040 .Case("nontrapping-fptoint", HasNontrappingFPToInt)
Dan Gohmand0c4e1e2018-01-19 17:16:32 +000041 .Case("sign-ext", HasSignExt)
Heejin Ahn8b6af222018-03-02 00:39:16 +000042 .Case("exception-handling", HasExceptionHandling)
Thomas Lively88058d42019-01-31 21:02:19 +000043 .Case("bulk-memory", HasBulkMemory)
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__");
Thomas Lively88058d42019-01-31 21:02:19 +000063 if (HasNontrappingFPToInt)
64 Builder.defineMacro("__wasm_nontrapping_fptoint__");
65 if (HasSignExt)
66 Builder.defineMacro("__wasm_sign_ext__");
67 if (HasExceptionHandling)
68 Builder.defineMacro("__wasm_exception_handling__");
69 if (HasBulkMemory)
70 Builder.defineMacro("__wasm_bulk_memory__");
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +000071}
72
73void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
74 SIMDEnum Level) {
75 switch (Level) {
76 case UnimplementedSIMD128:
77 Features["unimplemented-simd128"] = true;
78 LLVM_FALLTHROUGH;
79 case SIMD128:
80 Features["simd128"] = true;
81 LLVM_FALLTHROUGH;
82 case NoSIMD:
83 break;
84 }
85}
86
87bool WebAssemblyTargetInfo::initFeatureMap(
88 llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
89 const std::vector<std::string> &FeaturesVec) const {
90 if (CPU == "bleeding-edge") {
91 Features["nontrapping-fptoint"] = true;
92 Features["sign-ext"] = true;
93 setSIMDLevel(Features, SIMD128);
94 }
95 // Other targets do not consider user-configured features here, but while we
96 // are actively developing new features it is useful to let user-configured
97 // features control availability of builtins
98 setSIMDLevel(Features, SIMDLevel);
99 if (HasNontrappingFPToInt)
100 Features["nontrapping-fptoint"] = true;
101 if (HasSignExt)
102 Features["sign-ext"] = true;
103 if (HasExceptionHandling)
104 Features["exception-handling"] = true;
Thomas Lively88058d42019-01-31 21:02:19 +0000105 if (HasBulkMemory)
106 Features["bulk-memory"] = true;
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +0000107
108 return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
Erich Keaneebba5922017-07-21 22:37:03 +0000109}
110
111bool WebAssemblyTargetInfo::handleTargetFeatures(
112 std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
113 for (const auto &Feature : Features) {
114 if (Feature == "+simd128") {
115 SIMDLevel = std::max(SIMDLevel, SIMD128);
116 continue;
117 }
118 if (Feature == "-simd128") {
119 SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
120 continue;
121 }
Thomas Livelyb7b9fdc2019-01-10 23:49:00 +0000122 if (Feature == "+unimplemented-simd128") {
123 SIMDLevel = std::max(SIMDLevel, SIMDEnum(UnimplementedSIMD128));
124 continue;
125 }
126 if (Feature == "-unimplemented-simd128") {
127 SIMDLevel = std::min(SIMDLevel, SIMDEnum(UnimplementedSIMD128 - 1));
128 continue;
129 }
Dan Gohman0811cd12017-11-28 01:13:45 +0000130 if (Feature == "+nontrapping-fptoint") {
131 HasNontrappingFPToInt = true;
132 continue;
133 }
134 if (Feature == "-nontrapping-fptoint") {
135 HasNontrappingFPToInt = false;
136 continue;
137 }
Dan Gohmand0c4e1e2018-01-19 17:16:32 +0000138 if (Feature == "+sign-ext") {
139 HasSignExt = true;
140 continue;
141 }
142 if (Feature == "-sign-ext") {
143 HasSignExt = false;
144 continue;
145 }
Heejin Ahn8b6af222018-03-02 00:39:16 +0000146 if (Feature == "+exception-handling") {
147 HasExceptionHandling = true;
148 continue;
149 }
150 if (Feature == "-exception-handling") {
151 HasExceptionHandling = false;
152 continue;
153 }
Thomas Lively88058d42019-01-31 21:02:19 +0000154 if (Feature == "+bulk-memory") {
155 HasBulkMemory = true;
156 continue;
157 }
158 if (Feature == "-bulk-memory") {
159 HasBulkMemory = false;
160 continue;
161 }
Erich Keaneebba5922017-07-21 22:37:03 +0000162
163 Diags.Report(diag::err_opt_not_valid_with_opt)
164 << Feature << "-target-feature";
165 return false;
166 }
167 return true;
168}
169
170ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
171 return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
172 Builtin::FirstTSBuiltin);
173}
174
175void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
176 MacroBuilder &Builder) const {
177 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
178 defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
179}
180
181void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
182 MacroBuilder &Builder) const {
183 WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
184 defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
185}