2017-07-22 06:37:03 +08:00
|
|
|
//===--- WebAssembly.cpp - Implement WebAssembly target feature support ---===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-07-22 06:37:03 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements WebAssembly TargetInfo objects.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "WebAssembly.h"
|
|
|
|
#include "Targets.h"
|
|
|
|
#include "clang/Basic/Builtins.h"
|
|
|
|
#include "clang/Basic/Diagnostic.h"
|
|
|
|
#include "clang/Basic/TargetBuiltins.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::targets;
|
|
|
|
|
|
|
|
const Builtin::Info WebAssemblyTargetInfo::BuiltinInfo[] = {
|
|
|
|
#define BUILTIN(ID, TYPE, ATTRS) \
|
|
|
|
{#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, nullptr},
|
2019-01-11 07:49:00 +08:00
|
|
|
#define TARGET_BUILTIN(ID, TYPE, ATTRS, FEATURE) \
|
|
|
|
{#ID, TYPE, ATTRS, nullptr, ALL_LANGUAGES, FEATURE},
|
2017-07-22 06:37:03 +08:00
|
|
|
#define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) \
|
|
|
|
{#ID, TYPE, ATTRS, HEADER, ALL_LANGUAGES, nullptr},
|
|
|
|
#include "clang/Basic/BuiltinsWebAssembly.def"
|
|
|
|
};
|
|
|
|
|
2018-02-09 07:16:55 +08:00
|
|
|
static constexpr llvm::StringLiteral ValidCPUNames[] = {
|
|
|
|
{"mvp"}, {"bleeding-edge"}, {"generic"}};
|
|
|
|
|
2017-07-22 06:37:03 +08:00
|
|
|
bool WebAssemblyTargetInfo::hasFeature(StringRef Feature) const {
|
|
|
|
return llvm::StringSwitch<bool>(Feature)
|
|
|
|
.Case("simd128", SIMDLevel >= SIMD128)
|
2019-01-11 07:49:00 +08:00
|
|
|
.Case("unimplemented-simd128", SIMDLevel >= UnimplementedSIMD128)
|
2017-11-28 09:13:45 +08:00
|
|
|
.Case("nontrapping-fptoint", HasNontrappingFPToInt)
|
2018-01-20 01:16:32 +08:00
|
|
|
.Case("sign-ext", HasSignExt)
|
2018-03-02 08:39:16 +08:00
|
|
|
.Case("exception-handling", HasExceptionHandling)
|
2019-02-01 05:02:19 +08:00
|
|
|
.Case("bulk-memory", HasBulkMemory)
|
2019-02-06 09:41:26 +08:00
|
|
|
.Case("atomics", HasAtomics)
|
[WebAssembly] Add mutable globals feature
Summary:
This feature is not actually used for anything in the WebAssembly
backend, but adding it allows users to get it into the target features
sections of their objects, which makes these objects
future-compatible.
Reviewers: aheejin, dschuff
Subscribers: sbc100, jgravelle-google, hiraditya, sunfish, jdoerfert, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D60013
llvm-svn: 357321
2019-03-30 06:00:18 +08:00
|
|
|
.Case("mutable-globals", HasMutableGlobals)
|
2019-05-24 01:26:47 +08:00
|
|
|
.Case("multivalue", HasMultivalue)
|
|
|
|
.Case("tail-call", HasTailCall)
|
2020-01-24 11:22:51 +08:00
|
|
|
.Case("reference-types", HasReferenceTypes)
|
2017-07-22 06:37:03 +08:00
|
|
|
.Default(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAssemblyTargetInfo::isValidCPUName(StringRef Name) const {
|
2018-02-09 07:16:55 +08:00
|
|
|
return llvm::find(ValidCPUNames, Name) != std::end(ValidCPUNames);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAssemblyTargetInfo::fillValidCPUList(
|
|
|
|
SmallVectorImpl<StringRef> &Values) const {
|
|
|
|
Values.append(std::begin(ValidCPUNames), std::end(ValidCPUNames));
|
2017-07-22 06:37:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebAssemblyTargetInfo::getTargetDefines(const LangOptions &Opts,
|
|
|
|
MacroBuilder &Builder) const {
|
|
|
|
defineCPUMacros(Builder, "wasm", /*Tuning=*/false);
|
|
|
|
if (SIMDLevel >= SIMD128)
|
|
|
|
Builder.defineMacro("__wasm_simd128__");
|
2019-01-11 07:49:00 +08:00
|
|
|
if (SIMDLevel >= UnimplementedSIMD128)
|
|
|
|
Builder.defineMacro("__wasm_unimplemented_simd128__");
|
2019-02-01 05:02:19 +08:00
|
|
|
if (HasNontrappingFPToInt)
|
|
|
|
Builder.defineMacro("__wasm_nontrapping_fptoint__");
|
|
|
|
if (HasSignExt)
|
|
|
|
Builder.defineMacro("__wasm_sign_ext__");
|
|
|
|
if (HasExceptionHandling)
|
|
|
|
Builder.defineMacro("__wasm_exception_handling__");
|
|
|
|
if (HasBulkMemory)
|
|
|
|
Builder.defineMacro("__wasm_bulk_memory__");
|
2019-02-06 09:41:26 +08:00
|
|
|
if (HasAtomics)
|
|
|
|
Builder.defineMacro("__wasm_atomics__");
|
[WebAssembly] Add mutable globals feature
Summary:
This feature is not actually used for anything in the WebAssembly
backend, but adding it allows users to get it into the target features
sections of their objects, which makes these objects
future-compatible.
Reviewers: aheejin, dschuff
Subscribers: sbc100, jgravelle-google, hiraditya, sunfish, jdoerfert, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D60013
llvm-svn: 357321
2019-03-30 06:00:18 +08:00
|
|
|
if (HasMutableGlobals)
|
|
|
|
Builder.defineMacro("__wasm_mutable_globals__");
|
2019-05-24 01:26:47 +08:00
|
|
|
if (HasMultivalue)
|
|
|
|
Builder.defineMacro("__wasm_multivalue__");
|
|
|
|
if (HasTailCall)
|
|
|
|
Builder.defineMacro("__wasm_tail_call__");
|
2020-01-24 11:22:51 +08:00
|
|
|
if (HasReferenceTypes)
|
|
|
|
Builder.defineMacro("__wasm_reference_types__");
|
2019-01-11 07:49:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebAssemblyTargetInfo::setSIMDLevel(llvm::StringMap<bool> &Features,
|
|
|
|
SIMDEnum Level) {
|
|
|
|
switch (Level) {
|
|
|
|
case UnimplementedSIMD128:
|
|
|
|
Features["unimplemented-simd128"] = true;
|
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case SIMD128:
|
|
|
|
Features["simd128"] = true;
|
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case NoSIMD:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAssemblyTargetInfo::initFeatureMap(
|
|
|
|
llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags, StringRef CPU,
|
|
|
|
const std::vector<std::string> &FeaturesVec) const {
|
|
|
|
if (CPU == "bleeding-edge") {
|
|
|
|
Features["nontrapping-fptoint"] = true;
|
|
|
|
Features["sign-ext"] = true;
|
2019-02-06 09:41:26 +08:00
|
|
|
Features["atomics"] = true;
|
[WebAssembly] Add mutable globals feature
Summary:
This feature is not actually used for anything in the WebAssembly
backend, but adding it allows users to get it into the target features
sections of their objects, which makes these objects
future-compatible.
Reviewers: aheejin, dschuff
Subscribers: sbc100, jgravelle-google, hiraditya, sunfish, jdoerfert, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D60013
llvm-svn: 357321
2019-03-30 06:00:18 +08:00
|
|
|
Features["mutable-globals"] = true;
|
2019-01-11 07:49:00 +08:00
|
|
|
setSIMDLevel(Features, SIMD128);
|
|
|
|
}
|
|
|
|
// Other targets do not consider user-configured features here, but while we
|
|
|
|
// are actively developing new features it is useful to let user-configured
|
|
|
|
// features control availability of builtins
|
|
|
|
setSIMDLevel(Features, SIMDLevel);
|
|
|
|
if (HasNontrappingFPToInt)
|
|
|
|
Features["nontrapping-fptoint"] = true;
|
|
|
|
if (HasSignExt)
|
|
|
|
Features["sign-ext"] = true;
|
|
|
|
if (HasExceptionHandling)
|
|
|
|
Features["exception-handling"] = true;
|
2019-02-01 05:02:19 +08:00
|
|
|
if (HasBulkMemory)
|
|
|
|
Features["bulk-memory"] = true;
|
2019-02-06 09:41:26 +08:00
|
|
|
if (HasAtomics)
|
|
|
|
Features["atomics"] = true;
|
[WebAssembly] Add mutable globals feature
Summary:
This feature is not actually used for anything in the WebAssembly
backend, but adding it allows users to get it into the target features
sections of their objects, which makes these objects
future-compatible.
Reviewers: aheejin, dschuff
Subscribers: sbc100, jgravelle-google, hiraditya, sunfish, jdoerfert, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D60013
llvm-svn: 357321
2019-03-30 06:00:18 +08:00
|
|
|
if (HasMutableGlobals)
|
|
|
|
Features["mutable-globals"] = true;
|
2019-05-24 01:26:47 +08:00
|
|
|
if (HasMultivalue)
|
|
|
|
Features["multivalue"] = true;
|
|
|
|
if (HasTailCall)
|
|
|
|
Features["tail-call"] = true;
|
2020-01-24 11:22:51 +08:00
|
|
|
if (HasReferenceTypes)
|
|
|
|
Features["reference-types"] = true;
|
2019-01-11 07:49:00 +08:00
|
|
|
|
|
|
|
return TargetInfo::initFeatureMap(Features, Diags, CPU, FeaturesVec);
|
2017-07-22 06:37:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebAssemblyTargetInfo::handleTargetFeatures(
|
|
|
|
std::vector<std::string> &Features, DiagnosticsEngine &Diags) {
|
|
|
|
for (const auto &Feature : Features) {
|
|
|
|
if (Feature == "+simd128") {
|
|
|
|
SIMDLevel = std::max(SIMDLevel, SIMD128);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Feature == "-simd128") {
|
|
|
|
SIMDLevel = std::min(SIMDLevel, SIMDEnum(SIMD128 - 1));
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-11 07:49:00 +08:00
|
|
|
if (Feature == "+unimplemented-simd128") {
|
|
|
|
SIMDLevel = std::max(SIMDLevel, SIMDEnum(UnimplementedSIMD128));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Feature == "-unimplemented-simd128") {
|
|
|
|
SIMDLevel = std::min(SIMDLevel, SIMDEnum(UnimplementedSIMD128 - 1));
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-28 09:13:45 +08:00
|
|
|
if (Feature == "+nontrapping-fptoint") {
|
|
|
|
HasNontrappingFPToInt = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Feature == "-nontrapping-fptoint") {
|
|
|
|
HasNontrappingFPToInt = false;
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-20 01:16:32 +08:00
|
|
|
if (Feature == "+sign-ext") {
|
|
|
|
HasSignExt = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Feature == "-sign-ext") {
|
|
|
|
HasSignExt = false;
|
|
|
|
continue;
|
|
|
|
}
|
2018-03-02 08:39:16 +08:00
|
|
|
if (Feature == "+exception-handling") {
|
|
|
|
HasExceptionHandling = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Feature == "-exception-handling") {
|
|
|
|
HasExceptionHandling = false;
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-01 05:02:19 +08:00
|
|
|
if (Feature == "+bulk-memory") {
|
|
|
|
HasBulkMemory = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Feature == "-bulk-memory") {
|
|
|
|
HasBulkMemory = false;
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-06 09:41:26 +08:00
|
|
|
if (Feature == "+atomics") {
|
|
|
|
HasAtomics = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Feature == "-atomics") {
|
|
|
|
HasAtomics = false;
|
|
|
|
continue;
|
|
|
|
}
|
[WebAssembly] Add mutable globals feature
Summary:
This feature is not actually used for anything in the WebAssembly
backend, but adding it allows users to get it into the target features
sections of their objects, which makes these objects
future-compatible.
Reviewers: aheejin, dschuff
Subscribers: sbc100, jgravelle-google, hiraditya, sunfish, jdoerfert, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D60013
llvm-svn: 357321
2019-03-30 06:00:18 +08:00
|
|
|
if (Feature == "+mutable-globals") {
|
|
|
|
HasMutableGlobals = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Feature == "-mutable-globals") {
|
|
|
|
HasMutableGlobals = false;
|
|
|
|
continue;
|
|
|
|
}
|
2019-05-24 01:26:47 +08:00
|
|
|
if (Feature == "+multivalue") {
|
|
|
|
HasMultivalue = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Feature == "-multivalue") {
|
|
|
|
HasMultivalue = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Feature == "+tail-call") {
|
|
|
|
HasTailCall = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Feature == "-tail-call") {
|
|
|
|
HasTailCall = false;
|
|
|
|
continue;
|
|
|
|
}
|
2020-01-24 11:22:51 +08:00
|
|
|
if (Feature == "+reference-types") {
|
|
|
|
HasReferenceTypes = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Feature == "-reference-types") {
|
|
|
|
HasReferenceTypes = false;
|
|
|
|
continue;
|
|
|
|
}
|
2017-07-22 06:37:03 +08:00
|
|
|
|
|
|
|
Diags.Report(diag::err_opt_not_valid_with_opt)
|
|
|
|
<< Feature << "-target-feature";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayRef<Builtin::Info> WebAssemblyTargetInfo::getTargetBuiltins() const {
|
|
|
|
return llvm::makeArrayRef(BuiltinInfo, clang::WebAssembly::LastTSBuiltin -
|
|
|
|
Builtin::FirstTSBuiltin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAssembly32TargetInfo::getTargetDefines(const LangOptions &Opts,
|
|
|
|
MacroBuilder &Builder) const {
|
|
|
|
WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
|
|
|
|
defineCPUMacros(Builder, "wasm32", /*Tuning=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAssembly64TargetInfo::getTargetDefines(const LangOptions &Opts,
|
|
|
|
MacroBuilder &Builder) const {
|
|
|
|
WebAssemblyTargetInfo::getTargetDefines(Opts, Builder);
|
|
|
|
defineCPUMacros(Builder, "wasm64", /*Tuning=*/false);
|
|
|
|
}
|