Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Basic/Targets/WebAssembly.cpp
Line
Count
Source (jump to first uncovered line)
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
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
290
bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
37
290
  return llvm::StringSwitch<bool>(Feature)
38
290
      .Case("simd128", SIMDLevel >= SIMD128)
39
290
      .Case("unimplemented-simd128", SIMDLevel >= UnimplementedSIMD128)
40
290
      .Case("nontrapping-fptoint", HasNontrappingFPToInt)
41
290
      .Case("sign-ext", HasSignExt)
42
290
      .Case("exception-handling", HasExceptionHandling)
43
290
      .Case("bulk-memory", HasBulkMemory)
44
290
      .Case("atomics", HasAtomics)
45
290
      .Case("mutable-globals", HasMutableGlobals)
46
290
      .Case("multivalue", HasMultivalue)
47
290
      .Case("tail-call", HasTailCall)
48
290
      .Default(false);
49
290
}
50
51
33
bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
52
33
  return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
53
33
}
54
55
void WebAssemblyTargetInfo::fillValidCPUList(
56
1
    SmallVectorImpl<StringRef> &Values) const {
57
1
  Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
58
1
}
59
60
void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
61
65
                                             MacroBuilder &Builder) const {
62
65
  defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
63
65
  if (SIMDLevel >= SIMD128)
64
8
    Builder.defineMacro("__wasm_simd128__");
65
65
  if (SIMDLevel >= UnimplementedSIMD128)
66
4
    Builder.defineMacro("__wasm_unimplemented_simd128__");
67
65
  if (HasNontrappingFPToInt)
68
9
    Builder.defineMacro("__wasm_nontrapping_fptoint__");
69
65
  if (HasSignExt)
70
6
    Builder.defineMacro("__wasm_sign_ext__");
71
65
  if (HasExceptionHandling)
72
7
    Builder.defineMacro("__wasm_exception_handling__");
73
65
  if (HasBulkMemory)
74
9
    Builder.defineMacro("__wasm_bulk_memory__");
75
65
  if (HasAtomics)
76
10
    Builder.defineMacro("__wasm_atomics__");
77
65
  if (HasMutableGlobals)
78
10
    Builder.defineMacro("__wasm_mutable_globals__");
79
65
  if (HasMultivalue)
80
2
    Builder.defineMacro("__wasm_multivalue__");
81
65
  if (HasTailCall)
82
2
    Builder.defineMacro("__wasm_tail_call__");
83
65
}
84
85
void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
86
201
                                         SIMDEnum Level) {
87
201
  switch (Level) {
88
201
  case UnimplementedSIMD128:
89
116
    Features["unimplemented-simd128"] = true;
90
116
    LLVM_FALLTHROUGH;
91
120
  case SIMD128:
92
120
    Features["simd128"] = true;
93
120
    LLVM_FALLTHROUGH;
94
201
  case NoSIMD:
95
201
    break;
96
201
  }
97
201
}
98
99
bool WebAssemblyTargetInfo::initFeatureMap(
100
    llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
101
197
    const std::vector<std::string> &FeaturesVec) const {
102
197
  if (CPU == "bleeding-edge") {
103
4
    Features["nontrapping-fptoint"] = true;
104
4
    Features["sign-ext"] = true;
105
4
    Features["atomics"] = true;
106
4
    Features["mutable-globals"] = true;
107
4
    setSIMDLevel(Features, SIMD128);
108
4
  }
109
197
  // Other targets do not consider user-configured features here, but while we
110
197
  // are actively developing new features it is useful to let user-configured
111
197
  // features control availability of builtins
112
197
  setSIMDLevel(Features, SIMDLevel);
113
197
  if (HasNontrappingFPToInt)
114
132
    Features["nontrapping-fptoint"] = true;
115
197
  if (HasSignExt)
116
0
    Features["sign-ext"] = true;
117
197
  if (HasExceptionHandling)
118
132
    Features["exception-handling"] = true;
119
197
  if (HasBulkMemory)
120
132
    Features["bulk-memory"] = true;
121
197
  if (HasAtomics)
122
0
    Features["atomics"] = true;
123
197
  if (HasMutableGlobals)
124
0
    Features["mutable-globals"] = true;
125
197
  if (HasMultivalue)
126
0
    Features["multivalue"] = true;
127
197
  if (HasTailCall)
128
0
    Features["tail-call"] = true;
129
197
130
197
  return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
131
197
}
132
133
bool WebAssemblyTargetInfo::handleTargetFeatures(
134
65
    std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
135
65
  for (const auto &Feature : Features) {
136
65
    if (Feature == "+simd128") {
137
4
      SIMDLevel = std::max(SIMDLevel, SIMD128);
138
4
      continue;
139
4
    }
140
61
    if (Feature == "-simd128") {
141
2
      SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
142
2
      continue;
143
2
    }
144
59
    if (Feature == "+unimplemented-simd128") {
145
4
      SIMDLevel = std::max(SIMDLevel, SIMDEnum(UnimplementedSIMD128));
146
4
      continue;
147
4
    }
148
55
    if (Feature == "-unimplemented-simd128") {
149
0
      SIMDLevel = std::min(SIMDLevel, SIMDEnum(UnimplementedSIMD128 - 1));
150
0
      continue;
151
0
    }
152
55
    if (Feature == "+nontrapping-fptoint") {
153
9
      HasNontrappingFPToInt = true;
154
9
      continue;
155
9
    }
156
46
    if (Feature == "-nontrapping-fptoint") {
157
0
      HasNontrappingFPToInt = false;
158
0
      continue;
159
0
    }
160
46
    if (Feature == "+sign-ext") {
161
6
      HasSignExt = true;
162
6
      continue;
163
6
    }
164
40
    if (Feature == "-sign-ext") {
165
0
      HasSignExt = false;
166
0
      continue;
167
0
    }
168
40
    if (Feature == "+exception-handling") {
169
7
      HasExceptionHandling = true;
170
7
      continue;
171
7
    }
172
33
    if (Feature == "-exception-handling") {
173
0
      HasExceptionHandling = false;
174
0
      continue;
175
0
    }
176
33
    if (Feature == "+bulk-memory") {
177
9
      HasBulkMemory = true;
178
9
      continue;
179
9
    }
180
24
    if (Feature == "-bulk-memory") {
181
0
      HasBulkMemory = false;
182
0
      continue;
183
0
    }
184
24
    if (Feature == "+atomics") {
185
10
      HasAtomics = true;
186
10
      continue;
187
10
    }
188
14
    if (Feature == "-atomics") {
189
0
      HasAtomics = false;
190
0
      continue;
191
0
    }
192
14
    if (Feature == "+mutable-globals") {
193
10
      HasMutableGlobals = true;
194
10
      continue;
195
10
    }
196
4
    if (Feature == "-mutable-globals") {
197
0
      HasMutableGlobals = false;
198
0
      continue;
199
0
    }
200
4
    if (Feature == "+multivalue") {
201
2
      HasMultivalue = true;
202
2
      continue;
203
2
    }
204
2
    if (Feature == "-multivalue") {
205
0
      HasMultivalue = false;
206
0
      continue;
207
0
    }
208
2
    if (Feature == "+tail-call") {
209
2
      HasTailCall = true;
210
2
      continue;
211
2
    }
212
0
    if (Feature == "-tail-call") {
213
0
      HasTailCall = false;
214
0
      continue;
215
0
    }
216
0
217
0
    Diags.Report(diag::err_opt_not_valid_with_opt)
218
0
        << Feature << "-target-feature";
219
0
    return false;
220
0
  }
221
65
  return true;
222
65
}
223
224
65
ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
225
65
  return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
226
65
                                             Builtin::FirstTSBuiltin);
227
65
}
228
229
void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
230
36
                                               MacroBuilder &Builder) const {
231
36
  WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
232
36
  defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
233
36
}
234
235
void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
236
29
                                               MacroBuilder &Builder) const {
237
29
  WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
238
29
  defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
239
29
}