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-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares WebAssembly TargetInfo objects.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H
14#define LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H
15
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, // Default
26 0, // opencl_global
27 0, // opencl_local
28 0, // opencl_constant
29 0, // opencl_private
30 0, // opencl_generic
31 0, // opencl_global_device
32 0, // opencl_global_host
33 0, // cuda_device
34 0, // cuda_constant
35 0, // cuda_shared
36 0, // sycl_global
37 0, // sycl_global_device
38 0, // sycl_global_host
39 0, // sycl_local
40 0, // sycl_private
41 0, // ptr32_sptr
42 0, // ptr32_uptr
43 0, // ptr64
44 0, // hlsl_groupshared
45 0, // hlsl_constant
46 0, // hlsl_private
47 0, // hlsl_device
48 0, // hlsl_input
49 0, // hlsl_push_constant
50 20, // wasm_funcref
51};
52
53class LLVM_LIBRARY_VISIBILITY WebAssemblyTargetInfo : public TargetInfo {
54
55 enum SIMDEnum {
56 NoSIMD,
57 SIMD128,
58 RelaxedSIMD,
59 } SIMDLevel = NoSIMD;
60
61 bool HasAtomics = false;
62 bool HasBulkMemory = false;
63 bool HasBulkMemoryOpt = false;
64 bool HasCallIndirectOverlong = false;
65 bool HasCompactImports = false;
66 bool HasExceptionHandling = false;
67 bool HasExtendedConst = false;
68 bool HasFP16 = false;
69 bool HasGC = false;
70 bool HasMultiMemory = false;
71 bool HasMultivalue = false;
72 bool HasMutableGlobals = false;
73 bool HasNontrappingFPToInt = false;
74 bool HasReferenceTypes = false;
75 bool HasSignExt = false;
76 bool HasTailCall = false;
77 bool HasWideArithmetic = false;
78
79 std::string ABI;
80
81public:
82 explicit WebAssemblyTargetInfo(const llvm::Triple &T, const TargetOptions &)
83 : TargetInfo(T) {
84 AddrSpaceMap = &WebAssemblyAddrSpaceMap;
85 UseAddrSpaceMapMangling = true;
86 NoAsmVariants = true;
87 SuitableAlign = 128;
88 LargeArrayMinWidth = 128;
89 LargeArrayAlign = 128;
90 SigAtomicType = SignedLong;
91 LongDoubleWidth = LongDoubleAlign = 128;
92 LongDoubleFormat = &llvm::APFloat::IEEEquad();
93 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
94 HasUnalignedAccess = true;
95 if (T.isWALI()) {
96 // The WALI ABI is documented here:
97 // https://doc.rust-lang.org/rustc/platform-support/wasm32-wali-linux.html
98 // Currently, this ABI only applies to wasm32 targets and notably requires
99 // 64-bit longs
100 LongAlign = LongWidth = 64;
101 SizeType = UnsignedInt;
102 PtrDiffType = SignedInt;
103 IntPtrType = SignedInt;
104 } else {
105 // size_t being unsigned long for both wasm32 and wasm64 makes mangled
106 // names more consistent between the two.
107 SizeType = UnsignedLong;
108 PtrDiffType = SignedLong;
109 IntPtrType = SignedLong;
110 }
111 }
112
113 StringRef getABI() const override;
114 bool setABI(const std::string &Name) override;
115 bool useFP16ConversionIntrinsics() const override { return !HasFP16; }
116
117protected:
118 void getTargetDefines(const LangOptions &Opts,
119 MacroBuilder &Builder) const override;
120
121private:
122 static void setSIMDLevel(llvm::StringMap<bool> &Features, SIMDEnum Level,
123 bool Enabled);
124
125 bool
126 initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
127 StringRef CPU,
128 const std::vector<std::string> &FeaturesVec) const override;
129 bool hasFeature(StringRef Feature) const final;
130
131 void setFeatureEnabled(llvm::StringMap<bool> &Features, StringRef Name,
132 bool Enabled) const final;
133
134 bool handleTargetFeatures(std::vector<std::string> &Features,
135 DiagnosticsEngine &Diags) final;
136
137 bool isValidCPUName(StringRef Name) const final;
138 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const final;
139
140 bool setCPU(const std::string &Name) final { return isValidCPUName(Name); }
141
142 llvm::SmallVector<Builtin::InfosShard> getTargetBuiltins() const final;
143
144 BuiltinVaListKind getBuiltinVaListKind() const final {
145 return VoidPtrBuiltinVaList;
146 }
147
148 ArrayRef<const char *> getGCCRegNames() const final { return {}; }
149
150 ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const final {
151 return {};
152 }
153
154 bool validateAsmConstraint(const char *&Name,
155 TargetInfo::ConstraintInfo &Info) const final {
156 return false;
157 }
158
159 std::string_view getClobbers() const final { return ""; }
160
161 bool isCLZForZeroUndef() const final { return false; }
162
163 bool hasInt128Type() const final { return true; }
164
165 IntType getIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
166 // WebAssembly prefers long long for explicitly 64-bit integers.
167 return BitWidth == 64 ? (IsSigned ? SignedLongLong : UnsignedLongLong)
168 : TargetInfo::getIntTypeByWidth(BitWidth, IsSigned);
169 }
170
171 IntType getLeastIntTypeByWidth(unsigned BitWidth, bool IsSigned) const final {
172 // WebAssembly uses long long for int_least64_t and int_fast64_t.
173 return BitWidth == 64
174 ? (IsSigned ? SignedLongLong : UnsignedLongLong)
175 : TargetInfo::getLeastIntTypeByWidth(BitWidth, IsSigned);
176 }
177
178 CallingConvCheckResult checkCallingConvention(CallingConv CC) const override {
179 switch (CC) {
180 case CC_C:
181 case CC_Swift:
182 return CCCR_OK;
183 case CC_SwiftAsync:
184 return CCCR_Error;
185 default:
186 return CCCR_Warning;
187 }
188 }
189
190 bool hasBitIntType() const override { return true; }
191
192 bool hasProtectedVisibility() const override { return false; }
193
194 void adjust(DiagnosticsEngine &Diags, LangOptions &Opts,
195 const TargetInfo *Aux) override;
196};
197
198class LLVM_LIBRARY_VISIBILITY WebAssembly32TargetInfo
199 : public WebAssemblyTargetInfo {
200public:
201 explicit WebAssembly32TargetInfo(const llvm::Triple &T,
202 const TargetOptions &Opts)
203 : WebAssemblyTargetInfo(T, Opts) {
204 resetDataLayout();
205 }
206
207protected:
208 void getTargetDefines(const LangOptions &Opts,
209 MacroBuilder &Builder) const override;
210};
211
212class LLVM_LIBRARY_VISIBILITY WebAssembly64TargetInfo
213 : public WebAssemblyTargetInfo {
214public:
215 explicit WebAssembly64TargetInfo(const llvm::Triple &T,
216 const TargetOptions &Opts)
217 : WebAssemblyTargetInfo(T, Opts) {
218 LongAlign = LongWidth = 64;
219 PointerAlign = PointerWidth = 64;
220 SizeType = UnsignedLong;
221 PtrDiffType = SignedLong;
222 IntPtrType = SignedLong;
223 resetDataLayout();
224 }
225
226protected:
227 void getTargetDefines(const LangOptions &Opts,
228 MacroBuilder &Builder) const override;
229};
230} // namespace targets
231} // namespace clang
232#endif // LLVM_CLANG_LIB_BASIC_TARGETS_WEBASSEMBLY_H
233