242 lines · c
1//=== WebAssembly.h - Declare WebAssembly target feature support *- C++ -*-===//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-exception6//7//===----------------------------------------------------------------------===//8//9// This file declares WebAssembly TargetInfo objects.10//11//===----------------------------------------------------------------------===//12 13#ifndef LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H14#define LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H15 16#include "clang/Basic/TargetInfo.h"17#include "clang/Basic/TargetOptions.h"18#include "llvm/Support/Compiler.h"19#include "llvm/TargetParser/Triple.h"20 21namespace clang {22namespace targets {23 24static const unsigned WebAssemblyAddrSpaceMap[] = {25 0, // Default26 0, // opencl_global27 0, // opencl_local28 0, // opencl_constant29 0, // opencl_private30 0, // opencl_generic31 0, // opencl_global_device32 0, // opencl_global_host33 0, // cuda_device34 0, // cuda_constant35 0, // cuda_shared36 0, // sycl_global37 0, // sycl_global_device38 0, // sycl_global_host39 0, // sycl_local40 0, // sycl_private41 0, // ptr32_sptr42 0, // ptr32_uptr43 0, // ptr6444 0, // hlsl_groupshared45 0, // hlsl_constant46 0, // hlsl_private47 0, // hlsl_device48 0, // hlsl_input49 20, // wasm_funcref50};51 52class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {53 54 enum SIMDEnum {55 NoSIMD,56 SIMD128,57 RelaxedSIMD,58 } SIMDLevel = NoSIMD;59 60 bool HasAtomics = false;61 bool HasBulkMemory = false;62 bool HasBulkMemoryOpt = false;63 bool HasCallIndirectOverlong = false;64 bool HasExceptionHandling = false;65 bool HasExtendedConst = false;66 bool HasFP16 = false;67 bool HasGC = false;68 bool HasMultiMemory = false;69 bool HasMultivalue = false;70 bool HasMutableGlobals = false;71 bool HasNontrappingFPToInt = false;72 bool HasReferenceTypes = false;73 bool HasSignExt = false;74 bool HasTailCall = false;75 bool HasWideArithmetic = false;76 77 std::string ABI;78 79public:80 explicit WebAssemblyTargetInfo(const llvm::Triple &T, const TargetOptions &)81 : TargetInfo(T) {82 AddrSpaceMap = &WebAssemblyAddrSpaceMap;83 NoAsmVariants = true;84 SuitableAlign = 128;85 LargeArrayMinWidth = 128;86 LargeArrayAlign = 128;87 SigAtomicType = SignedLong;88 LongDoubleWidth = LongDoubleAlign = 128;89 LongDoubleFormat = &llvm::APFloat::IEEEquad();90 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;91 HasUnalignedAccess = true;92 if (T.isWALI()) {93 // The WALI ABI is documented here:94 // https://doc.rust-lang.org/rustc/platform-support/wasm32-wali-linux.html95 // Currently, this ABI only applies to wasm32 targets and notably requires96 // 64-bit longs97 LongAlign = LongWidth = 64;98 SizeType = UnsignedInt;99 PtrDiffType = SignedInt;100 IntPtrType = SignedInt;101 } else {102 // size_t being unsigned long for both wasm32 and wasm64 makes mangled103 // names more consistent between the two.104 SizeType = UnsignedLong;105 PtrDiffType = SignedLong;106 IntPtrType = SignedLong;107 }108 }109 110 StringRef getABI() const override;111 bool setABI(const std::string &Name) override;112 bool useFP16ConversionIntrinsics() const override { return !HasFP16; }113 114protected:115 void getTargetDefines(const LangOptions &Opts,116 MacroBuilder &Builder) const override;117 118private:119 static void setSIMDLevel(llvm::StringMap<bool> &Features, SIMDEnum Level,120 bool Enabled);121 122 bool123 initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,124 StringRef CPU,125 const std::vector<std::string> &FeaturesVec) const override;126 bool hasFeature(StringRef Feature) const final;127 128 void setFeatureEnabled(llvm::StringMap<bool> &Features, StringRef Name,129 bool Enabled) const final;130 131 bool handleTargetFeatures(std::vector<std::string> &Features,132 DiagnosticsEngine &Diags) final;133 134 bool isValidCPUName(StringRef Name) const final;135 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const final;136 137 bool setCPU(const std::string &Name) final { return isValidCPUName(Name); }138 139 llvm::SmallVector<Builtin::InfosShard> getTargetBuiltins() const final;140 141 BuiltinVaListKind getBuiltinVaListKind() const final {142 return VoidPtrBuiltinVaList;143 }144 145 ArrayRef<const char *> getGCCRegNames() const final { return {}; }146 147 ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const final {148 return {};149 }150 151 bool validateAsmConstraint(const char *&Name,152 TargetInfo::ConstraintInfo &Info) const final {153 return false;154 }155 156 std::string_view getClobbers() const final { return ""; }157 158 bool isCLZForZeroUndef() const final { return false; }159 160 bool hasInt128Type() const final { return true; }161 162 IntType getIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {163 // WebAssembly prefers long long for explicitly 64-bit integers.164 return BitWidth == 64 ? (IsSigned ? SignedLongLong : UnsignedLongLong)165 : TargetInfo::getIntTypeByWidth(BitWidth, IsSigned);166 }167 168 IntType getLeastIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {169 // WebAssembly uses long long for int_least64_t and int_fast64_t.170 return BitWidth == 64171 ? (IsSigned ? SignedLongLong : UnsignedLongLong)172 : TargetInfo::getLeastIntTypeByWidth(BitWidth, IsSigned);173 }174 175 CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {176 switch (CC) {177 case CC_C:178 case CC_Swift:179 return CCCR_OK;180 case CC_SwiftAsync:181 return CCCR_Error;182 default:183 return CCCR_Warning;184 }185 }186 187 bool hasBitIntType() const override { return true; }188 189 bool hasProtectedVisibility() const override { return false; }190 191 void adjust(DiagnosticsEngine &Diags, LangOptions &Opts,192 const TargetInfo *Aux) override;193};194 195class LLVM_LIBRARY_VISIBILITY WebAssembly32TargetInfo196 : public WebAssemblyTargetInfo {197public:198 explicit WebAssembly32TargetInfo(const llvm::Triple &T,199 const TargetOptions &Opts)200 : WebAssemblyTargetInfo(T, Opts) {201 if (T.isOSEmscripten())202 resetDataLayout(203 "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-f128:64-n32:64-"204 "S128-ni:1:10:20");205 else206 resetDataLayout("e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-"207 "S128-ni:1:10:20");208 }209 210protected:211 void getTargetDefines(const LangOptions &Opts,212 MacroBuilder &Builder) const override;213};214 215class LLVM_LIBRARY_VISIBILITY WebAssembly64TargetInfo216 : public WebAssemblyTargetInfo {217public:218 explicit WebAssembly64TargetInfo(const llvm::Triple &T,219 const TargetOptions &Opts)220 : WebAssemblyTargetInfo(T, Opts) {221 LongAlign = LongWidth = 64;222 PointerAlign = PointerWidth = 64;223 SizeType = UnsignedLong;224 PtrDiffType = SignedLong;225 IntPtrType = SignedLong;226 if (T.isOSEmscripten())227 resetDataLayout(228 "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-i128:128-f128:64-n32:64-"229 "S128-ni:1:10:20");230 else231 resetDataLayout("e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-"232 "S128-ni:1:10:20");233 }234 235protected:236 void getTargetDefines(const LangOptions &Opts,237 MacroBuilder &Builder) const override;238};239} // namespace targets240} // namespace clang241#endif // LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H242