clang  14.0.0git
WebAssembly.cpp
Go to the documentation of this file.
1 //===--- WebAssembly.cpp - Implement WebAssembly target feature support ---===//
2 //
3 // 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
6 //
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"
18 #include "llvm/ADT/StringSwitch.h"
19 
20 using namespace clang;
21 using namespace clang::targets;
22 
23 const Builtin::Info WebAssemblyTargetInfo::BuiltinInfo[] = {
24 #define BUILTIN(ID, TYPE, ATTRS) \
25  {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
26 #define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
27  {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
28 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
29  {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
30 #include "clang/Basic/BuiltinsWebAssembly.def"
31 };
32 
33 static constexpr llvm::StringLiteral ValidCPUNames[] = {
34  {"mvp"}, {"bleeding-edge"}, {"generic"}};
35 
36 StringRef WebAssemblyTargetInfo::getABI() const { return ABI; }
37 
39  if (Name != "mvp" && Name != "experimental-mv")
40  return false;
41 
42  ABI = Name;
43  return true;
44 }
45 
46 bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
47  return llvm::StringSwitch<bool>(Feature)
48  .Case("simd128", SIMDLevel >= SIMD128)
49  .Case("relaxed-simd", SIMDLevel >= RelaxedSIMD)
50  .Case("nontrapping-fptoint", HasNontrappingFPToInt)
51  .Case("sign-ext", HasSignExt)
52  .Case("exception-handling", HasExceptionHandling)
53  .Case("bulk-memory", HasBulkMemory)
54  .Case("atomics", HasAtomics)
55  .Case("mutable-globals", HasMutableGlobals)
56  .Case("multivalue", HasMultivalue)
57  .Case("tail-call", HasTailCall)
58  .Case("reference-types", HasReferenceTypes)
59  .Default(false);
60 }
61 
62 bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
63  return llvm::is_contained(ValidCPUNames, Name);
64 }
65 
66 void WebAssemblyTargetInfo::fillValidCPUList(
67  SmallVectorImpl<StringRef> &Values) const {
68  Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
69 }
70 
72  MacroBuilder &Builder) const {
73  defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
74  if (SIMDLevel >= SIMD128)
75  Builder.defineMacro("__wasm_simd128__");
76  if (SIMDLevel >= RelaxedSIMD)
77  Builder.defineMacro("__wasm_relaxed_simd__");
78  if (HasNontrappingFPToInt)
79  Builder.defineMacro("__wasm_nontrapping_fptoint__");
80  if (HasSignExt)
81  Builder.defineMacro("__wasm_sign_ext__");
82  if (HasExceptionHandling)
83  Builder.defineMacro("__wasm_exception_handling__");
84  if (HasBulkMemory)
85  Builder.defineMacro("__wasm_bulk_memory__");
86  if (HasAtomics)
87  Builder.defineMacro("__wasm_atomics__");
88  if (HasMutableGlobals)
89  Builder.defineMacro("__wasm_mutable_globals__");
90  if (HasMultivalue)
91  Builder.defineMacro("__wasm_multivalue__");
92  if (HasTailCall)
93  Builder.defineMacro("__wasm_tail_call__");
94  if (HasReferenceTypes)
95  Builder.defineMacro("__wasm_reference_types__");
96 }
97 
98 void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
99  SIMDEnum Level, bool Enabled) {
100  if (Enabled) {
101  switch (Level) {
102  case RelaxedSIMD:
103  Features["relaxed-simd"] = true;
104  LLVM_FALLTHROUGH;
105  case SIMD128:
106  Features["simd128"] = true;
107  LLVM_FALLTHROUGH;
108  case NoSIMD:
109  break;
110  }
111  return;
112  }
113 
114  switch (Level) {
115  case NoSIMD:
116  case SIMD128:
117  Features["simd128"] = false;
118  LLVM_FALLTHROUGH;
119  case RelaxedSIMD:
120  Features["relaxed-simd"] = false;
121  break;
122  }
123 }
124 
125 void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
126  StringRef Name,
127  bool Enabled) const {
128  if (Name == "simd128")
129  setSIMDLevel(Features, SIMD128, Enabled);
130  else if (Name == "relaxed-simd")
131  setSIMDLevel(Features, RelaxedSIMD, Enabled);
132  else
133  Features[Name] = Enabled;
134 }
135 
136 bool WebAssemblyTargetInfo::initFeatureMap(
137  llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
138  const std::vector<std::string> &FeaturesVec) const {
139  if (CPU == "bleeding-edge") {
140  Features["nontrapping-fptoint"] = true;
141  Features["sign-ext"] = true;
142  Features["bulk-memory"] = true;
143  Features["atomics"] = true;
144  Features["mutable-globals"] = true;
145  Features["tail-call"] = true;
146  setSIMDLevel(Features, SIMD128, true);
147  }
148 
149  return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
150 }
151 
152 bool WebAssemblyTargetInfo::handleTargetFeatures(
153  std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
154  for (const auto &Feature : Features) {
155  if (Feature == "+simd128") {
156  SIMDLevel = std::max(SIMDLevel, SIMD128);
157  continue;
158  }
159  if (Feature == "-simd128") {
160  SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
161  continue;
162  }
163  if (Feature == "+relaxed-simd") {
164  SIMDLevel = std::max(SIMDLevel, RelaxedSIMD);
165  continue;
166  }
167  if (Feature == "-relaxed-simd") {
168  SIMDLevel = std::min(SIMDLevel, SIMDEnum(RelaxedSIMD - 1));
169  continue;
170  }
171  if (Feature == "+nontrapping-fptoint") {
172  HasNontrappingFPToInt = true;
173  continue;
174  }
175  if (Feature == "-nontrapping-fptoint") {
176  HasNontrappingFPToInt = false;
177  continue;
178  }
179  if (Feature == "+sign-ext") {
180  HasSignExt = true;
181  continue;
182  }
183  if (Feature == "-sign-ext") {
184  HasSignExt = false;
185  continue;
186  }
187  if (Feature == "+exception-handling") {
188  HasExceptionHandling = true;
189  continue;
190  }
191  if (Feature == "-exception-handling") {
192  HasExceptionHandling = false;
193  continue;
194  }
195  if (Feature == "+bulk-memory") {
196  HasBulkMemory = true;
197  continue;
198  }
199  if (Feature == "-bulk-memory") {
200  HasBulkMemory = false;
201  continue;
202  }
203  if (Feature == "+atomics") {
204  HasAtomics = true;
205  continue;
206  }
207  if (Feature == "-atomics") {
208  HasAtomics = false;
209  continue;
210  }
211  if (Feature == "+mutable-globals") {
212  HasMutableGlobals = true;
213  continue;
214  }
215  if (Feature == "-mutable-globals") {
216  HasMutableGlobals = false;
217  continue;
218  }
219  if (Feature == "+multivalue") {
220  HasMultivalue = true;
221  continue;
222  }
223  if (Feature == "-multivalue") {
224  HasMultivalue = false;
225  continue;
226  }
227  if (Feature == "+tail-call") {
228  HasTailCall = true;
229  continue;
230  }
231  if (Feature == "-tail-call") {
232  HasTailCall = false;
233  continue;
234  }
235  if (Feature == "+reference-types") {
236  HasReferenceTypes = true;
237  continue;
238  }
239  if (Feature == "-reference-types") {
240  HasReferenceTypes = false;
241  continue;
242  }
243 
244  Diags.Report(diag::err_opt_not_valid_with_opt)
245  << Feature << "-target-feature";
246  return false;
247  }
248  return true;
249 }
250 
251 ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
252  return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
254 }
255 
256 void WebAssemblyTargetInfo::adjust(DiagnosticsEngine &Diags,
257  LangOptions &Opts) {
258  // If the Atomics feature isn't available, turn off POSIXThreads and
259  // ThreadModel, so that we don't predefine _REENTRANT or __STDCPP_THREADS__.
260  if (!HasAtomics) {
261  Opts.POSIXThreads = false;
262  Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
263  }
264 }
265 
267  MacroBuilder &Builder) const {
269  defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
270 }
271 
273  MacroBuilder &Builder) const {
275  defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
276 }
Builtins.h
max
__DEVICE__ int max(int __a, int __b)
Definition: __clang_cuda_math.h:196
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:191
clang::WebAssembly::LastTSBuiltin
@ LastTSBuiltin
Definition: TargetBuiltins.h:330
clang::targets::WebAssemblyTargetInfo::setABI
bool setABI(const std::string &Name) override
Use the specified ABI.
Definition: WebAssembly.cpp:38
clang::targets::WebAssembly32TargetInfo::getTargetDefines
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition: WebAssembly.cpp:266
min
__DEVICE__ int min(int __a, int __b)
Definition: __clang_cuda_math.h:197
Diagnostic.h
Targets.h
clang::TargetInfo::initFeatureMap
virtual bool initFeatureMap(llvm::StringMap< bool > &Features, DiagnosticsEngine &Diags, StringRef CPU, const std::vector< std::string > &FeatureVec) const
Initialize the map with the default set of target features for the CPU this should include all legal ...
Definition: TargetInfo.cpp:461
clang::targets::WebAssemblyTargetInfo::getABI
StringRef getABI() const override
Get the ABI currently in use.
Definition: WebAssembly.cpp:36
llvm::ArrayRef
Definition: LLVM.h:34
clang::LangOptions::ThreadModelKind::Single
@ Single
Single Threaded Environment.
TargetBuiltins.h
WebAssembly.h
clang::targets::WebAssemblyTargetInfo::getTargetDefines
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition: WebAssembly.cpp:71
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:58
ValidCPUNames
static constexpr llvm::StringLiteral ValidCPUNames[]
Definition: WebAssembly.cpp:33
clang
Definition: CalledOnceCheck.h:17
clang::targets
Definition: AArch64.h:21
clang::Builtin::Info
Definition: Builtins.h:55
clang::prec::Level
Level
Definition: OperatorPrecedence.h:26
clang::targets::WebAssembly64TargetInfo::getTargetDefines
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods -----------------------—===//
Definition: WebAssembly.cpp:272
clang::Builtin::FirstTSBuiltin
@ FirstTSBuiltin
Definition: Builtins.h:52
llvm::SmallVectorImpl
Definition: LLVM.h:39
clang::MacroBuilder
Definition: MacroBuilder.h:23
clang::targets::defineCPUMacros
void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning)
Definition: Targets.cpp:72
clang::DiagnosticsEngine::Report
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1523