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"
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},
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
33static constexpr llvm::StringLiteral ValidCPUNames[] = {
34    {"mvp"}, {"bleeding-edge"}, {"generic"}};
35
36StringRef WebAssemblyTargetInfo::getABI() const { return ABI; }
37
38bool WebAssemblyTargetInfo::setABI(const std::string &Name) {
39  if (Name != "mvp" && Name != "experimental-mv")
40    return false;
41
42  ABI = Name;
43  return true;
44}
45
46bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
47  return llvm::StringSwitch<bool>(Feature)
48      .Case("simd128", SIMDLevel >= SIMD128)
49      .Case("nontrapping-fptoint", HasNontrappingFPToInt)
50      .Case("sign-ext", HasSignExt)
51      .Case("exception-handling", HasExceptionHandling)
52      .Case("bulk-memory", HasBulkMemory)
53      .Case("atomics", HasAtomics)
54      .Case("mutable-globals", HasMutableGlobals)
55      .Case("multivalue", HasMultivalue)
56      .Case("tail-call", HasTailCall)
57      .Case("reference-types", HasReferenceTypes)
58      .Default(false);
59}
60
61bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
62  return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
63}
64
65void WebAssemblyTargetInfo::fillValidCPUList(
66    SmallVectorImpl<StringRef> &Values) const {
67  Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
68}
69
70void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
71                                             MacroBuilder &Builder) const {
72  defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
73  if (SIMDLevel >= SIMD128)
74    Builder.defineMacro("__wasm_simd128__");
75  if (HasNontrappingFPToInt)
76    Builder.defineMacro("__wasm_nontrapping_fptoint__");
77  if (HasSignExt)
78    Builder.defineMacro("__wasm_sign_ext__");
79  if (HasExceptionHandling)
80    Builder.defineMacro("__wasm_exception_handling__");
81  if (HasBulkMemory)
82    Builder.defineMacro("__wasm_bulk_memory__");
83  if (HasAtomics)
84    Builder.defineMacro("__wasm_atomics__");
85  if (HasMutableGlobals)
86    Builder.defineMacro("__wasm_mutable_globals__");
87  if (HasMultivalue)
88    Builder.defineMacro("__wasm_multivalue__");
89  if (HasTailCall)
90    Builder.defineMacro("__wasm_tail_call__");
91  if (HasReferenceTypes)
92    Builder.defineMacro("__wasm_reference_types__");
93}
94
95void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
96                                         SIMDEnum Level, bool Enabled) {
97  if (Enabled) {
98    switch (Level) {
99    case SIMD128:
100      Features["simd128"] = true;
101      LLVM_FALLTHROUGH;
102    case NoSIMD:
103      break;
104    }
105    return;
106  }
107
108  switch (Level) {
109  case NoSIMD:
110  case SIMD128:
111    Features["simd128"] = false;
112    break;
113  }
114}
115
116void WebAssemblyTargetInfo::setFeatureEnabled(llvm::StringMap<bool> &Features,
117                                              StringRef Name,
118                                              bool Enabled) const {
119  if (Name == "simd128")
120    setSIMDLevel(Features, SIMD128, Enabled);
121  else
122    Features[Name] = Enabled;
123}
124
125bool WebAssemblyTargetInfo::initFeatureMap(
126    llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
127    const std::vector<std::string> &FeaturesVec) const {
128  if (CPU == "bleeding-edge") {
129    Features["nontrapping-fptoint"] = true;
130    Features["sign-ext"] = true;
131    Features["bulk-memory"] = true;
132    Features["atomics"] = true;
133    Features["mutable-globals"] = true;
134    Features["tail-call"] = true;
135    setSIMDLevel(Features, SIMD128, true);
136  }
137
138  return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
139}
140
141bool WebAssemblyTargetInfo::handleTargetFeatures(
142    std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
143  for (const auto &Feature : Features) {
144    if (Feature == "+simd128") {
145      SIMDLevel = std::max(SIMDLevel, SIMD128);
146      continue;
147    }
148    if (Feature == "-simd128") {
149      SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
150      continue;
151    }
152    if (Feature == "+nontrapping-fptoint") {
153      HasNontrappingFPToInt = true;
154      continue;
155    }
156    if (Feature == "-nontrapping-fptoint") {
157      HasNontrappingFPToInt = false;
158      continue;
159    }
160    if (Feature == "+sign-ext") {
161      HasSignExt = true;
162      continue;
163    }
164    if (Feature == "-sign-ext") {
165      HasSignExt = false;
166      continue;
167    }
168    if (Feature == "+exception-handling") {
169      HasExceptionHandling = true;
170      continue;
171    }
172    if (Feature == "-exception-handling") {
173      HasExceptionHandling = false;
174      continue;
175    }
176    if (Feature == "+bulk-memory") {
177      HasBulkMemory = true;
178      continue;
179    }
180    if (Feature == "-bulk-memory") {
181      HasBulkMemory = false;
182      continue;
183    }
184    if (Feature == "+atomics") {
185      HasAtomics = true;
186      continue;
187    }
188    if (Feature == "-atomics") {
189      HasAtomics = false;
190      continue;
191    }
192    if (Feature == "+mutable-globals") {
193      HasMutableGlobals = true;
194      continue;
195    }
196    if (Feature == "-mutable-globals") {
197      HasMutableGlobals = false;
198      continue;
199    }
200    if (Feature == "+multivalue") {
201      HasMultivalue = true;
202      continue;
203    }
204    if (Feature == "-multivalue") {
205      HasMultivalue = false;
206      continue;
207    }
208    if (Feature == "+tail-call") {
209      HasTailCall = true;
210      continue;
211    }
212    if (Feature == "-tail-call") {
213      HasTailCall = false;
214      continue;
215    }
216    if (Feature == "+reference-types") {
217      HasReferenceTypes = true;
218      continue;
219    }
220    if (Feature == "-reference-types") {
221      HasReferenceTypes = false;
222      continue;
223    }
224
225    Diags.Report(diag::err_opt_not_valid_with_opt)
226        << Feature << "-target-feature";
227    return false;
228  }
229  return true;
230}
231
232ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
233  return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
234                                             Builtin::FirstTSBuiltin);
235}
236
237void WebAssemblyTargetInfo::adjust(LangOptions &Opts) {
238  // If the Atomics feature isn't available, turn off POSIXThreads and
239  // ThreadModel, so that we don't predefine _REENTRANT or __STDCPP_THREADS__.
240  if (!HasAtomics) {
241    Opts.POSIXThreads = false;
242    Opts.setThreadModel(LangOptions::ThreadModelKind::Single);
243  }
244}
245
246void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
247                                               MacroBuilder &Builder) const {
248  WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
249  defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
250}
251
252void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
253                                               MacroBuilder &Builder) const {
254  WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
255  defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
256}
257