clang  8.0.0
WebAssembly.cpp
Go to the documentation of this file.
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"
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 TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
28  {#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
29 #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
30  {#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
31 #include "clang/Basic/BuiltinsWebAssembly.def"
32 };
33 
34 static constexpr llvm::StringLiteral ValidCPUNames[] = {
35  {"mvp"}, {"bleeding-edge"}, {"generic"}};
36 
37 bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
38  return llvm::StringSwitch<bool>(Feature)
39  .Case("simd128", SIMDLevel >= SIMD128)
40  .Case("unimplemented-simd128", SIMDLevel >= UnimplementedSIMD128)
41  .Case("nontrapping-fptoint", HasNontrappingFPToInt)
42  .Case("sign-ext", HasSignExt)
43  .Case("exception-handling", HasExceptionHandling)
44  .Default(false);
45 }
46 
47 bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
48  return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
49 }
50 
51 void WebAssemblyTargetInfo::fillValidCPUList(
52  SmallVectorImpl<StringRef> &Values) const {
53  Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
54 }
55 
57  MacroBuilder &Builder) const {
58  defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
59  if (SIMDLevel >= SIMD128)
60  Builder.defineMacro("__wasm_simd128__");
61  if (SIMDLevel >= UnimplementedSIMD128)
62  Builder.defineMacro("__wasm_unimplemented_simd128__");
63 }
64 
65 void 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 
79 bool 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);
99 }
100 
101 bool 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  }
112  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  }
120  if (Feature == "+nontrapping-fptoint") {
121  HasNontrappingFPToInt = true;
122  continue;
123  }
124  if (Feature == "-nontrapping-fptoint") {
125  HasNontrappingFPToInt = false;
126  continue;
127  }
128  if (Feature == "+sign-ext") {
129  HasSignExt = true;
130  continue;
131  }
132  if (Feature == "-sign-ext") {
133  HasSignExt = false;
134  continue;
135  }
136  if (Feature == "+exception-handling") {
137  HasExceptionHandling = true;
138  continue;
139  }
140  if (Feature == "-exception-handling") {
141  HasExceptionHandling = false;
142  continue;
143  }
144 
145  Diags.Report(diag::err_opt_not_valid_with_opt)
146  << Feature << "-target-feature";
147  return false;
148  }
149  return true;
150 }
151 
152 ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
153  return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
155 }
156 
158  MacroBuilder &Builder) const {
160  defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
161 }
162 
164  MacroBuilder &Builder) const {
166  defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
167 }
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1295
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:50
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
Defines the Diagnostic-related interfaces.
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:386
Enumerates target-specific builtins in their own namespaces within namespace clang.
Dataflow Directional Tag Classes.
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods --------------------——===//
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods --------------------——===//
static constexpr llvm::StringLiteral ValidCPUNames[]
Definition: WebAssembly.cpp:34
void getTargetDefines(const LangOptions &Opts, MacroBuilder &Builder) const override
===-— Other target property query methods --------------------——===//
Definition: WebAssembly.cpp:56
__DEVICE__ int max(int __a, int __b)
void defineCPUMacros(MacroBuilder &Builder, StringRef CPUName, bool Tuning)
Definition: Targets.cpp:71
__DEVICE__ int min(int __a, int __b)
void defineMacro(const Twine &Name, const Twine &Value="1")
Append a #define line for macro of the form "\#define Name Value\n".
Definition: MacroBuilder.h:30
Defines enum values for all the target-independent builtin functions.