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